Skip to content

Commit c3f19e6

Browse files
committed
If register_worker() acquires the worker-record, advancing the index
becomes optional, i.e. it's OK if some other thread advances the index. This should be slightly faster, i.e. less reasons to retry. Also modified the stress-test to print the number of bytes transmitted, and the number of bytes each thread tried to transmit.
1 parent 6ce2969 commit c3f19e6

File tree

2 files changed

+14
-15
lines changed

2 files changed

+14
-15
lines changed

src/ringbuf.c

+5-11
Original file line numberDiff line numberDiff line change
@@ -183,18 +183,12 @@ register_worker(ringbuf_t *rbuf, unsigned registration_type)
183183
if (!atomic_compare_exchange_weak(&w->registered, not_registered, being_registered))
184184
continue;
185185
acquired = true;
186+
w->seen_off = RBUF_OFF_MAX;
187+
atomic_thread_fence(memory_order_release);
188+
w->registered = registration_type;
186189

187-
/* Swap the indexes to exclusively acquire the worker-record. */
188-
if (atomic_compare_exchange_weak(p_free_worker, prev_free_worker, new_free_worker)) {
189-
w->seen_off = RBUF_OFF_MAX;
190-
atomic_thread_fence(memory_order_release);
191-
w->registered = registration_type;
192-
break;
193-
}
194-
195-
/* The worker-record was not successfully acquired. */
196-
w->registered = not_registered;
197-
acquired = false;
190+
/* Advance the index if no one else has. */
191+
atomic_compare_exchange_weak(p_free_worker, prev_free_worker, new_free_worker);
198192
}
199193
}
200194

src/t_stress.c

+9-4
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ ringbuf_stress(void *arg)
106106
w = ringbuf_register(ringbuf);
107107
assert (w != NULL);
108108
}
109-
uint64_t total_recv = 0;
109+
uint64_t total_xmit = 0, total_not_xmit = 0;
110110

111111
/*
112112
* There are NCPU threads concurrently generating and producing
@@ -125,7 +125,7 @@ ringbuf_stress(void *arg)
125125

126126
if (id == 0) {
127127
if ((len = ringbuf_consume(ringbuf, &off)) != 0) {
128-
total_recv += len;
128+
total_xmit += len;
129129
size_t rem = len;
130130
assert(off < RBUF_SIZE);
131131
while (rem) {
@@ -140,15 +140,20 @@ ringbuf_stress(void *arg)
140140
}
141141
len = generate_message(buf, sizeof(buf) - 1);
142142
if ((ret = ringbuf_acquire(ringbuf, &w, len)) != -1) {
143+
total_xmit += len;
143144
off = (size_t)ret;
144145
assert(off < RBUF_SIZE);
145146
memcpy(&rbuf[off], buf, len);
146147
ringbuf_produce(ringbuf, &w);
147-
}
148+
} else
149+
total_not_xmit += len;
148150
}
149151
pthread_barrier_wait(&barrier);
150152
if (id == 0)
151-
printf ("Total received: %" PRIu64 "\n", total_recv);
153+
printf ("Thread 0: received %" PRIu64 "\n", total_xmit);
154+
else
155+
printf ("Thread %d: sent %" PRIu64 ", unsent %" PRIu64 "\n",
156+
id, total_xmit, total_not_xmit);
152157
pthread_exit(NULL);
153158
return NULL;
154159
}

0 commit comments

Comments
 (0)