前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >POSIX之Reader-Writer Lock

POSIX之Reader-Writer Lock

作者头像
Taishan3721
发布2022-12-05 15:51:40
3400
发布2022-12-05 15:51:40
举报
文章被收录于专栏:这里只有VxWorks

VxWorks从69开始,提供了完善的读写信号量。而在Vx7RTP里实现了Posix定义的Reader-Writer Lock(读写锁),这是Thread库的一种互斥机制,比Mutex有着更高的适用性。可以有多个Thread以Reader的模式同时lock读写锁,或者只有一个Thread以Writer的模式lock读写锁。这种锁适用对数据进行读的次数比写的次数多的情况,因为读锁可以进行共享

代码语言:javascript
复制
/*
 * 分配读写锁的资源,使用pAttr初始化为unlocked状态 
 * 如果pAttr为NULL,则使用默认属性
 */
int pthread_rwlock_init(pthread_rwlock_t *pRwlock, pthread_rwlockattr_t *pAttr);
/* 销毁读写锁,释放其资源 */
int pthread_rwlock_destroy(pthread_rwlock_t *pRwlock);

使用pthread_rwlock_init(),根据属性pAttr来初始化pRwlock。如果pAttr为NULL,则使用默认属性

代码语言:javascript
复制
/* Vx7 RTP */
typedef struct
    {
    int          rwlockAttrStatus;
    int          rwlockAttrPshared;
    unsigned int rwlockAttrMaxReaders;
    unsigned int rwlockAttrConformOpt;
    }pthread_rwlockattr_t;

与属性相关的API有这些

代码语言:javascript
复制
/*
 * rwlockAttrStatus
 *   PTHREAD_INITIALIZED_OBJ - 默认值
 *   PTHREAD_DESTROYED_OBJ
 */
int pthread_rwlockattr_init(pthread_rwlockattr_t *pAttr);
int pthread_rwlockattr_destroy(pthread_rwlockattr_t *pAttr);
/*
 * rwlockAttrPshared
 *   PTHREAD_PROCESS_PRIVATE - 默认值
 *   PTHREAD_PROCESS_SHARED
 */
int pthread_rwlockattr_getpshared(pthread_rwlockattr_t *pAttr, int *pShared);
int pthread_rwlockattr_setpshared(pthread_rwlockattr_t *pAttr, int shared);
/*
 * rwlockAttrMaxReaders
 *   SEM_RW_MAX_CONCURRENT_READERS - 默认值,也是最大值
 */
int pthread_rwlockattr_setmaxreaders(pthread_rwlockattr_t *pAttr, unsigned int maxNumReaders);
/*
 * rwlockAttrConformOpt
 *   1 - 默认值
 *   0 - 表示允许Writer多次write lock读写锁
 */
int pthread_rwlockattr_setconformance(pthread_rwlockattr_t *pAttr, unsigned int confVar);

操作Reader-Writer Lock的API有

代码语言:javascript
复制
/*
 * 给pRwlock上锁,用于reading
 * 如果没有writer,给pRwlock加一个read lock,否则阻塞 
 * 如果当前Thread已经持有read lock,可以再次lock,但要unlock同样次数 
 * 如果read lock已经达到上限,则返回EAGAIN 
 * 如果当前Thread已经持有write lock,返回EDEADLK
 */
int pthread_rwlock_rdlock(pthread_rwlockpRwlock_t *pRwlock);
/*
 * 类似于pthread_rwlock_rdlock()
 * 但有write lock,或writer阻塞时,直接返回EBUSY
 */
int pthread_rwlock_tryrdlock(pthread_rwlock_t *pRwlock);
/*
 * 类似于pthread_rwlock_rdlock()
 * 但阻塞到绝对时间pAbstime时,返回ETIMEDOUT
 */
int pthread_rwlock_timedrdlock(pthread_rwlock_t *pRwlock, struct timespec *pAbstime); 
/*
 * 给pRwlock上锁,用于writing
 * 如果pRwlock空闲,给pRwlock加一个write lock,否则阻塞 
 * 如果当前Thread已经持有read lock,返回EDEADLK
 * 如果当前Thread已经持有write lock,且rwlockAttrConformOpt为1,返回EDEADLK
 * 如果当前Thread已经持有write lock,且rwlockAttrConformOpt为0,则上锁成功,但要解锁同样次数
 */
int pthread_rwlock_wrlock(pthread_rwlock_t *pRwlock);
/*
 * 类似于pthread_rwlock_wrlock()
 * 但不同于pthread_rwlock_wrlock()的阻塞,而是直接返回EBUSY
 */
int pthread_rwlock_trywrlock(pthread_rwlock_t *pRwlock);
/*
 * 类似于pthread_rwlock_wrlock()
 * 但阻塞到绝对时间pAbstime时,返回ETIMEDOUT
 */
int pthread_rwlock_timedwrlock(pthread_rwlock_t *pRwlock, struct timespec *pAbstime); 
/*
 * 解锁当前Thread之前lock的pRwlock
 *
 * 如果释放的是read锁,而该锁还被其它Thread以read锁方式持有,则该锁保持read锁的锁定状态 
 * 如果释放的是最后一个read锁,则置为未锁定状态 
 *
 * 如果释放的是write锁,则置为未锁定状态
 * 当有write锁和read锁同时阻塞在该锁上时,write锁优先
 * 当有多个write锁或只有多个read锁时,优先级最高者优先
 */
