00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00023 #ifndef ARTP_RWLOCKS_H
00024 #define ARTP_RWLOCKS_H 1
00025
00026
00030 #define RW_INIT(x, y, z, wsem, rsem, wcount, rcount) {\
00031 if ((pthread_mutex_init(&x, NULL) != 0)\
00032 || (pthread_mutex_init(&y, NULL) != 0)\
00033 || (pthread_mutex_init(&z, NULL) != 0)\
00034 || (pthread_mutex_init(&wsem, NULL) != 0)\
00035 || (pthread_mutex_init(&rsem, NULL) != 0))\
00036 return E_MEMORY_FAIL;\
00037 wcount = 0;\
00038 rcount = 0;\
00039 }
00040
00041
00047 #define READERS_LOCK(x, y, z, wsem, rsem, wcount, rcount) {\
00048 pthread_mutex_lock(&z);\
00049 pthread_mutex_lock(&rsem);\
00050 pthread_mutex_lock(&x);\
00051 (rcount)++;\
00052 if ((rcount) == 1)\
00053 pthread_mutex_lock(&wsem);\
00054 pthread_mutex_unlock(&x);\
00055 pthread_mutex_unlock(&rsem);\
00056 pthread_mutex_unlock(&z);\
00057 }
00058
00059
00064 #define READERS_UNLOCK(x, y, z, wsem, rsem, wcount, rcount) {\
00065 pthread_mutex_lock(&x);\
00066 (rcount)--;\
00067 if ((rcount) == 0)\
00068 pthread_mutex_unlock(&wsem);\
00069 pthread_mutex_unlock(&x);\
00070 }
00071
00072
00078 #define WRITERS_LOCK(x, y, z, wsem, rsem, wcount, rcount) {\
00079 pthread_mutex_lock(&y);\
00080 (wcount)++;\
00081 if ((wcount) == 1)\
00082 pthread_mutex_lock(&rsem);\
00083 pthread_mutex_unlock(&y);\
00084 pthread_mutex_lock(&wsem);\
00085 }
00086
00087
00092 #define WRITERS_UNLOCK(x, y, z, wsem, rsem, wcount, rcount) {\
00093 pthread_mutex_unlock(&wsem);\
00094 pthread_mutex_lock(&y);\
00095 (wcount)--;\
00096 if ((wcount) == 0)\
00097 pthread_mutex_unlock(&rsem);\
00098 pthread_mutex_unlock(&y);\
00099 }
00100
00101
00102 #endif
00103
00104