d1cd50e4bc70a4ed04683fb86fcadb1021815585
[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 #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_attr(YAZ_MUTEX *p, int flags) {
42     if (!*p)
43     {
44         *p = (YAZ_MUTEX) malloc(sizeof(**p));
45 #ifdef WIN32
46         InitializeCriticalSection(&(*p)->handle);
47 #elif YAZ_POSIX_THREADS
48         (*p)->attr = malloc(sizeof( (*p)->attr));
49         pthread_mutexattr_init((*p)->attr);
50         pthread_mutexattr_settype((*p)->attr, flags);
51         pthread_mutex_init(&(*p)->handle, (*p)->attr);
52 #endif
53         (*p)->name = 0;
54         (*p)->log_level = 0;
55     }
56 }
57
58 void yaz_mutex_create(YAZ_MUTEX *p) {
59     yaz_mutex_create(YAZ_MUTEX *p, 0);
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_mutexattr_destroy(&(*p)->attr);
152         free((*p)->attr);
153         pthread_mutex_destroy(&(*p)->handle);
154 #endif
155         if ((*p)->name)
156             free((*p)->name);
157         free(*p);
158         *p = 0;
159     }
160 }
161
162 /*
163  * Local variables:
164  * c-basic-offset: 4
165  * c-file-style: "Stroustrup"
166  * indent-tabs-mode: nil
167  * End:
168  * vim: shiftwidth=4 tabstop=8 expandtab
169  */
170