Skip to content

Commit

Permalink
fix for race condition when callback is invoked from thread pool
Browse files Browse the repository at this point in the history
  • Loading branch information
nikmikov committed May 16, 2014
1 parent 8c8af2a commit 4b564f3
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 3 deletions.
9 changes: 8 additions & 1 deletion src/ffi.h
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,14 @@ class CallbackInfo {
static uv_async_t g_async;
};


/**
* Synchronization object to ensure following order of execution:
* -> WaitForExecution() invoked
* -> SignalDoneExecuting() returned
* -> WaitForExecution() returned
*
* ^WaitForExecution() must always be called from the thread which owns the object
*/
class ThreadedCallbackInvokation {
public:
ThreadedCallbackInvokation(callback_info *cbinfo, void *retval, void **parameters);
Expand Down
4 changes: 2 additions & 2 deletions src/threaded_callback_invokation.cc
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@ ThreadedCallbackInvokation::ThreadedCallbackInvokation(callback_info *cbinfo, vo
m_parameters = parameters;

pthread_mutex_init(&m_mutex, NULL);
pthread_mutex_lock(&m_mutex);
pthread_cond_init(&m_cond, NULL);
}

ThreadedCallbackInvokation::~ThreadedCallbackInvokation() {
pthread_mutex_unlock(&m_mutex);
pthread_cond_destroy(&m_cond);
pthread_mutex_destroy(&m_mutex);
}
Expand All @@ -21,7 +23,5 @@ void ThreadedCallbackInvokation::SignalDoneExecuting() {
}

void ThreadedCallbackInvokation::WaitForExecution() {
pthread_mutex_lock(&m_mutex);
pthread_cond_wait(&m_cond, &m_mutex);
pthread_mutex_unlock(&m_mutex);
}

0 comments on commit 4b564f3

Please sign in to comment.