ref: 4814d5828d65459e122d445f23a91a5fe0a5bc97
parent: 5480ffafdf395a429ae5b9f966ecbd6b3169b36c
author: Martin Storsjö <martin@martin.st>
date: Sun Mar 2 18:13:39 EST 2014
Use unnamed semaphores on linux This avoids the risk of namespace collisions for named semaphores (where the names are global for the whole machine), on platforms where we strictly don't need to use the named semaphores.
--- a/codec/common/WelsThreadLib.cpp
+++ b/codec/common/WelsThreadLib.cpp
@@ -58,6 +58,7 @@
#include "WelsThreadLib.h"
#include <stdio.h>
+#include <stdlib.h>
#ifdef MT_ENABLED
@@ -254,15 +255,8 @@
// unnamed semaphores aren't supported on OS X
-WELS_THREAD_ERROR_CODE WelsEventInit (WELS_EVENT* event) {
- return sem_init (*event, 0, 0);
-}
-
-WELS_THREAD_ERROR_CODE WelsEventDestroy (WELS_EVENT* event) {
- return sem_destroy (*event); // match with sem_init
-}
-
WELS_THREAD_ERROR_CODE WelsEventOpen (WELS_EVENT* p_event, const char* event_name) {
+#ifdef __APPLE__
if (p_event == NULL || event_name == NULL)
return WELS_THREAD_ERROR_GENERAL;
*p_event = sem_open (event_name, O_CREAT, (S_IRUSR | S_IWUSR)/*0600*/, 0);
@@ -273,12 +267,30 @@
} else {
return WELS_THREAD_ERROR_OK;
}
+#else
+ WELS_EVENT event = (WELS_EVENT) malloc(sizeof(*event));
+ if (event == NULL)
+ return WELS_THREAD_ERROR_GENERAL;
+ WELS_THREAD_ERROR_CODE err = sem_init(event, 0, 0);
+ if (!err) {
+ *p_event = event;
+ return err;
+ }
+ free(event);
+ return err;
+#endif
}
WELS_THREAD_ERROR_CODE WelsEventClose (WELS_EVENT* event, const char* event_name) {
+#ifdef __APPLE__
WELS_THREAD_ERROR_CODE err = sem_close (*event); // match with sem_open
if (event_name)
sem_unlink (event_name);
return err;
+#else
+ WELS_THREAD_ERROR_CODE err = sem_destroy (*event); // match with sem_init
+ free(*event);
+ return err;
+#endif
}
WELS_THREAD_ERROR_CODE WelsEventSignal (WELS_EVENT* event) {