]> git.cworth.org Git - vogl/blob - src/voglcore/vogl_threading_null.h
Initial vogl checkin
[vogl] / src / voglcore / vogl_threading_null.h
1 /**************************************************************************
2  *
3  * Copyright 2013-2014 RAD Game Tools and Valve Software
4  * Copyright 2010-2014 Rich Geldreich and Tenacious Software LLC
5  * All Rights Reserved.
6  *
7  * Permission is hereby granted, free of charge, to any person obtaining a copy
8  * of this software and associated documentation files (the "Software"), to deal
9  * in the Software without restriction, including without limitation the rights
10  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11  * copies of the Software, and to permit persons to whom the Software is
12  * furnished to do so, subject to the following conditions:
13  *
14  * The above copyright notice and this permission notice shall be included in
15  * all copies or substantial portions of the Software.
16  *
17  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23  * THE SOFTWARE.
24  *
25  **************************************************************************/
26
27 // File: vogl_threading_null.h
28 #pragma once
29
30 #include "vogl_core.h"
31 #include "vogl_atomics.h"
32
33 namespace vogl
34 {
35     const uint g_number_of_processors = 1;
36
37     inline void vogl_threading_init()
38     {
39     }
40
41     typedef uint64_t vogl_thread_id_t;
42     inline vogl_thread_id_t vogl_get_current_thread_id()
43     {
44         return 0;
45     }
46
47     inline void vogl_sleep(unsigned int milliseconds)
48     {
49         VOGL_NOTE_UNUSED(milliseconds);
50     }
51
52     inline uint vogl_get_max_helper_threads()
53     {
54         return 0;
55     }
56
57     class mutex
58     {
59         VOGL_NO_COPY_OR_ASSIGNMENT_OP(mutex);
60
61     public:
62         inline mutex(unsigned int spin_count = 0)
63         {
64             VOGL_NOTE_UNUSED(spin_count);
65         }
66
67         inline ~mutex()
68         {
69         }
70
71         inline void lock()
72         {
73         }
74
75         inline void unlock()
76         {
77         }
78
79         inline void set_spin_count(unsigned int count)
80         {
81             VOGL_NOTE_UNUSED(count);
82         }
83     };
84
85     class scoped_mutex
86     {
87         scoped_mutex(const scoped_mutex &);
88         scoped_mutex &operator=(const scoped_mutex &);
89
90     public:
91         inline scoped_mutex(mutex &lock)
92             : m_lock(lock)
93         {
94             m_lock.lock();
95         }
96         inline ~scoped_mutex()
97         {
98             m_lock.unlock();
99         }
100
101     private:
102         mutex &m_lock;
103     };
104
105     // Simple non-recursive spinlock.
106     class spinlock
107     {
108     public:
109         inline spinlock()
110         {
111         }
112
113         inline void lock(uint32 max_spins = 4096, bool yielding = true, bool memoryBarrier = true)
114         {
115             VOGL_NOTE_UNUSED(max_spins), VOGL_NOTE_UNUSED(yielding), VOGL_NOTE_UNUSED(memoryBarrier);
116         }
117
118         inline void lock_no_barrier(uint32 max_spins = 4096, bool yielding = true)
119         {
120             VOGL_NOTE_UNUSED(max_spins), VOGL_NOTE_UNUSED(yielding);
121         }
122
123         inline void unlock()
124         {
125         }
126
127         inline void unlock_no_barrier()
128         {
129         }
130     };
131
132     class scoped_spinlock
133     {
134         scoped_spinlock(const scoped_spinlock &);
135         scoped_spinlock &operator=(const scoped_spinlock &);
136
137     public:
138         inline scoped_spinlock(spinlock &lock)
139             : m_lock(lock)
140         {
141             m_lock.lock();
142         }
143         inline ~scoped_spinlock()
144         {
145             m_lock.unlock();
146         }
147
148     private:
149         spinlock &m_lock;
150     };
151
152     class semaphore
153     {
154         VOGL_NO_COPY_OR_ASSIGNMENT_OP(semaphore);
155
156     public:
157         inline semaphore(uint32 initialCount, uint32 maximumCount, const char *pName = NULL)
158         {
159             VOGL_NOTE_UNUSED(initialCount), VOGL_NOTE_UNUSED(maximumCount), VOGL_NOTE_UNUSED(pName);
160         }
161
162         inline ~semaphore()
163         {
164         }
165
166         inline void release(uint32 releaseCount = 1)
167         {
168             VOGL_NOTE_UNUSED(releaseCount);
169         }
170
171         inline bool wait(uint32 milliseconds = cUINT32_MAX)
172         {
173             VOGL_NOTE_UNUSED(milliseconds);
174             return true;
175         }
176     };
177
178     class task_pool
179     {
180     public:
181         inline task_pool()
182         {
183         }
184         inline task_pool(uint num_threads)
185         {
186             num_threads;
187         }
188         inline ~task_pool()
189         {
190         }
191
192         inline bool init(uint num_threads)
193         {
194             num_threads;
195             return true;
196         }
197         inline void deinit()
198         {
199         }
200
201         inline uint get_num_threads() const
202         {
203             return 0;
204         }
205         inline uint get_num_outstanding_tasks() const
206         {
207             return 0;
208         }
209
210         // C-style task callback
211         typedef void (*task_callback_func)(uint64_t data, void *pData_ptr);
212         inline bool queue_task(task_callback_func pFunc, uint64_t data = 0, void *pData_ptr = NULL)
213         {
214             pFunc(data, pData_ptr);
215             return true;
216         }
217
218         class executable_task
219         {
220         public:
221             virtual void execute_task(uint64_t data, void *pData_ptr) = 0;
222         };
223
224         // It's the caller's responsibility to delete pObj within the execute_task() method, if needed!
225         inline bool queue_task(executable_task *pObj, uint64_t data = 0, void *pData_ptr = NULL)
226         {
227             pObj->execute_task(data, pData_ptr);
228             return true;
229         }
230
231         template <typename S, typename T>
232         inline bool queue_object_task(S *pObject, T pObject_method, uint64_t data = 0, void *pData_ptr = NULL)
233         {
234             (pObject->*pObject_method)(data, pData_ptr);
235             return true;
236         }
237
238         template <typename S, typename T>
239         inline bool queue_multiple_object_tasks(S *pObject, T pObject_method, uint64_t first_data, uint num_tasks, void *pData_ptr = NULL)
240         {
241             for (uint i = 0; i < num_tasks; i++)
242             {
243                 (pObject->*pObject_method)(first_data + i, pData_ptr);
244             }
245             return true;
246         }
247
248         inline void join()
249         {
250         }
251     };
252
253 } // namespace vogl