浏览代码

atomic: replace the legacy __sync with __atomic aotmic builtins

Nick Peng 4 月之前
父节点
当前提交
bc81b36a91
共有 6 个文件被更改,包括 45 次插入71 次删除
  1. 4 4
      src/dns_client/client_socket.c
  2. 2 1
      src/dns_server/request_pending.c
  3. 2 22
      src/include/smartdns/dns_stats.h
  4. 23 30
      src/include/smartdns/lib/atomic.h
  5. 12 12
      src/tlog.c
  6. 2 2
      src/utils/ssl.c

+ 4 - 4
src/dns_client/client_socket.c

@@ -77,10 +77,6 @@ int _dns_client_create_socket(struct dns_server_info *server_info)
 
 void _dns_client_close_socket_ext(struct dns_server_info *server_info, int no_del_conn_list)
 {
-	if (server_info->fd <= 0) {
-		return;
-	}
-
 	if (server_info->ssl) {
 		/* Shutdown ssl */
 		if (server_info->status == DNS_SERVER_STATUS_CONNECTED) {
@@ -124,6 +120,10 @@ void _dns_client_close_socket_ext(struct dns_server_info *server_info, int no_de
 		server_info->bio_method = NULL;
 	}
 
+	if (server_info->fd <= 0) {
+		return;
+	}
+
 	/* remove fd from epoll */
 	if (server_info->fd > 0) {
 		epoll_ctl(client.epoll_fd, EPOLL_CTL_DEL, server_info->fd, NULL);

+ 2 - 1
src/dns_server/request_pending.c

@@ -96,11 +96,12 @@ int _dns_server_reply_all_pending_list(struct dns_request *request, struct dns_s
 	struct dns_request *tmp = NULL;
 	int ret = 0;
 
+	pthread_mutex_lock(&server.request_pending_lock);
 	if (request->request_pending_list == NULL) {
+		pthread_mutex_unlock(&server.request_pending_lock);
 		return 0;
 	}
 
-	pthread_mutex_lock(&server.request_pending_lock);
 	pending_list = request->request_pending_list;
 	request->request_pending_list = NULL;
 	hlist_del_init(&pending_list->node);

+ 2 - 22
src/include/smartdns/dns_stats.h

@@ -60,57 +60,37 @@ extern struct dns_stats dns_stats;
 
 static inline uint64_t stats_read(const uint64_t *s)
 {
-	return READ_ONCE((*s));
+	return __atomic_load_n(s, __ATOMIC_SEQ_CST);
 }
 
 static inline uint64_t stats_read_and_set(uint64_t *s, uint64_t v)
 {
-#ifdef USE_ATOMIC
 	return __atomic_test_and_set(s, v);
-#else
-	return __sync_lock_test_and_set(s, v);
-#endif
 }
 
 static inline void stats_set(uint64_t *s, uint64_t v)
 {
-	*s = v;
+	__atomic_store_n(s, v, __ATOMIC_SEQ_CST);
 }
 
 static inline void stats_add(uint64_t *s, uint64_t v)
 {
-#ifdef USE_ATOMIC
 	(void)__atomic_add_fetch(s, v, __ATOMIC_SEQ_CST);
-#else
-	(void)__sync_add_and_fetch(s, v);
-#endif
 }
 
 static inline void stats_inc(uint64_t *s)
 {
-#ifdef USE_ATOMIC
 	(void)__atomic_add_fetch(s, 1, __ATOMIC_SEQ_CST);
-#else
-	(void)__sync_add_and_fetch(s, 1);
-#endif
 }
 
 static inline void stats_sub(uint64_t *s, uint64_t v)
 {
-#ifdef USE_ATOMIC
 	(void)__atomic_sub_fetch(s, v, __ATOMIC_SEQ_CST);
-#else
-	(void)__sync_sub_and_fetch(s, v);
-#endif
 }
 
 static inline void stats_dec(uint64_t *s)
 {
-#ifdef USE_ATOMIC
 	(void)__atomic_sub_fetch(s, 1, __ATOMIC_SEQ_CST);
-#else
-	(void)__sync_sub_and_fetch(s, 1);
-#endif
 }
 
 void dns_stats_avg_time_update(struct dns_stats_avg_time *avg_time);

+ 23 - 30
src/include/smartdns/lib/atomic.h

@@ -16,15 +16,9 @@
  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
  */
 
-
 #ifndef _GENERIC_ATOMIC_H
 #define _GENERIC_ATOMIC_H
 
-#define ACCESS_ONCE(x) (*(volatile typeof(x) *)&(x))
-
-#define READ_ONCE(x) \
-({ typeof(x) ___x = ACCESS_ONCE(x); ___x; })
-
 /**
  * Atomic type.
  */
@@ -32,7 +26,7 @@ typedef struct {
 	long counter;
 } atomic_t;
 
-#define ATOMIC_INIT(i)  { (i) }
+#define ATOMIC_INIT(i) {(i)}
 
 /**
  * Read atomic variable
@@ -42,7 +36,7 @@ typedef struct {
  */
 static inline long atomic_read(const atomic_t *v)
 {
-	return READ_ONCE((v)->counter);
+	return __atomic_load_n(&v->counter, __ATOMIC_SEQ_CST);
 }
 
 /**
@@ -52,7 +46,7 @@ static inline long atomic_read(const atomic_t *v)
  */
 static inline void atomic_set(atomic_t *v, long i)
 {
-    v->counter = i;
+	__atomic_store_n(&v->counter, i, __ATOMIC_SEQ_CST);
 }
 
 /**
@@ -60,9 +54,9 @@ static inline void atomic_set(atomic_t *v, long i)
  * @param i integer value to add
  * @param v pointer of type atomic_t
  */
-static inline void atomic_add( long i, atomic_t *v )
+static inline void atomic_add(long i, atomic_t *v)
 {
-	(void)__sync_add_and_fetch(&v->counter, i);
+	__atomic_add_fetch(&v->counter, i, __ATOMIC_SEQ_CST);
 }
 
 /**
@@ -72,9 +66,9 @@ static inline void atomic_add( long i, atomic_t *v )
  *
  * Atomically subtracts @i from @v.
  */
-static inline void atomic_sub( long i, atomic_t *v )
+static inline void atomic_sub(long i, atomic_t *v)
 {
-	(void)__sync_sub_and_fetch(&v->counter, i);
+	__atomic_sub_fetch(&v->counter, i, __ATOMIC_SEQ_CST);
 }
 
 /**
@@ -86,9 +80,9 @@ static inline void atomic_sub( long i, atomic_t *v )
  * true if the result is zero, or false for all
  * other cases.
  */
-static inline long atomic_sub_and_test( long i, atomic_t *v )
+static inline long atomic_sub_and_test(long i, atomic_t *v)
 {
-	return !(__sync_sub_and_fetch(&v->counter, i));
+	return !(__atomic_sub_fetch(&v->counter, i, __ATOMIC_SEQ_CST));
 }
 
 /**
@@ -97,9 +91,9 @@ static inline long atomic_sub_and_test( long i, atomic_t *v )
  *
  * Atomically increments @v by 1.
  */
-static inline void atomic_inc( atomic_t *v )
+static inline void atomic_inc(atomic_t *v)
 {
-	(void)__sync_add_and_fetch(&v->counter, 1);
+	__atomic_add_fetch(&v->counter, 1, __ATOMIC_SEQ_CST);
 }
 
 /**
@@ -109,9 +103,9 @@ static inline void atomic_inc( atomic_t *v )
  * Atomically decrements @v by 1.  Note that the guaranteed
  * useful range of an atomic_t is only 24 bits.
  */
-static inline void atomic_dec( atomic_t *v )
+static inline void atomic_dec(atomic_t *v)
 {
-	(void)__sync_sub_and_fetch(&v->counter, 1);
+	__atomic_sub_fetch(&v->counter, 1, __ATOMIC_SEQ_CST);
 }
 
 /**
@@ -120,9 +114,9 @@ static inline void atomic_dec( atomic_t *v )
  *
  * Atomically increments @v by 1.
  */
-static inline long atomic_inc_return( atomic_t *v )
+static inline long atomic_inc_return(atomic_t *v)
 {
-	return __sync_add_and_fetch(&v->counter, 1);
+	return __atomic_add_fetch(&v->counter, 1, __ATOMIC_SEQ_CST);
 }
 
 /**
@@ -132,9 +126,9 @@ static inline long atomic_inc_return( atomic_t *v )
  * Atomically decrements @v by 1.  Note that the guaranteed
  * useful range of an atomic_t is only 24 bits.
  */
-static inline long atomic_dec_return( atomic_t *v )
+static inline long atomic_dec_return(atomic_t *v)
 {
-	return __sync_sub_and_fetch(&v->counter, 1);
+	return __atomic_sub_fetch(&v->counter, 1, __ATOMIC_SEQ_CST);
 }
 
 /**
@@ -145,9 +139,9 @@ static inline long atomic_dec_return( atomic_t *v )
  * returns true if the result is 0, or false for all other
  * cases.
  */
-static inline long atomic_dec_and_test( atomic_t *v )
+static inline long atomic_dec_and_test(atomic_t *v)
 {
-	return !(__sync_sub_and_fetch(&v->counter, 1));
+	return !(__atomic_sub_fetch(&v->counter, 1, __ATOMIC_SEQ_CST));
 }
 
 /**
@@ -158,9 +152,9 @@ static inline long atomic_dec_and_test( atomic_t *v )
  * and returns true if the result is zero, or false for all
  * other cases.
  */
-static inline long atomic_inc_and_test( atomic_t *v )
+static inline long atomic_inc_and_test(atomic_t *v)
 {
-	return !(__sync_add_and_fetch(&v->counter, 1));
+	return !(__atomic_add_fetch(&v->counter, 1, __ATOMIC_SEQ_CST));
 }
 
 /**
@@ -172,10 +166,9 @@ static inline long atomic_inc_and_test( atomic_t *v )
  * if the result is negative, or false when
  * result is greater than or equal to zero.
  */
-static inline long atomic_add_negative( long i, atomic_t *v )
+static inline long atomic_add_negative(long i, atomic_t *v)
 {
-	return (__sync_add_and_fetch(&v->counter, i) < 0);
+	return (__atomic_add_fetch(&v->counter, i, __ATOMIC_SEQ_CST) < 0);
 }
 
 #endif
-

+ 12 - 12
src/tlog.c

@@ -190,7 +190,7 @@ static inline void _tlog_spin_unlock(unsigned int *lock)
 
 static int _tlog_mkdir(const char *path)
 {
-    char path_c[PATH_MAX];
+    char path_c[PATH_MAX + 1];
     char *path_end;
     char str;
     int len;
@@ -892,7 +892,7 @@ static int _tlog_get_oldest_callback(const char *path, struct dirent *entry, voi
 
     if (oldestlog->mtime == 0 || oldestlog->mtime > sb.st_mtime) {
         oldestlog->mtime = sb.st_mtime;
-        strncpy(oldestlog->name, entry->d_name, sizeof(oldestlog->name));
+        strncpy(oldestlog->name, entry->d_name, sizeof(oldestlog->name) - 1);
         oldestlog->name[sizeof(oldestlog->name) - 1] = '\0';
         return 0;
     }
@@ -1199,7 +1199,7 @@ static int _tlog_archive_log(struct tlog_log *log)
 
 static void _tlog_get_log_name_dir(struct tlog_log *log)
 {
-    char log_file[PATH_MAX];
+    char log_file[PATH_MAX + 1];
     if (log->fd > 0) {
         close(log->fd);
         log->fd = -1;
@@ -1248,7 +1248,7 @@ static int _tlog_write(struct tlog_log *log, const char *buff, int bufflen)
         log->rename_pending = 0;
     }
 
-    if (log->logcount <= 0) {
+    if (log->logcount <= 0 || log->logsize <= 0) {
         return 0;
     }
 
@@ -1663,7 +1663,7 @@ static void *_tlog_work(void *arg)
         log_len = 0;
         log_extlen = 0;
         log_extend = 0;
-        if (tlog.run == 0) {
+        if (__atomic_load_n(&tlog.run, __ATOMIC_RELAXED) == 0) {
             if (_tlog_any_has_data() == 0) {
                 break;
             }
@@ -1683,11 +1683,11 @@ static void *_tlog_work(void *arg)
         }
 
         /* if buffer is empty, wait */
-        if (_tlog_any_has_data_locked() == 0 && tlog.run) {
+        if (_tlog_any_has_data_locked() == 0 && __atomic_load_n(&tlog.run, __ATOMIC_RELAXED)) {
             log = _tlog_wait_log_locked(log);
             if (log == NULL) {
                 pthread_mutex_unlock(&tlog.lock);
-                if (errno != ETIMEDOUT && tlog.run) {
+                if (errno != ETIMEDOUT && __atomic_load_n(&tlog.run, __ATOMIC_RELAXED)) {
                     sleep(1);
                 }
                 continue;
@@ -1909,7 +1909,7 @@ tlog_log *tlog_open(const char *logfile, int maxlogsize, int maxlogcount, int bu
 {
     struct tlog_log *log = NULL;
 
-    if (tlog.run == 0) {
+    if (__atomic_load_n(&tlog.run, __ATOMIC_RELAXED) == 0) {
         fprintf(stderr, "tlog: tlog is not initialized.\n");
         return NULL;
     }
@@ -2090,7 +2090,7 @@ int tlog_init(const char *logfile, int maxlogsize, int maxlogcount, int buffsize
     pthread_attr_init(&attr);
     pthread_cond_init(&tlog.cond, NULL);
     pthread_mutex_init(&tlog.lock, NULL);
-    tlog.run = 1;
+    __atomic_store_n(&tlog.run, 1, __ATOMIC_RELAXED);
 
     log = tlog_open(logfile, maxlogsize, maxlogcount, buffsize, flag);
     if (log == NULL) {
@@ -2120,14 +2120,14 @@ int tlog_init(const char *logfile, int maxlogsize, int maxlogcount, int buffsize
 errout:
     if (tlog.tid) {
         void *retval = NULL;
-        tlog.run = 0;
+        __atomic_store_n(&tlog.run, 0, __ATOMIC_RELAXED);
         pthread_join(tlog.tid, &retval);
         tlog.tid = 0;
     }
 
     pthread_cond_destroy(&tlog.cond);
     pthread_mutex_destroy(&tlog.lock);
-    tlog.run = 0;
+    __atomic_store_n(&tlog.run, 0, __ATOMIC_RELAXED);
     tlog.root = NULL;
     tlog.root_format = NULL;
 
@@ -2144,7 +2144,7 @@ void tlog_exit(void)
 
     if (tlog.tid) {
         void *ret = NULL;
-        tlog.run = 0;
+        __atomic_store_n(&tlog.run, 0, __ATOMIC_RELAXED);
         pthread_mutex_lock(&tlog.lock);
         pthread_cond_signal(&tlog.cond);
         pthread_mutex_unlock(&tlog.lock);

+ 2 - 2
src/utils/ssl.c

@@ -346,7 +346,7 @@ static X509 *_generate_smartdns_cert(EVP_PKEY *pkey, X509 *issuer_cert, EVP_PKEY
 	const unsigned char *country = (unsigned char *)"smartdns";
 	const unsigned char *company = (unsigned char *)"smartdns";
 	const unsigned char *common_name = (unsigned char *)(is_ca ? "SmartDNS Root" : "smartdns");
-	const char *BASIC_CONSTRAINTS = is_ca ? "CA:TRUE" : "CA:FALSE";
+	const char *CA_BASIC_CONSTRAINTS = is_ca ? "CA:TRUE" : "CA:FALSE";
 	const char *KEY_USAGE = is_ca ? "keyCertSign,cRLSign" : "digitalSignature,keyEncipherment";
 	const char *EXT_KEY_USAGE = is_ca ? "clientAuth,serverAuth,codeSigning,timeStamping" : "serverAuth";
 
@@ -370,7 +370,7 @@ static X509 *_generate_smartdns_cert(EVP_PKEY *pkey, X509 *issuer_cert, EVP_PKEY
 	}
 
 	// Add X509v3 extensions
-	cert_ext = X509V3_EXT_conf_nid(NULL, NULL, NID_basic_constraints, BASIC_CONSTRAINTS);
+	cert_ext = X509V3_EXT_conf_nid(NULL, NULL, NID_basic_constraints, CA_BASIC_CONSTRAINTS);
 	X509_add_ext(cert, cert_ext, -1);
 	X509_EXTENSION_free(cert_ext);