4#include "../Common/CompilerMacrosExport.h"
5#ifndef SC_EXPORT_LIBRARY_THREADING
6#define SC_EXPORT_LIBRARY_THREADING 0
8#define SC_THREADING_EXPORT SC_COMPILER_LIBRARY_EXPORT(SC_EXPORT_LIBRARY_THREADING)
10#include "../Common/AlignedStorage.h"
11#include "../Common/Assert.h"
12#include "../Common/Function.h"
13#include "../Common/PlatformMacrosType.h"
14#include "../Common/PrimitiveDefinitions.h"
15#include "../Common/Result.h"
16#include "Internal/Optional.h"
20SC_DECLARE_ASSERT_PROVIDER(ThreadingAssert, SC_THREADING_EXPORT);
22#define SC_THREADING_ASSERT_RELEASE(e) SC_ASSERT_PROVIDER_RELEASE(SC::ThreadingAssert, e)
23#define SC_THREADING_ASSERT_DEBUG(e) SC_ASSERT_PROVIDER_DEBUG(SC::ThreadingAssert, e)
24#define SC_THREADING_TRUST_RESULT(expression) SC_THREADING_ASSERT_RELEASE(expression)
53 static constexpr int OpaqueMutexSize = 56 +
sizeof(long);
54 static constexpr int OpaqueMutexAlignment =
alignof(long);
55#elif SC_PLATFORM_WINDOWS
56 static constexpr int OpaqueMutexSize = 4 *
sizeof(
void*) + 2 *
sizeof(
long);
57 static constexpr int OpaqueMutexAlignment =
alignof(
void*);
58#elif SC_PLATFORM_EMSCRIPTEN
59 static constexpr int OpaqueMutexSize =
sizeof(
void*) * 6 +
sizeof(
long);
60 static constexpr int OpaqueMutexAlignment =
alignof(long);
62 static constexpr int OpaqueMutexSize =
sizeof(
void*) * 6;
63 static constexpr int OpaqueMutexAlignment =
alignof(long);
66 using OpaqueMutex = AlignedStorage<OpaqueMutexSize, OpaqueMutexAlignment>;
82 void wait(
Mutex& mutex);
88 static constexpr int OpaqueCVSize = 40 +
sizeof(long);
89 static constexpr int OpaqueCVAlignment =
alignof(long);
90#elif SC_PLATFORM_WINDOWS
91 static constexpr int OpaqueCVSize =
sizeof(
void*);
92 static constexpr int OpaqueCVAlignment =
alignof(
void*);
93#elif SC_PLATFORM_EMSCRIPTEN
94 static constexpr int OpaqueCVSize =
sizeof(
void*) * 12;
95 static constexpr int OpaqueCVAlignment =
alignof(long);
97 static constexpr int OpaqueCVSize =
sizeof(
void*) * 6;
98 static constexpr int OpaqueCVAlignment =
alignof(long);
102 using OpaqueConditionVariable = AlignedStorage<OpaqueCVSize, OpaqueCVAlignment>;
103 OpaqueConditionVariable condition;
168 static void Sleep(uint32_t milliseconds);
171 void setThreadNameInternal(
const native_char_t* name);
173 using OpaqueThread = AlignedStorage<
sizeof(
void*),
alignof(
void*)>;
174 UniqueOptional<OpaqueThread> thread;
175 Function<void(
Thread&)> userFunction;
210 int activeReaders = 0;
211 int waitingWriters = 0;
212 bool writerActive =
false;
223 Barrier(uint32_t count) : threadCount(count) {}
229 const uint32_t threadCount;
231 uint32_t waitCount = 0;
232 uint32_t generation = 0;
244 bool autoReset =
true;
253 bool isSignaled =
false;
A synchronization point that blocks threads until the required number of threads have reached it.
Definition Threading.h:220
Barrier(uint32_t count)
Creates a barrier that waits for the specified number of threads.
Definition Threading.h:223
void wait()
Wait at the barrier until all threads have reached it.
A native OS condition variable.
Definition Threading.h:72
An automatically reset event object to synchronize two threads.
Definition Threading.h:243
void signal()
Unblocks another thread, waiting on EventObject::wait.
void wait()
Waits on a thread for EventObject::signal to be called from another thread.
A native OS mutex to synchronize access to shared resources.
Definition Threading.h:37
A Read-Write lock that allows multiple concurrent readers but only one writer.
Definition Threading.h:183
void lockWrite()
Acquire a write lock. Only one writer can hold the lock, and no readers can hold it simultaneously.
void unlockWrite()
Release a previously acquired write lock.
void lockRead()
Acquire a read lock. Multiple readers can hold the lock concurrently.
void unlockRead()
Release a previously acquired read lock.
A semaphore synchronization primitive that maintains a count for resource management.
Definition Threading.h:264
void acquire()
Wait for a resource to become available.
void release()
Make a resource available.
Semaphore(int initialCount=0)
Creates a semaphore with an initial count.
A native OS thread.
Definition Threading.h:127
bool wasStarted() const
Check if thread has been started.
static void Sleep(uint32_t milliseconds)
Puts current thread to sleep.
Result start(Function< void(Thread &)> &&func)
Starts the new thread with given name and func.
void setThreadName(const native_char_t *name)
Sets current thread name ONLY if called from inside the thread.
Result detach()
Detaches the thread so that its resources are automatically released back to the system without Threa...
static uint64_t CurrentThreadID()
Returns thread id of the thread calling the function.
uint64_t threadID()
Returns thread id of this thread object (not current thread)
Result join()
Waits for thread to finish and releases its resources.