📄 rwlock.c
字号:
#include <errno.h>
#include "pthread_rwlock.h"
/*
int pthread_rwlock_init(pthread_rwlock_t *rw, int *attr)
{
int result;
if(attr != 0);
return(EINVAL);
if( (result = pthread_mutex_init(&rw->rw_mutex,NULL)) != 0)
goto err1;
if( (result = pthread_cond_init(&rw->rw_condreaders,NULL)) != 0)
goto err2;
if( (result = pthread_cond_init(&rw->rw_condwriter,NULL)) != 0)
goto err3;
rw->rw_nwaitreaders = 0;
rw->rw_nwaitwriters = 0;
rw->rw_magic = 1;
return(0);
err3:
pthread_cond_destroy(&rw->rw_condreaders);
err2:
pthread_mutex_destroy(&rw->rw_mutex);
err1:
return(result);
}
*/
int pthread_rwlock_destroy(pthread_rwlock_t *rw)
{
if(rw -> rw_magic != RW_MAGIC)
return(EINVAL);
if(rw->rw_refcount != 0 ||
rw -> rw_nwaitreaders != 0 || rw -> rw_nwaitwriters !=0)
return(EBUSY);
pthread_mutex_destroy(&rw->rw_mutex);
pthread_cond_destroy(&rw->rw_condreaders);
pthread_cond_destroy(&rw->rw_condwriters);
rw -> rw_magic = 0;
return(0);
}
int pthread_rwlock_rdlock(pthread_rwlock_t *rw)
{
int result;
if(rw -> rw_magic != RW_MAGIC)
return(EINVAL);
if( (result = pthread_mutex_lock(&rw->rw_mutex)) != 0 )
return(result);
while(rw -> rw_refcount < 0 || rw -> rw_nwaitwriters >0)
{
rw -> rw_nwaitreaders++;
result = pthread_cond_wait( &rw -> rw_condreaders, &rw -> rw_mutex);
rw -> rw_nwaitreaders--;
if(result != 0)
break;
}
if(result == 0)
rw -> rw_refcount++;
pthread_mutex_unlock( &rw -> rw_mutex);
return(result);
}
int pthread_rwlock_tryrdlock(pthread_rwlock_t *rw)
{
int result;
if(rw -> rw_magic != RW_MAGIC)
return(EINVAL);
if( (result = pthread_mutex_lock(&rw->rw_mutex)) != 0 )
return(result);
if(rw -> rw_refcount < 0 || rw -> rw_nwaitwriters >0)
result = EBUSY;
else
rw -> rw_refcount++;
pthread_mutex_unlock(&rw->rw_mutex);
return(result);
}
int pthread_rwlock_trywrlock(pthread_rwlock_t *rw)
{
int result;
if(rw -> rw_magic != RW_MAGIC)
return(EINVAL);
if( (result = pthread_mutex_lock(&rw->rw_mutex)) != 0 )
return(result);
if(rw -> rw_refcount != 0)
result = EBUSY;
else
rw -> rw_refcount = -1;
pthread_mutex_unlock(&rw->rw_mutex);
return(result);
}
int pthread_rwlock_wrlock(pthread_rwlock_t *rw)
{
int result;
if(rw -> rw_magic != RW_MAGIC)
return(EINVAL);
if( (result = pthread_mutex_lock(&rw->rw_mutex)) != 0 )
return(result);
while(rw -> rw_refcount != 0)
{
rw -> rw_nwaitwriters++;
result = pthread_cond_wait( &rw -> rw_condwriters, &rw -> rw_mutex);
rw -> rw_nwaitwriters--;
if(result != 0)
break;
}
if(result == 0)
rw -> rw_refcount = -1;
pthread_mutex_unlock( &rw -> rw_mutex);
return(result);
}
int pthread_rwlock_unlock(pthread_rwlock_t *rw)
{
int result;
if(rw -> rw_magic != RW_MAGIC)
return(EINVAL);
if( (result = pthread_mutex_lock(&rw->rw_mutex)) != 0 )
return(result);
if(rw -> rw_refcount > 0)
rw -> rw_refcount--;
else if (rw -> rw_refcount == -1)
rw -> rw_refcount = 0;
else
printf("unlock error!\n");
if(rw -> rw_nwaitwriters > 0)
{
if(rw -> rw_refcount ==0)
result = pthread_cond_signal(&rw -> rw_condwriters);
}
else if (rw -> rw_nwaitreaders > 0)
result = pthread_cond_broadcast(&rw -> rw_condreaders);
pthread_mutex_unlock( &rw -> rw_mutex);
return(result);
}
pthread_rwlock_t rw = PTHREAD_RWLOCK_INITIALIZER;
int i = 10;
void reader1()
{
while(1)
{
pthread_rwlock_rdlock(&rw);
printf("11 is %d\n",i);
sleep(10);
pthread_rwlock_unlock(&rw);
}
}
void reader2()
{
while(1)
{
sleep(2);
pthread_rwlock_rdlock(&rw);
printf("12 is %d\n",i);
pthread_rwlock_unlock(&rw);
usleep(1);
}
}
void writer()
{
sleep(2);
pthread_rwlock_wrlock(&rw);
i = 30;
printf("13 is %d\n",i);
sleep(10);
pthread_rwlock_unlock(&rw);
}
int main()
{
pthread_t id[3];
int i,ret1,ret2,ret3;
ret1 = pthread_create(&id[0],NULL,(void *) reader1,NULL);
ret2 = pthread_create(&id[1],NULL,(void *) reader2,NULL);
// ret3 = pthread_create(&id[2],NULL,(void *) writer,NULL);
pthread_join(id[0],NULL);
pthread_join(id[1],NULL);
pthread_join(id[2],NULL);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -