Log when session is created
[pazpar2-moved-to-github.git] / src / ppmutex.c
1 /* This file is part of Pazpar2.
2    Copyright (C) 2006-2013 Index Data
3
4 Pazpar2 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 Pazpar2 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 /** \file
21     \brief control MUTEX debugging
22 */
23
24 #if HAVE_CONFIG_H
25 #include <config.h>
26 #endif
27
28 #include <assert.h>
29 #include <yaz/log.h>
30 #include "ppmutex.h"
31
32 static int ppmutex_level = 0;
33
34 void pazpar2_mutex_init(void)
35 {
36     ppmutex_level = yaz_log_module_level("mutex");
37 }
38
39 void pazpar2_mutex_create(YAZ_MUTEX *p, const char *name)
40 {
41     assert(p);
42     yaz_mutex_create(p);
43     yaz_mutex_set_name(*p, ppmutex_level, name);
44 }
45
46 void pazpar2_lock_rdwr_init(Pazpar2_lock_rdwr *p)
47 {
48     p->readers_reading = 0;
49     p->writers_writing = 0;
50 #if YAZ_POSIX_THREADS
51     pthread_mutex_init(&p->mutex, 0);
52     pthread_cond_init(&p->lock_free, 0);
53 #endif
54 }
55
56 void pazpar2_lock_rdwr_destroy(Pazpar2_lock_rdwr *p)
57 {
58     assert (p->readers_reading == 0);
59     assert (p->writers_writing == 0);
60 #if YAZ_POSIX_THREADS
61     pthread_mutex_destroy(&p->mutex);
62     pthread_cond_destroy(&p->lock_free);
63 #endif
64 }
65
66 void pazpar2_lock_rdwr_rlock(Pazpar2_lock_rdwr *p)
67 {
68 #if YAZ_POSIX_THREADS
69     pthread_mutex_lock(& p->mutex);
70     while (p->writers_writing)
71         pthread_cond_wait(&p->lock_free, &p->mutex);
72     p->readers_reading++;
73     pthread_mutex_unlock(&p->mutex);
74 #endif
75 }
76
77 void pazpar2_lock_rdwr_wlock(Pazpar2_lock_rdwr *p)
78 {
79 #if YAZ_POSIX_THREADS
80     pthread_mutex_lock(&p->mutex);
81     while (p->writers_writing || p->readers_reading)
82         pthread_cond_wait(&p->lock_free, &p->mutex);
83     p->writers_writing++;
84     pthread_mutex_unlock(&p->mutex);
85 #endif
86 }
87
88 void pazpar2_lock_rdwr_upgrade(Pazpar2_lock_rdwr *p)
89 {
90 #if YAZ_POSIX_THREADS
91     assert(p->readers_reading > 0);
92     pthread_mutex_lock(&p->mutex);
93     --p->readers_reading;
94     while (p->writers_writing || p->readers_reading)
95         pthread_cond_wait(&p->lock_free, &p->mutex);
96     p->writers_writing++;
97     pthread_mutex_unlock(&p->mutex);
98 #endif
99 }
100
101 void pazpar2_lock_rdwr_downgrade(Pazpar2_lock_rdwr *p)
102 {
103 #if YAZ_POSIX_THREADS
104     assert(p->writers_writing == 1);
105     pthread_mutex_lock(&p->mutex);
106     p->writers_writing--;
107     p->readers_reading++;
108     pthread_cond_broadcast(&p->lock_free);
109     pthread_mutex_unlock(&p->mutex);
110 #endif
111 }
112
113 void pazpar2_lock_rdwr_runlock(Pazpar2_lock_rdwr *p)
114 {
115 #if YAZ_POSIX_THREADS
116     assert(p->readers_reading > 0);
117     pthread_mutex_lock(&p->mutex);
118     p->readers_reading--;
119     if (p->readers_reading == 0)
120         pthread_cond_signal(&p->lock_free);
121     pthread_mutex_unlock(&p->mutex);
122 #endif
123 }
124
125 void pazpar2_lock_rdwr_wunlock(Pazpar2_lock_rdwr *p)
126 {
127 #if YAZ_POSIX_THREADS
128     assert(p->writers_writing == 1);
129     pthread_mutex_lock(&p->mutex);
130     p->writers_writing--;
131     pthread_cond_broadcast(&p->lock_free);
132     pthread_mutex_unlock(&p->mutex);
133 #endif
134 }
135
136 /*
137  * Local variables:
138  * c-basic-offset: 4
139  * c-file-style: "Stroustrup"
140  * indent-tabs-mode: nil
141  * End:
142  * vim: shiftwidth=4 tabstop=8 expandtab
143  */
144