medfall

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs

commit f9b0431908aed740a5cd35988aba5424111060bf
parent c756c6337b10d8042e0c4c239cc311f2857d18b0
Author: Michael Savage <mikejsavage@gmail.com>
Date:   Sat Sep 10 18:11:19 -0700

Properly check if a WorkQueue is full

Diffstat:
work_queue.cc | 13+++----------
work_queue.h | 3---
2 files changed, 3 insertions(+), 13 deletions(-)
diff --git a/work_queue.cc b/work_queue.cc @@ -22,8 +22,6 @@ static bool workqueue_step( u32 thread_id, WorkQueue * queue ) { if( compare_exchange_seqcst( &queue->head, &current_head, new_head ) ) { const Job & job = queue->jobs[ current_head % array_count( queue->jobs ) ]; job.callback( job.data, &queue->arenas[ thread_id ] ); - - fetch_add_seqcst( &queue->jobs_completed, 1 ); } return true; @@ -83,23 +81,18 @@ void workqueue_init( WorkQueue * queue, MemoryArena * arena, u32 num_threads ) { } void workqueue_enqueue( WorkQueue * queue, WorkQueueCallback * callback, void * data ) { - assert( queue->jobs_queued < array_count( queue->jobs ) ); + u32 tail = fetch_add_release( &queue->tail, 1 ); + assert( tail - load_acquire( &queue->head ) < array_count( queue->jobs ) - 1 ); Job job = { callback, data }; - u32 tail = fetch_add_seqcst( &queue->tail, 1 ); - queue->jobs[ tail % array_count( queue->jobs ) ] = job; - queue->jobs_queued++; semaphore_signal( &queue->sem ); } void workqueue_exhaust( WorkQueue * queue ) { - while( load_seqcst( &queue->jobs_completed ) < queue->jobs_queued ) { + while( load_relaxed( &queue->head ) != load_acquire( &queue->tail ) ) { workqueue_step( queue->num_threads, queue ); } - - queue->jobs_queued = 0; - store_seqcst( &queue->jobs_completed, 0 ); } diff --git a/work_queue.h b/work_queue.h @@ -23,9 +23,6 @@ struct WorkQueue { atomic_u32 head; atomic_u32 tail; - u32 jobs_queued; - atomic_u32 jobs_completed; - u32 num_threads; MemoryArena * arenas; };