Simplify atomicvar.h usage by having the mutex name implicit.

This commit is contained in:
antirez 2017-05-04 17:00:53 +02:00
parent 52bc74f221
commit 2a51bac44e
3 changed files with 25 additions and 27 deletions

View File

@ -54,40 +54,40 @@
#if defined(__ATOMIC_RELAXED) && !defined(__sun) && (!defined(__clang__) || !defined(__APPLE__) || __apple_build_version__ > 4210057) #if defined(__ATOMIC_RELAXED) && !defined(__sun) && (!defined(__clang__) || !defined(__APPLE__) || __apple_build_version__ > 4210057)
/* Implementation using __atomic macros. */ /* Implementation using __atomic macros. */
#define atomicIncr(var,count,mutex) __atomic_add_fetch(&var,(count),__ATOMIC_RELAXED) #define atomicIncr(var,count) __atomic_add_fetch(&var,(count),__ATOMIC_RELAXED)
#define atomicDecr(var,count,mutex) __atomic_sub_fetch(&var,(count),__ATOMIC_RELAXED) #define atomicDecr(var,count) __atomic_sub_fetch(&var,(count),__ATOMIC_RELAXED)
#define atomicGet(var,dstvar,mutex) do { \ #define atomicGet(var,dstvar) do { \
dstvar = __atomic_load_n(&var,__ATOMIC_RELAXED); \ dstvar = __atomic_load_n(&var,__ATOMIC_RELAXED); \
} while(0) } while(0)
#elif defined(HAVE_ATOMIC) #elif defined(HAVE_ATOMIC)
/* Implementation using __sync macros. */ /* Implementation using __sync macros. */
#define atomicIncr(var,count,mutex) __sync_add_and_fetch(&var,(count)) #define atomicIncr(var,count) __sync_add_and_fetch(&var,(count))
#define atomicDecr(var,count,mutex) __sync_sub_and_fetch(&var,(count)) #define atomicDecr(var,count) __sync_sub_and_fetch(&var,(count))
#define atomicGet(var,dstvar,mutex) do { \ #define atomicGet(var,dstvar) do { \
dstvar = __sync_sub_and_fetch(&var,0); \ dstvar = __sync_sub_and_fetch(&var,0); \
} while(0) } while(0)
#else #else
/* Implementation using pthread mutex. */ /* Implementation using pthread mutex. */
#define atomicIncr(var,count,mutex) do { \ #define atomicIncr(var,count) do { \
pthread_mutex_lock(&mutex); \ pthread_mutex_lock(&var ## _mutex); \
var += (count); \ var += (count); \
pthread_mutex_unlock(&mutex); \ pthread_mutex_unlock(&var ## _mutex); \
} while(0) } while(0)
#define atomicDecr(var,count,mutex) do { \ #define atomicDecr(var,count) do { \
pthread_mutex_lock(&mutex); \ pthread_mutex_lock(&var ## _mutex); \
var -= (count); \ var -= (count); \
pthread_mutex_unlock(&mutex); \ pthread_mutex_unlock(&var ## _mutex); \
} while(0) } while(0)
#define atomicGet(var,dstvar,mutex) do { \ #define atomicGet(var,dstvar) do { \
pthread_mutex_lock(&mutex); \ pthread_mutex_lock(&var ## _mutex); \
dstvar = var; \ dstvar = var; \
pthread_mutex_unlock(&mutex); \ pthread_mutex_unlock(&var ## _mutex); \
} while(0) } while(0)
#endif #endif

View File

