libxbps: regpkgdb: use a mutex for {inc,dec}rementing the refcnt.

This commit is contained in:
Juan RP 2011-01-18 20:18:27 +01:00
parent fe15380e1b
commit 9180bfce23

View File

@ -28,6 +28,7 @@
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <errno.h> #include <errno.h>
#include <pthread.h>
#include <xbps_api.h> #include <xbps_api.h>
#include "xbps_api_impl.h" #include "xbps_api_impl.h"
@ -58,15 +59,22 @@
*/ */
static prop_dictionary_t regpkgdb_dict; static prop_dictionary_t regpkgdb_dict;
static size_t regpkgdb_refcount; static size_t regpkgdb_refcnt;
static bool regpkgdb_initialized; static bool regpkgdb_initialized;
static pthread_mutex_t refcnt_mtx = PTHREAD_MUTEX_INITIALIZER;
prop_dictionary_t prop_dictionary_t
xbps_regpkgdb_dictionary_get(void) xbps_regpkgdb_dictionary_get(void)
{ {
char *plist; char *plist;
if (regpkgdb_initialized == false) { if (regpkgdb_initialized) {
pthread_mutex_lock(&refcnt_mtx);
regpkgdb_refcnt++;
pthread_mutex_unlock(&refcnt_mtx);
return regpkgdb_dict;
}
plist = xbps_xasprintf("%s/%s/%s", xbps_get_rootdir(), plist = xbps_xasprintf("%s/%s/%s", xbps_get_rootdir(),
XBPS_META_PATH, XBPS_REGPKGDB); XBPS_META_PATH, XBPS_REGPKGDB);
if (plist == NULL) if (plist == NULL)
@ -83,8 +91,10 @@ xbps_regpkgdb_dictionary_get(void)
free(plist); free(plist);
regpkgdb_initialized = true; regpkgdb_initialized = true;
xbps_dbg_printf("%s: initialized ok.\n", __func__); xbps_dbg_printf("%s: initialized ok.\n", __func__);
}
regpkgdb_refcount++; pthread_mutex_lock(&refcnt_mtx);
regpkgdb_refcnt = 1;
pthread_mutex_unlock(&refcnt_mtx);
return regpkgdb_dict; return regpkgdb_dict;
} }
@ -92,7 +102,13 @@ xbps_regpkgdb_dictionary_get(void)
void void
xbps_regpkgdb_dictionary_release(void) xbps_regpkgdb_dictionary_release(void)
{ {
if (--regpkgdb_refcount > 0) size_t cnt;
pthread_mutex_lock(&refcnt_mtx);
cnt = regpkgdb_refcnt--;
pthread_mutex_unlock(&refcnt_mtx);
if (cnt != 1)
return; return;
prop_object_release(regpkgdb_dict); prop_object_release(regpkgdb_dict);