#define PARTICLE_COUNT 2500
// :ArbitraryConstant
-#define NUM_THREADS 4
+#define NUM_THREADS 2
// TODO(Brendan): Maybe this can be removed because it isn't really necessary?
internal void
return vao;
}
+struct SimState;
+struct ThreadData
+{
+ i32 id;
+ SimState* state;
+};
+
struct SimState
{
Array<Body> bodies;
FixedArenaAllocator<QuadTree<Body>> qt_body_allocator;
Camera camera;
+
+ pthread_barrier_t thread_sync_barrier;
+ pthread_t threads[NUM_THREADS - 1];
+ ThreadData thread_data[NUM_THREADS - 1];
};
internal void
persist const f64 step = 0.01;
- foreach (i, low, high)
- {
- body_accumulate_move(&state->bodies[i], &state->qt_bodies, step);
- }
-
- foreach (i, low, high)
- {
- body_apply_move(&state->bodies[i], step);
- }
+ foreach (i, low, high) body_accumulate_move(&state->bodies[i], &state->qt_bodies, step);
+ foreach (i, low, high) body_apply_move(&state->bodies[i], step);
}
-internal pthread_barrier_t thread_sync_barrier;
-internal pthread_t threads[NUM_THREADS - 1];
-
-struct ThreadData
-{
- i32 id;
- SimState* state;
-};
-
internal void*
thread_start(void* data)
{
while (true)
{
- pthread_barrier_wait(&thread_sync_barrier);
+ pthread_barrier_wait(&state->thread_sync_barrier);
update_bodies_partial(state, thread_id + 1);
- pthread_barrier_wait(&thread_sync_barrier);
+ pthread_barrier_wait(&state->thread_sync_barrier);
if (thread_id == 0)
{
state->qt_bodies.init(AABB { -2000, -2000, 4000, 4000 });
state->qt_body_allocator.reset();
For (state->bodies) state->qt_bodies.insert(&it, &state->qt_body_allocator);
}
- pthread_barrier_wait(&thread_sync_barrier);
+ pthread_barrier_wait(&state->thread_sync_barrier);
}
return NULL;
state->camera.scale = 1.0f;
- pthread_barrier_init(&thread_sync_barrier, NULL, NUM_THREADS);
+ pthread_barrier_init(&state->thread_sync_barrier, NULL, NUM_THREADS);
foreach (i, 0, NUM_THREADS - 1)
{
- auto td = alloc<ThreadData>(1); // LEAK
- td->id = i;
- td->state = state;
- pthread_create(&threads[i], NULL, thread_start, (void *) td);
+ state->thread_data[i] = ThreadData { i, state };
+ pthread_create(&state->threads[i], NULL, thread_start, (void *) &state->thread_data[i]);
}
}
if (glfwGetKey(window, GLFW_KEY_Q)) state->camera.scale *= 1.02f;
if (glfwGetKey(window, GLFW_KEY_A)) state->camera.scale /= 1.02f;
- pthread_barrier_wait(&thread_sync_barrier);
+ pthread_barrier_wait(&state->thread_sync_barrier);
update_bodies_partial(state, 0);
- pthread_barrier_wait(&thread_sync_barrier);
+ pthread_barrier_wait(&state->thread_sync_barrier);
}
// NOTE CLEANUP(Brendan): Bunch of graphics state that should go elsewhere.
// NOTE(Brendan): Present the changes to the screen.
glfwSwapBuffers(window);
- pthread_barrier_wait(&thread_sync_barrier);
+ pthread_barrier_wait(&state->thread_sync_barrier);
}
internal void
while (!glfwWindowShouldClose(window))
{
- glfwPollEvents();
-
curr_time = glfwGetTime();
delta = curr_time - last_time;
last_time = curr_time;
frame_rate = frame_count;
frame_count = 0;
}
+
+ glfwPollEvents();
}
}