1 // [AsmJit]
2 // Machine Code Generation for C++.
3 //
4 // [License]
5 // Zlib - See LICENSE.md file in the package.
6 
7 #ifndef _ASMJIT_CORE_OSUTILS_H
8 #define _ASMJIT_CORE_OSUTILS_H
9 
10 #include "../core/globals.h"
11 
12 ASMJIT_BEGIN_NAMESPACE
13 
14 //! \addtogroup asmjit_support
15 //! \{
16 
17 // ============================================================================
18 // [asmjit::OSUtils]
19 // ============================================================================
20 
21 //! Operating system utilities.
22 namespace OSUtils {
23   //! Gets the current CPU tick count, used for benchmarking (1ms resolution).
24   ASMJIT_API uint32_t getTickCount() noexcept;
25 };
26 
27 // ============================================================================
28 // [asmjit::Lock]
29 // ============================================================================
30 
31 //! \cond INTERNAL
32 
33 //! Lock.
34 class Lock {
35 public:
36   ASMJIT_NONCOPYABLE(Lock)
37 
38   #if defined(_WIN32)
39 
40   typedef CRITICAL_SECTION Handle;
41   Handle _handle;
42 
Lock()43   inline Lock() noexcept { InitializeCriticalSection(&_handle); }
~Lock()44   inline ~Lock() noexcept { DeleteCriticalSection(&_handle); }
45 
lock()46   inline void lock() noexcept { EnterCriticalSection(&_handle); }
unlock()47   inline void unlock() noexcept { LeaveCriticalSection(&_handle); }
48 
49   #elif !defined(__EMSCRIPTEN__)
50 
51   typedef pthread_mutex_t Handle;
52   Handle _handle;
53 
54   inline Lock() noexcept { pthread_mutex_init(&_handle, nullptr); }
55   inline ~Lock() noexcept { pthread_mutex_destroy(&_handle); }
56 
57   inline void lock() noexcept { pthread_mutex_lock(&_handle); }
58   inline void unlock() noexcept { pthread_mutex_unlock(&_handle); }
59 
60   #else
61 
62   // Browser or other unsupported OS.
63   inline Lock() noexcept {}
64   inline ~Lock() noexcept {}
65 
66   inline void lock() noexcept {}
67   inline void unlock() noexcept {}
68 
69   #endif
70 };
71 
72 //! \endcond
73 
74 // ============================================================================
75 // [asmjit::ScopedLock]
76 // ============================================================================
77 
78 //! \cond INTERNAL
79 
80 //! Scoped lock.
81 struct ScopedLock {
82   ASMJIT_NONCOPYABLE(ScopedLock)
83 
84   Lock& _target;
85 
ScopedLockScopedLock86   inline ScopedLock(Lock& target) noexcept : _target(target) { _target.lock(); }
~ScopedLockScopedLock87   inline ~ScopedLock() noexcept { _target.unlock(); }
88 };
89 
90 //! \endcond
91 
92 //! \}
93 
94 ASMJIT_END_NAMESPACE
95 
96 #endif // _ASMJIT_CORE_OSUTILS_H
97