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