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 | #include "threads.h" |
22 | #include "Thread.h" |
23 | |
24 | namespace love |
25 | { |
26 | namespace thread |
27 | { |
28 | namespace sdl |
29 | { |
30 | |
31 | Mutex::Mutex() |
32 | { |
33 | mutex = SDL_CreateMutex(); |
34 | } |
35 | |
36 | Mutex::~Mutex() |
37 | { |
38 | SDL_DestroyMutex(mutex); |
39 | } |
40 | |
41 | void Mutex::lock() |
42 | { |
43 | SDL_LockMutex(mutex); |
44 | } |
45 | |
46 | void Mutex::unlock() |
47 | { |
48 | SDL_UnlockMutex(mutex); |
49 | } |
50 | |
51 | Conditional::Conditional() |
52 | { |
53 | cond = SDL_CreateCond(); |
54 | } |
55 | |
56 | Conditional::~Conditional() |
57 | { |
58 | SDL_DestroyCond(cond); |
59 | } |
60 | |
61 | void Conditional::signal() |
62 | { |
63 | SDL_CondSignal(cond); |
64 | } |
65 | |
66 | void Conditional::broadcast() |
67 | { |
68 | SDL_CondBroadcast(cond); |
69 | } |
70 | |
71 | bool Conditional::wait(thread::Mutex *_mutex, int timeout) |
72 | { |
73 | // Yes, I realise this can be dangerous, |
74 | // however, you're asking for it if you're |
75 | // mixing thread implementations. |
76 | Mutex *mutex = (Mutex *) _mutex; |
77 | if (timeout < 0) |
78 | return !SDL_CondWait(cond, mutex->mutex); |
79 | else |
80 | return (SDL_CondWaitTimeout(cond, mutex->mutex, timeout) == 0); |
81 | } |
82 | |
83 | } // sdl |
84 | |
85 | |
86 | /** |
87 | * Implementations of the functions declared in src/modules/threads.h. |
88 | **/ |
89 | |
90 | thread::Mutex *newMutex() |
91 | { |
92 | return new sdl::Mutex(); |
93 | } |
94 | |
95 | thread::Conditional *newConditional() |
96 | { |
97 | return new sdl::Conditional(); |
98 | } |
99 | |
100 | thread::Thread *newThread(Threadable *t) |
101 | { |
102 | return new sdl::Thread(t); |
103 | } |
104 | |
105 | } // thread |
106 | } // love |
107 | |