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

📄 thread_rwlock.h

📁 自己开发基于P2P通讯的网络服务器
💻 H
字号:


#ifndef THREAD_RWLOCK_H
#define THREAD_RWLOCK_H

/* this gives us pthread_rwlock_t */
#include <pthread.h>
#include <errno.h>


#define PTHREAD_SETS_ERRNO


typedef struct  thread_rwlock_t {
    pthread_rwlock_t *rwlock;
}thread_rwlock_t;


/**
 * Create and initialize a read-write lock that can be used to synchronize
 * threads.
 * @param rwlock the memory address where the newly created readwrite lock
 *        will be stored.
 * @param pool the pool from which to allocate the mutex.
 */
int thread_rwlock_create(thread_rwlock_t **rwlock); 
/**
 * Acquire a shared-read lock on the given read-write lock. This will allow
 * multiple threads to enter the same critical section while they have acquired
 * the read lock.
 * @param rwlock the read-write lock on which to acquire the shared read.
 */
int thread_rwlock_rdlock(thread_rwlock_t *rwlock);

/**
 * Attempt to acquire the shread-read lock on the given read-write lock. This
 * is the same as thread_rwlock_rdlock(), only that the funtion fails
 * if there is another thread holding the write lock, or if there are any
 * write threads blocking on the lock. If the function failes for this case,
 * APR_EBUSY will be returned. Note: it is important that the
 * APR_STATUS_IS_EBUSY(s) macro be used to determine if the return value was
 * APR_EBUSY, for portability reasons.
 * @param rwlock the rwlock on which to attempt the shared read.
 */
int thread_rwlock_tryrdlock(thread_rwlock_t *rwlock);

/**
 * Acquire an exclusive-write lock on the given read-write lock. This will
 * allow only one single thread to enter the critical sections. If there
 * are any threads currently holding thee read-lock, this thread is put to
 * sleep until it can have exclusive access to the lock.
 * @param rwlock the read-write lock on which to acquire the exclusive write.
 */
int thread_rwlock_wrlock(thread_rwlock_t *rwlock);

/**
 * Attempt to acquire the exclusive-write lock on the given read-write lock. 
 * This is the same as thread_rwlock_wrlock(), only that the funtion fails
 * if there is any other thread holding the lock (for reading or writing),
 * in which case the function will return APR_EBUSY. Note: it is important
 * that the APR_STATUS_IS_EBUSY(s) macro be used to determine if the return
 * value was APR_EBUSY, for portability reasons.
 * @param rwlock the rwlock on which to attempt the exclusive write.
 */
int thread_rwlock_trywrlock(thread_rwlock_t *rwlock);

/**
 * Release either the read or write lock currently held by the calling thread
 * associated with the given read-write lock.
 * @param rwlock the read-write lock to be released (unlocked).
 */
int thread_rwlock_unlock(thread_rwlock_t *rwlock);

/**
 * Destroy the read-write lock and free the associated memory.
 * @param rwlock the rwlock to destroy.
 */
int thread_rwlock_destroy(thread_rwlock_t *rwlock);


#endif  /* THREAD_RWLOCK_H */

⌨️ 快捷键说明

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