Merge branch 'master' of ssh://git.indexdata.com/home/git/pub/idzebra
[idzebra-moved-to-github.git] / include / zebra-lock.h
1 /* This file is part of the Zebra server.
2    Copyright (C) Index Data
3
4 Zebra is free software; you can redistribute it and/or modify it under
5 the terms of the GNU General Public License as published by the Free
6 Software Foundation; either version 2, or (at your option) any later
7 version.
8
9 Zebra is distributed in the hope that it will be useful, but WITHOUT ANY
10 WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12 for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
17
18 */
19
20
21
22 #ifndef ZEBRA_LOCK_H
23 #define ZEBRA_LOCK_H
24
25 #ifdef WIN32
26 #include <windows.h>
27 #endif
28 #if YAZ_POSIX_THREADS
29 #include <pthread.h>
30 #endif
31
32 #include <yaz/yconfig.h>
33
34 YAZ_BEGIN_CDECL
35
36 typedef struct {
37 #ifdef WIN32
38     CRITICAL_SECTION mutex;
39 #else
40 # if YAZ_POSIX_THREADS
41     pthread_mutex_t mutex;
42 # else
43     int dummy;
44 # endif
45 #endif
46     int state;
47 } Zebra_mutex;
48
49 YAZ_EXPORT int zebra_mutex_init (Zebra_mutex *p);
50 YAZ_EXPORT int zebra_mutex_destroy (Zebra_mutex *p);
51 YAZ_EXPORT int zebra_mutex_lock (Zebra_mutex *p);
52 YAZ_EXPORT int zebra_mutex_unlock (Zebra_mutex *p);
53
54 typedef struct {
55     int readers_reading;
56     int writers_writing;
57 #if YAZ_POSIX_THREADS
58     pthread_mutex_t mutex;
59     pthread_cond_t lock_free;
60 #endif
61 } Zebra_lock_rdwr;
62
63 YAZ_EXPORT int zebra_lock_rdwr_init (Zebra_lock_rdwr *p);
64 YAZ_EXPORT int zebra_lock_rdwr_destroy (Zebra_lock_rdwr *p);
65 YAZ_EXPORT int zebra_lock_rdwr_rlock (Zebra_lock_rdwr *p);
66 YAZ_EXPORT int zebra_lock_rdwr_wlock (Zebra_lock_rdwr *p);
67 YAZ_EXPORT int zebra_lock_rdwr_runlock (Zebra_lock_rdwr *p);
68 YAZ_EXPORT int zebra_lock_rdwr_wunlock (Zebra_lock_rdwr *p);
69
70 typedef struct {
71 #if YAZ_POSIX_THREADS
72     pthread_mutex_t mutex;
73     pthread_cond_t cond;
74 #else
75     int dummy;
76 #endif
77 } Zebra_mutex_cond;
78
79 YAZ_EXPORT int zebra_mutex_cond_init (Zebra_mutex_cond *p);
80 YAZ_EXPORT int zebra_mutex_cond_destroy (Zebra_mutex_cond *p);
81 YAZ_EXPORT int zebra_mutex_cond_lock (Zebra_mutex_cond *p);
82 YAZ_EXPORT int zebra_mutex_cond_unlock (Zebra_mutex_cond *p);
83 YAZ_EXPORT int zebra_mutex_cond_wait (Zebra_mutex_cond *p);
84 YAZ_EXPORT int zebra_mutex_cond_signal (Zebra_mutex_cond *p);
85
86 YAZ_END_CDECL
87
88 #endif
89 /*
90  * Local variables:
91  * c-basic-offset: 4
92  * c-file-style: "Stroustrup"
93  * indent-tabs-mode: nil
94  * End:
95  * vim: shiftwidth=4 tabstop=8 expandtab
96  */
97