Two other trunk build fixes (reported by seanbright on #asterisk-dev)
[asterisk/asterisk.git] / main / threadstorage.c
index 35d3eca..83c9ebe 100644 (file)
@@ -52,7 +52,24 @@ struct tls_object {
        AST_LIST_ENTRY(tls_object) entry;
 };
 
-static AST_RWLIST_HEAD_STATIC(tls_objects, tls_object);
+static AST_LIST_HEAD_NOLOCK_STATIC(tls_objects, tls_object);
+
+/* Allow direct use of pthread_mutex_t and friends */
+#undef pthread_mutex_t
+#undef pthread_mutex_lock
+#undef pthread_mutex_unlock
+#undef pthread_mutex_init
+#undef pthread_mutex_destroy
+
+/*!
+ * \brief lock for the tls_objects list
+ *
+ * \note We can not use an ast_mutex_t for this.  The reason is that this
+ *       lock is used within the context of thread-local data destructors,
+ *       and the ast_mutex_* API uses thread-local data.  Allocating more
+ *       thread-local data at that point just causes a memory leak.
+ */
+static pthread_mutex_t threadstoragelock;
 
 void __ast_threadstorage_object_add(void *key, size_t len, const char *file, const char *function, unsigned int line)
 {
@@ -68,16 +85,16 @@ void __ast_threadstorage_object_add(void *key, size_t len, const char *file, con
        to->line = line;
        to->thread = pthread_self();
 
-       AST_RWLIST_WRLOCK(&tls_objects);
+       pthread_mutex_lock(&threadstoragelock);
        AST_LIST_INSERT_TAIL(&tls_objects, to, entry);
-       AST_RWLIST_UNLOCK(&tls_objects);
+       pthread_mutex_unlock(&threadstoragelock);
 }
 
 void __ast_threadstorage_object_remove(void *key)
 {
        struct tls_object *to;
 
-       AST_RWLIST_WRLOCK(&tls_objects);
+       pthread_mutex_lock(&threadstoragelock);
        AST_LIST_TRAVERSE_SAFE_BEGIN(&tls_objects, to, entry) {
                if (to->key == key) {
                        AST_LIST_REMOVE_CURRENT(entry);
@@ -85,7 +102,7 @@ void __ast_threadstorage_object_remove(void *key)
                }
        }
        AST_LIST_TRAVERSE_SAFE_END;
-       AST_RWLIST_UNLOCK(&tls_objects);
+       pthread_mutex_unlock(&threadstoragelock);
        if (to)
                ast_free(to);
 }
@@ -94,7 +111,7 @@ void __ast_threadstorage_object_replace(void *key_old, void *key_new, size_t len
 {
        struct tls_object *to;
 
-       AST_RWLIST_WRLOCK(&tls_objects);
+       pthread_mutex_lock(&threadstoragelock);
        AST_LIST_TRAVERSE_SAFE_BEGIN(&tls_objects, to, entry) {
                if (to->key == key_old) {
                        to->key = key_new;
@@ -103,12 +120,12 @@ void __ast_threadstorage_object_replace(void *key_old, void *key_new, size_t len
                }
        }
        AST_LIST_TRAVERSE_SAFE_END;
-       AST_RWLIST_UNLOCK(&tls_objects);
+       pthread_mutex_unlock(&threadstoragelock);
 }
 
 static char *handle_cli_threadstorage_show_allocations(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
 {
-       char *fn = NULL;
+       const char *fn = NULL;
        size_t len = 0;
        unsigned int count = 0;
        struct tls_object *to;
@@ -131,7 +148,7 @@ static char *handle_cli_threadstorage_show_allocations(struct ast_cli_entry *e,
        if (a->argc > 3)
                fn = a->argv[3];
 
-       AST_RWLIST_RDLOCK(&tls_objects);
+       pthread_mutex_lock(&threadstoragelock);
 
        AST_LIST_TRAVERSE(&tls_objects, to, entry) {
                if (fn && strcasecmp(to->file, fn))
@@ -143,7 +160,7 @@ static char *handle_cli_threadstorage_show_allocations(struct ast_cli_entry *e,
                count++;
        }
 
-       AST_RWLIST_UNLOCK(&tls_objects);
+       pthread_mutex_unlock(&threadstoragelock);
 
        ast_cli(a->fd, "%10d bytes allocated in %d allocation%s\n", (int) len, count, count > 1 ? "s" : "");
        
@@ -152,7 +169,7 @@ static char *handle_cli_threadstorage_show_allocations(struct ast_cli_entry *e,
 
 static char *handle_cli_threadstorage_show_summary(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
 {
-       char *fn = NULL;
+       const char *fn = NULL;
        size_t len = 0;
        unsigned int count = 0;
        struct tls_object *to;
@@ -182,7 +199,7 @@ static char *handle_cli_threadstorage_show_summary(struct ast_cli_entry *e, int
        if (a->argc > 3)
                fn = a->argv[3];
 
-       AST_RWLIST_RDLOCK(&tls_objects);
+       pthread_mutex_lock(&threadstoragelock);
 
        AST_LIST_TRAVERSE(&tls_objects, to, entry) {
                if (fn && strcasecmp(to->file, fn))
@@ -204,7 +221,7 @@ static char *handle_cli_threadstorage_show_summary(struct ast_cli_entry *e, int
                file->count++;
        }
 
-       AST_RWLIST_UNLOCK(&tls_objects);
+       pthread_mutex_unlock(&threadstoragelock);
        
        AST_LIST_TRAVERSE(&file_summary, file, entry) {
                len += file->len;
@@ -230,7 +247,8 @@ static struct ast_cli_entry cli[] = {
 
 void threadstorage_init(void)
 {
-       ast_cli_register_multiple(cli, sizeof(cli) / sizeof(cli[0]));
+       pthread_mutex_init(&threadstoragelock, NULL);
+       ast_cli_register_multiple(cli, ARRAY_LEN(cli));
 }
 
 #endif /* !defined(DEBUG_THREADLOCALS) */