tag_pool: use GStaticMutex

Eliminates explicit global initialisation.
This commit is contained in:
Max Kellermann
2012-08-08 21:01:25 +02:00
parent 510097cc37
commit c1f90a99f4
9 changed files with 24 additions and 49 deletions

View File

@@ -53,7 +53,6 @@
#include "tag.h"
#include "zeroconf.h"
#include "event_pipe.h"
#include "tag_pool.h"
#include "mpd_error.h"
#ifdef ENABLE_INOTIFY
@@ -359,7 +358,6 @@ int mpd_main(int argc, char *argv[])
io_thread_init();
winsock_init();
idle_init();
tag_pool_init();
config_global_init();
success = parse_cmdline(argc, argv, &options, &error);
@@ -544,7 +542,6 @@ int mpd_main(int argc, char *argv[])
archive_plugin_deinit_all();
#endif
config_global_finish();
tag_pool_deinit();
idle_deinit();
stats_global_finish();
io_thread_deinit();

View File

@@ -168,9 +168,9 @@ static void tag_delete_item(struct tag *tag, unsigned idx)
assert(idx < tag->num_items);
tag->num_items--;
g_mutex_lock(tag_pool_lock);
g_static_mutex_lock(&tag_pool_lock);
tag_pool_put_item(tag->items[idx]);
g_mutex_unlock(tag_pool_lock);
g_static_mutex_unlock(&tag_pool_lock);
if (tag->num_items - idx > 0) {
memmove(tag->items + idx, tag->items + idx + 1,
@@ -202,10 +202,10 @@ void tag_free(struct tag *tag)
assert(tag != NULL);
g_mutex_lock(tag_pool_lock);
g_static_mutex_lock(&tag_pool_lock);
for (i = tag->num_items; --i >= 0; )
tag_pool_put_item(tag->items[i]);
g_mutex_unlock(tag_pool_lock);
g_static_mutex_unlock(&tag_pool_lock);
if (tag->items == bulk.items) {
#ifndef NDEBUG
@@ -231,10 +231,10 @@ struct tag *tag_dup(const struct tag *tag)
ret->num_items = tag->num_items;
ret->items = ret->num_items > 0 ? g_malloc(items_size(tag)) : NULL;
g_mutex_lock(tag_pool_lock);
g_static_mutex_lock(&tag_pool_lock);
for (unsigned i = 0; i < tag->num_items; i++)
ret->items[i] = tag_pool_dup_item(tag->items[i]);
g_mutex_unlock(tag_pool_lock);
g_static_mutex_unlock(&tag_pool_lock);
return ret;
}
@@ -255,7 +255,7 @@ tag_merge(const struct tag *base, const struct tag *add)
ret->num_items = base->num_items + add->num_items;
ret->items = ret->num_items > 0 ? g_malloc(items_size(ret)) : NULL;
g_mutex_lock(tag_pool_lock);
g_static_mutex_lock(&tag_pool_lock);
/* copy all items from "add" */
@@ -270,7 +270,7 @@ tag_merge(const struct tag *base, const struct tag *add)
if (!tag_has_type(add, base->items[i]->type))
ret->items[n++] = tag_pool_dup_item(base->items[i]);
g_mutex_unlock(tag_pool_lock);
g_static_mutex_unlock(&tag_pool_lock);
assert(n <= ret->num_items);
@@ -502,9 +502,9 @@ tag_add_item_internal(struct tag *tag, enum tag_type type,
items_size(tag) - sizeof(struct tag_item *));
}
g_mutex_lock(tag_pool_lock);
g_static_mutex_lock(&tag_pool_lock);
tag->items[i] = tag_pool_get_item(type, value, len);
g_mutex_unlock(tag_pool_lock);
g_static_mutex_unlock(&tag_pool_lock);
g_free(p);
}

View File

@@ -22,7 +22,17 @@
#include <assert.h>
GMutex *tag_pool_lock = NULL;
#if GCC_CHECK_VERSION(4, 2)
/* workaround for a warning caused by G_STATIC_MUTEX_INIT */
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wmissing-field-initializers"
#endif
GStaticMutex tag_pool_lock = G_STATIC_MUTEX_INIT;
#if GCC_CHECK_VERSION(4, 2)
#pragma GCC diagnostic pop
#endif
#define NUM_SLOTS 4096
@@ -81,19 +91,6 @@ static struct slot *slot_alloc(struct slot *next,
return slot;
}
void tag_pool_init(void)
{
g_assert(tag_pool_lock == NULL);
tag_pool_lock = g_mutex_new();
}
void tag_pool_deinit(void)
{
g_assert(tag_pool_lock != NULL);
g_mutex_free(tag_pool_lock);
tag_pool_lock = NULL;
}
struct tag_item *
tag_pool_get_item(enum tag_type type, const char *value, size_t length)
{

View File

@@ -24,14 +24,10 @@
#include <glib.h>
extern GMutex *tag_pool_lock;
extern GStaticMutex tag_pool_lock;
struct tag_item;
void tag_pool_init(void);
void tag_pool_deinit(void);
struct tag_item *
tag_pool_get_item(enum tag_type type, const char *value, size_t length);