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
31namespace love
32{
33namespace thread
34{
35
36class Mutex
37{
38public:
39 virtual ~Mutex() {}
40
41 virtual void lock() = 0;
42 virtual void unlock() = 0;
43};
44
45class Conditional
46{
47public:
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
55class Lock
56{
57public:
58 Lock(Mutex *m);
59 Lock(Mutex &m);
60 Lock(Lock &&other);
61 ~Lock();
62
63private:
64 Mutex *mutex;
65};
66
67class EmptyLock
68{
69public:
70 EmptyLock();
71 ~EmptyLock();
72
73 void setLock(Mutex *m);
74 void setLock(Mutex &m);
75
76private:
77 Mutex *mutex;
78};
79
80class Threadable : public love::Object
81{
82public:
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
95protected:
96
97 Thread *owner;
98 std::string threadName;
99
100};
101
102class MutexRef
103{
104public:
105 MutexRef();
106 ~MutexRef();
107
108 operator Mutex*() const;
109 Mutex *operator->() const;
110
111private:
112 Mutex *mutex;
113};
114
115class ConditionalRef
116{
117public:
118 ConditionalRef();
119 ~ConditionalRef();
120
121 operator Conditional*() const;
122 Conditional *operator->() const;
123
124private:
125 Conditional *conditional;
126};
127
128Mutex *newMutex();
129Conditional *newConditional();
130Thread *newThread(Threadable *t);
131
132#if defined(LOVE_LINUX)
133void disableSignals();
134void reenableSignals();
135
136struct ScopedDisableSignals
137{
138 ScopedDisableSignals() { disableSignals(); }
139 ~ScopedDisableSignals() { reenableSignals(); }
140};
141#endif
142
143} // thread
144} // love
145
146#endif /* LOVE_THREAD_THREADS_H */
147