int pthread_rwlock_unlock(pthread_rwlock_t *pRwlock);

在Vx7里写个RTP的例子

代码语言:javascript
复制
/*
 * 版权所有  公众号  VxWorks567
 * Vx7 RTP
 */

#include <stdio.h>    /* printf() */
#include <unistd.h>   /* sleep() */
#include <pthread.h>  /* pthread_create() */

static pthread_rwlock_t rwlockid;

static void *thread1(void *arg)
{
    int ret;

    printf("POSIX RWLOCK: in thread1, pthread_rwlock_rdlock begin\n");
    ret = pthread_rwlock_rdlock (&rwlockid);
    printf("POSIX RWLOCK: in thread1, pthread_rwlock_rdlock end\n\n");
    sleep(2);
    printf("POSIX RWLOCK: in thread1, pthread_rwlock_unlock begin\n");
    ret = pthread_rwlock_unlock (&rwlockid);
    printf("POSIX RWLOCK: in thread1, pthread_rwlock_unlock end\n");

    sleep(1);

    /* #define EBUSY   16  Device or resource busy */
    printf("POSIX RWLOCK: in thread1, pthread_rwlock_tryrdlock begin\n");
    ret = pthread_rwlock_tryrdlock (&rwlockid);
    printf("POSIX RWLOCK: in thread1, pthread_rwlock_tryrdlock end, ret=%d\n", ret);

    printf("POSIX RWLOCK: in thread1, pthread_rwlock_rdlock begin\n\n");
    ret = pthread_rwlock_rdlock (&rwlockid);
    printf("POSIX RWLOCK: in thread1, pthread_rwlock_rdlock end\n");

    printf("POSIX RWLOCK: in thread1, pthread_rwlock_unlock begin\n");
    ret = pthread_rwlock_unlock (&rwlockid);
    printf("POSIX RWLOCK: in thread1, pthread_rwlock_unlock end\n");
    return NULL;
    }
    
static void *thread2(void *arg)
{
    int ret;

    sleep(1);
    printf("POSIX RWLOCK: in thread2, pthread_rwlock_rdlock begin\n");
    ret = pthread_rwlock_rdlock (&rwlockid);
    printf("POSIX RWLOCK: in thread2, pthread_rwlock_rdlock end\n");

    printf("POSIX RWLOCK: in thread2, pthread_rwlock_unlock begin\n");
    ret = pthread_rwlock_unlock (&rwlockid);
    printf("POSIX RWLOCK: in thread2, pthread_rwlock_unlock end\n");

#if 0 /* #define EBUSY   16  Device or resource busy */
    printf("POSIX RWLOCK: in thread2, pthread_rwlock_rdlock begin\n");
    ret = pthread_rwlock_rdlock (&rwlockid);
    printf("POSIX RWLOCK: in thread2, pthread_rwlock_rdlock end\n");

    printf("POSIX RWLOCK: in thread2, pthread_rwlock_trywrlock begin\n\n");
    ret = pthread_rwlock_trywrlock (&rwlockid);
    printf("POSIX RWLOCK: in thread2, pthread_rwlock_trywrlock end, ret=%d\n", ret);
#endif

    printf("POSIX RWLOCK: in thread2, pthread_rwlock_wrlock begin\n\n");
    ret = pthread_rwlock_wrlock (&rwlockid);
    printf("POSIX RWLOCK: in thread2, pthread_rwlock_wrlock end, ret=%d\n\n", ret);

#if 0 /* #define EDEADLK 35 Resource deadlock would occur */
    printf("POSIX RWLOCK: in thread2, pthread_rwlock_rdlock begin\n");
    ret = pthread_rwlock_rdlock (&rwlockid);
    printf("POSIX RWLOCK: in thread2, pthread_rwlock_rdlock end, ret=%d\n", ret);
#endif

    sleep(2);
    printf("POSIX RWLOCK: in thread2, pthread_rwlock_unlock begin\n");
    ret = pthread_rwlock_unlock (&rwlockid);
    printf("POSIX RWLOCK: in thread2, pthread_rwlock_unlock end\n");
    return NULL;
    }

int main()
{
    pthread_t tid1;
    pthread_t tid2;
    int ret;

    ret = pthread_rwlock_init (&rwlockid, NULL);
    printf("POSIX RWLOCK: in thread0, pthread_rwlock_init\n");

    ret = pthread_create(&tid1, NULL, &thread1, NULL);
    ret = pthread_create(&tid2, NULL, &thread2, NULL);
    ret = pthread_join(tid1, NULL);
    ret = pthread_join(tid2, NULL);

    ret = pthread_rwlock_destroy (&rwlockid);
    printf("POSIX RWLOCK: in thread0, pthread_rwlock_destroy\n");
    return 0;
    }

这个读写锁是不是与读写信号量非常非常的像呢?

本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2022-07-11,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 这里只有VxWorks 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档