17 #ifndef INC_KY_Kernel_Threads_H
18 #define INC_KY_Kernel_Threads_H
25 #if defined(KY_OS_WINMETRO)
27 #if defined(KY_USE_STD11_THREADS)
32 #if defined(KY_OS_WII) || defined(KY_OS_WIIU)
36 #elif defined(KY_OS_3DS)
38 #include <nn/os/os_Thread.h>
42 #define KY_WAIT_INFINITE 0xFFFFFFFF
50 #ifdef KY_ENABLE_THREADS
54 class AcquireInterface;
65 typedef void* ThreadId;
69 ThreadId GetCurrentThreadId();
75 Mutex(
bool recursive = 1,
bool multiWait = 0) { KY_UNUSED2(recursive, multiWait); }
77 bool TryLock() {
return false; }
79 bool IsLockedByAnotherThread() {
return false; }
80 bool IsSignaled()
const {
return false; }
81 void* GetAcquireInterface() {
return NULL; }
82 bool CanAcquire() {
return false; }
83 bool TryAcquire() {
return false; }
84 bool TryAcquireCommit() {
return false; }
85 bool TryAcquireCancel() {
return false; }
91 Locker(Mutex *pmutex) { KY_UNUSED(pmutex); }
99 bool Wait(Mutex *pmutex,
unsigned delay = KY_WAIT_INFINITE) { KY_UNUSED2(pmutex, delay);
return false; }
105 class Thread :
public RefCountBase<Thread, Stat_Default_Mem>
124 typedef int (*ThreadFn)(Thread *pthread,
void* h);
127 CreateParams(ThreadFn func = 0,
void* hand = 0, UPInt ssize = 128 * 1024,
128 int proc = -1, ThreadState state = NotRunning, ThreadPriority prior = NormalPriority)
129 { KY_UNUSED6(func, hand, ssize, proc, state, prior); }
132 Thread(UPInt stackSize = 128 * 1024,
int processor = -1) { KY_UNUSED2(stackSize, processor); }
133 Thread(ThreadFn threadFunction,
void* userHandle = 0, UPInt stackSize = 128 * 1024,
134 int processor = -1, ThreadState initialState = NotRunning) { KY_UNUSED5(threadFunction, userHandle, stackSize, processor, initialState); }
135 explicit KY_EXPORT Thread(
const CreateParams& params) { KY_UNUSED(params); }
136 static void KY_CDECL FinishAllThreads() { }
137 int Run() {
return 0; }
139 bool Start(ThreadState initialState = Running) { KY_UNUSED(initialState);
return true; }
140 void Exit(
int exitCode=0) { KY_UNUSED(exitCode); }
141 bool Suspend() {
return true; }
142 bool Resume() {
return true; }
144 bool GetExitFlag()
const {
return true; }
145 void SetExitFlag(
bool exitFlag) { KY_UNUSED(exitFlag); }
146 bool IsFinished()
const {
return true; }
147 bool IsSuspended()
const {
return false; }
148 ThreadState GetThreadState()
const {
return NotRunning; }
149 static int GetCPUCount() {
return 1; }
150 KY_INLINE
int GetExitCode()
const {
return 0; }
153 #if defined(KY_OS_WIN32) || defined(KY_OS_XBOX) || defined(KY_OS_XBOX360)
154 void* GetOSHandle()
const {
return NULL; }
155 #elif defined(KY_OS_WII) || defined(KY_OS_WIIU)
156 OSThread * GetOSHandle()
const {
return NULL; }
157 #elif defined(KY_OS_PS3)
158 sys_ppu_thread_t GetOSHandle()
const {
return 0; }
159 #elif defined(KY_OS_PSVITA)
160 SceUID GetOSHandle()
const {
return 0; }
161 #elif defined(KY_OS_3DS)
162 nn::os::Thread* GetOSHandle()
const {
return NULL; }
164 pthread_t GetOSHandle()
const {
return 0; }
167 ThreadId GetThreadId()
const {
return 0; }
169 static int GetOSPriority(ThreadPriority) {
return 0; }
170 static bool Sleep(
unsigned secs) { KY_UNUSED(secs);
return false; }
171 static bool MSleep(
unsigned msecs) { KY_UNUSED(msecs);
return false; }
173 bool IsSignaled()
const {
return false; }
174 void* GetAcquireInterface() {
return this; }
175 bool CanAcquire() {
return IsSignaled(); }
176 bool TryAcquire() {
return IsSignaled(); }
177 void SetThreadName(
const char* name ) { KY_UNUSED(name); }
178 #if defined(KY_OS_WII) || defined(KY_OS_WIIU)
180 struct ThreadDataWii :
public ListNode<ThreadDataWii>
182 OSThread ThreadHandle;
184 UByte Stack[32] __attribute__((aligned(32)));
193 Event(
bool setInitially = 0,
bool multiWait = 0) { KY_UNUSED2(setInitially, multiWait); }
194 bool Wait(
unsigned delay = KY_WAIT_INFINITE) { KY_UNUSED(delay);
return true; }
198 bool IsSignaled()
const {
return false; }
199 void* GetAcquireInterface() {
return NULL; }
200 bool CanAcquire() {
return false; }
201 bool TryAcquire() {
return false; }
202 bool TryAcquireCommit(){
return false; }
203 bool TryAcquireCancel(){
return false; }
209 #ifdef KY_ENABLE_THREADS
228 class AcquireInterface
231 KY_EXPORT
virtual ~AcquireInterface() { }
237 KY_EXPORT
virtual bool CanAcquire();
247 KY_EXPORT
virtual bool TryAcquire();
249 KY_EXPORT
virtual bool TryAcquireCommit();
251 KY_EXPORT
virtual bool TryAcquireCancel();
254 KY_EXPORT
static bool AcquireMultipleObjects(Waitable** waitList,
unsigned waitCount,
255 unsigned delay = KY_WAIT_INFINITE);
258 KY_EXPORT
static int AcquireOneOfMultipleObjects(Waitable** waitList,
unsigned waitCount,
259 unsigned delay = KY_WAIT_INFINITE);
267 class DefaultAcquireInterface :
public AcquireInterface
270 KY_EXPORT
virtual ~DefaultAcquireInterface() { }
274 KY_EXPORT
static DefaultAcquireInterface* GetDefaultAcquireInterface();
294 class Waitable :
public RefCountBase<Waitable, Stat_Default_Mem>
299 typedef void (*WaitHandler)(
void* pdata);
315 HandlerStruct(WaitHandler h,
void* pd)
320 HandlerStruct(
const HandlerStruct &src)
322 Handler = src.Handler;
323 pUserData = src.pUserData;
326 HandlerStruct & operator = (
const HandlerStruct &src)
328 Handler = src.Handler;
329 pUserData = src.pUserData;
333 bool operator == (
const HandlerStruct &src)
335 return (Handler == src.Handler) && (pUserData==src.pUserData);
344 struct HandlerArray :
public NewOverrideBase<Stat_Default_Mem>
346 typedef ArrayConstPolicy<0, 16, true> SizePolicyType;
347 typedef Array<HandlerStruct, Stat_Default_Mem, SizePolicyType> HandlerArrayType;
349 AtomicInt<SInt32> RefCount;
350 HandlerArrayType Handlers;
353 HandlerArray() { RefCount.Value = 1; }
355 void AddRef() { RefCount++; }
358 KY_EXPORT
void Release();
361 KY_EXPORT
void CallWaitHandlers();
369 HandlerArray* pHandlers;
375 Waitable(
bool enable);
381 KY_EXPORT
void CallWaitHandlers();
385 class CallableHandlers
387 friend class Waitable;
388 Ptr<HandlerArray> pArray;
390 CallableHandlers() { }
392 inline void CallWaitHandlers() {
if (pArray) pArray->CallWaitHandlers(); }
395 inline void GetCallableHandlers(CallableHandlers *ph) {
if (pHandlers) ph->pArray = pHandlers; }
401 KY_EXPORT
bool AddWaitHandler(WaitHandler handler,
void* pdata);
402 KY_EXPORT
bool RemoveWaitHandler(WaitHandler handler,
void* pdata);
405 KY_EXPORT
bool Wait(
unsigned delay = KY_WAIT_INFINITE);
407 KY_EXPORT
bool Acquire(
unsigned delay = KY_WAIT_INFINITE);
412 KY_EXPORT
virtual bool IsSignaled()
const;
415 KY_EXPORT
virtual AcquireInterface* GetAcquireInterface();
428 class Mutex :
public Waitable,
public AcquireInterface
430 friend class WaitConditionImpl;
431 friend class MutexImpl;
438 KY_EXPORT Mutex(
bool recursive = 1,
bool multiWait = 0);
442 KY_EXPORT
void DoLock();
443 KY_EXPORT
bool TryLock();
444 KY_EXPORT
void Unlock();
448 KY_EXPORT
bool IsLockedByAnotherThread();
454 KY_EXPORT
virtual bool IsSignaled()
const;
455 KY_EXPORT
virtual AcquireInterface* GetAcquireInterface();
458 KY_EXPORT
virtual bool CanAcquire();
459 KY_EXPORT
virtual bool TryAcquire();
460 KY_EXPORT
virtual bool TryAcquireCommit();
461 KY_EXPORT
virtual bool TryAcquireCancel();
469 Locker(Mutex *pmutex)
470 { pMutex = pmutex; pMutex->DoLock(); }
472 { pMutex->Unlock(); }
491 class WaitConditionImpl;
495 friend class WaitConditionImpl;
497 WaitConditionImpl *pImpl;
501 KY_EXPORT WaitCondition();
502 KY_EXPORT ~WaitCondition();
506 KY_EXPORT
bool Wait(Mutex *pmutex,
unsigned delay = KY_WAIT_INFINITE);
509 KY_EXPORT
void Notify();
511 KY_EXPORT
void NotifyAll();
518 class Event :
public Waitable,
public AcquireInterface
522 volatile bool Temporary;
523 mutable Mutex StateMutex;
524 WaitCondition StateWaitCondition;
528 KY_EXPORT Event(
bool setInitially = 0,
bool multiWait = 0);
533 KY_EXPORT
bool Wait(
unsigned delay = KY_WAIT_INFINITE);
536 KY_EXPORT
void SetEvent();
538 KY_EXPORT
void ResetEvent();
542 KY_EXPORT
void PulseEvent();
545 KY_EXPORT
virtual bool IsSignaled()
const;
547 KY_EXPORT
virtual AcquireInterface* GetAcquireInterface();
550 KY_EXPORT
virtual bool CanAcquire();
551 KY_EXPORT
virtual bool TryAcquire();
552 KY_EXPORT
virtual bool TryAcquireCommit();
553 KY_EXPORT
virtual bool TryAcquireCancel();
568 class Semaphore :
public Waitable,
public AcquireInterface
575 mutable Mutex ValueMutex;
576 WaitCondition ValueWaitCondition;
580 KY_EXPORT Semaphore(
int maxValue = 1,
bool multiWait = 0);
581 KY_EXPORT ~Semaphore();
585 int GetMaxValue()
const;
586 int GetValue()
const;
587 int GetAvailable()
const;
595 KY_EXPORT
bool ObtainSemaphore(
int count = 1,
unsigned delay = KY_WAIT_INFINITE);
598 KY_EXPORT
bool ReleaseSemaphore(
int count = 1);
604 KY_EXPORT
int operator ++ (
int);
605 KY_EXPORT
int operator -- (
int);
607 KY_EXPORT
int operator += (
int count);
608 KY_EXPORT
int operator -= (
int count);
617 KY_EXPORT Waitable* CreateWaitableIncrement(
int count);
621 KY_EXPORT
virtual bool CanAcquire();
622 KY_EXPORT
virtual bool TryAcquire();
623 KY_EXPORT
virtual bool TryAcquireCommit();
624 KY_EXPORT
virtual bool TryAcquireCancel();
627 KY_EXPORT
virtual bool IsSignaled()
const;
628 KY_EXPORT
virtual AcquireInterface* GetAcquireInterface();
635 Semaphore* pSemaphore;
637 Locker(Semaphore *psemaphore,
int count = 1)
638 { pSemaphore = psemaphore; Count = count;
639 pSemaphore->ObtainSemaphore(count); }
641 { pSemaphore->ReleaseSemaphore(Count); }
650 #if defined(KY_OS_PS3)
651 typedef sys_ppu_thread_t ThreadId;
652 #elif defined(KY_OS_PSVITA)
653 typedef SceUID ThreadId;
654 #elif defined(KY_OS_WINMETRO)
655 #if defined(KY_USE_STD11_THREADS)
656 typedef size_t ThreadId;
658 typedef const concurrency::task_group* ThreadId;
661 typedef void* ThreadId;
669 #define KY_THREAD_STARTED 0x01
671 #define KY_THREAD_FINISHED 0x02
673 #define KY_THREAD_START_SUSPENDED 0x08
676 #define KY_THREAD_EXIT 0x10
679 class Thread :
public Waitable,
public AcquireInterface
689 typedef int (*ThreadFn)(Thread *pthread,
void* h);
692 ThreadFn ThreadFunction;
719 CreateParams(ThreadFn func = 0,
void* hand = 0, UPInt ssize = 128 * 1024,
720 int proc = -1, ThreadState state = NotRunning, ThreadPriority prior = NormalPriority)
721 : threadFunction(func), userHandle(hand), stackSize(ssize),
722 processor(proc), initialState(state), priority(prior) {}
723 ThreadFn threadFunction;
727 ThreadState initialState;
728 ThreadPriority priority;
737 KY_EXPORT Thread(UPInt stackSize = 128 * 1024,
int processor = -1);
742 KY_EXPORT Thread(ThreadFn threadFunction,
void* userHandle = 0, UPInt stackSize = 128 * 1024,
743 int processor = -1, ThreadState initialState = NotRunning);
745 explicit KY_EXPORT Thread(
const CreateParams& params);
748 KY_EXPORT
virtual ~Thread();
753 KY_EXPORT
static void KY_CDECL FinishAllThreads();
760 KY_EXPORT
virtual int Run();
762 KY_EXPORT
virtual void OnExit();
771 KY_EXPORT
virtual bool Start(ThreadState initialState = Running);
774 KY_EXPORT
virtual void Exit(
int exitCode=0);
778 KY_EXPORT
bool Suspend();
781 KY_EXPORT
bool Resume();
789 KY_EXPORT
bool GetExitFlag()
const;
790 KY_EXPORT
void SetExitFlag(
bool exitFlag);
793 KY_EXPORT
bool IsFinished()
const;
795 KY_EXPORT
bool IsSuspended()
const;
797 KY_EXPORT ThreadState GetThreadState()
const;
800 KY_EXPORT
static int GetCPUCount();
804 KY_INLINE
int GetExitCode()
const {
return ExitCode; }
806 #if (defined(KY_OS_WIN32) || defined(KY_OS_XBOX) || defined(KY_OS_XBOX360)) && !defined(KY_OS_WINMETRO)
807 void* GetOSHandle()
const {
return ThreadHandle; }
808 #elif defined(KY_OS_WINMETRO)
809 #if defined(KY_USE_STD11_THREADS)
810 std::thread* GetOSHandle()
const {
return ThreadHandle; }
812 ThreadId GetOSHandle()
const {
return &ThreadHandle; }
814 #elif defined(KY_OS_WII) || defined(KY_OS_WIIU)
815 OSThread * GetOSHandle()
const {
return ThreadData ?
const_cast<OSThread*
>(&ThreadData->ThreadHandle) : 0; }
816 #elif defined(KY_OS_PS3)
817 sys_ppu_thread_t GetOSHandle()
const {
return ThreadHandle; }
818 #elif defined(KY_OS_PSVITA)
819 SceUID GetOSHandle()
const {
return ThreadHandle; }
820 #elif defined(KY_OS_3DS)
821 nn::os::Thread* GetOSHandle()
const {
return ThreadHandle; }
823 pthread_t GetOSHandle()
const {
return ThreadHandle; }
826 #if (defined(KY_OS_WIN32) || defined(KY_OS_XBOX) || defined(KY_OS_XBOX360)) && !defined(KY_OS_WINMETRO)
827 ThreadId GetThreadId()
const {
return IdValue; }
828 #elif defined(KY_OS_WINMETRO)
829 #if defined(KY_USE_STD11_THREADS)
830 ThreadId GetThreadId()
const {
return IdValue != std::thread::id() ? IdValue.hash() : 0; }
832 ThreadId GetThreadId()
const {
return (ThreadId)GetOSHandle(); }
835 ThreadId GetThreadId()
const {
return (ThreadId)GetOSHandle(); }
838 static int GetOSPriority(ThreadPriority);
842 KY_EXPORT
static bool Sleep(
unsigned secs);
844 KY_EXPORT
static bool MSleep(
unsigned msecs);
851 virtual bool IsSignaled()
const {
return (ThreadFlags & KY_THREAD_FINISHED) != 0; }
855 virtual AcquireInterface* GetAcquireInterface() {
return this; }
858 virtual bool CanAcquire() {
return IsSignaled(); }
859 virtual bool TryAcquire() {
return IsSignaled(); }
865 #if (defined(KY_OS_WIN32) || defined(KY_OS_XBOX360)) && !defined(KY_OS_WINMETRO)
866 virtual void SetThreadName(
const char* name );
868 virtual void SetThreadName(
const char* name ) { KY_UNUSED(name); }
872 #if (defined(KY_OS_WIN32) || defined(KY_OS_XBOX) || defined(KY_OS_XBOX360)) && ! defined(KY_OS_WINMETRO)
873 friend unsigned WINAPI Thread_Win32StartFn(
void *pthread);
875 #elif defined(KY_OS_WINMETRO)
876 #if defined(KY_USE_STD11_THREADS)
877 friend void Thread_Std11StartFunc(
void *phandle);
879 friend void WINAPIV Thread_WinRTStartFn(
void *pthread);
881 #elif defined(KY_OS_WII)
882 friend void *Thread_PthreadStartFn(
void * phandle);
883 friend class ThreadList;
885 #elif defined(KY_OS_WIIU)
886 friend int Thread_PthreadStartFn(
int,
void * phandle);
887 friend class ThreadList;
889 #elif defined(KY_OS_3DS)
890 friend void Thread_PthreadStartFn(
void * phandle);
892 #elif defined(KY_OS_PS3)
893 friend void Thread_PpuThreadStartFn(uint64_t phandle);
895 #elif defined(KY_OS_PSVITA)
896 friend int Thread_Psp2ThreadStartFn(SceSize size,
void* phandle);
899 friend void *Thread_PthreadStartFn(
void * phandle);
902 static pthread_attr_t Attr;
907 AtomicInt<UInt32> ThreadFlags;
908 AtomicInt<SInt32> SuspendCount;
913 ThreadPriority Priority;
915 #if (defined(KY_OS_WIN32) || defined(KY_OS_XBOX) || defined(KY_OS_XBOX360)) && !defined(KY_OS_WINMETRO)
917 volatile ThreadId IdValue;
920 void CleanupSystemThread();
922 #elif defined(KY_OS_WINMETRO)
923 #if defined(KY_USE_STD11_THREADS)
924 std::thread* ThreadHandle;
925 std::thread::id IdValue;
927 concurrency::task_group ThreadHandle;
930 #elif defined(KY_OS_WII) || defined(KY_OS_WIIU)
932 struct ThreadDataWii :
public ListNode<ThreadDataWii>
934 OSThread ThreadHandle;
936 UByte Stack[32] __attribute__((aligned(32)));
939 ThreadDataWii* ThreadData;
940 #elif defined(KY_OS_3DS)
942 nn::os::Thread* ThreadHandle;
943 #elif defined(KY_OS_PS3)
944 sys_ppu_thread_t ThreadHandle;
945 #elif defined(KY_OS_PSVITA)
948 pthread_t ThreadHandle;
957 void FinishAndRelease();
959 void Init(
const CreateParams& params);
962 Thread(
const Thread &source) : Waitable(1) { KY_UNUSED(source); }
969 ThreadId GetCurrentThreadId();
973 #endif // KY_ENABLE_THREADS
977 #endif // INC_KY_Kernel_Threads_H
Definition: gamekitcrowddispersion.h:20