fixed thread overspinning problem

master
Allen Webster 2017-11-07 20:15:09 -05:00
parent fccb93ccf9
commit 5f7de6acaa
8 changed files with 83 additions and 25 deletions

View File

@ -80,6 +80,7 @@ PLAT_THREAD_SIG(coroutine_main){
Assert(me->yield_ctx != 0); Assert(me->yield_ctx != 0);
Assert(me->function != 0); Assert(me->function != 0);
DBG_POINT();
me->function(&me->head); me->function(&me->head);
// NOTE(allen): Wake up the caller and set this coroutine back to being dead. // NOTE(allen): Wake up the caller and set this coroutine back to being dead.

View File

@ -76,6 +76,7 @@
#include "4ed_shared_thread_constants.h" #include "4ed_shared_thread_constants.h"
#include "unix_threading_wrapper.h" #include "unix_threading_wrapper.h"
#include "linux_semaphore_wrapper.h"
// //
// Linux macros // Linux macros
@ -1821,7 +1822,7 @@ main(int argc, char **argv){
// HACK(allen): THIS SHIT IS FUCKED (happens on mac too) // HACK(allen): THIS SHIT IS FUCKED (happens on mac too)
b32 keep_running = linuxvars.keep_running; b32 keep_running = linuxvars.keep_running;
app.step(&sysfunc, &target, &memory_vars, &linuxvars.input, &result); app.step(&sysfunc, &target, &memory_vars, &linuxvars.input, &result);
if (result.perform_kill){ if (result.perform_kill){

View File

@ -0,0 +1,34 @@
/*
* Mr. 4th Dimention - Allen Webster
*
* 07.11.2017
*
* Linux semaphore wrapper
*
*/
// TOP
union Semaphore{
sem_t s;
FixSize(SEMAPHORE_TYPE_SIZE);
};
internal void
system_init_semaphore(Semaphore *s, u32 count){
sem_init(&s->s, 0, 0);
}
internal void
system_wait_on_semaphore(Semaphore *s){
sem_wait(&s->s);
}
internal void
system_release_semaphore(Semaphore *s){
sem_post(&s->s);
}
// BOTTOM

View File

@ -31,6 +31,8 @@
# include "4coder_default_bindings.cpp" # include "4coder_default_bindings.cpp"
#endif #endif
#include "osx_objective_c_to_cpp_links.h"
#include "4ed_math.h" #include "4ed_math.h"
#include "4ed_system.h" #include "4ed_system.h"
@ -48,6 +50,11 @@
#include <OpenGL/OpenGL.h> #include <OpenGL/OpenGL.h>
#include <OpenGL/gl.h> #include <OpenGL/gl.h>
#undef external
#undef internal
#include <mach/mach.h>
#define external extern "C"
#define internal static
#include <mach-o/dyld.h> #include <mach-o/dyld.h>
#include <stdlib.h> #include <stdlib.h>
@ -56,6 +63,7 @@
#include "4ed_shared_thread_constants.h" #include "4ed_shared_thread_constants.h"
#include "unix_threading_wrapper.h" #include "unix_threading_wrapper.h"
#include "mac_semaphore_wrapper.h"
// TODO(allen): Make an intrinsics header that uses the cracked OS to define a single set of intrinsic names. // TODO(allen): Make an intrinsics header that uses the cracked OS to define a single set of intrinsic names.
#define InterlockedCompareExchange(dest, ex, comp) \ #define InterlockedCompareExchange(dest, ex, comp) \
@ -86,7 +94,6 @@ struct OSX_Vars{
//////////////////////////////// ////////////////////////////////
#include "osx_objective_c_to_cpp_links.h"
OSX_Objective_C_Vars osx_objc; OSX_Objective_C_Vars osx_objc;
OSX_Vars osxvars; OSX_Vars osxvars;
global Render_Target target; global Render_Target target;

View File

@ -582,13 +582,13 @@ osx_get_file_change_event(char *buffer, i32 max, i32 *size){
struct kevent event_out; struct kevent event_out;
i32 count = kevent(file_change_queue.kq, 0, 0, &event_out, 1, &t); i32 count = kevent(file_change_queue.kq, 0, 0, &event_out, 1, &t);
if (count < 0 || (count > 0 && event_out.flags == EV_ERROR)){ if (count < 0 || (count > 0 && event_out.flags == EV_ERROR)){
fprintf(stdout, "count: %4d error: %s\n", count, strerror(errno)); //fprintf(stdout, "count: %4d error: %s\n", count, strerror(errno));
} }
else if (count > 0){ else if (count > 0){
if (event_out.udata != 0){ if (event_out.udata != 0){
i32 len = *(i32*)event_out.udata; i32 len = *(i32*)event_out.udata;
char *str = (char*)((i32*)event_out.udata + 1); char *str = (char*)((i32*)event_out.udata + 1);
fprintf(stdout, "got an event for file: %.*s\n", len, str); //fprintf(stdout, "got an event for file: %.*s\n", len, str);
if (len <= max){ if (len <= max){
*size = len; *size = len;
memcpy(buffer, str, len); memcpy(buffer, str, len);

View File

@ -0,0 +1,35 @@
/*
* Mr. 4th Dimention - Allen Webster
*
* 07.11.2017
*
* Mac semaphore wrapper
*
*/
// TOP
union Semaphore{
semaphore_t s;
FixSize(SEMAPHORE_TYPE_SIZE);
};
internal void
system_init_semaphore(Semaphore *s, u32 count){
task_t task = mach_task_self();
semaphore_create(task, &s->s, SYNC_POLICY_FIFO, 0);
}
internal void
system_wait_on_semaphore(Semaphore *s){
semaphore_wait(s->s);
}
internal void
system_release_semaphore(Semaphore *s){
semaphore_signal(s->s);
}
// BOTTOM

View File

@ -14,7 +14,7 @@
#include <stdio.h> #include <stdio.h>
#if 1 #if 0
#define DBG_POINT() fprintf(stdout, "%s\n", __FILE__ ":" LINE_STR ":") #define DBG_POINT() fprintf(stdout, "%s\n", __FILE__ ":" LINE_STR ":")
#else #else
#define DBG_POINT() #define DBG_POINT()

View File

@ -30,11 +30,6 @@ union Condition_Variable{
FixSize(CONDITION_VARIABLE_TYPE_SIZE); FixSize(CONDITION_VARIABLE_TYPE_SIZE);
}; };
union Semaphore{
sem_t s;
FixSize(SEMAPHORE_TYPE_SIZE);
};
internal void internal void
system_init_and_launch_thread(Thread *t, Thread_Function *proc, void *ptr){ system_init_and_launch_thread(Thread *t, Thread_Function *proc, void *ptr){
pthread_create(&t->t, 0, proc, ptr); pthread_create(&t->t, 0, proc, ptr);
@ -70,21 +65,6 @@ system_signal_cv(Condition_Variable *cv, Mutex *m){
pthread_cond_signal(&cv->cv); pthread_cond_signal(&cv->cv);
} }
internal void
system_init_semaphore(Semaphore *s, u32 count){
sem_init(&s->s, 0, 0);
}
internal void
system_wait_on_semaphore(Semaphore *s){
sem_wait(&s->s);
}
internal void
system_release_semaphore(Semaphore *s){
sem_post(&s->s);
}
#endif #endif
// BOTTOM // BOTTOM