Benny Prijono | 42c5b9e | 2006-05-10 19:24:40 +0000 | [diff] [blame] | 1 | /* $Id$ */ |
| 2 | /* |
Benny Prijono | a771a51 | 2007-02-19 01:13:53 +0000 | [diff] [blame] | 3 | * Copyright (C)2003-2007 Benny Prijono <benny@prijono.org> |
Benny Prijono | 42c5b9e | 2006-05-10 19:24:40 +0000 | [diff] [blame] | 4 | * |
| 5 | * This program is free software; you can redistribute it and/or modify |
| 6 | * it under the terms of the GNU General Public License as published by |
| 7 | * the Free Software Foundation; either version 2 of the License, or |
| 8 | * (at your option) any later version. |
| 9 | * |
| 10 | * This program is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | * GNU General Public License for more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License |
| 16 | * along with this program; if not, write to the Free Software |
| 17 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 18 | */ |
| 19 | |
| 20 | /* |
| 21 | * Note: |
| 22 | * |
| 23 | * DO NOT BUILD THIS FILE DIRECTLY. THIS FILE WILL BE INCLUDED BY os_core_*.c |
| 24 | * WHEN MACRO PJ_EMULATE_RWMUTEX IS SET. |
| 25 | */ |
| 26 | |
| 27 | /* |
| 28 | * os_rwmutex.c: |
| 29 | * |
| 30 | * Implementation of Read-Write mutex for platforms that lack it (e.g. |
| 31 | * Win32, RTEMS). |
| 32 | */ |
| 33 | |
| 34 | |
| 35 | struct pj_rwmutex_t |
| 36 | { |
| 37 | pj_mutex_t *read_lock; |
| 38 | /* write_lock must use semaphore, because write_lock may be released |
| 39 | * by thread other than the thread that acquire the write_lock in the |
| 40 | * first place. |
| 41 | */ |
| 42 | pj_sem_t *write_lock; |
| 43 | pj_int32_t reader_count; |
| 44 | }; |
| 45 | |
| 46 | /* |
| 47 | * Create reader/writer mutex. |
| 48 | * |
| 49 | */ |
| 50 | PJ_DEF(pj_status_t) pj_rwmutex_create(pj_pool_t *pool, const char *name, |
| 51 | pj_rwmutex_t **p_mutex) |
| 52 | { |
| 53 | pj_status_t status; |
| 54 | pj_rwmutex_t *rwmutex; |
| 55 | |
| 56 | PJ_ASSERT_RETURN(pool && p_mutex, PJ_EINVAL); |
| 57 | |
| 58 | *p_mutex = NULL; |
| 59 | rwmutex = pj_pool_alloc(pool, sizeof(struct pj_rwmutex_t)); |
| 60 | |
| 61 | status = pj_mutex_create_simple(pool, name, &rwmutex ->read_lock); |
| 62 | if (status != PJ_SUCCESS) |
| 63 | return status; |
| 64 | |
| 65 | status = pj_sem_create(pool, name, 1, 1, &rwmutex->write_lock); |
| 66 | if (status != PJ_SUCCESS) { |
| 67 | pj_mutex_destroy(rwmutex->read_lock); |
| 68 | return status; |
| 69 | } |
| 70 | |
| 71 | rwmutex->reader_count = 0; |
| 72 | *p_mutex = rwmutex; |
| 73 | return PJ_SUCCESS; |
| 74 | } |
| 75 | |
| 76 | /* |
| 77 | * Lock the mutex for reading. |
| 78 | * |
| 79 | */ |
| 80 | PJ_DEF(pj_status_t) pj_rwmutex_lock_read(pj_rwmutex_t *mutex) |
| 81 | { |
| 82 | pj_status_t status; |
| 83 | |
| 84 | PJ_ASSERT_RETURN(mutex, PJ_EINVAL); |
| 85 | |
| 86 | status = pj_mutex_lock(mutex->read_lock); |
| 87 | if (status != PJ_SUCCESS) { |
| 88 | pj_assert(!"This pretty much is unexpected"); |
| 89 | return status; |
| 90 | } |
| 91 | |
| 92 | mutex->reader_count++; |
| 93 | |
| 94 | pj_assert(mutex->reader_count < 0x7FFFFFF0L); |
| 95 | |
| 96 | if (mutex->reader_count == 1) |
| 97 | pj_sem_wait(mutex->write_lock); |
| 98 | |
| 99 | status = pj_mutex_unlock(mutex->read_lock); |
| 100 | return status; |
| 101 | } |
| 102 | |
| 103 | /* |
| 104 | * Lock the mutex for writing. |
| 105 | * |
| 106 | */ |
| 107 | PJ_DEF(pj_status_t) pj_rwmutex_lock_write(pj_rwmutex_t *mutex) |
| 108 | { |
| 109 | PJ_ASSERT_RETURN(mutex, PJ_EINVAL); |
| 110 | return pj_sem_wait(mutex->write_lock); |
| 111 | } |
| 112 | |
| 113 | /* |
| 114 | * Release read lock. |
| 115 | * |
| 116 | */ |
| 117 | PJ_DEF(pj_status_t) pj_rwmutex_unlock_read(pj_rwmutex_t *mutex) |
| 118 | { |
| 119 | pj_status_t status; |
| 120 | |
| 121 | PJ_ASSERT_RETURN(mutex, PJ_EINVAL); |
| 122 | |
| 123 | status = pj_mutex_lock(mutex->read_lock); |
| 124 | if (status != PJ_SUCCESS) { |
| 125 | pj_assert(!"This pretty much is unexpected"); |
| 126 | return status; |
| 127 | } |
| 128 | |
| 129 | pj_assert(mutex->reader_count >= 1); |
| 130 | |
| 131 | --mutex->reader_count; |
| 132 | if (mutex->reader_count == 0) |
| 133 | pj_sem_post(mutex->write_lock); |
| 134 | |
| 135 | status = pj_mutex_unlock(mutex->read_lock); |
| 136 | return status; |
| 137 | } |
| 138 | |
| 139 | /* |
| 140 | * Release write lock. |
| 141 | * |
| 142 | */ |
| 143 | PJ_DEF(pj_status_t) pj_rwmutex_unlock_write(pj_rwmutex_t *mutex) |
| 144 | { |
| 145 | PJ_ASSERT_RETURN(mutex, PJ_EINVAL); |
| 146 | pj_assert(mutex->reader_count <= 1); |
| 147 | return pj_sem_post(mutex->write_lock); |
| 148 | } |
| 149 | |
| 150 | |
| 151 | /* |
| 152 | * Destroy reader/writer mutex. |
| 153 | * |
| 154 | */ |
| 155 | PJ_DEF(pj_status_t) pj_rwmutex_destroy(pj_rwmutex_t *mutex) |
| 156 | { |
| 157 | PJ_ASSERT_RETURN(mutex, PJ_EINVAL); |
| 158 | pj_mutex_destroy(mutex->read_lock); |
| 159 | pj_sem_destroy(mutex->write_lock); |
| 160 | return PJ_SUCCESS; |
| 161 | } |
| 162 | |