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