redict/src/atomicvar.h

95 lines
3.6 KiB
C
Raw Normal View History

/* This file implements atomic counters using __atomic or __sync macros if
* available, otherwise synchronizing different threads using a mutex.
*
* The exported interaface is composed of three macros:
*
* atomicIncr(var,count,mutex) -- Increment the atomic counter
* atomicDecr(var,count,mutex) -- Decrement the atomic counter
* atomicGet(var,dstvar,mutex) -- Fetch the atomic counter value
*
* If atomic primitives are availble (tested in config.h) the mutex
* is not used.
*
* Never use return value from the macros. To update and get use instead:
*
* atomicIncr(mycounter,...);
* atomicGet(mycounter,newvalue);
* doSomethingWith(newvalue);
*
* ----------------------------------------------------------------------------
*
* Copyright (c) 2015, Salvatore Sanfilippo <antirez at gmail dot com>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* * Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * Neither the name of Redis nor the names of its contributors may be used
* to endorse or promote products derived from this software without
* specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
#include <pthread.h>
#ifndef __ATOMIC_VAR_H
#define __ATOMIC_VAR_H
Fixes compilation on MacOS 10.8.5, Clang tags/Apple/clang-421.0.57 Redis fails to compile on MacOS 10.8.5 with Clang 4, version 421.0.57 (based on LLVM 3.1svn). When compiling zmalloc.c, we get these warnings: CC zmalloc.o zmalloc.c:109:5: warning: implicit declaration of function '__atomic_add_fetch' is invalid in C99 [-Wimplicit-function-declaration] update_zmalloc_stat_alloc(zmalloc_size(ptr)); ^ zmalloc.c:75:9: note: expanded from macro 'update_zmalloc_stat_alloc' atomicIncr(used_memory,__n,used_memory_mutex); \ ^ ./atomicvar.h:57:37: note: expanded from macro 'atomicIncr' #define atomicIncr(var,count,mutex) __atomic_add_fetch(&var,(count),__ATOMIC_RELAXED) ^ zmalloc.c:145:5: warning: implicit declaration of function '__atomic_sub_fetch' is invalid in C99 [-Wimplicit-function-declaration] update_zmalloc_stat_free(oldsize); ^ zmalloc.c:85:9: note: expanded from macro 'update_zmalloc_stat_free' atomicDecr(used_memory,__n,used_memory_mutex); \ ^ ./atomicvar.h:58:37: note: expanded from macro 'atomicDecr' #define atomicDecr(var,count,mutex) __atomic_sub_fetch(&var,(count),__ATOMIC_RELAXED) ^ zmalloc.c:205:9: warning: implicit declaration of function '__atomic_load_n' is invalid in C99 [-Wimplicit-function-declaration] atomicGet(used_memory,um,used_memory_mutex); ^ ./atomicvar.h:60:14: note: expanded from macro 'atomicGet' dstvar = __atomic_load_n(&var,__ATOMIC_RELAXED); \ ^ 3 warnings generated. Also on lazyfree.c: CC lazyfree.o lazyfree.c:68:13: warning: implicit declaration of function '__atomic_add_fetch' is invalid in C99 [-Wimplicit-function-declaration] atomicIncr(lazyfree_objects,1,lazyfree_objects_mutex); ^ ./atomicvar.h:57:37: note: expanded from macro 'atomicIncr' #define atomicIncr(var,count,mutex) __atomic_add_fetch(&var,(count),__ATOMIC_RELAXED) ^ lazyfree.c:111:5: warning: implicit declaration of function '__atomic_sub_fetch' is invalid in C99 [-Wimplicit-function-declaration] atomicDecr(lazyfree_objects,1,lazyfree_objects_mutex); ^ ./atomicvar.h:58:37: note: expanded from macro 'atomicDecr' #define atomicDecr(var,count,mutex) __atomic_sub_fetch(&var,(count),__ATOMIC_RELAXED) ^ 2 warnings generated. Then in the linking stage: LINK redis-server Undefined symbols for architecture x86_64: "___atomic_add_fetch", referenced from: _zmalloc in zmalloc.o _zcalloc in zmalloc.o _zrealloc in zmalloc.o _dbAsyncDelete in lazyfree.o _emptyDbAsync in lazyfree.o _slotToKeyFlushAsync in lazyfree.o "___atomic_load_n", referenced from: _zmalloc_used_memory in zmalloc.o _zmalloc_get_fragmentation_ratio in zmalloc.o "___atomic_sub_fetch", referenced from: _zrealloc in zmalloc.o _zfree in zmalloc.o _lazyfreeFreeObjectFromBioThread in lazyfree.o _lazyfreeFreeDatabaseFromBioThread in lazyfree.o _lazyfreeFreeSlotsMapFromBioThread in lazyfree.o ld: symbol(s) not found for architecture x86_64 clang: error: linker command failed with exit code 1 (use -v to see invocation) make[1]: *** [redis-server] Error 1 make: *** [all] Error 2 With this patch, the compilation is sucessful, no warnings. Running `make test` we get a almost clean bill of health. Test pass with one exception: [err]: Check for memory leaks (pid 52793) in tests/unit/dump.tcl [err]: Check for memory leaks (pid 53103) in tests/unit/auth.tcl [err]: Check for memory leaks (pid 53117) in tests/unit/auth.tcl [err]: Check for memory leaks (pid 53131) in tests/unit/protocol.tcl [err]: Check for memory leaks (pid 53145) in tests/unit/protocol.tcl [ok]: Check for memory leaks (pid 53160) [err]: Check for memory leaks (pid 53175) in tests/unit/scan.tcl [ok]: Check for memory leaks (pid 53189) [err]: Check for memory leaks (pid 53221) in tests/unit/type/incr.tcl . . . Full debug log (289MB, uncompressed) available at https://dl.dropboxusercontent.com/u/75548/logs/redis-debug-log-macos-10.8.5.log.xz Most if not all of the memory leak tests fail. Not sure if this is related. They are the only ones that fail. I belive they are not related, but just the memory leak detector is not working properly on 10.8.5. Signed-off-by: Pedro Melo <melo@simplicidade.org>
2016-10-17 12:50:04 +01:00
#if defined(__ATOMIC_RELAXED) && (!defined(__clang__) || __apple_build_version__ > 4210057)
/* Implementation using __atomic macros. */
#define atomicIncr(var,count,mutex) __atomic_add_fetch(&var,(count),__ATOMIC_RELAXED)
#define atomicDecr(var,count,mutex) __atomic_sub_fetch(&var,(count),__ATOMIC_RELAXED)
#define atomicGet(var,dstvar,mutex) do { \
dstvar = __atomic_load_n(&var,__ATOMIC_RELAXED); \
} while(0)
#elif defined(HAVE_ATOMIC)
/* Implementation using __sync macros. */
#define atomicIncr(var,count,mutex) __sync_add_and_fetch(&var,(count))
#define atomicDecr(var,count,mutex) __sync_sub_and_fetch(&var,(count))
#define atomicGet(var,dstvar,mutex) do { \
dstvar = __sync_sub_and_fetch(&var,0); \
} while(0)
#else
/* Implementation using pthread mutex. */
#define atomicIncr(var,count,mutex) do { \
pthread_mutex_lock(&mutex); \
var += (count); \
pthread_mutex_unlock(&mutex); \
} while(0)
#define atomicDecr(var,count,mutex) do { \
pthread_mutex_lock(&mutex); \
var -= (count); \
pthread_mutex_unlock(&mutex); \
} while(0)
#define atomicGet(var,dstvar,mutex) do { \
pthread_mutex_lock(&mutex); \
dstvar = var; \
pthread_mutex_unlock(&mutex); \
} while(0)
#endif
#endif /* __ATOMIC_VAR_H */