diff --git a/libobs/CMakeLists.txt b/libobs/CMakeLists.txt index 573cec110..d248afd43 100644 --- a/libobs/CMakeLists.txt +++ b/libobs/CMakeLists.txt @@ -59,6 +59,7 @@ if(WIN32) util/pipe-windows.c util/platform-windows.c) set(libobs_PLATFORM_HEADERS + util/threading-windows.h util/windows/win-version.h util/windows/ComPtr.hpp util/windows/CoTaskMemPtr.hpp @@ -77,6 +78,8 @@ elseif(APPLE) util/pipe-posix.c util/platform-nix.c util/platform-cocoa.m) + set(libobs_PLATFORM_HEADERS + util/threading-posix.h) set_source_files_properties(${libobs_PLATFORM_SOURCES} PROPERTIES @@ -110,6 +113,8 @@ elseif(UNIX) util/threading-posix.c util/pipe-posix.c util/platform-nix.c) + set(libobs_PLATFORM_HEADERS + util/threading-posix.h) if(DBUS_FOUND) set(libobs_PLATFORM_SOURCES ${libobs_PLATFORM_SOURCES} diff --git a/libobs/util/threading-posix.c b/libobs/util/threading-posix.c index 92672d638..435ff1b7b 100644 --- a/libobs/util/threading-posix.c +++ b/libobs/util/threading-posix.c @@ -246,41 +246,6 @@ int os_sem_wait(os_sem_t *sem) #endif -long os_atomic_inc_long(volatile long *val) -{ - return __sync_add_and_fetch(val, 1); -} - -long os_atomic_dec_long(volatile long *val) -{ - return __sync_sub_and_fetch(val, 1); -} - -long os_atomic_set_long(volatile long *ptr, long val) -{ - return __sync_lock_test_and_set(ptr, val); -} - -long os_atomic_load_long(const volatile long *ptr) -{ - return __atomic_load_n(ptr, __ATOMIC_SEQ_CST); -} - -bool os_atomic_compare_swap_long(volatile long *val, long old_val, long new_val) -{ - return __sync_bool_compare_and_swap(val, old_val, new_val); -} - -bool os_atomic_set_bool(volatile bool *ptr, bool val) -{ - return __sync_lock_test_and_set(ptr, val); -} - -bool os_atomic_load_bool(const volatile bool *ptr) -{ - return __atomic_load_n(ptr, __ATOMIC_SEQ_CST); -} - void os_set_thread_name(const char *name) { #if defined(__APPLE__) diff --git a/libobs/util/threading-posix.h b/libobs/util/threading-posix.h new file mode 100644 index 000000000..204a9dce4 --- /dev/null +++ b/libobs/util/threading-posix.h @@ -0,0 +1,53 @@ +/* + * Copyright (c) 2015 Hugh Bailey + * + * Permission to use, copy, modify, and distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + */ + +#pragma once + +static inline long os_atomic_inc_long(volatile long *val) +{ + return __sync_add_and_fetch(val, 1); +} + +static inline long os_atomic_dec_long(volatile long *val) +{ + return __sync_sub_and_fetch(val, 1); +} + +static inline long os_atomic_set_long(volatile long *ptr, long val) +{ + return __sync_lock_test_and_set(ptr, val); +} + +static inline long os_atomic_load_long(const volatile long *ptr) +{ + return __atomic_load_n(ptr, __ATOMIC_SEQ_CST); +} + +static inline bool os_atomic_compare_swap_long(volatile long *val, + long old_val, long new_val) +{ + return __sync_bool_compare_and_swap(val, old_val, new_val); +} + +static inline bool os_atomic_set_bool(volatile bool *ptr, bool val) +{ + return __sync_lock_test_and_set(ptr, val); +} + +static inline bool os_atomic_load_bool(const volatile bool *ptr) +{ + return __atomic_load_n(ptr, __ATOMIC_SEQ_CST); +} diff --git a/libobs/util/threading-windows.c b/libobs/util/threading-windows.c index 193749dd1..917067f32 100644 --- a/libobs/util/threading-windows.c +++ b/libobs/util/threading-windows.c @@ -149,41 +149,6 @@ int os_sem_wait(os_sem_t *sem) return (ret == WAIT_OBJECT_0) ? 0 : -1; } -long os_atomic_inc_long(volatile long *val) -{ - return InterlockedIncrement(val); -} - -long os_atomic_dec_long(volatile long *val) -{ - return InterlockedDecrement(val); -} - -long os_atomic_set_long(volatile long *ptr, long val) -{ - return (long)InterlockedExchange((volatile long*)ptr, (long)val); -} - -long os_atomic_load_long(const volatile long *ptr) -{ - return (long)InterlockedOr((volatile long*)ptr, 0); -} - -bool os_atomic_compare_swap_long(volatile long *val, long old_val, long new_val) -{ - return InterlockedCompareExchange(val, new_val, old_val) == old_val; -} - -bool os_atomic_set_bool(volatile bool *ptr, bool val) -{ - return (bool)InterlockedExchange8((volatile char*)ptr, (char)val); -} - -bool os_atomic_load_bool(const volatile bool *ptr) -{ - return (bool)InterlockedOr8((volatile char*)ptr, 0); -} - #define VC_EXCEPTION 0x406D1388 #pragma pack(push,8) diff --git a/libobs/util/threading-windows.h b/libobs/util/threading-windows.h new file mode 100644 index 000000000..6e382d8e6 --- /dev/null +++ b/libobs/util/threading-windows.h @@ -0,0 +1,55 @@ +/* + * Copyright (c) 2015 Hugh Bailey + * + * Permission to use, copy, modify, and distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + */ + +#pragma once + +#include + +static inline long os_atomic_inc_long(volatile long *val) +{ + return _InterlockedIncrement(val); +} + +static inline long os_atomic_dec_long(volatile long *val) +{ + return _InterlockedDecrement(val); +} + +static inline long os_atomic_set_long(volatile long *ptr, long val) +{ + return (long)_InterlockedExchange((volatile long*)ptr, (long)val); +} + +static inline long os_atomic_load_long(const volatile long *ptr) +{ + return (long)_InterlockedOr((volatile long*)ptr, 0); +} + +static inline bool os_atomic_compare_swap_long(volatile long *val, + long old_val, long new_val) +{ + return _InterlockedCompareExchange(val, new_val, old_val) == old_val; +} + +static inline bool os_atomic_set_bool(volatile bool *ptr, bool val) +{ + return !!_InterlockedExchange8((volatile char*)ptr, (char)val); +} + +static inline bool os_atomic_load_bool(const volatile bool *ptr) +{ + return !!_InterlockedOr8((volatile char*)ptr, 0); +} diff --git a/libobs/util/threading.h b/libobs/util/threading.h index f7d0277b4..50212921d 100644 --- a/libobs/util/threading.h +++ b/libobs/util/threading.h @@ -37,6 +37,12 @@ extern "C" { #endif +#ifdef _WIN32 +#include "threading-windows.h" +#else +#include "threading-posix.h" +#endif + /* this may seem strange, but you can't use it unless it's an initializer */ static inline void pthread_mutex_init_value(pthread_mutex_t *mutex) { @@ -70,17 +76,6 @@ EXPORT void os_sem_destroy(os_sem_t *sem); EXPORT int os_sem_post(os_sem_t *sem); EXPORT int os_sem_wait(os_sem_t *sem); -EXPORT long os_atomic_inc_long(volatile long *val); -EXPORT long os_atomic_dec_long(volatile long *val); -EXPORT long os_atomic_set_long(volatile long *ptr, long val); -EXPORT long os_atomic_load_long(const volatile long *ptr); - -EXPORT bool os_atomic_compare_swap_long(volatile long *val, - long old_val, long new_val); - -EXPORT bool os_atomic_set_bool(volatile bool *ptr, bool val); -EXPORT bool os_atomic_load_bool(const volatile bool *ptr); - EXPORT void os_set_thread_name(const char *name);