medfall

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

commit 96937285d59fc8f842a016b7ea381cc3a20d6434
parent 06fb1f2e131847e1a7a8cc132acd28b3874c09f3
Author: Michael Savage <mikejsavage@gmail.com>
Date:   Tue Aug 11 22:42:41 +0200

Add some background task test code that segfaults

Diffstat:
Makefile | 2+-
game.h | 3+++
hm.cc | 15+++++++++++++++
work_queue.cc | 23+++++------------------
work_queue.h | 26++++++++++++++++++++++++--
5 files changed, 48 insertions(+), 21 deletions(-)
diff --git a/Makefile b/Makefile @@ -2,7 +2,7 @@ all: medfall hm.so pp OBJS = main.o gl.o work_queue.o BSPOBJS = bsp.o bsp_renderer.o gl.o -HMOBJS = hm.o heightmap.o terrain_manager.o stb_image.o stb_perlin.o +HMOBJS = hm.o heightmap.o terrain_manager.o work_queue.o stb_image.o stb_perlin.o PPOBJS = pp.o stb_image.o stb_image_write.o WARNINGS = -Wall -Wextra -Wno-unused-parameter -Wno-unused-function -Wno-write-strings diff --git a/game.h b/game.h @@ -6,11 +6,14 @@ #include "int.h" #include "terrain_manager.h" +#include "work_queue.h" struct GameState { glm::vec3 pos; glm::vec3 angles; TerrainManager tm; + + WorkQueue background_tasks; }; struct GameMemory { diff --git a/hm.cc b/hm.cc @@ -12,6 +12,7 @@ #include "int.h" #include "heightmap.h" #include "terrain_manager.h" +#include "work_queue.h" // #include "stb_easy_font.h" #include "stb_image.h" #include "stb_perlin.h" @@ -45,12 +46,26 @@ void print_vec3( const std::string & name, const glm::vec3 & v ) { glm::mat4 P( glm::perspective( glm::radians( 120.0f ), 640.0f / 480.0f, 0.1f, 10000.0f ) ); +WORK_QUEUE_CALLBACK( testwq ) { + u32 i = *( u32 * ) data; + printf( "the thread got called %u\n", i ); +} extern "C" GAME_INIT( game_init ) { state->pos = glm::vec3( 15000, 3000, 50 ); state->angles = glm::vec3( glm::radians( glm::vec3( -90, 45, 0 ) ) ); state->tm.use( "Srtm_ramp2.world.21600x10800.jpg.parts" ); state->tm.teleport( state->pos ); + workqueue_init( &state->background_tasks, 2 ); + u32 nums[ 10 ]; + for( u32 i = 0; i < 10; i++ ) { + nums[ i ] = i; + workqueue_enqueue( &state->background_tasks, testwq, &nums[ i ] ); + } + printf( "let's exhaust\n" ); + workqueue_exhaust( &state->background_tasks ); + printf( "init done\n" ); + glClearColor( 0, 0.5, 0.7, 1 ); } diff --git a/work_queue.cc b/work_queue.cc @@ -11,30 +11,16 @@ #define read_barrier() asm volatile ( "" ::: "memory" ) #define write_barrier() asm volatile ( "" ::: "memory" ) -struct Job { - WorkQueueCallback * callback; - void * data; -}; - -struct WorkQueue { - Job jobs[ 256 ]; - - sem_t * sem; - - // using head/length means we need to an atomic pair which is a pain - volatile u16 head; - volatile u16 tail; - - volatile u16 jobs_queued; - volatile u16 jobs_completed; -}; - +#include <stdio.h> static bool workqueue_step( WorkQueue * const queue ) { const u16 current_head = queue->head; const u16 new_head = ( current_head + 1 ) % array_len( queue->jobs ); + // read_barrier(); TODO: + if( queue->jobs_completed < queue->jobs_queued ) { if( __sync_bool_compare_and_swap( &queue->head, current_head, new_head ) ) { + printf( "let's do a step\n" ); Job & job = queue->jobs[ current_head ]; job.callback( job.data ); @@ -48,6 +34,7 @@ static bool workqueue_step( WorkQueue * const queue ) { return false; } +#include <stdio.h> static void * workqueue_worker( void * data ) { WorkQueue * queue = ( WorkQueue * ) data; diff --git a/work_queue.h b/work_queue.h @@ -1,11 +1,33 @@ #ifndef _WORK_QUEUE_H_ #define _WORK_QUEUE_H_ -#include "int.h" +#include <semaphore.h> -struct WorkQueue; +#include "int.h" #define WORK_QUEUE_CALLBACK( name ) void name( void * const data ) typedef WORK_QUEUE_CALLBACK( WorkQueueCallback ); +struct Job { + WorkQueueCallback * callback; + void * data; +}; + +struct WorkQueue { + Job jobs[ 256 ]; + + sem_t * sem; + + // using head/length means we need to an atomic pair which is a pain + volatile u16 head; + volatile u16 tail; + + volatile u16 jobs_queued; + volatile u16 jobs_completed; +}; + +void workqueue_init( WorkQueue * const queue, const u32 num_threads ); +void workqueue_enqueue( WorkQueue * const queue, WorkQueueCallback * const callback, void * const data ); +void workqueue_exhaust( WorkQueue * const queue ); + #endif // _WORK_QUEUE_H_