-
#include "../unipc.h"
-
#include "pthread_rwlock.h"
-
-
void rwlock_cancelrdwait(void *arg)
-
{
-
my_pthread_rwlock_t *rw;
-
-
rw = (my_pthread_rwlock_t*)arg;
-
rw->rw_nwaitreaders--;
-
pthread_mutex_unlock(&rw->rw_mutex);
-
}
-
-
int my_pthread_rwlock_rdlock(my_pthread_rwlock_t *rw)
-
{
-
int rc;
-
-
if(rw->rw_magic != RW_MAGIC)
-
return EINVAL;
-
-
rc = pthread_mutex_lock(&rw->rw_mutex);
-
if (rc != 0)
-
return rc;
-
-
while((rw->rw_refcount == -1) || (rw->rw_nwaitwriters > 0)) {
-
rw->rw_nwaitreaders++;
-
pthread_cleanup_push(rwlock_cancelrdwait,(void*)rw);
-
rc = pthread_cond_wait(&rw->rw_condreaders,&rw->rw_mutex);
-
pthread_cleanup_pop(0);
-
rw->rw_nwaitreaders--;
-
}
-
-
rw->rw_refcount++;
-
pthread_mutex_unlock(&rw->rw_mutex);
-
return rc;
-
//pthread_cond_signal(&rw->rw_condwriters);
-
}
-
#include "../unipc.h"
-
#include "pthread_rwlock.h"
-
-
void rwlock_cancelrdwait(void *arg)
-
{
-
my_pthread_rwlock_t *rw;
-
-
rw = (my_pthread_rwlock_t*)arg;
-
rw->rw_nwaitreaders--;
-
pthread_mutex_unlock(&rw->rw_mutex);
-
}
-
-
int my_pthread_rwlock_rdlock(my_pthread_rwlock_t *rw)
-
{
-
int rc;
-
-
if(rw->rw_magic != RW_MAGIC)
-
return EINVAL;
-
-
rc = pthread_mutex_lock(&rw->rw_mutex);
-
if (rc != 0)
-
return rc;
-
-
while((rw->rw_refcount == -1) || (rw->rw_nwaitwriters > 0)) {
-
rw->rw_nwaitreaders++;
-
pthread_cleanup_push(rwlock_cancelrdwait,(void*)rw);
-
rc = pthread_cond_wait(&rw->rw_condreaders,&rw->rw_mutex);
-
pthread_cleanup_pop(0);
-
rw->rw_nwaitreaders--;
-
}
-
-
rw->rw_refcount++;
-
pthread_mutex_unlock(&rw->rw_mutex);
-
return rc;
-
//pthread_cond_signal(&rw->rw_condwriters);
-
}
无论何时操作pthread_rwlock_t类型的结构,都必须给mutex上锁。
rw_refcount 等于-1表示写线程持有该读写锁,rw_nwaitwriters表示有线程正等着获取该读写锁的的一个写入锁,此时无法获取读写锁的一个读出锁。
阅读(519) | 评论(0) | 转发(0) |