Line data Source code
1 : #ifdef __PLUMED_HAS_ASMJIT 2 : // This file is part of AsmJit project <https://asmjit.com> 3 : // 4 : // See asmjit.h or LICENSE.md for license and copyright information 5 : // SPDX-License-Identifier: Zlib 6 : 7 : #ifndef ASMJIT_CORE_OSUTILS_P_H_INCLUDED 8 : #define ASMJIT_CORE_OSUTILS_P_H_INCLUDED 9 : 10 : #include "osutils.h" 11 : 12 : ASMJIT_BEGIN_NAMESPACE 13 : 14 : //! \cond INTERNAL 15 : //! \addtogroup asmjit_utilities 16 : //! \{ 17 : 18 : #if defined(_WIN32) 19 : 20 : // Windows implementation. 21 : static_assert(sizeof(Lock::Handle) == sizeof(CRITICAL_SECTION), "asmjit::Lock::Handle layout must match CRITICAL_SECTION"); 22 : static_assert(alignof(Lock::Handle) == alignof(CRITICAL_SECTION), "asmjit::Lock::Handle alignment must match CRITICAL_SECTION"); 23 : 24 : ASMJIT_FORCE_INLINE Lock::Lock() noexcept { InitializeCriticalSection(reinterpret_cast<CRITICAL_SECTION*>(&_handle)); } 25 : ASMJIT_FORCE_INLINE Lock::~Lock() noexcept { DeleteCriticalSection(reinterpret_cast<CRITICAL_SECTION*>(&_handle)); } 26 : ASMJIT_FORCE_INLINE void Lock::lock() noexcept { EnterCriticalSection(reinterpret_cast<CRITICAL_SECTION*>(&_handle)); } 27 : ASMJIT_FORCE_INLINE void Lock::unlock() noexcept { LeaveCriticalSection(reinterpret_cast<CRITICAL_SECTION*>(&_handle)); } 28 : 29 : #elif !defined(__EMSCRIPTEN__) 30 : 31 : // PThread implementation. 32 : #ifdef PTHREAD_MUTEX_INITIALIZER 33 27422 : ASMJIT_FORCE_INLINE Lock::Lock() noexcept : _handle(PTHREAD_MUTEX_INITIALIZER) {} 34 : #else 35 : ASMJIT_FORCE_INLINE Lock::Lock() noexcept { pthread_mutex_init(&_handle, nullptr); } 36 : #endif 37 27422 : ASMJIT_FORCE_INLINE Lock::~Lock() noexcept { pthread_mutex_destroy(&_handle); } 38 27592 : ASMJIT_FORCE_INLINE void Lock::lock() noexcept { pthread_mutex_lock(&_handle); } 39 27592 : ASMJIT_FORCE_INLINE void Lock::unlock() noexcept { pthread_mutex_unlock(&_handle); } 40 : 41 : #else 42 : 43 : // Dummy implementation - Emscripten or other unsupported platform. 44 : ASMJIT_FORCE_INLINE Lock::Lock() noexcept {} 45 : ASMJIT_FORCE_INLINE Lock::~Lock() noexcept {} 46 : ASMJIT_FORCE_INLINE void Lock::lock() noexcept {} 47 : ASMJIT_FORCE_INLINE void Lock::unlock() noexcept {} 48 : 49 : #endif 50 : 51 : //! Scoped lock. 52 : class LockGuard { 53 : public: 54 : ASMJIT_NONCOPYABLE(LockGuard) 55 : 56 : Lock& _target; 57 : 58 : inline LockGuard(Lock& target) noexcept 59 : : _target(target) { _target.lock(); } 60 27592 : inline ~LockGuard() noexcept { _target.unlock(); } 61 : }; 62 : 63 : //! \} 64 : //! \endcond 65 : 66 : ASMJIT_END_NAMESPACE 67 : 68 : #endif // ASMJIT_CORE_OSUTILS_P_H_INCLUDED 69 : 70 : 71 : #endif // __PLUMED_HAS_ASMJIT