diff --git a/HISTORY b/HISTORY index d8ab971..b4de744 100644 --- a/HISTORY +++ b/HISTORY @@ -1,8 +1,9 @@ -Version 1.54 2021-08-11 +Version 1.54 2021-08-13 * fast_allocator.[hc]: correct reclaim_interval logic * shared_func.[hc]: add functions getFileContentEx1 and getFileContent1 * fc_queue.[hc]: add function fc_queue_timedpeek + * pthread_func.[hc]: add function init_pthread_rwlock Version 1.53 2021-06-30 * process_action support action status diff --git a/src/pthread_func.c b/src/pthread_func.c index 25868b9..2f0290b 100644 --- a/src/pthread_func.c +++ b/src/pthread_func.c @@ -71,6 +71,49 @@ int init_pthread_lock(pthread_mutex_t *pthread_lock) return 0; } +int init_pthread_rwlock(pthread_rwlock_t *rwlock) +{ + pthread_rwlockattr_t attr; + int result; + + if ((result=pthread_rwlockattr_init(&attr)) != 0) { + logError("file: "__FILE__", line: %d, " + "call pthread_rwlockattr_init fail, " + "errno: %d, error info: %s", + __LINE__, result, STRERROR(result)); + return result; + } + +#ifdef OS_LINUX + if ((result=pthread_rwlockattr_setkind_np(&attr, + PTHREAD_RWLOCK_PREFER_WRITER_NONRECURSIVE_NP)) != 0) + { + logError("file: "__FILE__", line: %d, " + "call pthread_rwlockattr_settype fail, " + "errno: %d, error info: %s", + __LINE__, result, STRERROR(result)); + return result; + } +#endif + + if ((result=pthread_rwlock_init(rwlock, &attr)) != 0) { + logError("file: "__FILE__", line: %d, " + "call pthread_rwlock_init fail, " + "errno: %d, error info: %s", + __LINE__, result, STRERROR(result)); + return result; + } + if ((result=pthread_rwlockattr_destroy(&attr)) != 0) { + logError("file: "__FILE__", line: %d, " + "call thread_rwlockattr_destroy fail, " + "errno: %d, error info: %s", + __LINE__, result, STRERROR(result)); + return result; + } + + return 0; +} + int init_pthread_attr(pthread_attr_t *pattr, const int stack_size) { size_t old_stack_size; diff --git a/src/pthread_func.h b/src/pthread_func.h index 02c9080..8f5c0a3 100644 --- a/src/pthread_func.h +++ b/src/pthread_func.h @@ -32,6 +32,7 @@ extern "C" { #endif int init_pthread_lock(pthread_mutex_t *pthread_lock); +int init_pthread_rwlock(pthread_rwlock_t *rwlock); int init_pthread_attr(pthread_attr_t *pattr, const int stack_size); int init_pthread_lock_cond_pair(pthread_lock_cond_pair_t *lcp); @@ -63,6 +64,45 @@ void destroy_pthread_lock_cond_pair(pthread_lock_cond_pair_t *lcp); } while (0) +#define PTHREAD_RWLOCK_WRLOCK(rwlock) \ + do { \ + int rwlock_res; \ + if ((rwlock_res=pthread_rwlock_wrlock(rwlock)) != 0) \ + { \ + logWarning("file: "__FILE__", line: %d, " \ + "call pthread_rwlock_wrlock fail, " \ + "errno: %d, error info: %s", \ + __LINE__, rwlock_res, STRERROR(rwlock_res)); \ + } \ + } while (0) + + +#define PTHREAD_RWLOCK_RDLOCK(rwlock) \ + do { \ + int rwlock_res; \ + if ((rwlock_res=pthread_rwlock_rdlock(rwlock)) != 0) \ + { \ + logWarning("file: "__FILE__", line: %d, " \ + "call pthread_rwlock_rdlock fail, " \ + "errno: %d, error info: %s", \ + __LINE__, rwlock_res, STRERROR(rwlock_res)); \ + } \ + } while (0) + + +#define PTHREAD_RWLOCK_UNLOCK(rwlock) \ + do { \ + int unlock_res; \ + if ((unlock_res=pthread_rwlock_unlock(rwlock)) != 0) \ + { \ + logWarning("file: "__FILE__", line: %d, " \ + "call pthread_rwlock_unlock fail, " \ + "errno: %d, error info: %s", \ + __LINE__, unlock_res, STRERROR(unlock_res)); \ + } \ + } while (0) + + #define lcp_timedwait_sec(lcp, timeout) \ fc_timedwait_sec(&(lcp)->lock, &(lcp)->cond, timeout)