#define LOG_TAG "VCONF"
-#define HASH_SIZE 10
-
static void _restore_noti_cb(gpointer key, gpointer value, gpointer user_data);
static pthread_mutex_t vconf_lock = PTHREAD_MUTEX_INITIALIZER;
static struct buxton_layer *system_layer;
static struct buxton_layer *memory_layer;
static GHashTable *noti_tbl;
-static GHashTable *cache_tbl;
-static GQueue *victims;
struct noti {
char *key;
g_hash_table_destroy(noti_tbl);
noti_tbl = NULL;
- g_hash_table_destroy(cache_tbl);
- cache_tbl = NULL;
-
- g_queue_free_full(victims, (GDestroyNotify)free);
- victims = NULL;
-
buxton_close(client);
client = NULL;
}
NULL, (GDestroyNotify)free_noti);
}
- if (!cache_tbl) {
- cache_tbl = g_hash_table_new_full(g_str_hash, g_str_equal,
- (GDestroyNotify)free,
- (GDestroyNotify)buxton_value_free);
- }
-
- if (!victims) {
- victims = g_queue_new();
- g_queue_init(victims);
- }
-
if (!system_layer)
system_layer = buxton_create_layer("system");
if (!memory_layer)
}
r = buxton_set_value_sync(client, get_layer(key), key, val);
- if (r == -1) {
+ if (r == -1)
LOGE("set value: key '%s' errno %d", key, errno);
- } else {
- if (cache_tbl) {
- if (g_hash_table_contains(cache_tbl, key)) {
- g_hash_table_replace(cache_tbl,
- strdup(key),
- buxton_value_duplicate(val));
- }
- }
- }
_close();
pthread_mutex_unlock(&vconf_lock);
return r;
}
-static void cache_cb(const struct buxton_layer *layer, const char *key,
- const struct buxton_value *val, void *user_data)
-{
- if (g_hash_table_contains(cache_tbl, key)) {
- g_hash_table_replace(cache_tbl,
- strdup(key), buxton_value_duplicate(val));
- }
-}
-
-
-static void _reduce_cache()
-{
- char *key;
- int r;
-
- key = (char *)g_queue_pop_tail(victims);
- if (key == NULL)
- return;
-
- r = g_hash_table_remove(cache_tbl, key);
- if (r == false)
- return;
-
- r = buxton_unregister_notification_sync(client, get_layer(key),
- key, cache_cb);
- if (r == -1)
- return;
-
- _close();
-
- free(key);
-}
-
-static void _insert_cache(const char *key, struct buxton_value *val)
-{
- int r;
-
- r = _open();
- if (r == -1)
- return;
-
- if (g_hash_table_size(cache_tbl) >= HASH_SIZE)
- _reduce_cache();
-
- r = buxton_register_notification_sync(client, get_layer(key), key,
- cache_cb, NULL);
-
- if (r == -1) {
- _close();
- return;
- }
-
- g_queue_push_head(victims, (char *)strdup(key));
- r = g_hash_table_insert(cache_tbl, strdup(key),
- buxton_value_duplicate(val));
-}
-
static int _vconf_get(const char *key, enum buxton_key_type type,
struct buxton_value **val)
{
assert(val);
pthread_mutex_lock(&vconf_lock);
-
r = _open();
if (r == -1) {
pthread_mutex_unlock(&vconf_lock);
return -1;
}
- v = g_hash_table_lookup(cache_tbl, key);
- if (v) {
- *val = buxton_value_duplicate(v);
- _close();
- pthread_mutex_unlock(&vconf_lock);
- return 0;
- }
-
r = buxton_get_value_sync(client, get_layer(key), key, &v);
if (r == -1) {
LOGE("get value: key '%s' errno %d", key, errno);
}
}
- _insert_cache(key, v);
_close();
-
pthread_mutex_unlock(&vconf_lock);
return r;