YAZ 5.35.1
mutex.c
Go to the documentation of this file.
1/* This file is part of the YAZ toolkit.
2 * Copyright (C) Index Data
3 * See the file LICENSE for details.
4 */
5
11#if HAVE_CONFIG_H
12#include <config.h>
13#endif
14
15#include <yaz/yconfig.h>
16
17#include <assert.h>
18#include <stdlib.h>
19#include <string.h>
20#include <errno.h>
21#include <stddef.h>
22#include <yaz/xmalloc.h>
23#include <yaz/nmem.h>
24#include <yaz/log.h>
25#include <yaz/mutex.h>
26#include <yaz/gettimeofday.h>
27#ifdef WIN32
28#include <windows.h>
29#include <sys/timeb.h>
30#endif
31#include <time.h>
32
33#if HAVE_SYS_TIME_H
34#include <sys/time.h>
35#endif
36
37#if YAZ_POSIX_THREADS
38#include <pthread.h>
39#endif
40
41#include "mutex-p.h"
42
44{
45 if (!*p)
46 {
47 *p = (YAZ_MUTEX) malloc(sizeof(**p));
48#ifdef WIN32
49 InitializeCriticalSection(&(*p)->handle);
50#elif YAZ_POSIX_THREADS
51 pthread_mutex_init(&(*p)->handle, 0);
52#endif
53 (*p)->name = 0;
54 (*p)->log_level = 0;
55 }
56}
57
58void yaz_mutex_set_name(YAZ_MUTEX p, int log_level, const char *name)
59{
60 if (p->name)
61 free(p->name);
62 p->name = 0;
63 p->log_level = 0;
64 if (name)
65 {
66 p->name = malloc(strlen(name)+1);
67 strcpy(p->name, name);
69 }
70}
71
73{
74 if (p)
75 {
76#ifdef WIN32
77 EnterCriticalSection(&p->handle);
78#elif YAZ_POSIX_THREADS
79 int r = 1; /* signal : not locked (yet) */
80
81 if (p->log_level)
82 { /* debugging */
83 r = pthread_mutex_trylock(&p->handle);
84 if (r)
85 {
86#if HAVE_SYS_TIME_H
87 long long d;
88 struct timeval tv1, tv2;
89 gettimeofday(&tv1, 0);
90#endif
92 "yaz_mutex_enter: %p tid=%p name=%s waiting",
93 p, (void *) pthread_self(), p->name);
94#if HAVE_SYS_TIME_H
95 r = pthread_mutex_lock(&p->handle);
96 gettimeofday(&tv2, 0);
97 d = 1000000LL * ((long long) tv2.tv_sec - tv1.tv_sec) +
98 tv2.tv_usec - tv1.tv_usec;
99 yaz_log(p->log_level, "yaz_mutex_enter: %p tid=%p name=%s "
100 "lock delay %lld",
101 p, (void *) pthread_self(), p->name, d);
102#endif
103 }
104 else
105 {
106 yaz_log(p->log_level, "yaz_mutex_enter: %p tid=%p name=%s lock",
107 p, (void *) pthread_self(), p->name);
108 }
109 }
110 if (r)
111 {
112 r = pthread_mutex_lock(&p->handle);
113 if (p->log_level)
114 {
115 yaz_log(p->log_level, "yaz_mutex_enter: %p tid=%p name=%s lock",
116 p, (void *) pthread_self(), p->name);
117 }
118 }
119#endif
120 }
121}
122
124{
125 if (p)
126 {
127#ifdef WIN32
128 LeaveCriticalSection(&p->handle);
129#elif YAZ_POSIX_THREADS
130 pthread_mutex_unlock(&p->handle);
131 if (p->log_level)
132 {
134 "yaz_mutex_leave: %p tid=%p name=%s unlock",
135 p, (void *) pthread_self(), p->name);
136 }
137#endif
138 }
139}
140
142{
143 if (*p)
144 {
145#ifdef WIN32
146 DeleteCriticalSection(&(*p)->handle);
147#elif YAZ_POSIX_THREADS
148 pthread_mutex_destroy(&(*p)->handle);
149#endif
150 if ((*p)->name)
151 free((*p)->name);
152 free(*p);
153 *p = 0;
154 }
155}
156
157/*
158 * Local variables:
159 * c-basic-offset: 4
160 * c-file-style: "Stroustrup"
161 * indent-tabs-mode: nil
162 * End:
163 * vim: shiftwidth=4 tabstop=8 expandtab
164 */
165
void * malloc(YYSIZE_T)
void free(void *)
Header for errno utilities.
static int log_level
Definition eventl.c:39
Header for gettimeofday wrapper.
char * name
Definition initopt.c:18
void yaz_log(int level, const char *fmt,...)
Writes log message.
Definition log.c:487
Logging utility.
Declares internal definitions of for Mutex functions.
void yaz_mutex_leave(YAZ_MUTEX p)
leave critical section / AKA unlock
Definition mutex.c:123
void yaz_mutex_enter(YAZ_MUTEX p)
enter critical section / AKA lock
Definition mutex.c:72
void yaz_mutex_set_name(YAZ_MUTEX p, int log_level, const char *name)
sets name of MUTEX for debugging purposes
Definition mutex.c:58
void yaz_mutex_create(YAZ_MUTEX *p)
create MUTEX
Definition mutex.c:43
void yaz_mutex_destroy(YAZ_MUTEX *p)
destroy MUTEX
Definition mutex.c:141
Header for Mutex functions.
struct yaz_mutex * YAZ_MUTEX
YAZ MUTEX opaque pointer.
Definition mutex.h:42
Header for Nibble Memory functions.
int log_level
Definition mutex-p.h:40
char * name
Definition mutex-p.h:39
Header for memory handling functions.
Header with fundamental macros.