1 /**************************************************************************
3 * Copyright 2013-2014 RAD Game Tools and Valve Software
4 * Copyright 2010-2014 Rich Geldreich and Tenacious Software LLC
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:
14 * The above copyright notice and this permission notice shall be included in
15 * all copies or substantial portions of the Software.
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
25 **************************************************************************/
27 // File: vogl_threading_null.h
30 #include "vogl_core.h"
31 #include "vogl_atomics.h"
35 const uint g_number_of_processors = 1;
37 inline void vogl_threading_init()
41 typedef uint64_t vogl_thread_id_t;
42 inline vogl_thread_id_t vogl_get_current_thread_id()
47 inline void vogl_sleep(unsigned int milliseconds)
49 VOGL_NOTE_UNUSED(milliseconds);
52 inline uint vogl_get_max_helper_threads()
59 VOGL_NO_COPY_OR_ASSIGNMENT_OP(mutex);
62 inline mutex(unsigned int spin_count = 0)
64 VOGL_NOTE_UNUSED(spin_count);
79 inline void set_spin_count(unsigned int count)
81 VOGL_NOTE_UNUSED(count);
87 scoped_mutex(const scoped_mutex &);
88 scoped_mutex &operator=(const scoped_mutex &);
91 inline scoped_mutex(mutex &lock)
96 inline ~scoped_mutex()
105 // Simple non-recursive spinlock.
113 inline void lock(uint32 max_spins = 4096, bool yielding = true, bool memoryBarrier = true)
115 VOGL_NOTE_UNUSED(max_spins), VOGL_NOTE_UNUSED(yielding), VOGL_NOTE_UNUSED(memoryBarrier);
118 inline void lock_no_barrier(uint32 max_spins = 4096, bool yielding = true)
120 VOGL_NOTE_UNUSED(max_spins), VOGL_NOTE_UNUSED(yielding);
127 inline void unlock_no_barrier()
132 class scoped_spinlock
134 scoped_spinlock(const scoped_spinlock &);
135 scoped_spinlock &operator=(const scoped_spinlock &);
138 inline scoped_spinlock(spinlock &lock)
143 inline ~scoped_spinlock()
154 VOGL_NO_COPY_OR_ASSIGNMENT_OP(semaphore);
157 inline semaphore(uint32 initialCount, uint32 maximumCount, const char *pName = NULL)
159 VOGL_NOTE_UNUSED(initialCount), VOGL_NOTE_UNUSED(maximumCount), VOGL_NOTE_UNUSED(pName);
166 inline void release(uint32 releaseCount = 1)
168 VOGL_NOTE_UNUSED(releaseCount);
171 inline bool wait(uint32 milliseconds = cUINT32_MAX)
173 VOGL_NOTE_UNUSED(milliseconds);
184 inline task_pool(uint num_threads)
192 inline bool init(uint num_threads)
201 inline uint get_num_threads() const
205 inline uint get_num_outstanding_tasks() const
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)
214 pFunc(data, pData_ptr);
218 class executable_task
221 virtual void execute_task(uint64_t data, void *pData_ptr) = 0;
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)
227 pObj->execute_task(data, pData_ptr);
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)
234 (pObject->*pObject_method)(data, pData_ptr);
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)
241 for (uint i = 0; i < num_tasks; i++)
243 (pObject->*pObject_method)(first_data + i, pData_ptr);