Ignore:
Timestamp:
Feb 7, 2006 10:17:55 PM (18 years ago)
Author:
bennylp
Message:

Implement rwlock for Linux

File:
1 edited

Legend:

Unmodified
Added
Removed
  • pjproject/trunk/pjlib/src/pj/os_core_unix.c

    r66 r150  
    1717 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA  
    1818 */ 
     19#define _GNU_SOURCE 
    1920#include <pj/os.h> 
    2021#include <pj/assert.h> 
     
    3536#include <errno.h>          // errno 
    3637 
    37 #define __USE_GNU 
    38 //uncomment this to get pthread_mutexattr_settype declaration. 
    39 //unfortunately this causes syntax error in pthread.h! :( 
    40 //#define __USE_UNIX98 
    4138#include <pthread.h> 
    4239 
     
    999996} 
    1000997#endif 
     998 
     999/////////////////////////////////////////////////////////////////////////////// 
     1000struct pj_rwmutex_t 
     1001{ 
     1002    pthread_rwlock_t rwlock; 
     1003}; 
     1004 
     1005PJ_DEF(pj_status_t) pj_rwmutex_create(pj_pool_t *pool, const char *name, 
     1006                                      pj_rwmutex_t **p_mutex) 
     1007{ 
     1008    pj_rwmutex_t *rwm; 
     1009    pj_status_t status; 
     1010 
     1011    PJ_UNUSED_ARG(name); 
     1012     
     1013    rwm = pj_pool_alloc(pool, sizeof(pj_rwmutex_t)); 
     1014    PJ_ASSERT_RETURN(rwm, PJ_ENOMEM); 
     1015 
     1016    status = pthread_rwlock_init(&rwm->rwlock, NULL); 
     1017    if (status != 0) 
     1018        return PJ_RETURN_OS_ERROR(status); 
     1019 
     1020    *p_mutex = rwm; 
     1021    return PJ_SUCCESS; 
     1022} 
     1023 
     1024/* 
     1025 * Lock the mutex for reading. 
     1026 * 
     1027 */ 
     1028PJ_DEF(pj_status_t) pj_rwmutex_lock_read(pj_rwmutex_t *mutex) 
     1029{ 
     1030    pj_status_t status; 
     1031 
     1032    status = pthread_rwlock_rdlock(&mutex->rwlock); 
     1033    if (status != 0) 
     1034        return PJ_RETURN_OS_ERROR(status); 
     1035 
     1036    return PJ_SUCCESS; 
     1037} 
     1038 
     1039/* 
     1040 * Lock the mutex for writing. 
     1041 * 
     1042 */ 
     1043PJ_DEF(pj_status_t) pj_rwmutex_lock_write(pj_rwmutex_t *mutex) 
     1044{ 
     1045    pj_status_t status; 
     1046 
     1047    status = pthread_rwlock_wrlock(&mutex->rwlock); 
     1048    if (status != 0) 
     1049        return PJ_RETURN_OS_ERROR(status); 
     1050 
     1051    return PJ_SUCCESS; 
     1052} 
     1053 
     1054/* 
     1055 * Release read lock. 
     1056 * 
     1057 */ 
     1058PJ_DEF(pj_status_t) pj_rwmutex_unlock_read(pj_rwmutex_t *mutex) 
     1059{ 
     1060    return pj_rwmutex_unlock_write(mutex); 
     1061} 
     1062 
     1063/* 
     1064 * Release write lock. 
     1065 * 
     1066 */ 
     1067PJ_DEF(pj_status_t) pj_rwmutex_unlock_write(pj_rwmutex_t *mutex) 
     1068{ 
     1069    pj_status_t status; 
     1070 
     1071    status = pthread_rwlock_unlock(&mutex->rwlock); 
     1072    if (status != 0) 
     1073        return PJ_RETURN_OS_ERROR(status); 
     1074 
     1075    return PJ_SUCCESS; 
     1076} 
     1077 
     1078/* 
     1079 * Destroy reader/writer mutex. 
     1080 * 
     1081 */ 
     1082PJ_DEF(pj_status_t) pj_rwmutex_destroy(pj_rwmutex_t *mutex) 
     1083{ 
     1084    pj_status_t status; 
     1085 
     1086    status = pthread_rwlock_destroy(&mutex->rwlock); 
     1087    if (status != 0) 
     1088        return PJ_RETURN_OS_ERROR(status); 
     1089 
     1090    return PJ_SUCCESS; 
     1091} 
    10011092 
    10021093/////////////////////////////////////////////////////////////////////////////// 
Note: See TracChangeset for help on using the changeset viewer.