From 4d5f5a2f9d10acf057c821d005c7d6613f397c80 Mon Sep 17 00:00:00 2001 From: NishiOwO Date: Thu, 13 Nov 2025 10:50:32 +0000 Subject: [PATCH] add mutex git-svn-id: http://svn2.nishi.boats/svn/milsko/trunk@691 b9cfdab3-6d41-4d17-bbe4-086880011989 --- Makefile.pl | 3 ++- include/Mw/Abstract/Mutex.h | 43 ++++++++++++++++++++++++++++++++++++ include/Mw/Milsko.h | 1 + pl/ostype/Windows.pl | 1 + src/abstract/dynamic.c | 26 ++++++++++++---------- src/abstract/mutex.c | 44 +++++++++++++++++++++++++++++++++++++ src/abstract/time.c | 4 ++-- 7 files changed, 107 insertions(+), 15 deletions(-) create mode 100644 include/Mw/Abstract/Mutex.h create mode 100644 src/abstract/mutex.c diff --git a/Makefile.pl b/Makefile.pl index b82ac5f..1e1d30d 100755 --- a/Makefile.pl +++ b/Makefile.pl @@ -17,6 +17,7 @@ our $cflags = "-fPIC -D_MILSKO"; our $libdir = ""; our $ldflags = ""; our $math = "-lm"; +our $thread = "-lpthread"; our $shared = "-shared"; our $backend = ""; @@ -142,7 +143,7 @@ print(OUT "INCDIR = ${incdir}\n"); print(OUT "CFLAGS = ${cflags}\n"); print(OUT "LIBDIR = ${libdir}\n"); print(OUT "LDFLAGS = ${ldflags}\n"); -print(OUT "LIBS = ${math} ${libs}\n"); +print(OUT "LIBS = ${math} ${thread} ${libs}\n"); print(OUT "MATH = ${math}\n"); print(OUT "SHARED = ${shared}\n"); print(OUT "\n"); diff --git a/include/Mw/Abstract/Mutex.h b/include/Mw/Abstract/Mutex.h new file mode 100644 index 0000000..932320f --- /dev/null +++ b/include/Mw/Abstract/Mutex.h @@ -0,0 +1,43 @@ +/* $Id$ */ +/*! + * @file Mw/Abstract/Mutex.h + * @brief Mutex + */ +#ifndef __MW_ABSTRACT_MUTEX_H__ +#define __MW_ABSTRACT_MUTEX_H__ + +#include + +#ifdef __cplusplus +extern "C" { +#endif + +/*! + * @brief Creates a mutex + * @return Handle + */ +MWDECL void* MwMutexCreate(void); + +/*! + * @brief Destroys a mutex + * @param handle Handle + */ +MWDECL void MwMutexDestroy(void* handle); + +/*! + * @brief Locks a mutex + * @param handle Handle + */ +MWDECL void MwMutexLock(void* handle); + +/*! + * @brief Unlocks a mutex + * @param handle Handle + */ +MWDECL void MwMutexUnlock(void* handle); + +#ifdef __cplusplus +} +#endif + +#endif diff --git a/include/Mw/Milsko.h b/include/Mw/Milsko.h index 60f25bb..62fde7d 100644 --- a/include/Mw/Milsko.h +++ b/include/Mw/Milsko.h @@ -23,6 +23,7 @@ #include #include #include +#include #include #include diff --git a/pl/ostype/Windows.pl b/pl/ostype/Windows.pl index 98f6f4d..5a00da4 100644 --- a/pl/ostype/Windows.pl +++ b/pl/ostype/Windows.pl @@ -3,6 +3,7 @@ $library_prefix = ""; $library_suffix = ".dll"; $executable_suffix = ".exe"; $math = ""; +$thread = ""; add_ldflags("-Wl,--out-implib,src/libMw.a -static-libgcc"); use_backend("gdi"); diff --git a/src/abstract/dynamic.c b/src/abstract/dynamic.c index f541820..c9da513 100644 --- a/src/abstract/dynamic.c +++ b/src/abstract/dynamic.c @@ -1,26 +1,28 @@ /* $Id$ */ #include +#if defined(_WIN32) void* MwDynamicOpen(const char* path) { -#ifdef _WIN32 return LoadLibrary(path); -#else - return dlopen(path, RTLD_LAZY | RTLD_LOCAL); -#endif } void* MwDynamicSymbol(void* handle, const char* symbol) { -#ifdef _WIN32 return GetProcAddress(handle, symbol); -#else - return dlsym(handle, symbol); -#endif } void MwDynamicClose(void* handle) { -#ifdef _WIN32 FreeLibrary(handle); -#else - dlclose(handle); -#endif } +#elif defined(__unix__) +void* MwDynamicOpen(const char* path) { + return dlopen(path, RTLD_LOCAL | RTLD_LAZY); +} + +void* MwDynamicSymbol(void* handle, const char* symbol) { + return dlsym(handle, symbol); +} + +void MwDynamicClose(void* handle) { + dlclose(handle); +} +#endif diff --git a/src/abstract/mutex.c b/src/abstract/mutex.c new file mode 100644 index 0000000..8deecf8 --- /dev/null +++ b/src/abstract/mutex.c @@ -0,0 +1,44 @@ +/* $Id$ */ +#include + +#if defined(_WIN32) +void* MwMutexCreate(void) { + return CreateEvent(NULL, FALSE, TRUE, NULL); +} + +void MwMutexDestroy(void* handle) { + CloseHandle(handle); +} + +void MwMutexLock(void* handle) { + WaitForSingleObject(handle, INFINITE); +} + +void MwMutexUnlock(void* handle) { + SetEvent(handle); +} +#elif defined(__unix__) +#include + +void* MwMutexCreate(void) { + pthread_mutex_t* m = malloc(sizeof(*m)); + + pthread_mutex_init(m, NULL); + + return m; +} + +void MwMutexDestroy(void* handle) { + pthread_mutex_destroy(handle); + + free(handle); +} + +void MwMutexLock(void* handle) { + pthread_mutex_lock(handle); +} + +void MwMutexUnlock(void* handle) { + pthread_mutex_unlock(handle); +} +#endif diff --git a/src/abstract/time.c b/src/abstract/time.c index 24303aa..58f4e0f 100644 --- a/src/abstract/time.c +++ b/src/abstract/time.c @@ -1,7 +1,7 @@ /* $Id$ */ #include -#ifdef _WIN32 +#if defined(_WIN32) long MwTimeGetTick(void) { return GetTickCount(); } @@ -9,7 +9,7 @@ long MwTimeGetTick(void) { void MwTimeSleep(int ms) { Sleep(ms); } -#else +#elif defined(__unix__) long MwTimeGetTick(void) { struct timespec ts; long n = 0;