Changeset 434 for pjproject/trunk
- Timestamp:
- May 11, 2006 10:33:19 AM (19 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
pjproject/trunk/pjlib/src/pj/os_core_win32.c
r315 r434 869 869 870 870 /////////////////////////////////////////////////////////////////////////////// 871 872 struct pj_rwmutex_t 873 { 874 pj_mutex_t *read_lock; 875 /* write_lock must use semaphore, because write_lock may be released 876 * by thread other than the thread that acquire the write_lock in the 877 * first place. 878 */ 879 pj_sem_t *write_lock; 880 pj_int32_t reader_count; 881 }; 882 883 /* 884 * Create reader/writer mutex. 885 * 886 */ 887 PJ_DEF(pj_status_t) pj_rwmutex_create(pj_pool_t *pool, const char *name, 888 pj_rwmutex_t **p_mutex) 889 { 890 pj_status_t status; 891 pj_rwmutex_t *rwmutex; 892 893 PJ_ASSERT_RETURN(pool && p_mutex, PJ_EINVAL); 894 895 *p_mutex = NULL; 896 rwmutex = pj_pool_alloc(pool, sizeof(struct pj_rwmutex_t)); 897 898 status = pj_mutex_create_simple(pool, name, &rwmutex ->read_lock); 899 if (status != PJ_SUCCESS) 900 return status; 901 902 status = pj_sem_create(pool, name, 1, 1, &rwmutex->write_lock); 903 if (status != PJ_SUCCESS) { 904 pj_mutex_destroy(rwmutex->read_lock); 905 return status; 906 } 907 908 rwmutex->reader_count = 0; 909 *p_mutex = rwmutex; 910 return PJ_SUCCESS; 911 } 912 913 /* 914 * Lock the mutex for reading. 915 * 916 */ 917 PJ_DEF(pj_status_t) pj_rwmutex_lock_read(pj_rwmutex_t *mutex) 918 { 919 pj_status_t status; 920 921 PJ_ASSERT_RETURN(mutex, PJ_EINVAL); 922 923 status = pj_mutex_lock(mutex->read_lock); 924 if (status != PJ_SUCCESS) { 925 pj_assert(!"This pretty much is unexpected"); 926 return status; 927 } 928 929 mutex->reader_count++; 930 931 pj_assert(mutex->reader_count < 0x7FFFFFF0L); 932 933 if (mutex->reader_count == 1) 934 pj_sem_wait(mutex->write_lock); 935 936 status = pj_mutex_unlock(mutex->read_lock); 937 return status; 938 } 939 940 /* 941 * Lock the mutex for writing. 942 * 943 */ 944 PJ_DEF(pj_status_t) pj_rwmutex_lock_write(pj_rwmutex_t *mutex) 945 { 946 PJ_ASSERT_RETURN(mutex, PJ_EINVAL); 947 return pj_sem_wait(mutex->write_lock); 948 } 949 950 /* 951 * Release read lock. 952 * 953 */ 954 PJ_DEF(pj_status_t) pj_rwmutex_unlock_read(pj_rwmutex_t *mutex) 955 { 956 pj_status_t status; 957 958 PJ_ASSERT_RETURN(mutex, PJ_EINVAL); 959 960 status = pj_mutex_lock(mutex->read_lock); 961 if (status != PJ_SUCCESS) { 962 pj_assert(!"This pretty much is unexpected"); 963 return status; 964 } 965 966 pj_assert(mutex->reader_count >= 1); 967 968 --mutex->reader_count; 969 if (mutex->reader_count == 0) 970 pj_sem_post(mutex->write_lock); 971 972 status = pj_mutex_unlock(mutex->read_lock); 973 return status; 974 } 975 976 /* 977 * Release write lock. 978 * 979 */ 980 PJ_DEF(pj_status_t) pj_rwmutex_unlock_write(pj_rwmutex_t *mutex) 981 { 982 PJ_ASSERT_RETURN(mutex, PJ_EINVAL); 983 pj_assert(mutex->reader_count <= 1); 984 return pj_sem_post(mutex->write_lock); 985 } 986 987 988 /* 989 * Destroy reader/writer mutex. 990 * 991 */ 992 PJ_DEF(pj_status_t) pj_rwmutex_destroy(pj_rwmutex_t *mutex) 993 { 994 PJ_ASSERT_RETURN(mutex, PJ_EINVAL); 995 pj_mutex_destroy(mutex->read_lock); 996 pj_sem_destroy(mutex->write_lock); 997 return PJ_SUCCESS; 998 } 871 /* 872 * Win32 lacks Read/Write mutex, so include the emulation. 873 */ 874 #include "os_rwmutex.c" 999 875 1000 876 ///////////////////////////////////////////////////////////////////////////////
Note: See TracChangeset
for help on using the changeset viewer.