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

全部博文(142)

文章存档

2016年(10)

2015年(60)

2014年(72)

我的朋友

分类: C/C++

2014-12-16 15:07:42


点击(此处)折叠或打开

  1. #include "../unipc.h"
  2. #include "pthread_rwlock.h"


  3. int my_pthread_rwlock_unlock(pthread_rwlock_t *rw)
  4. {
  5.     int rc;

  6.     if(rw->rw_magic != RW_MAGIC)
  7.         return EINVAL;

  8.     rc = pthread_mutex_lock(&rw->rw_mutex);
  9.     if(rc != 0)
  10.         return rc;
  11.     
  12.     
  13.     if(rw->rw_refcount == -1) {
  14.         rw->rw_refcount = 0;
  15.     }else {
  16.         rw->rw_refcount--;
  17.     }
  18.     /*write's priority is higher than read's priority,prevent continue read will let write wait forever*/
  19.     if(rw->rw_nwaitwriters > 0) {
  20.      /*if the lock is write, then refcount is zero, if the lock is read, maybe refcount is > zero.send cond_signal when all lock is free*/
  21.         if(rw->rw_refcount == 0)
  22.             rc = pthread_cond_signal(&rw->rw_condwriters);
  23.     }else if(rw->rw_nwaitreaders > 0) { /*no writers waits */
  24.         rc = pthread_cond_broadcast(&rw->rw_condreaders);
  25.     }


  26.     pthread_mutex_unlock(&rw->rw_mutex);

  27.     return rc;
  28. }

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