Skip to content
Browse files

Resolvers: Replace mutex and condition variables with nested loop

  • Loading branch information...
1 parent f8e531b commit fd1f59d9a737a3e91bfaf92df7141b7ae241b75b @nikhilm committed Sep 16, 2012
Showing with 13 additions and 39 deletions.
  1. +11 −33 src/taglib.cc
  2. +2 −6 src/taglib.h
View
44 src/taglib.cc
@@ -312,14 +312,15 @@ void CallbackResolver::invokeResolverCb(uv_async_t *handle, int status)
{
AsyncResolverBaton *baton = (AsyncResolverBaton *) handle->data;
invokeResolver(baton);
-#ifdef _WIN32
- WakeAllConditionVariable(&baton->cv);
-#else
- pthread_cond_broadcast(&baton->cv);
-#endif
+ uv_async_send((uv_async_t*) &baton->idler);
uv_close((uv_handle_t*)&baton->request, 0);
}
+void CallbackResolver::stopIdling(uv_async_t *handle, int status)
+{
+ uv_close((uv_handle_t*) handle, 0);
+}
+
void CallbackResolver::invokeResolver(AsyncResolverBaton *baton)
{
HandleScope scope;
@@ -345,43 +346,20 @@ TagLib::File *CallbackResolver::createFile(TagLib::FileName fileName, bool readA
#else
if (created_in != pthread_self()) {
#endif
- uv_async_init(uv_default_loop(), &baton.request, invokeResolverCb);
- uv_mutex_init(&baton.mutex);
- uv_mutex_lock(&baton.mutex);
-
-#ifdef _WIN32
- InitializeConditionVariable(&baton.cv);
-#else
- if (pthread_cond_init(&baton.cv, NULL))
- abort();
-#endif
+ uv_loop_t *wait_loop = uv_loop_new();
+ uv_async_init(wait_loop, &baton.idler, CallbackResolver::stopIdling);
+ uv_async_init(uv_default_loop(), &baton.request, invokeResolverCb);
uv_async_send(&baton.request);
-#ifdef _WIN32
- SleepConditionVariableCS(&baton.cv, &baton.mutex, INFINITE);
-#else
- pthread_cond_wait(&baton.cv, &baton.mutex);
-#endif
+ uv_run(wait_loop);
+ uv_loop_delete(wait_loop);
}
else {
invokeResolver(&baton);
}
TagLib::FileStream *stream = new TagLib::FileStream(fileName);
-#ifdef _WIN32
- if (created_in != GetCurrentThreadId()) {
-#else
- if (created_in != pthread_self()) {
-#endif
- uv_mutex_unlock(&baton.mutex);
- uv_mutex_destroy(&baton.mutex);
-#ifdef _WIN32
-#else
- pthread_cond_destroy(&baton.cv);
-#endif
- }
-
return node_taglib::createFile(stream, baton.type);
}
#endif // ENABLE_RESOLVERS
View
8 src/taglib.h
@@ -54,15 +54,10 @@ class CallbackResolver;
struct AsyncResolverBaton {
uv_async_t request;
- uv_mutex_t mutex;
-#ifdef _WIN32
- CONDITION_VARIABLE cv;
-#else
- pthread_cond_t cv;
-#endif
const CallbackResolver *resolver;
TagLib::FileName fileName;
TagLib::String type;
+ uv_async_t idler;
};
class CallbackResolver : public TagLib::FileRef::FileTypeResolver {
@@ -73,6 +68,7 @@ class CallbackResolver : public TagLib::FileRef::FileTypeResolver {
CallbackResolver(v8::Persistent<v8::Function> func);
TagLib::File *createFile(TagLib::FileName fileName, bool readAudioProperties, TagLib::AudioProperties::ReadStyle audioPropertiesStyle) const;
static void invokeResolverCb(uv_async_t *handle, int status);
+ static void stopIdling(uv_async_t *handle, int status);
static void invokeResolver(AsyncResolverBaton *baton);
};
#endif

0 comments on commit fd1f59d

Please sign in to comment.
Something went wrong with that request. Please try again.