1 /*
2  * Copyright 2010-2019 Branimir Karadzic. All rights reserved.
3  * License: https://github.com/bkaradzic/bx#license-bsd-2-clause
4  */
5 
6 #ifndef BX_THREAD_H_HEADER_GUARD
7 #define BX_THREAD_H_HEADER_GUARD
8 
9 #include "allocator.h"
10 #include "mpscqueue.h"
11 
12 namespace bx
13 {
14 	///
15 	typedef int32_t (*ThreadFn)(class Thread* _self, void* _userData);
16 
17 	///
18 	class Thread
19 	{
20 		BX_CLASS(Thread
21 			, NO_COPY
22 			, NO_ASSIGNMENT
23 			);
24 
25 	public:
26 		///
27 		Thread();
28 
29 		///
30 		virtual ~Thread();
31 
32 		///
33 		void init(ThreadFn _fn, void* _userData = NULL, uint32_t _stackSize = 0, const char* _name = NULL);
34 
35 		///
36 		void shutdown();
37 
38 		///
39 		bool isRunning() const;
40 
41 		///
42 		int32_t getExitCode() const;
43 
44 		///
45 		void setThreadName(const char* _name);
46 
47 		///
48 		void push(void* _ptr);
49 
50 		///
51 		void* pop();
52 
53 	private:
54 		friend struct ThreadInternal;
55 		int32_t entry();
56 
57 		BX_ALIGN_DECL(16, uint8_t) m_internal[64];
58 
59 		ThreadFn  m_fn;
60 		void*     m_userData;
61 		MpScUnboundedBlockingQueue<void> m_queue;
62 		Semaphore m_sem;
63 		uint32_t  m_stackSize;
64 		int32_t   m_exitCode;
65 		bool      m_running;
66 	};
67 
68 	///
69 	class TlsData
70 	{
71 	public:
72 		///
73 		TlsData();
74 
75 		///
76 		~TlsData();
77 
78 		///
79 		void* get() const;
80 
81 		///
82 		void set(void* _ptr);
83 
84 	private:
85 		BX_ALIGN_DECL(16, uint8_t) m_internal[64];
86 	};
87 
88 } // namespace bx
89 
90 #endif // BX_THREAD_H_HEADER_GUARD
91