From e115ec9ea512ee655c9a265420ca781aa7e78293 Mon Sep 17 00:00:00 2001
From: Sergey Fedorov <barracuda@macos-powerpc.org>
Date: Wed, 30 Apr 2025 00:49:35 +0800
Subject: [PATCH] Replace old sync* builtins with atomic*

---
 src/gkhash.c   |  8 ++++----
 src/gstorage.c |  8 ++++----
 src/parser.c   | 42 ++++++++++++++++++++++++++++++++++--------
 3 files changed, 42 insertions(+), 16 deletions(-)

diff --git src/gkhash.c src/gkhash.c
index 88710ae1..2e6f95c2 100644
--- src/gkhash.c
+++ 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_add_fetch(&kh_val(hash, k), 0, __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_add_fetch(&kh_val(hash, k), 0, __ATOMIC_SEQ_CST);
 
   return 0;
 }
diff --git src/gstorage.c src/gstorage.c
index 412d84f0..d151f48d 100644
--- src/gstorage.c
+++ src/gstorage.c
@@ -610,10 +610,10 @@ 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);
-  lock_spinner ();
-  ht_inc_cnt_overall ("total_requests", 1);
-  unlock_spinner ();
+  __atomic_add_fetch(&glog->processed, 1, __ATOMIC_SEQ_CST);
+  lock_spinner();
+  ht_inc_cnt_overall("total_requests", 1);
+  unlock_spinner();
 }
 
 void
diff --git src/parser.c src/parser.c
index 7aea911f..68adade7 100644
--- src/parser.c
+++ src/parser.c
@@ -969,6 +969,8 @@ parse_specifier (GLogItem *logitem, const char **str, const char *p, const char
   uint64_t bandw = 0, serve_time = 0;
   int dspc = 0, fmtspcs = 0;
 
+  int expected = 0;
+
   errno = 0;
   memset (&tm, 0, sizeof (tm));
   tm.tm_isdst = -1;
@@ -1195,7 +1197,9 @@ 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 */
+    __atomic_compare_exchange_n(&conf.bandwidth, &expected, 1, false,
+                                __ATOMIC_SEQ_CST, __ATOMIC_SEQ_CST
+                                ); /* set flag */
     free (tkn);
     break;
     /* referrer */
@@ -1264,7 +1268,9 @@ 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 */
+    __atomic_compare_exchange_n(&conf.serve_usecs, &expected, 1, false,
+                                __ATOMIC_SEQ_CST, __ATOMIC_SEQ_CST
+                                ); /* set flag */
     free (tkn);
     break;
     /* time taken to serve the request, in seconds with a milliseconds
@@ -1287,7 +1293,9 @@ 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 */
+    __atomic_compare_exchange_n(&conf.serve_usecs, &expected, 1, false,
+                                __ATOMIC_SEQ_CST, __ATOMIC_SEQ_CST
+                                ); /* set flag */
     free (tkn);
     break;
     /* time taken to serve the request, in microseconds */
@@ -1304,7 +1312,9 @@ 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 */
+    __atomic_compare_exchange_n(&conf.serve_usecs, &expected, 1, false,
+                                __ATOMIC_SEQ_CST, __ATOMIC_SEQ_CST
+                                ); /* set flag */
     free (tkn);
     break;
     /* time taken to serve the request, in nanoseconds */
@@ -1323,7 +1333,9 @@ 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 */
+    __atomic_compare_exchange_n(&conf.serve_usecs, &expected, 1, false,
+                                __ATOMIC_SEQ_CST, __ATOMIC_SEQ_CST
+                                ); /* set flag */
     free (tkn);
     break;
     /* UMS: Krypto (TLS) "ECDHE-RSA-AES128-GCM-SHA256" */
@@ -1935,10 +1947,24 @@ 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 */
+  while (1) {
+    uint64_t expected = oldts; // Expected value for CAS
+    int success = __atomic_compare_exchange_n(
+        &glog->lp.ts,           // Address of the variable
+        &expected,              // Pointer to the expected value
+        newts,                  // New value to set
+        false,                  // Strong CAS
+        __ATOMIC_SEQ_CST,       // Memory order for success
+        __ATOMIC_SEQ_CST        // Memory order for failure
+    );
+
+    if (success) {
+      break; // CAS succeeded
+    }
+
+    oldts = expected; // Update oldts with the current value if CAS failed
     if (oldts >= newts) {
-      break;    /* No need to update if oldts is already greater */
+      break; // No need to update if oldts is already greater
     }
   }
 
