From cc602b252fe83219c927d0c0a272e8e5cc9f0fc2 Mon Sep 17 00:00:00 2001 From: Eric Wong Date: Wed, 27 Aug 2008 22:35:21 -0700 Subject: tagTracker: locks around {get,remove}TagItemString Hopefully this fixes a segfault I experienced inside freeMpdTag earlier with the metadata_pipe. I could not reproduce the segfault again, however. Regardless, if multiple threads rely on this, we need to atomically increment/decrement these counters. --- src/tagTracker.c | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/src/tagTracker.c b/src/tagTracker.c index c44ce8199..acc8a4d2e 100644 --- a/src/tagTracker.c +++ b/src/tagTracker.c @@ -22,6 +22,7 @@ #include "tree.h" #include "utils.h" #include "myfprintf.h" +#include "os_compat.h" static Tree *tagTrees[TAG_NUM_OF_ITEM_TYPES]; @@ -30,9 +31,14 @@ typedef struct tagTrackerItem { mpd_sint8 visited; } TagTrackerItem; +static pthread_mutex_t tag_item_lock = PTHREAD_MUTEX_INITIALIZER; + char *getTagItemString(int type, char *string) { TreeIterator iter; + char *ret; + + pthread_mutex_lock(&tag_item_lock); if (tagTrees[type] == NULL) { @@ -44,7 +50,7 @@ char *getTagItemString(int type, char *string) if (FindInTree(tagTrees[type], string, &iter)) { ((TagTrackerItem *)GetTreeKeyData(&iter)->data)->count++; - return (char *)GetTreeKeyData(&iter)->key; + ret = (char *)GetTreeKeyData(&iter)->key; } else { @@ -53,8 +59,11 @@ char *getTagItemString(int type, char *string) item->count = 1; item->visited = 0; InsertInTree(tagTrees[type], key, item); - return key; + ret = key; } + + pthread_mutex_unlock(&tag_item_lock); + return ret; } void removeTagItemString(int type, char *string) @@ -67,6 +76,7 @@ void removeTagItemString(int type, char *string) if (tagTrees[type] == NULL) return; + pthread_mutex_lock(&tag_item_lock); if (FindInTree(tagTrees[type], string, &iter)) { TagTrackerItem * item = @@ -81,6 +91,7 @@ void removeTagItemString(int type, char *string) FreeTree(tagTrees[type]); tagTrees[type] = NULL; } + pthread_mutex_unlock(&tag_item_lock); } int getNumberOfTagItems(int type) -- cgit v1.2.3