1 /* This file is part of Pazpar2.
2 Copyright (C) 2006-2013 Index Data
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
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
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
21 \brief control MUTEX debugging
32 static int ppmutex_level = 0;
34 void pazpar2_mutex_init(void)
36 ppmutex_level = yaz_log_module_level("mutex");
39 void pazpar2_mutex_create(YAZ_MUTEX *p, const char *name)
43 yaz_mutex_set_name(*p, ppmutex_level, name);
46 void pazpar2_lock_rdwr_init(Pazpar2_lock_rdwr *p)
48 p->readers_reading = 0;
49 p->writers_writing = 0;
51 pthread_mutex_init(&p->mutex, 0);
52 pthread_cond_init(&p->lock_free, 0);
56 void pazpar2_lock_rdwr_destroy(Pazpar2_lock_rdwr *p)
58 assert (p->readers_reading == 0);
59 assert (p->writers_writing == 0);
61 pthread_mutex_destroy(&p->mutex);
62 pthread_cond_destroy(&p->lock_free);
66 void pazpar2_lock_rdwr_rlock(Pazpar2_lock_rdwr *p)
69 pthread_mutex_lock(& p->mutex);
70 while (p->writers_writing)
71 pthread_cond_wait(&p->lock_free, &p->mutex);
73 pthread_mutex_unlock(&p->mutex);
77 void pazpar2_lock_rdwr_wlock(Pazpar2_lock_rdwr *p)
80 pthread_mutex_lock(&p->mutex);
81 while (p->writers_writing || p->readers_reading)
82 pthread_cond_wait(&p->lock_free, &p->mutex);
84 pthread_mutex_unlock(&p->mutex);
88 void pazpar2_lock_rdwr_upgrade(Pazpar2_lock_rdwr *p)
91 assert(p->readers_reading > 0);
92 pthread_mutex_lock(&p->mutex);
94 while (p->writers_writing || p->readers_reading)
95 pthread_cond_wait(&p->lock_free, &p->mutex);
97 pthread_mutex_unlock(&p->mutex);
101 void pazpar2_lock_rdwr_downgrade(Pazpar2_lock_rdwr *p)
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);
113 void pazpar2_lock_rdwr_runlock(Pazpar2_lock_rdwr *p)
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);
125 void pazpar2_lock_rdwr_wunlock(Pazpar2_lock_rdwr *p)
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);
139 * c-file-style: "Stroustrup"
140 * indent-tabs-mode: nil
142 * vim: shiftwidth=4 tabstop=8 expandtab