Skip to content
This repository
Browse code

Resolvers: Replace mutex and condition variables with nested loop

  • Loading branch information...
commit fd1f59d9a737a3e91bfaf92df7141b7ae241b75b 1 parent f8e531b
Nikhil Marathe authored

Showing 2 changed files with 13 additions and 39 deletions. Show diff stats Hide diff stats

  1. +11 33 src/taglib.cc
  2. +2 6 src/taglib.h
44 src/taglib.cc
@@ -312,14 +312,15 @@ void CallbackResolver::invokeResolverCb(uv_async_t *handle, int status)
312 312 {
313 313 AsyncResolverBaton *baton = (AsyncResolverBaton *) handle->data;
314 314 invokeResolver(baton);
315   -#ifdef _WIN32
316   - WakeAllConditionVariable(&baton->cv);
317   -#else
318   - pthread_cond_broadcast(&baton->cv);
319   -#endif
  315 + uv_async_send((uv_async_t*) &baton->idler);
320 316 uv_close((uv_handle_t*)&baton->request, 0);
321 317 }
322 318
  319 +void CallbackResolver::stopIdling(uv_async_t *handle, int status)
  320 +{
  321 + uv_close((uv_handle_t*) handle, 0);
  322 +}
  323 +
323 324 void CallbackResolver::invokeResolver(AsyncResolverBaton *baton)
324 325 {
325 326 HandleScope scope;
@@ -345,23 +346,13 @@ TagLib::File *CallbackResolver::createFile(TagLib::FileName fileName, bool readA
345 346 #else
346 347 if (created_in != pthread_self()) {
347 348 #endif
348   - uv_async_init(uv_default_loop(), &baton.request, invokeResolverCb);
349   - uv_mutex_init(&baton.mutex);
350   - uv_mutex_lock(&baton.mutex);
351   -
352   -#ifdef _WIN32
353   - InitializeConditionVariable(&baton.cv);
354   -#else
355   - if (pthread_cond_init(&baton.cv, NULL))
356   - abort();
357   -#endif
  349 + uv_loop_t *wait_loop = uv_loop_new();
  350 + uv_async_init(wait_loop, &baton.idler, CallbackResolver::stopIdling);
358 351
  352 + uv_async_init(uv_default_loop(), &baton.request, invokeResolverCb);
359 353 uv_async_send(&baton.request);
360   -#ifdef _WIN32
361   - SleepConditionVariableCS(&baton.cv, &baton.mutex, INFINITE);
362   -#else
363   - pthread_cond_wait(&baton.cv, &baton.mutex);
364   -#endif
  354 + uv_run(wait_loop);
  355 + uv_loop_delete(wait_loop);
365 356 }
366 357 else {
367 358 invokeResolver(&baton);
@@ -369,19 +360,6 @@ TagLib::File *CallbackResolver::createFile(TagLib::FileName fileName, bool readA
369 360
370 361 TagLib::FileStream *stream = new TagLib::FileStream(fileName);
371 362
372   -#ifdef _WIN32
373   - if (created_in != GetCurrentThreadId()) {
374   -#else
375   - if (created_in != pthread_self()) {
376   -#endif
377   - uv_mutex_unlock(&baton.mutex);
378   - uv_mutex_destroy(&baton.mutex);
379   -#ifdef _WIN32
380   -#else
381   - pthread_cond_destroy(&baton.cv);
382   -#endif
383   - }
384   -
385 363 return node_taglib::createFile(stream, baton.type);
386 364 }
387 365 #endif // ENABLE_RESOLVERS
8 src/taglib.h
@@ -54,15 +54,10 @@ class CallbackResolver;
54 54
55 55 struct AsyncResolverBaton {
56 56 uv_async_t request;
57   - uv_mutex_t mutex;
58   -#ifdef _WIN32
59   - CONDITION_VARIABLE cv;
60   -#else
61   - pthread_cond_t cv;
62   -#endif
63 57 const CallbackResolver *resolver;
64 58 TagLib::FileName fileName;
65 59 TagLib::String type;
  60 + uv_async_t idler;
66 61 };
67 62
68 63 class CallbackResolver : public TagLib::FileRef::FileTypeResolver {
@@ -73,6 +68,7 @@ class CallbackResolver : public TagLib::FileRef::FileTypeResolver {
73 68 CallbackResolver(v8::Persistent<v8::Function> func);
74 69 TagLib::File *createFile(TagLib::FileName fileName, bool readAudioProperties, TagLib::AudioProperties::ReadStyle audioPropertiesStyle) const;
75 70 static void invokeResolverCb(uv_async_t *handle, int status);
  71 + static void stopIdling(uv_async_t *handle, int status);
76 72 static void invokeResolver(AsyncResolverBaton *baton);
77 73 };
78 74 #endif

0 comments on commit fd1f59d

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