File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff 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
Original file line number Diff line number Diff 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}
You can’t perform that action at this time.
0 commit comments