Skip to content

Commit 23b330f

Browse files
authored
Merge pull request #3 from OscarShiang/atomic
Replace __sync with C11 Atomics
2 parents 6bf04db + b7d81ff commit 23b330f

File tree

2 files changed

+11
-10
lines changed

2 files changed

+11
-10
lines changed

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
KDIR=/lib/modules/$(shell uname -r)/build
22

3-
CFLAGS_user = -std=gnu99 -Wall -Wextra -Werror
3+
CFLAGS_user = -std=gnu11 -Wall -Wextra -Werror
44
LDFLAGS_user = -lpthread
55

66
obj-m += khttpd.o

htstress.c

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
#include <netinet/tcp.h>
4141
#include <pthread.h>
4242
#include <signal.h>
43+
#include <stdatomic.h>
4344
#include <stdio.h>
4445
#include <stdlib.h>
4546
#include <string.h>
@@ -81,11 +82,11 @@ static int num_threads = 1;
8182

8283
static char *udaddr = "";
8384

84-
static volatile uint64_t num_requests = 0;
85+
static volatile _Atomic uint64_t num_requests = 0;
8586
static volatile uint64_t max_requests = 0;
86-
static volatile uint64_t good_requests = 0;
87-
static volatile uint64_t bad_requests = 0;
88-
static volatile uint64_t socket_errors = 0;
87+
static volatile _Atomic uint64_t good_requests = 0;
88+
static volatile _Atomic uint64_t bad_requests = 0;
89+
static volatile _Atomic uint64_t socket_errors = 0;
8990
static volatile uint64_t in_bytes = 0;
9091
static volatile uint64_t out_bytes = 0;
9192

@@ -214,7 +215,7 @@ static void *worker(void *arg)
214215
if (number_of_errors_logged % 100 == 0) {
215216
fprintf(stderr, "EPOLLERR caused by unknown error\n");
216217
}
217-
__sync_fetch_and_add(&socket_errors, 1);
218+
atomic_fetch_add(&socket_errors, 1);
218219
close(ec->fd);
219220
if (num_requests > max_requests)
220221
continue;
@@ -284,14 +285,14 @@ static void *worker(void *arg)
284285
if (!ret) {
285286
close(ec->fd);
286287

287-
int m = __sync_fetch_and_add(&num_requests, 1);
288+
int m = atomic_fetch_add(&num_requests, 1);
288289

289290
if (max_requests && (m + 1 > (int) max_requests))
290-
__sync_fetch_and_sub(&num_requests, 1);
291+
atomic_fetch_sub(&num_requests, 1);
291292
else if (ec->flags & BAD_REQUEST)
292-
__sync_fetch_and_add(&bad_requests, 1);
293+
atomic_fetch_add(&bad_requests, 1);
293294
else
294-
__sync_fetch_and_add(&good_requests, 1);
295+
atomic_fetch_add(&good_requests, 1);
295296

296297
if (max_requests && (m + 1 >= (int) max_requests)) {
297298
end_time();

0 commit comments

Comments
 (0)