diff options
author | Vsevolod Stakhov <vsevolod@highsecure.ru> | 2015-11-26 18:50:03 +0000 |
---|---|---|
committer | Vsevolod Stakhov <vsevolod@highsecure.ru> | 2015-11-26 18:50:03 +0000 |
commit | 86c5962983c4e1f97eccfa8636ebe5bdc88599cc (patch) | |
tree | aa20f30dc112c0f53efa8a6821f2fd4bc16127b9 | |
parent | ef3d454616a1b1bae06f035374a64fbde5bbbfff (diff) | |
download | rspamd-86c5962983c4e1f97eccfa8636ebe5bdc88599cc.tar.gz rspamd-86c5962983c4e1f97eccfa8636ebe5bdc88599cc.zip |
Use atomic ops if possible
-rw-r--r-- | CMakeLists.txt | 11 | ||||
-rw-r--r-- | config.h.in | 1 | ||||
-rw-r--r-- | src/controller.c | 5 | ||||
-rw-r--r-- | src/fuzzy_storage.c | 10 | ||||
-rw-r--r-- | src/libserver/protocol.c | 11 | ||||
-rw-r--r-- | src/libutil/ref.h | 4 |
6 files changed, 37 insertions, 5 deletions
diff --git a/CMakeLists.txt b/CMakeLists.txt index 01d7d940b..6c702e76d 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -923,15 +923,22 @@ CHECK_C_SOURCE_COMPILES ("#include <sys/types.h> ENDIF(NOT CMAKE_SYSTEM_NAME STREQUAL "SunOS") CHECK_C_SOURCE_RUNS(" +#include <stdbool.h> int main(int argc, char **argv) { - int a = 0; - if (__sync_bool_compare_and_swap(&a, 0, 1)) { + int a = 0, b = 0; + if (__atomic_compare_exchange_n(&a, &b, 1, false, __ATOMIC_RELEASE, __ATOMIC_RELAXED)) { return 0; } return -1; } " HAVE_ATOMIC_BUILTINS) +IF(NOT HAVE_ATOMIC_BUILTINS) + MESSAGE(STATUS "atomic builtins are -NOT- supported") +ELSE() + MESSAGE(STATUS "atomic builtins are supported") +ENDIF() + # Check queue.h compatibility IF(NOT HAVE_COMPATIBLE_QUEUE_H) INCLUDE_DIRECTORIES(compat) diff --git a/config.h.in b/config.h.in index 3ecb964be..001506a6a 100644 --- a/config.h.in +++ b/config.h.in @@ -226,6 +226,7 @@ #cmakedefine HAVE_SETSIG 1 #cmakedefine HAVE_OASYNC 1 #cmakedefine WITH_SNOWBALL 1 +#cmakedefine HAVE_ATOMIC_BUILTINS 1 /* Configure allocator */ #define uthash_malloc(sz) g_slice_alloc(sz) diff --git a/src/controller.c b/src/controller.c index bb494108f..d04aba5c7 100644 --- a/src/controller.c +++ b/src/controller.c @@ -1831,7 +1831,12 @@ rspamd_controller_handle_stat_common ( ham += stat->actions_stat[i]; } if (do_reset) { +#ifndef HAVE_ATOMIC_BUILTINS session->ctx->worker->srv->stat->actions_stat[i] = 0; +#else + __atomic_store_n(&session->ctx->worker->srv->stat->actions_stat[i], + 0, __ATOMIC_RELEASE); +#endif } } ucl_object_insert_key (top, sub, "actions", 0, false); diff --git a/src/fuzzy_storage.c b/src/fuzzy_storage.c index f3be2ab68..bde583387 100644 --- a/src/fuzzy_storage.c +++ b/src/fuzzy_storage.c @@ -288,11 +288,21 @@ rspamd_fuzzy_process_command (struct fuzzy_session *session) result = rspamd_fuzzy_backend_check (session->ctx->backend, cmd, session->ctx->expire); /* XXX: actually, these updates are not atomic, but we don't care */ +#ifndef HAVE_ATOMIC_BUILTINS server_stat->fuzzy_hashes_checked[session->epoch] ++; if (result.prob > 0.5) { server_stat->fuzzy_hashes_found[session->epoch] ++; } +#else + __atomic_add_fetch (&server_stat->fuzzy_hashes_checked[session->epoch], + 1, __ATOMIC_RELEASE); + + if (result.prob > 0.5) { + __atomic_add_fetch (&server_stat->fuzzy_hashes_found[session->epoch], + 1, __ATOMIC_RELEASE); + } +#endif } else { result.flag = cmd->flag; diff --git a/src/libserver/protocol.c b/src/libserver/protocol.c index d20c50ff8..5cd3589f6 100644 --- a/src/libserver/protocol.c +++ b/src/libserver/protocol.c @@ -1035,13 +1035,22 @@ rspamd_protocol_http_reply (struct rspamd_http_message *msg, action = rspamd_check_action_metric (task, metric_res->score, &required_score, metric_res->metric); if (action <= METRIC_ACTION_NOACTION) { +#ifndef HAVE_ATOMIC_BUILTINS task->worker->srv->stat->actions_stat[action]++; +#else + __atomic_add_fetch (&task->worker->srv->stat->actions_stat[action], + 1, __ATOMIC_RELEASE); +#endif } } /* Increase counters */ - +#ifndef HAVE_ATOMIC_BUILTINS task->worker->srv->stat->messages_scanned++; +#else + __atomic_add_fetch (&task->worker->srv->stat->messages_scanned, + 1, __ATOMIC_RELEASE); +#endif } } diff --git a/src/libutil/ref.h b/src/libutil/ref.h index 18e2c3903..1ad7d24a1 100644 --- a/src/libutil/ref.h +++ b/src/libutil/ref.h @@ -55,13 +55,13 @@ typedef struct ref_entry_s { #ifdef HAVE_ATOMIC_BUILTINS #define REF_RETAIN(obj) do { \ if ((obj) != NULL) { \ - __sync_add_and_fetch (&(obj)->ref.refcount, 1); \ + __atomic_add_fetch (&(obj)->ref.refcount, 1, __ATOMIC_RELEASE); \ } \ } while (0) #define REF_RELEASE(obj) do { \ if ((obj) != NULL) { \ - unsigned int _rc_priv = __sync_sub_and_fetch (&(obj)->ref.refcount, 1); \ + unsigned int _rc_priv = __atomic_sub_fetch (&(obj)->ref.refcount, 1, __ATOMIC_ACQ_REL); \ if (_rc_priv == 0 && (obj)->ref.dtor) { \ (obj)->ref.dtor (obj); \ } \ |