⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 thread_rwlock.c

📁 自己开发基于P2P通讯的网络服务器
💻 C
字号:
#include "natsvr.h"
#include "thread_rwlock.h"
#include <memory.h>
#include <stdlib.h>

static int thread_rwlock_cleanup(void *data)
{
    thread_rwlock_t *rwlock = (thread_rwlock_t *)data;
    int stat;

    pthread_rwlock_unlock(rwlock->rwlock);
    stat = pthread_rwlock_destroy(rwlock->rwlock);
#ifdef PTHREAD_SETS_ERRNO
    if (stat) {
        stat = errno;
    }
#endif
    return stat;
} 

int thread_rwlock_create(thread_rwlock_t **rwlock)
{
    thread_rwlock_t *new_rwlock;
    int stat;

    new_rwlock = (thread_rwlock_t *)malloc(sizeof(thread_rwlock_t));

    if (new_rwlock == NULL) {
        return -1;
    }
	memset(new_rwlock, 0, sizeof(thread_rwlock_t));

    new_rwlock->rwlock = (pthread_rwlock_t *)malloc(sizeof(pthread_rwlock_t));

    if (new_rwlock->rwlock == NULL) {
        return -1;
    }
	memset(new_rwlock->rwlock, 0, sizeof(pthread_rwlock_t));

    if ((stat = pthread_rwlock_init(new_rwlock->rwlock, NULL))) {
#ifdef PTHREAD_SETS_ERRNO
        stat = errno;
#endif
        thread_rwlock_cleanup(new_rwlock);
        return stat;
    }

    *rwlock = new_rwlock;
    return 0;
}

int thread_rwlock_rdlock(thread_rwlock_t *rwlock)
{
    int stat;

    stat = pthread_rwlock_rdlock(rwlock->rwlock);
#ifdef PTHREAD_SETS_ERRNO
    if (stat) {
        stat = errno;
    }
#endif
    return stat;
}

int thread_rwlock_tryrdlock(thread_rwlock_t *rwlock)
{
    int stat;

    stat = pthread_rwlock_tryrdlock(rwlock->rwlock);
#ifdef PTHREAD_SETS_ERRNO
    if (stat) {
        stat = errno;
    }
#endif
    return stat;
}

int thread_rwlock_wrlock(thread_rwlock_t *rwlock)
{
    int stat;

    stat = pthread_rwlock_wrlock(rwlock->rwlock);
#ifdef PTHREAD_SETS_ERRNO
    if (stat) {
        stat = errno;
    }
#endif
    return stat;
}

int thread_rwlock_trywrlock(thread_rwlock_t *rwlock)
{
    int stat;

    stat = pthread_rwlock_trywrlock(rwlock->rwlock);
#ifdef PTHREAD_SETS_ERRNO
    if (stat) {
        stat = errno;
    }
#endif
    return stat;
}

int thread_rwlock_unlock(thread_rwlock_t *rwlock)
{
    int stat;

    stat = pthread_rwlock_unlock(rwlock->rwlock);
#ifdef PTHREAD_SETS_ERRNO
    if (stat) {
        stat = errno;
    }
#endif
    return stat;
}

int thread_rwlock_destroy(thread_rwlock_t *rwlock)
{
    int stat;
    if ((stat = thread_rwlock_cleanup(rwlock)) == 0) {
        free(rwlock->rwlock);
		free(rwlock);
        return 0;
    }
    return stat;
}


⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -