Plasma Engine  2.0
Loading...
Searching...
No Matches
OSThread_Posix.h
1#include <Foundation/FoundationInternal.h>
2PL_FOUNDATION_INTERNAL_HEADER
3
4#include <Foundation/Threading/Implementation/OSThread.h>
5#include <Foundation/Threading/Thread.h>
6
7plAtomicInteger32 plOSThread::s_iThreadCount;
8
9// Posix specific implementation of the thread class
10
12 plOSThreadEntryPoint pThreadEntryPoint, void* pUserData /*= nullptr*/, plStringView sName /*= "plThread"*/, plUInt32 uiStackSize /*= 128 * 1024*/)
13{
14 s_iThreadCount.Increment();
15
16 m_EntryPoint = pThreadEntryPoint;
17 m_pUserData = pUserData;
18 m_sName = sName;
19 m_uiStackSize = uiStackSize;
20
21 // Thread creation is deferred since Posix threads can't be created sleeping
22}
23
25{
26 s_iThreadCount.Decrement();
27}
28
31{
32 pthread_attr_t ThreadAttributes;
33 pthread_attr_init(&ThreadAttributes);
34 pthread_attr_setdetachstate(&ThreadAttributes, PTHREAD_CREATE_JOINABLE);
35 pthread_attr_setstacksize(&ThreadAttributes, m_uiStackSize);
36
37 int iReturnCode = pthread_create(&m_hHandle, &ThreadAttributes, m_EntryPoint, m_pUserData);
38 PL_IGNORE_UNUSED(iReturnCode);
39 PL_ASSERT_RELEASE(iReturnCode == 0, "Thread creation failed!");
40
41#if PL_ENABLED(PL_PLATFORM_LINUX) || PL_ENABLED(PL_PLATFORM_ANDROID)
42 if (iReturnCode == 0 && !m_sName.IsEmpty())
43 {
44 // pthread has a thread name limit of 16 bytes.
45 // This means 15 characters and the terminating '\0'
46 if (m_sName.GetElementCount() < 16)
47 {
48 pthread_setname_np(m_hHandle, m_sName.GetData());
49 }
50 else
51 {
52 char threadName[16];
53 strncpy(threadName, m_sName.GetData(), 15);
54 threadName[15] = '\0';
55 pthread_setname_np(m_hHandle, threadName);
56 }
57 }
58#endif
59
60 m_ThreadID = m_hHandle;
61
62 pthread_attr_destroy(&ThreadAttributes);
63}
64
67{
68 pthread_join(m_hHandle, nullptr);
69}
T Increment()
Increments the internal value and returns the incremented value.
Definition AtomicInteger_inl.h:35
T Decrement()
Decrements the internal value and returns the decremented value.
Definition AtomicInteger_inl.h:41
virtual ~plOSThread()
Destructor.
Definition OSThread_Posix.h:24
void Join()
Waits in the calling thread until the thread has finished execution (e.g. returned from the thread fu...
Definition OSThread_Posix.h:66
plOSThread(plOSThreadEntryPoint threadEntryPoint, void *pUserData=nullptr, plStringView sName="plOSThread", plUInt32 uiStackSize=128 *1024)
Initializes the thread instance (e.g. thread creation etc.)
Definition OSThread_Posix.h:11
void Start()
Starts the thread.
Definition OSThread_Posix.h:30
plStringView represent a read-only sub-string of a larger string, as it can store a dedicated string ...
Definition StringView.h:34
const char * GetData() const
Returns a pointer to the internal Utf8 string.
Definition String_inl.h:56
plUInt32 GetElementCount() const
Returns the amount of bytes that this string takes (excluding the '\0' terminator).
Definition String_inl.h:65
bool IsEmpty() const
Returns whether the string is an empty string.
Definition StringBase_inl.h:25