1 | /** |
2 | * Copyright (c) 2006-2023 LOVE Development Team |
3 | * |
4 | * This software is provided 'as-is', without any express or implied |
5 | * warranty. In no event will the authors be held liable for any damages |
6 | * arising from the use of this software. |
7 | * |
8 | * Permission is granted to anyone to use this software for any purpose, |
9 | * including commercial applications, and to alter it and redistribute it |
10 | * freely, subject to the following restrictions: |
11 | * |
12 | * 1. The origin of this software must not be misrepresented; you must not |
13 | * claim that you wrote the original software. If you use this software |
14 | * in a product, an acknowledgment in the product documentation would be |
15 | * appreciated but is not required. |
16 | * 2. Altered source versions must be plainly marked as such, and must not be |
17 | * misrepresented as being the original software. |
18 | * 3. This notice may not be removed or altered from any source distribution. |
19 | **/ |
20 | |
21 | #ifndef LOVE_THREAD_THREADS_H |
22 | #define LOVE_THREAD_THREADS_H |
23 | |
24 | // LOVE |
25 | #include "common/config.h" |
26 | #include "Thread.h" |
27 | |
28 | // C++ |
29 | #include <string> |
30 | |
31 | namespace love |
32 | { |
33 | namespace thread |
34 | { |
35 | |
36 | class Mutex |
37 | { |
38 | public: |
39 | virtual ~Mutex() {} |
40 | |
41 | virtual void lock() = 0; |
42 | virtual void unlock() = 0; |
43 | }; |
44 | |
45 | class Conditional |
46 | { |
47 | public: |
48 | virtual ~Conditional() {} |
49 | |
50 | virtual void signal() = 0; |
51 | virtual void broadcast() = 0; |
52 | virtual bool wait(Mutex *mutex, int timeout=-1) = 0; |
53 | }; |
54 | |
55 | class Lock |
56 | { |
57 | public: |
58 | Lock(Mutex *m); |
59 | Lock(Mutex &m); |
60 | Lock(Lock &&other); |
61 | ~Lock(); |
62 | |
63 | private: |
64 | Mutex *mutex; |
65 | }; |
66 | |
67 | class EmptyLock |
68 | { |
69 | public: |
70 | EmptyLock(); |
71 | ~EmptyLock(); |
72 | |
73 | void setLock(Mutex *m); |
74 | void setLock(Mutex &m); |
75 | |
76 | private: |
77 | Mutex *mutex; |
78 | }; |
79 | |
80 | class Threadable : public love::Object |
81 | { |
82 | public: |
83 | static love::Type type; |
84 | |
85 | Threadable(); |
86 | virtual ~Threadable(); |
87 | |
88 | virtual void threadFunction() = 0; |
89 | |
90 | bool start(); |
91 | void wait(); |
92 | bool isRunning() const; |
93 | const char *getThreadName() const; |
94 | |
95 | protected: |
96 | |
97 | Thread *owner; |
98 | std::string threadName; |
99 | |
100 | }; |
101 | |
102 | class MutexRef |
103 | { |
104 | public: |
105 | MutexRef(); |
106 | ~MutexRef(); |
107 | |
108 | operator Mutex*() const; |
109 | Mutex *operator->() const; |
110 | |
111 | private: |
112 | Mutex *mutex; |
113 | }; |
114 | |
115 | class ConditionalRef |
116 | { |
117 | public: |
118 | ConditionalRef(); |
119 | ~ConditionalRef(); |
120 | |
121 | operator Conditional*() const; |
122 | Conditional *operator->() const; |
123 | |
124 | private: |
125 | Conditional *conditional; |
126 | }; |
127 | |
128 | Mutex *newMutex(); |
129 | Conditional *newConditional(); |
130 | Thread *newThread(Threadable *t); |
131 | |
132 | #if defined(LOVE_LINUX) |
133 | void disableSignals(); |
134 | void reenableSignals(); |
135 | |
136 | struct ScopedDisableSignals |
137 | { |
138 | ScopedDisableSignals() { disableSignals(); } |
139 | ~ScopedDisableSignals() { reenableSignals(); } |
140 | }; |
141 | #endif |
142 | |
143 | } // thread |
144 | } // love |
145 | |
146 | #endif /* LOVE_THREAD_THREADS_H */ |
147 | |