1 | // |
---|
2 | // Lol Engine |
---|
3 | // |
---|
4 | // Copyright: (c) 2010-2011 Sam Hocevar <sam@hocevar.net> |
---|
5 | // This program is free software; you can redistribute it and/or |
---|
6 | // modify it under the terms of the Do What The Fuck You Want To |
---|
7 | // Public License, Version 2, as published by Sam Hocevar. See |
---|
8 | // http://www.wtfpl.net/ for more details. |
---|
9 | // |
---|
10 | |
---|
11 | // |
---|
12 | // The ThreadBase class |
---|
13 | // -------------------- |
---|
14 | // |
---|
15 | |
---|
16 | #if !defined __LOL_THREADBASE_H__ |
---|
17 | #define __LOL_THREADBASE_H__ |
---|
18 | |
---|
19 | #include <sys/ppu_thread.h> |
---|
20 | #include <sys/event.h> |
---|
21 | |
---|
22 | namespace lol |
---|
23 | { |
---|
24 | |
---|
25 | class MutexBase |
---|
26 | { |
---|
27 | public: |
---|
28 | MutexBase() |
---|
29 | { |
---|
30 | sys_lwmutex_attribute_t attr; |
---|
31 | sys_lwmutex_attribute_initialize(attr); |
---|
32 | sys_lwmutex_create(&m_mutex, &attr); |
---|
33 | } |
---|
34 | |
---|
35 | ~MutexBase() |
---|
36 | { |
---|
37 | while (sys_lwmutex_destroy(&m_mutex) == EBUSY) |
---|
38 | ; |
---|
39 | } |
---|
40 | |
---|
41 | void Lock() |
---|
42 | { |
---|
43 | sys_lwmutex_lock(&m_mutex, 0); |
---|
44 | } |
---|
45 | |
---|
46 | void Unlock() |
---|
47 | { |
---|
48 | sys_lwmutex_unlock(&m_mutex); |
---|
49 | } |
---|
50 | |
---|
51 | private: |
---|
52 | sys_lwmutex_t m_mutex; |
---|
53 | }; |
---|
54 | |
---|
55 | template<typename T, int N> class QueueBase |
---|
56 | { |
---|
57 | public: |
---|
58 | QueueBase() |
---|
59 | { |
---|
60 | m_start = m_count = 0; |
---|
61 | m_poppers = m_pushers = 0; |
---|
62 | |
---|
63 | sys_lwmutex_attribute_t mattr; |
---|
64 | sys_lwmutex_attribute_initialize(mattr); |
---|
65 | sys_lwmutex_create(&m_mutex, &mattr); |
---|
66 | |
---|
67 | sys_lwcond_attribute_t cattr; |
---|
68 | sys_lwcond_attribute_initialize(cattr); |
---|
69 | sys_lwcond_create(&m_empty_cond, &m_mutex, &cattr); |
---|
70 | sys_lwcond_create(&m_full_cond, &m_mutex, &cattr); |
---|
71 | } |
---|
72 | |
---|
73 | ~QueueBase() |
---|
74 | { |
---|
75 | while (sys_lwcond_destroy(&m_empty_cond) == EBUSY) |
---|
76 | ; |
---|
77 | while (sys_lwcond_destroy(&m_full_cond) == EBUSY) |
---|
78 | ; |
---|
79 | while (sys_lwmutex_destroy(&m_mutex) == EBUSY) |
---|
80 | ; |
---|
81 | } |
---|
82 | |
---|
83 | void Push(T value) |
---|
84 | { |
---|
85 | /* FIXME: this is a copy of the pthread implementation, but we |
---|
86 | * should really use libsync2 instead. */ |
---|
87 | sys_lwmutex_lock(&m_mutex, 0); |
---|
88 | m_pushers++; |
---|
89 | while (m_count == CAPACITY) |
---|
90 | sys_lwcond_wait(&m_full_cond, 0); |
---|
91 | m_pushers--; |
---|
92 | m_values[(m_start + m_count) % CAPACITY] = value; |
---|
93 | m_count++; |
---|
94 | if (m_poppers) |
---|
95 | sys_lwcond_signal(&m_empty_cond); |
---|
96 | sys_lwmutex_unlock(&m_mutex); |
---|
97 | } |
---|
98 | |
---|
99 | T Pop() |
---|
100 | { |
---|
101 | sys_lwmutex_lock(&m_mutex, 0); |
---|
102 | m_poppers++; |
---|
103 | while (m_count == 0) |
---|
104 | sys_lwcond_wait(&m_empty_cond, 0); |
---|
105 | m_poppers--; |
---|
106 | T ret = m_values[m_start]; |
---|
107 | m_start = (m_start + 1) % CAPACITY; |
---|
108 | m_count--; |
---|
109 | if (m_pushers) |
---|
110 | sys_lwcond_signal(&m_full_cond); |
---|
111 | sys_lwmutex_unlock(&m_mutex); |
---|
112 | return ret; |
---|
113 | } |
---|
114 | |
---|
115 | private: |
---|
116 | static size_t const CAPACITY = N; |
---|
117 | T m_values[CAPACITY]; |
---|
118 | size_t m_start, m_count; |
---|
119 | size_t m_poppers, m_pushers; |
---|
120 | sys_lwmutex_t m_mutex; |
---|
121 | sys_lwcond_t m_empty_cond, m_full_cond; |
---|
122 | }; |
---|
123 | |
---|
124 | class ThreadBase |
---|
125 | { |
---|
126 | public: |
---|
127 | ThreadBase(void *(*fn)(void *), void *data) |
---|
128 | { |
---|
129 | size_t stack_size = 128 * 1024; |
---|
130 | char const *thread_name = "new thread"; |
---|
131 | |
---|
132 | /* FIXME: choose priority more wisely */ |
---|
133 | sys_ppu_thread_create(&m_thread, (void (*)(uint64_t))fn, |
---|
134 | (uint64_t)data, 1000, stack_size, 0, thread_name); |
---|
135 | } |
---|
136 | |
---|
137 | virtual ~ThreadBase() |
---|
138 | { |
---|
139 | sys_ppu_thread_join(m_thread, NULL); |
---|
140 | } |
---|
141 | |
---|
142 | private: |
---|
143 | sys_ppu_thread_t m_thread; |
---|
144 | }; |
---|
145 | |
---|
146 | } /* namespace lol */ |
---|
147 | |
---|
148 | #endif // __LOL_THREADBASE_H__ |
---|
149 | |
---|