1 | // Copyright 2009-2021 Intel Corporation |
---|---|
2 | // SPDX-License-Identifier: Apache-2.0 |
3 | |
4 | #include "mutex.h" |
5 | #include "regression.h" |
6 | |
7 | #if defined(__WIN32__) && !defined(PTHREADS_WIN32) |
8 | |
9 | #define WIN32_LEAN_AND_MEAN |
10 | #include <windows.h> |
11 | |
12 | namespace embree |
13 | { |
14 | MutexSys::MutexSys() { mutex = new CRITICAL_SECTION; InitializeCriticalSection((CRITICAL_SECTION*)mutex); } |
15 | MutexSys::~MutexSys() { DeleteCriticalSection((CRITICAL_SECTION*)mutex); delete (CRITICAL_SECTION*)mutex; } |
16 | void MutexSys::lock() { EnterCriticalSection((CRITICAL_SECTION*)mutex); } |
17 | bool MutexSys::try_lock() { return TryEnterCriticalSection((CRITICAL_SECTION*)mutex) != 0; } |
18 | void MutexSys::unlock() { LeaveCriticalSection((CRITICAL_SECTION*)mutex); } |
19 | } |
20 | #endif |
21 | |
22 | #if defined(__UNIX__) || defined(PTHREADS_WIN32) |
23 | #include <pthread.h> |
24 | namespace embree |
25 | { |
26 | /*! system mutex using pthreads */ |
27 | MutexSys::MutexSys() |
28 | { |
29 | mutex = new pthread_mutex_t; |
30 | if (pthread_mutex_init(mutex: (pthread_mutex_t*)mutex, mutexattr: nullptr) != 0) |
31 | THROW_RUNTIME_ERROR("pthread_mutex_init failed"); |
32 | } |
33 | |
34 | MutexSys::~MutexSys() |
35 | { |
36 | MAYBE_UNUSED bool ok = pthread_mutex_destroy(mutex: (pthread_mutex_t*)mutex) == 0; |
37 | assert(ok); |
38 | delete (pthread_mutex_t*)mutex; |
39 | } |
40 | |
41 | void MutexSys::lock() |
42 | { |
43 | if (pthread_mutex_lock(mutex: (pthread_mutex_t*)mutex) != 0) |
44 | THROW_RUNTIME_ERROR("pthread_mutex_lock failed"); |
45 | } |
46 | |
47 | bool MutexSys::try_lock() { |
48 | return pthread_mutex_trylock(mutex: (pthread_mutex_t*)mutex) == 0; |
49 | } |
50 | |
51 | void MutexSys::unlock() |
52 | { |
53 | if (pthread_mutex_unlock(mutex: (pthread_mutex_t*)mutex) != 0) |
54 | THROW_RUNTIME_ERROR("pthread_mutex_unlock failed"); |
55 | } |
56 | }; |
57 | #endif |
58 |