medfall

A super great game engine
Log | Files | Refs

work_queue.h (817B)


      1 #pragma once
      2 
      3 #include "intrinsics.h"
      4 #include "array.h"
      5 #include "memory_arena.h"
      6 #include "platform_atomic.h"
      7 #include "platform_semaphore.h"
      8 #include "platform_thread.h"
      9 
     10 #define WORK_QUEUE_CALLBACK( name ) void name( const void * data, MemoryArena * arena )
     11 typedef WORK_QUEUE_CALLBACK( WorkQueueCallback );
     12 
     13 struct Job {
     14 	WorkQueueCallback * callback;
     15 	const void * data;
     16 };
     17 
     18 struct WorkQueue {
     19 	Job jobs[ 2048 ];
     20 
     21 	Semaphore sem;
     22 
     23 	atomic_u32 head, tail;
     24 	atomic_u32 shutting_down;
     25 
     26 	array< Thread > threads;
     27 	array< MemoryArena > arenas;
     28 };
     29 
     30 void workqueue_init( WorkQueue * queue, MemoryArena * arena, u32 num_threads );
     31 void workqueue_enqueue( WorkQueue * queue, WorkQueueCallback * callback, const void * data = NULL );
     32 void workqueue_exhaust( WorkQueue * queue );
     33 void workqueue_term( WorkQueue * queue );