1
/* $Id: os_rwmutex.c 3553 2011-05-05 06:14:19Z nanang $ */
3
* Copyright (C) 2008-2011 Teluu Inc. (http://www.teluu.com)
4
* Copyright (C) 2003-2008 Benny Prijono <benny@prijono.org>
6
* This program is free software; you can redistribute it and/or modify
7
* it under the terms of the GNU General Public License as published by
8
* the Free Software Foundation; either version 2 of the License, or
9
* (at your option) any later version.
11
* This program is distributed in the hope that it will be useful,
12
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
* GNU General Public License for more details.
16
* You should have received a copy of the GNU General Public License
17
* along with this program; if not, write to the Free Software
18
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24
* DO NOT BUILD THIS FILE DIRECTLY. THIS FILE WILL BE INCLUDED BY os_core_*.c
25
* WHEN MACRO PJ_EMULATE_RWMUTEX IS SET.
31
* Implementation of Read-Write mutex for platforms that lack it (e.g.
38
pj_mutex_t *read_lock;
39
/* write_lock must use semaphore, because write_lock may be released
40
* by thread other than the thread that acquire the write_lock in the
44
pj_int32_t reader_count;
48
* Create reader/writer mutex.
51
PJ_DEF(pj_status_t) pj_rwmutex_create(pj_pool_t *pool, const char *name,
52
pj_rwmutex_t **p_mutex)
55
pj_rwmutex_t *rwmutex;
57
PJ_ASSERT_RETURN(pool && p_mutex, PJ_EINVAL);
60
rwmutex = PJ_POOL_ALLOC_T(pool, pj_rwmutex_t);
62
status = pj_mutex_create_simple(pool, name, &rwmutex ->read_lock);
63
if (status != PJ_SUCCESS)
66
status = pj_sem_create(pool, name, 1, 1, &rwmutex->write_lock);
67
if (status != PJ_SUCCESS) {
68
pj_mutex_destroy(rwmutex->read_lock);
72
rwmutex->reader_count = 0;
78
* Lock the mutex for reading.
81
PJ_DEF(pj_status_t) pj_rwmutex_lock_read(pj_rwmutex_t *mutex)
85
PJ_ASSERT_RETURN(mutex, PJ_EINVAL);
87
status = pj_mutex_lock(mutex->read_lock);
88
if (status != PJ_SUCCESS) {
89
pj_assert(!"This pretty much is unexpected");
93
mutex->reader_count++;
95
pj_assert(mutex->reader_count < 0x7FFFFFF0L);
97
if (mutex->reader_count == 1)
98
pj_sem_wait(mutex->write_lock);
100
status = pj_mutex_unlock(mutex->read_lock);
105
* Lock the mutex for writing.
108
PJ_DEF(pj_status_t) pj_rwmutex_lock_write(pj_rwmutex_t *mutex)
110
PJ_ASSERT_RETURN(mutex, PJ_EINVAL);
111
return pj_sem_wait(mutex->write_lock);
118
PJ_DEF(pj_status_t) pj_rwmutex_unlock_read(pj_rwmutex_t *mutex)
122
PJ_ASSERT_RETURN(mutex, PJ_EINVAL);
124
status = pj_mutex_lock(mutex->read_lock);
125
if (status != PJ_SUCCESS) {
126
pj_assert(!"This pretty much is unexpected");
130
pj_assert(mutex->reader_count >= 1);
132
--mutex->reader_count;
133
if (mutex->reader_count == 0)
134
pj_sem_post(mutex->write_lock);
136
status = pj_mutex_unlock(mutex->read_lock);
141
* Release write lock.
144
PJ_DEF(pj_status_t) pj_rwmutex_unlock_write(pj_rwmutex_t *mutex)
146
PJ_ASSERT_RETURN(mutex, PJ_EINVAL);
147
pj_assert(mutex->reader_count <= 1);
148
return pj_sem_post(mutex->write_lock);
153
* Destroy reader/writer mutex.
156
PJ_DEF(pj_status_t) pj_rwmutex_destroy(pj_rwmutex_t *mutex)
158
PJ_ASSERT_RETURN(mutex, PJ_EINVAL);
159
pj_mutex_destroy(mutex->read_lock);
160
pj_sem_destroy(mutex->write_lock);