Debian Codegraph @

thirdparty/jolt_physics/Jolt/Core/Semaphore.cpp

raw symbols SCIP
1 // Jolt Physics Library (https://github.com/jrouwe/JoltPhysics)
2 // SPDX-FileCopyrightText: 2023 Jorrit Rouwe
3 // SPDX-License-Identifier: MIT
4
5 #include <Jolt/Jolt.h>
6
7 #include <Jolt/Core/Semaphore.h>
8
9 #ifdef JPH_PLATFORM_WINDOWS
10 JPH_SUPPRESS_WARNING_PUSH
11 JPH_MSVC_SUPPRESS_WARNING(5039) // winbase.h(13179): warning C5039: 'TpSetCallbackCleanupGroup': pointer or reference to potentially throwing function passed to 'extern "C"' function under -EHc. Undefined behavior may occur if this function throws an exception.
12 #ifndef WIN32_LEAN_AND_MEAN
13 #define WIN32_LEAN_AND_MEAN
14 #endif
15 #ifndef JPH_COMPILER_MINGW
16 #include <Windows.h>
17 #else
18 #include <windows.h>
19 #endif
20 JPH_SUPPRESS_WARNING_POP
21 #endif
22
23 JPH_NAMESPACE_BEGIN
24
25 Semaphore::Semaphore()
26 {
27 #ifdef JPH_PLATFORM_WINDOWS
28 mSemaphore = CreateSemaphore(nullptr, 0, INT_MAX, nullptr);
29 if (mSemaphore == nullptr)
30 {
31 Trace("Failed to create semaphore");
32 std::abort();
33 }
34 #elif defined(JPH_USE_PTHREADS)
35 int ret = sem_init(&mSemaphore, 0, 0);
36 if (ret == -1)
37 {
38 Trace("Failed to create semaphore");
39 std::abort();
40 }
41 #elif defined(JPH_USE_GRAND_CENTRAL_DISPATCH)
42 mSemaphore = dispatch_semaphore_create(0);
43 if (mSemaphore == nullptr)
44 {
45 Trace("Failed to create semaphore");
46 std::abort();
47 }
48 #elif defined(JPH_PLATFORM_BLUE)
49 if (!JPH_PLATFORM_BLUE_SEMAPHORE_INIT(mSemaphore))
50 {
51 Trace("Failed to create semaphore");
52 std::abort();
53 }
54 #endif
55 }
56
57 Semaphore::~Semaphore()
58 {
59 #ifdef JPH_PLATFORM_WINDOWS
60 CloseHandle(mSemaphore);
61 #elif defined(JPH_USE_PTHREADS)
62 sem_destroy(&mSemaphore);
63 #elif defined(JPH_USE_GRAND_CENTRAL_DISPATCH)
64 dispatch_release(mSemaphore);
65 #elif defined(JPH_PLATFORM_BLUE)
66 JPH_PLATFORM_BLUE_SEMAPHORE_DESTROY(mSemaphore);
67 #endif
68 }
69
70 void Semaphore::Release(uint inNumber)
71 {
72 JPH_ASSERT(inNumber > 0);
73
74 #if defined(JPH_PLATFORM_WINDOWS) || defined(JPH_USE_PTHREADS) || defined(JPH_USE_GRAND_CENTRAL_DISPATCH) || defined(JPH_PLATFORM_BLUE)
75 int old_value = mCount.fetch_add(inNumber, std::memory_order_release);
76 if (old_value < 0)
77 {
78 int new_value = old_value + (int)inNumber;
79 int num_to_release = min(new_value, 0) - old_value;
80 #ifdef JPH_PLATFORM_WINDOWS
81 ::ReleaseSemaphore(mSemaphore, num_to_release, nullptr);
82 #elif defined(JPH_USE_PTHREADS)
83 for (int i = 0; i < num_to_release; ++i)
84 sem_post(&mSemaphore);
85 #elif defined(JPH_USE_GRAND_CENTRAL_DISPATCH)
86 for (int i = 0; i < num_to_release; ++i)
87 dispatch_semaphore_signal(mSemaphore);
88 #elif defined(JPH_PLATFORM_BLUE)
89 JPH_PLATFORM_BLUE_SEMAPHORE_SIGNAL(mSemaphore, num_to_release);
90 #endif
91 }
92 #else
93 std::lock_guard lock(mLock);
94 mCount.fetch_add(inNumber, std::memory_order_relaxed);
95 if (inNumber > 1)
96 mWaitVariable.notify_all();
97 else
98 mWaitVariable.notify_one();
99 #endif
100 }
101
102 void Semaphore::Acquire(uint inNumber)
103 {
104 JPH_ASSERT(inNumber > 0);
105
106 #if defined(JPH_PLATFORM_WINDOWS) || defined(JPH_USE_PTHREADS) || defined(JPH_USE_GRAND_CENTRAL_DISPATCH) || defined(JPH_PLATFORM_BLUE)
107 int old_value = mCount.fetch_sub(inNumber, std::memory_order_acquire);
108 int new_value = old_value - (int)inNumber;
109 if (new_value < 0)
110 {
111 int num_to_acquire = min(old_value, 0) - new_value;
112 #ifdef JPH_PLATFORM_WINDOWS
113 for (int i = 0; i < num_to_acquire; ++i)
114 WaitForSingleObject(mSemaphore, INFINITE);
115 #elif defined(JPH_USE_PTHREADS)
116 for (int i = 0; i < num_to_acquire; ++i)
117 sem_wait(&mSemaphore);
118 #elif defined(JPH_USE_GRAND_CENTRAL_DISPATCH)
119 for (int i = 0; i < num_to_acquire; ++i)
120 dispatch_semaphore_wait(mSemaphore, DISPATCH_TIME_FOREVER);
121 #elif defined(JPH_PLATFORM_BLUE)
122 JPH_PLATFORM_BLUE_SEMAPHORE_WAIT(mSemaphore, num_to_acquire);
123 #endif
124 }
125 #else
126 std::unique_lock lock(mLock);
127 mWaitVariable.wait(lock, [this, inNumber]() {
128 return mCount.load(std::memory_order_relaxed) >= int(inNumber);
129 });
130 mCount.fetch_sub(inNumber, std::memory_order_relaxed);
131 #endif
132 }
133
134 JPH_NAMESPACE_END