From 78ab76b46ca50cdc544cd820cc187b4a2f6e5312 Mon Sep 17 00:00:00 2001 From: Richard Mudgett <rmudgett@digium.com> Date: Fri, 2 Oct 2015 17:05:16 -0500 Subject: [PATCH] Fix issue with AST_THREADSTORAGE_RAW when DEBUG_THREADLOCALS is enabled. When DEBUG_THREADLOCALS is enabled it causes the threadlocal cleanup to be called as a function. This causes a compile error with raw threadstorage as it uses NULL for cleanup. This fix uses a macro that provides NULL when DEBUG_THREADLOCALS is disabled, and replaces the call to "c_cleanup(data);" with "{};" when DEBUG_THREADLOCALS is enabled. ASTERISK-24975 #close Reported by: Ashley Sanders **** ASTERISK-24975 Change-Id: I3ef7428ee402816d9fcefa1b3b95830c00d5c402 Cherry-pick from v13 with additional definitions of AST_THREADSTORAGE_RAW(), ast_threadstorage_get_ptr() and ast_threadstorage_set_ptr() from commit d01706ce1ee518118456d5673f529204bdac73bb. Change-Id: I3222102d005f76744561b95a3b97700d82a5ee58 --- include/asterisk/threadstorage.h | 43 ++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) diff --git a/include/asterisk/threadstorage.h b/include/asterisk/threadstorage.h index e3ece8b67f..4e61f42d2f 100644 --- a/include/asterisk/threadstorage.h +++ b/include/asterisk/threadstorage.h @@ -64,6 +64,9 @@ struct ast_threadstorage { void __ast_threadstorage_object_add(void *key, size_t len, const char *file, const char *function, unsigned int line); void __ast_threadstorage_object_remove(void *key); void __ast_threadstorage_object_replace(void *key_old, void *key_new, size_t len); +#define THREADSTORAGE_RAW_CLEANUP(v) {} +#else +#define THREADSTORAGE_RAW_CLEANUP NULL #endif /* defined(DEBUG_THREADLOCALS) */ /*! @@ -84,6 +87,8 @@ void __ast_threadstorage_object_replace(void *key_old, void *key_new, size_t len AST_THREADSTORAGE_CUSTOM_SCOPE(name, NULL, ast_free_ptr,) #define AST_THREADSTORAGE_EXTERNAL(name) \ extern struct ast_threadstorage name +#define AST_THREADSTORAGE_RAW(name) \ + AST_THREADSTORAGE_CUSTOM_SCOPE(name, NULL, THREADSTORAGE_RAW_CLEANUP,) /*! * \brief Define a thread storage variable, with custom initialization and cleanup @@ -216,4 +221,42 @@ void *__ast_threadstorage_get(struct ast_threadstorage *ts, size_t init_size, co #define ast_threadstorage_get(ts, init_size) __ast_threadstorage_get(ts, init_size, __FILE__, __PRETTY_FUNCTION__, __LINE__) #endif /* defined(DEBUG_THREADLOCALS) */ +/*! + * \brief Retrieve a raw pointer from threadstorage. + * \param ts Threadstorage object to operate on. + * + * \return A pointer associated with the current thread, NULL + * if no pointer is associated yet. + * + * \note This should only be used on threadstorage declared + * by AST_THREADSTORAGE_RAW unless you really know what + * you are doing. + */ +AST_INLINE_API( +void *ast_threadstorage_get_ptr(struct ast_threadstorage *ts), +{ + pthread_once(&ts->once, ts->key_init); + return pthread_getspecific(ts->key); +} +) + +/*! + * \brief Set a raw pointer from threadstorage. + * \param ts Threadstorage object to operate on. + * + * \retval 0 Success + * \retval non-zero Failure + * + * \note This should only be used on threadstorage declared + * by AST_THREADSTORAGE_RAW unless you really know what + * you are doing. + */ +AST_INLINE_API( +int ast_threadstorage_set_ptr(struct ast_threadstorage *ts, void *ptr), +{ + pthread_once(&ts->once, ts->key_init); + return pthread_setspecific(ts->key, ptr); +} +) + #endif /* ASTERISK_THREADSTORAGE_H */ -- GitLab