Chinaunix首页 | 论坛 | 博客
  • 博客访问: 535964
  • 博文数量: 142
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 1452
  • 用 户 组: 普通用户
  • 注册时间: 2013-09-12 16:28
文章分类

全部博文(142)

文章存档

2016年(10)

2015年(60)

2014年(72)

我的朋友

分类: C/C++

2014-12-16 15:10:42


点击(此处)折叠或打开

  1. #include "../unipc.h"
  2. #include "pthread_rwlock.h"
  3. void rwlock_cancelwrwait(void *arg)
  4. {
  5.     int rc;
  6.     my_pthread_rwlock_t * rw;

  7.     rw = (my_pthread_rwlock_t *)arg;

  8.     rw->rw_nwaitwriters--;
  9.     rc = pthread_mutex_unlock(&rw->rw_mutex);
  10. }

  11. int my_pthread_rwlock_wrlock(my_pthread_rwlock_t *rw)
  12. {
  13.     int rc;

  14.     if(rw->rw_magic != RW_MAGIC)
  15.         return EINVAL;

  16.     rc = pthread_mutex_lock(&rw->rw_mutex);
  17.     if(rc != 0)
  18.         return rc;

  19.     while(rw->rw_refcount != 0) {
  20.         rw->rw_nwaitwriters++;
  21.         pthread_cleanup_push(rwlock_cancelwrwait,(void*)rw);
  22.         rc = pthread_cond_wait(&rw->rw_condwriters,&rw->rw_mutex);
  23.         pthread_cleanup_pop(0);
  24.         rw->rw_nwaitwriters--;
  25.         if(rc != 0)
  26.             break;
  27.     }
  28.     rw->rw_refcount = -1;
  29.     pthread_mutex_unlock(&rw->rw_mutex);
  30.     return rc;
  31. }


阅读(446) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~