Skip to content

Commit

Permalink
Use a different condition variable for each mutex. Fixes OSX hang and…
Browse files Browse the repository at this point in the history
… Linux screen corruption, both when more than 2 hardware threads are used.

Thanks to @ndryden for helping get to the bottom of this one.
  • Loading branch information
SolraBizna committed Oct 6, 2017
1 parent 32bd04e commit 70d1b40
Showing 1 changed file with 6 additions and 6 deletions.
12 changes: 6 additions & 6 deletions src/fx.cc
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,13 @@ namespace {
static Worker* worker_threads;
static unsigned int thread_count;
static SDL_threadID main_thread;
static SDL_cond* go_cond;
SDL_mutex* lock;
SDL_cond* cond;
int body() {
SDL_LockMutex(lock);
while(true) {
while(task == nullptr) {
SDL_CondWait(go_cond, lock);
SDL_CondWait(cond, lock);
}
task(start, stop);
task = nullptr;
Expand All @@ -41,8 +41,8 @@ namespace {
public:
Worker() {
static unsigned int num_workers_so_far = 0;
if(go_cond == nullptr) go_cond = SDL_CreateCond();
assert(go_cond);
cond = SDL_CreateCond();
assert(cond);
lock = SDL_CreateMutex();
assert(lock);
std::ostringstream str;
Expand Down Expand Up @@ -78,11 +78,12 @@ namespace {
worker_threads[i].start = start;
worker_threads[i].stop = stop;
SDL_UnlockMutex(worker_threads[i].lock);
SDL_CondBroadcast(worker_threads[i].cond);
start = stop;
}
}
SDL_CondBroadcast(go_cond);
if(start != big_stop) task(start, big_stop);
// wait for each worker to complete before continuing
for(unsigned int i = 0; i < thread_count-1; ++i) {
SDL_LockMutex(worker_threads[i].lock);
SDL_UnlockMutex(worker_threads[i].lock);
Expand All @@ -101,7 +102,6 @@ namespace {
Worker* Worker::worker_threads;
unsigned int Worker::thread_count = 0;
SDL_threadID Worker::main_thread;
SDL_cond* Worker::go_cond;
}

void FX::init(unsigned int init_thread_count) {
Expand Down

0 comments on commit 70d1b40

Please sign in to comment.