Quellcode durchsuchen

initialize cache mutexes in right file

Sean Barrett vor 10 Jahren
Ursprung
Commit
5a0dcc90d6
2 geänderte Dateien mit 13 neuen und 7 gelöschten Zeilen
  1. 6 0
      tests/caveview/cave_mesher.c
  2. 7 7
      tests/caveview/cave_render.c

+ 6 - 0
tests/caveview/cave_mesher.c

@@ -1,3 +1,5 @@
+// This file takes minecraft chunks (decoded by cave_parse) and
+// uses stb_voxel_render to turn them into vertex buffers.
 
 #define STB_GLEXT_DECLARE "glext_list.h"
 #include "stb_gl.h"
@@ -452,6 +454,7 @@ void reset_cache_size(int size)
    cache_size = size;
 }
 
+// this must be called inside mutex
 void deref_fastchunk(fast_chunk *fc)
 {
    if (fc) {
@@ -672,6 +675,9 @@ void mesh_init(void)
 {
    int i;
 
+   chunk_cache_mutex = SDL_CreateMutex();
+   chunk_get_mutex   = SDL_CreateMutex();
+
    for (i=0; i < 256; ++i) {
       memcpy(minecraft_tex1_for_blocktype[i], minecraft_info[i]+1, 6);
       minecraft_trans_for_blocktype[i] = (minecraft_info[i][0] != C_solid && minecraft_info[i][0] != C_water);

+ 7 - 7
tests/caveview/cave_render.c

@@ -1,3 +1,6 @@
+// This file takes renders vertex buffers and manages
+// threads that invoke mesh building (found in cave_mesher.c)
+
 #include "stb_voxel_render.h"
 
 #define STB_GLEXT_DECLARE "glext_list.h"
@@ -57,10 +60,6 @@ typedef struct
 
 } chunk_mesh;
 
-extern SDL_mutex * chunk_cache_mutex;
-extern SDL_mutex * chunk_get_mutex;
-
-
 void scale_texture(unsigned char *src, int x, int y, int w, int h)
 {
    int i,j,k;
@@ -511,6 +510,8 @@ void world_init(void)
    reset_cache_size(32);
 }
 
+extern SDL_mutex * chunk_cache_mutex;
+
 int mesh_worker_handler(void *data)
 {
    mesh_worker *mw = data;
@@ -549,6 +550,8 @@ int mesh_worker_handler(void *data)
 
       // when done, free the chunks
 
+      // for efficiency we just take the mutex once around the whole thing,
+      // though this spreads the mutex logic over two files
       SDL_LockMutex(chunk_cache_mutex);
       for (j=0; j < 4; ++j)
          for (i=0; i < 4; ++i) {
@@ -594,9 +597,6 @@ void prepare_threads(void)
    if (num_mesh_workers > MAX_MESH_WORKERS)
       num_mesh_workers = MAX_MESH_WORKERS;
 
-   chunk_cache_mutex = SDL_CreateMutex();
-   chunk_get_mutex   = SDL_CreateMutex();
-
    for (i=0; i < num_mesh_workers; ++i) {
       mesh_worker *data = &mesh_data[i];
       data->request_received = SDL_CreateSemaphore(0);