From 11ac167c942cc089cafe7eee25513e881031d12b Mon Sep 17 00:00:00 2001 From: Gerardo O Date: Wed, 28 May 2025 17:18:41 -0500 Subject: [PATCH] Replaced older deprecated __sync calls for atomic ones. Should address #2836 and help compile in gcc 14 and ppc. --- src/gkhash.c | 8 ++++---- src/gstorage.c | 2 +- src/parser.c | 52 ++++++++++++++++++++++++++++++++++++++------------ 3 files changed, 45 insertions(+), 17 deletions(-) diff --git a/src/gkhash.c b/src/gkhash.c index 88710ae1..5190faa4 100644 --- a/src/gkhash.c +++ b/src/gkhash.c @@ -1017,7 +1017,7 @@ inc_ii32 (khash_t (ii32) *hash, uint32_t key, uint32_t inc) { kh_val (hash, k) = 0; } - return __sync_add_and_fetch (&kh_val (hash, k), inc); + return __atomic_add_fetch (&kh_val (hash, k), inc, __ATOMIC_SEQ_CST); } /* Increase a uint64_t value given a string key. @@ -1110,7 +1110,7 @@ inc_si32 (khash_t (si32) *hash, const char *key, uint32_t inc) { kh_val (hash, k) = 0; } - return __sync_add_and_fetch (&kh_val (hash, k), inc); + return __atomic_add_fetch (&kh_val (hash, k), inc, __ATOMIC_SEQ_CST); } /* Insert a string key and auto increment int value. @@ -1202,7 +1202,7 @@ get_si32 (khash_t (si32) *hash, const char *key) { k = kh_get (si32, hash, key); /* key found, return current value */ if (k != kh_end (hash)) - return __sync_add_and_fetch (&kh_val (hash, k), 0); + return __atomic_load_n (&kh_val (hash, k), __ATOMIC_SEQ_CST); return 0; } @@ -1300,7 +1300,7 @@ get_ii32 (khash_t (ii32) *hash, uint32_t key) { k = kh_get (ii32, hash, key); /* key found, return current value */ if (k != kh_end (hash)) - return __sync_add_and_fetch (&kh_val (hash, k), 0); + return __atomic_load_n (&kh_val (hash, k), __ATOMIC_SEQ_CST); return 0; } diff --git a/src/gstorage.c b/src/gstorage.c index 412d84f0..a0cd5c23 100644 --- a/src/gstorage.c +++ b/src/gstorage.c @@ -610,7 +610,7 @@ count_valid (int numdate) { /* Keep track of all valid and processed log strings. */ void count_process (GLog *glog) { - __sync_add_and_fetch (&glog->processed, 1); + __atomic_add_fetch (&glog->processed, 1, __ATOMIC_SEQ_CST); lock_spinner (); ht_inc_cnt_overall ("total_requests", 1); unlock_spinner (); diff --git a/src/parser.c b/src/parser.c index 727effb2..bbcb1423 100644 --- a/src/parser.c +++ b/src/parser.c @@ -1195,7 +1195,11 @@ parse_specifier (GLogItem *logitem, const char **str, const char *p, const char if (tkn == bEnd || *bEnd != '\0' || errno == ERANGE) bandw = 0; logitem->resp_size = bandw; - __sync_bool_compare_and_swap (&conf.bandwidth, 0, 1); /* set flag */ + { + int expected = 0; + __atomic_compare_exchange_n (&conf.bandwidth, &expected, 1, false, __ATOMIC_SEQ_CST, + __ATOMIC_SEQ_CST); + } free (tkn); break; /* referrer */ @@ -1264,7 +1268,12 @@ parse_specifier (GLogItem *logitem, const char **str, const char *p, const char logitem->serve_time = (serve_secs > 0) ? serve_secs * MILS : 0; /* Determine if time-served data was stored on-disk. */ - __sync_bool_compare_and_swap (&conf.serve_usecs, 0, 1); /* set flag */ + { + int expected = 0; + __atomic_compare_exchange_n (&conf.serve_usecs, &expected, 1, false, __ATOMIC_SEQ_CST, + __ATOMIC_SEQ_CST); + } + free (tkn); break; /* time taken to serve the request, in seconds with a milliseconds @@ -1287,7 +1296,11 @@ parse_specifier (GLogItem *logitem, const char **str, const char *p, const char logitem->serve_time = (serve_secs > 0) ? serve_secs * SECS : 0; /* Determine if time-served data was stored on-disk. */ - __sync_bool_compare_and_swap (&conf.serve_usecs, 0, 1); /* set flag */ + { + int expected = 0; + __atomic_compare_exchange_n (&conf.serve_usecs, &expected, 1, false, __ATOMIC_SEQ_CST, + __ATOMIC_SEQ_CST); + } free (tkn); break; /* time taken to serve the request, in microseconds */ @@ -1304,7 +1317,11 @@ parse_specifier (GLogItem *logitem, const char **str, const char *p, const char logitem->serve_time = serve_time; /* Determine if time-served data was stored on-disk. */ - __sync_bool_compare_and_swap (&conf.serve_usecs, 0, 1); /* set flag */ + { + int expected = 0; + __atomic_compare_exchange_n (&conf.serve_usecs, &expected, 1, false, __ATOMIC_SEQ_CST, + __ATOMIC_SEQ_CST); + } free (tkn); break; /* time taken to serve the request, in nanoseconds */ @@ -1323,7 +1340,11 @@ parse_specifier (GLogItem *logitem, const char **str, const char *p, const char logitem->serve_time = (serve_time > 0) ? serve_time / MILS : 0; /* Determine if time-served data was stored on-disk. */ - __sync_bool_compare_and_swap (&conf.serve_usecs, 0, 1); /* set flag */ + { + int expected = 0; + __atomic_compare_exchange_n (&conf.serve_usecs, &expected, 1, false, __ATOMIC_SEQ_CST, + __ATOMIC_SEQ_CST); + } free (tkn); break; /* UMS: Krypto (TLS) "ECDHE-RSA-AES128-GCM-SHA256" */ @@ -1932,14 +1953,21 @@ parse_json_format (GLogItem *logitem, char *str) { */ static int atomic_lpts_update (GLog *glog, GLogItem *logitem) { - int64_t oldts = 0, newts = 0; - /* atomic update loop */ - newts = mktime (&logitem->dt); // Get timestamp from logitem->dt - while (!__sync_bool_compare_and_swap (&glog->lp.ts, oldts, newts)) { - oldts = glog->lp.ts; /* Reread glog->lp.ts if CAS failed */ - if (oldts >= newts) { - break; /* No need to update if oldts is already greater */ + int64_t newts = mktime (&logitem->dt); // Get timestamp from logitem->dt + int64_t oldts = __atomic_load_n (&glog->lp.ts, __ATOMIC_SEQ_CST); + int64_t expected; + + while (oldts < newts) { // Only update if new timestamp is later + expected = oldts; + // Attempt to update glog->lp.ts from expected (oldts) to newts. + if (__atomic_compare_exchange_n (&glog->lp.ts, &expected, newts, + false, __ATOMIC_SEQ_CST, __ATOMIC_SEQ_CST)) { + break; // Successful update. } + // If the CAS failed, expected now holds the current glog->lp.ts. + oldts = expected; + if (oldts >= newts) + break; } return newts;