From 997d89690cfdeab9624b2b9d584bde4394009033 Mon Sep 17 00:00:00 2001 From: file Date: Thu, 28 Dec 2006 19:43:15 +0000 Subject: Backport support for read/write locks. git-svn-id: http://svn.digium.com/svn/asterisk/branches/1.4@49022 f38db490-d61c-443f-a65b-d21fe96a405b --- include/asterisk/autoconfig.h.in | 6 ++++ include/asterisk/lock.h | 66 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 72 insertions(+) (limited to 'include') diff --git a/include/asterisk/autoconfig.h.in b/include/asterisk/autoconfig.h.in index 1e6274026..20e8ba398 100644 --- a/include/asterisk/autoconfig.h.in +++ b/include/asterisk/autoconfig.h.in @@ -250,6 +250,12 @@ /* Define to indicate the ${PRI_DESCRIP} library */ #undef HAVE_PRI +/* Define to 1 if your system has PTHREAD_RWLOCK_INITIALIZER. */ +#undef HAVE_PTHREAD_RWLOCK_INITIALIZER + +/* Define to 1 if your system has PTHREAD_RWLOCK_PREFER_WRITER_NP. */ +#undef HAVE_PTHREAD_RWLOCK_PREFER_WRITER_NP + /* Define to 1 if the system has the type `ptrdiff_t'. */ #undef HAVE_PTRDIFF_T diff --git a/include/asterisk/lock.h b/include/asterisk/lock.h index bcd2fdabe..c2aeecdf5 100644 --- a/include/asterisk/lock.h +++ b/include/asterisk/lock.h @@ -614,6 +614,72 @@ static void __attribute__ ((destructor)) fini_##mutex(void) \ #define pthread_create __use_ast_pthread_create_instead__ #endif +typedef pthread_rwlock_t ast_rwlock_t; + +static inline int ast_rwlock_init(ast_rwlock_t *prwlock) +{ + pthread_rwlockattr_t attr; + + pthread_rwlockattr_init(&attr); + +#ifdef HAVE_PTHREAD_RWLOCK_PREFER_WRITER_NP + pthread_rwlockattr_setkind_np(&attr, PTHREAD_RWLOCK_PREFER_WRITER_NP); +#endif + + return pthread_rwlock_init(prwlock, &attr); +} + +static inline int ast_rwlock_destroy(ast_rwlock_t *prwlock) +{ + return pthread_rwlock_destroy(prwlock); +} + +static inline int ast_rwlock_unlock(ast_rwlock_t *prwlock) +{ + return pthread_rwlock_unlock(prwlock); +} + +static inline int ast_rwlock_rdlock(ast_rwlock_t *prwlock) +{ + return pthread_rwlock_rdlock(prwlock); +} + +static inline int ast_rwlock_tryrdlock(ast_rwlock_t *prwlock) +{ + return pthread_rwlock_tryrdlock(prwlock); +} + +static inline int ast_rwlock_wrlock(ast_rwlock_t *prwlock) +{ + return pthread_rwlock_wrlock(prwlock); +} + +static inline int ast_rwlock_trywrlock(ast_rwlock_t *prwlock) +{ + return pthread_rwlock_trywrlock(prwlock); +} + +/* Statically declared read/write locks */ + +#ifndef HAVE_PTHREAD_RWLOCK_INITIALIZER +#define __AST_RWLOCK_DEFINE(scope, rwlock) \ + scope ast_rwlock_t rwlock; \ +static void __attribute__ ((constructor)) init_##rwlock(void) \ +{ \ + ast_rwlock_init(&rwlock); \ +} \ +static void __attribute__ ((destructor)) fini_##rwlock(void) \ +{ \ + ast_rwlock_destroy(&rwlock); \ +} +#else +#define AST_RWLOCK_INIT_VALUE PTHREAD_RWLOCK_INITIALIZER +#define __AST_RWLOCK_DEFINE(scope, rwlock) \ + scope ast_rwlock_t rwlock = AST_RWLOCK_INIT_VALUE +#endif + +#define AST_RWLOCK_DEFINE_STATIC(rwlock) __AST_RWLOCK_DEFINE(static, rwlock) + /* * Initial support for atomic instructions. * For platforms that have it, use the native cpu instruction to -- cgit v1.2.3