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,33 +59,42 @@
*/ */
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) {
plist = xbps_xasprintf("%s/%s/%s", xbps_get_rootdir(), pthread_mutex_lock(&refcnt_mtx);
XBPS_META_PATH, XBPS_REGPKGDB); regpkgdb_refcnt++;
if (plist == NULL) pthread_mutex_unlock(&refcnt_mtx);
return NULL; return regpkgdb_dict;
}
regpkgdb_dict = prop_dictionary_internalize_from_zfile(plist); plist = xbps_xasprintf("%s/%s/%s", xbps_get_rootdir(),
if (regpkgdb_dict == NULL) { XBPS_META_PATH, XBPS_REGPKGDB);
free(plist); if (plist == NULL)
if (errno != ENOENT) return NULL;
xbps_dbg_printf("[regpkgdb] cannot internalize "
"regpkgdb_dict %s\n", strerror(errno)); regpkgdb_dict = prop_dictionary_internalize_from_zfile(plist);
return NULL; if (regpkgdb_dict == NULL) {
}
free(plist); free(plist);
regpkgdb_initialized = true; if (errno != ENOENT)
xbps_dbg_printf("%s: initialized ok.\n", __func__); xbps_dbg_printf("[regpkgdb] cannot internalize "
} "regpkgdb_dict %s\n", strerror(errno));
regpkgdb_refcount++; return NULL;
}
free(plist);
regpkgdb_initialized = true;
xbps_dbg_printf("%s: initialized ok.\n", __func__);
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);