@ -9,7 +9,7 @@ pthread_mutex_t lazyfree_objects_mutex = PTHREAD_MUTEX_INITIALIZER;
/* Return the number of currently pending objects to free. */ /* Return the number of currently pending objects to free. */
size_t lazyfreeGetPendingObjectsCount(void) { size_t lazyfreeGetPendingObjectsCount(void) {
size_t aux; size_t aux;
atomicGet(lazyfree_objects,aux,lazyfree_objects_mutex); atomicGet(lazyfree_objects,aux);
return aux; return aux;
} }
@ -67,7 +67,7 @@ int dbAsyncDelete(redisDb *db, robj *key) {
/* If releasing the object is too much work, let's put it into the /* If releasing the object is too much work, let's put it into the
* lazy free list. */ * lazy free list. */
if (free_effort > LAZYFREE_THRESHOLD) { if (free_effort > LAZYFREE_THRESHOLD) {
atomicIncr(lazyfree_objects,1,lazyfree_objects_mutex); atomicIncr(lazyfree_objects,1);
bioCreateBackgroundJob(BIO_LAZY_FREE,val,NULL,NULL); bioCreateBackgroundJob(BIO_LAZY_FREE,val,NULL,NULL);
dictSetVal(db->dict,de,NULL); dictSetVal(db->dict,de,NULL);
} }
@ -91,8 +91,7 @@ void emptyDbAsync(redisDb *db) {
dict *oldht1 = db->dict, *oldht2 = db->expires; dict *oldht1 = db->dict, *oldht2 = db->expires;
db->dict = dictCreate(&dbDictType,NULL); db->dict = dictCreate(&dbDictType,NULL);
db->expires = dictCreate(&keyptrDictType,NULL); db->expires = dictCreate(&keyptrDictType,NULL);
atomicIncr(lazyfree_objects,dictSize(oldht1), atomicIncr(lazyfree_objects,dictSize(oldht1));
lazyfree_objects_mutex);
bioCreateBackgroundJob(BIO_LAZY_FREE,NULL,oldht1,oldht2); bioCreateBackgroundJob(BIO_LAZY_FREE,NULL,oldht1,oldht2);
} }
@ -104,8 +103,7 @@ void slotToKeyFlushAsync(void) {
server.cluster->slots_to_keys = raxNew(); server.cluster->slots_to_keys = raxNew();
memset(server.cluster->slots_keys_count,0, memset(server.cluster->slots_keys_count,0,
sizeof(server.cluster->slots_keys_count)); sizeof(server.cluster->slots_keys_count));
atomicIncr(lazyfree_objects,old->numele, atomicIncr(lazyfree_objects,old->numele);
lazyfree_objects_mutex);
bioCreateBackgroundJob(BIO_LAZY_FREE,NULL,NULL,old); bioCreateBackgroundJob(BIO_LAZY_FREE,NULL,NULL,old);
} }
@ -113,7 +111,7 @@ void slotToKeyFlushAsync(void) {
* updating the count of objects to release. */ * updating the count of objects to release. */
void lazyfreeFreeObjectFromBioThread(robj *o) { void lazyfreeFreeObjectFromBioThread(robj *o) {
decrRefCount(o); decrRefCount(o);
atomicDecr(lazyfree_objects,1,lazyfree_objects_mutex); atomicDecr(lazyfree_objects,1);
} }
/* Release a database from the lazyfree thread. The 'db' pointer is the /* Release a database from the lazyfree thread. The 'db' pointer is the
@ -125,7 +123,7 @@ void lazyfreeFreeDatabaseFromBioThread(dict *ht1, dict *ht2) {
size_t numkeys = dictSize(ht1); size_t numkeys = dictSize(ht1);
dictRelease(ht1); dictRelease(ht1);
dictRelease(ht2); dictRelease(ht2);
atomicDecr(lazyfree_objects,numkeys,lazyfree_objects_mutex); atomicDecr(lazyfree_objects,numkeys);
} }
/* Release the skiplist mapping Redis Cluster keys to slots in the /* Release the skiplist mapping Redis Cluster keys to slots in the
@ -133,5 +131,5 @@ void lazyfreeFreeDatabaseFromBioThread(dict *ht1, dict *ht2) {
void lazyfreeFreeSlotsMapFromBioThread(rax *rt) { void lazyfreeFreeSlotsMapFromBioThread(rax *rt) {
size_t len = rt->numele; size_t len = rt->numele;
raxFree(rt); raxFree(rt);
atomicDecr(lazyfree_objects,len,lazyfree_objects_mutex); atomicDecr(lazyfree_objects,len);
} }

View File

@ -74,7 +74,7 @@ void zlibc_free(void *ptr) {
size_t _n = (__n); \ size_t _n = (__n); \
if (_n&(sizeof(long)-1)) _n += sizeof(long)-(_n&(sizeof(long)-1)); \ if (_n&(sizeof(long)-1)) _n += sizeof(long)-(_n&(sizeof(long)-1)); \
if (zmalloc_thread_safe) { \ if (zmalloc_thread_safe) { \
atomicIncr(used_memory,__n,used_memory_mutex); \ atomicIncr(used_memory,__n); \
} else { \ } else { \
used_memory += _n; \ used_memory += _n; \
} \ } \
@ -84,7 +84,7 @@ void zlibc_free(void *ptr) {
size_t _n = (__n); \ size_t _n = (__n); \
if (_n&(sizeof(long)-1)) _n += sizeof(long)-(_n&(sizeof(long)-1)); \ if (_n&(sizeof(long)-1)) _n += sizeof(long)-(_n&(sizeof(long)-1)); \
if (zmalloc_thread_safe) { \ if (zmalloc_thread_safe) { \
atomicDecr(used_memory,__n,used_memory_mutex); \ atomicDecr(used_memory,__n); \
} else { \ } else { \
used_memory -= _n; \ used_memory -= _n; \
} \ } \
@ -222,7 +222,7 @@ size_t zmalloc_used_memory(void) {
size_t um; size_t um;
if (zmalloc_thread_safe) { if (zmalloc_thread_safe) {
atomicGet(used_memory,um,used_memory_mutex); atomicGet(used_memory,um);
} else { } else {
um = used_memory; um = used_memory;
} }