summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBent Bisballe Nyeng <deva@aasimon.org>2013-01-25 22:04:23 +0100
committerBent Bisballe Nyeng <deva@aasimon.org>2013-01-25 22:04:23 +0100
commit3ef6d4b553a19b00bac47fdae8917672ec2c2f29 (patch)
treee8b3e4cd32078cc465b8fc5926bdb485df98a977
parent08827265399f3f69f7f25512e991f67da5bbac63 (diff)
Added win32 mutex support.
-rw-r--r--hugin.c93
1 files changed, 69 insertions, 24 deletions
diff --git a/hugin.c b/hugin.c
index 6aff5b8..98fb276 100644
--- a/hugin.c
+++ b/hugin.c
@@ -38,8 +38,64 @@
#include <fcntl.h>
#ifdef WITH_HUG_MUTEX
-#include <pthread.h>
+# ifdef WIN32
+# include <windows.h>
+typedef HANDLE mutex_t;
+# else
+# include <pthread.h>
+typedef pthread_mutex_t mutex_t;
+# endif
+
+void mutex_init(mutex_t *mutex)
+{
+#ifdef WIN32
+ *mutex = CreateMutex(NULL, // default security attributes
+ FALSE, // initially not owned
+ NULL); // unnamed mutex
+#else
+ pthread_mutex_init (mutex, NULL);
+#endif
+}
+
+void mutex_close(mutex_t *mutex)
+{
+#ifdef WIN32
+ CloseHandle(*mutex);
+#else
+ pthread_mutex_destroy(mutex);
#endif
+}
+
+void mutex_lock(mutex_t *mutex)
+{
+#ifdef WIN32
+ WaitForSingleObject(*mutex, // handle to mutex
+ INFINITE); // no time-out interval
+#else
+ pthread_mutex_lock(mutex);
+#endif
+}
+
+void mutex_unlock(mutex_t *mutex)
+{
+#ifdef WIN32
+ ReleaseMutex(*mutex);
+#else
+ pthread_mutex_unlock(mutex);
+#endif
+}
+
+#else/*WITH_HUG_MUTEX*/
+
+typedef int mutex_t;
+void mutex_init(mutex_t *mutex) {}
+void mutex_close(mutex_t *mutex) {}
+void mutex_lock(mutex_t *mutex) {}
+void mutex_unlock(mutex_t *mutex) {}
+
+#endif/*WITH_HUG_MUTEX*/
+
+mutex_t localtime_mutex;
#ifdef WITH_HUG_FILTER
#include "hugin_filter.h"
@@ -54,9 +110,7 @@ struct hug_config_t {
int fd;
int file_fd;
int stdout_no_date;
-#ifdef WITH_HUG_MUTEX
- pthread_mutex_t mutex;
-#endif
+ mutex_t mutex;
#ifdef WITH_HUG_SYSLOG
const char* syslog_host;
int syslog_port;
@@ -66,9 +120,9 @@ struct hug_config_t {
-1, // fd
-1, // file_fd
0, //stdout_no_date
-#ifdef WITH_HUG_MUTEX
- {}, // mutex;
-#endif
+ //#ifdef WITH_HUG_MUTEX
+ 0, // mutex;
+ //#endif
#ifdef WITH_HUG_SYSLOG
"", // syslog_host;
-1, // syslog_port;
@@ -81,26 +135,17 @@ struct hug_config_t {
*/
};
-#ifdef WITH_HUG_MUTEX
- pthread_mutex_t localtime_mutex;
-#endif
-
struct tm *hug_localtime(const time_t *timep, struct tm *result)
{
struct tm *res = NULL;
-#ifdef WITH_HUG_MUTEX
- pthread_mutex_lock(&localtime_mutex);
-#endif
+ mutex_lock(&localtime_mutex);
if(timep && result) {
memcpy(result,localtime(timep),sizeof(*result));
res = result;
}
-#ifdef WITH_HUG_MUTEX
- pthread_mutex_unlock(&localtime_mutex);
-#endif
-
+ mutex_unlock(&localtime_mutex);
return res;
}
@@ -109,10 +154,10 @@ static void hug_mutex_init()
{
#ifdef WITH_HUG_MUTEX
if(hug_config.flags & HUG_FLAG_USE_MUTEX) {
- pthread_mutex_init(&hug_config.mutex, NULL);
+ mutex_init(&hug_config.mutex);
}
- pthread_mutex_init(&localtime_mutex, NULL);
+ mutex_init(&localtime_mutex);
#endif
}
@@ -120,7 +165,7 @@ static void hug_mutex_lock()
{
#ifdef WITH_HUG_MUTEX
if(hug_config.flags & HUG_FLAG_USE_MUTEX) {
- pthread_mutex_lock(&hug_config.mutex);
+ mutex_lock(&hug_config.mutex);
}
#endif
}
@@ -129,7 +174,7 @@ static void hug_mutex_unlock()
{
#ifdef WITH_HUG_MUTEX
if(hug_config.flags & HUG_FLAG_USE_MUTEX) {
- pthread_mutex_unlock(&hug_config.mutex);
+ mutex_unlock(&hug_config.mutex);
}
#endif
}
@@ -141,10 +186,10 @@ static void hug_mutex_close()
// Make sure we don't destroy the mutex while another thread is using it.
hug_mutex_lock();
hug_mutex_unlock();
- pthread_mutex_destroy(&hug_config.mutex);
+ mutex_close(&hug_config.mutex);
}
- pthread_mutex_destroy(&localtime_mutex);
+ mutex_close(&localtime_mutex);
#endif
}