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_win32_threading.cpp
28 #include "vogl_core.h"
29 #include "vogl_threading_win32.h"
30 #include "vogl_winhdr.h"
35 uint g_number_of_processors = 1;
37 void vogl_threading_init()
39 SYSTEM_INFO g_system_info;
40 GetSystemInfo(&g_system_info);
42 g_number_of_processors = math::maximum<uint>(1U, g_system_info.dwNumberOfProcessors);
45 vogl_thread_id_t vogl_get_current_thread_id()
47 return static_cast<vogl_thread_id_t>(GetCurrentThreadId());
50 void vogl_sleep(unsigned int milliseconds)
55 uint vogl_get_max_helper_threads()
57 if (g_number_of_processors > 1)
60 return VOGL_MIN((int)task_pool::cMaxThreads, (int)g_number_of_processors - 1);
66 mutex::mutex(unsigned int spin_count)
68 VOGL_ASSUME(sizeof(mutex) >= sizeof(CRITICAL_SECTION));
71 CRITICAL_SECTION &m_cs = *static_cast<CRITICAL_SECTION *>(p);
74 status = InitializeCriticalSectionAndSpinCount(&m_cs, spin_count);
76 vogl_fail("mutex::mutex: InitializeCriticalSectionAndSpinCount failed", __FILE__, __LINE__);
78 #ifdef VOGL_BUILD_DEBUG
86 CRITICAL_SECTION &m_cs = *static_cast<CRITICAL_SECTION *>(p);
88 #ifdef VOGL_BUILD_DEBUG
90 vogl_assert("mutex::~mutex: mutex is still locked", __FILE__, __LINE__);
92 DeleteCriticalSection(&m_cs);
98 CRITICAL_SECTION &m_cs = *static_cast<CRITICAL_SECTION *>(p);
100 EnterCriticalSection(&m_cs);
101 #ifdef VOGL_BUILD_DEBUG
109 CRITICAL_SECTION &m_cs = *static_cast<CRITICAL_SECTION *>(p);
111 #ifdef VOGL_BUILD_DEBUG
113 vogl_assert("mutex::unlock: mutex is not locked", __FILE__, __LINE__);
116 LeaveCriticalSection(&m_cs);
119 void mutex::set_spin_count(unsigned int count)
122 CRITICAL_SECTION &m_cs = *static_cast<CRITICAL_SECTION *>(p);
124 SetCriticalSectionSpinCount(&m_cs, count);
127 void spinlock::lock(uint32 max_spins, bool yielding)
129 if (g_number_of_processors <= 1)
132 uint32 spinCount = 0;
133 uint32 yieldCount = 0;
137 VOGL_ASSUME(sizeof(long) == sizeof(int32));
138 if (!InterlockedExchange((volatile long *)&m_flag, TRUE))
151 if ((yielding) && (spinCount >= max_spins))
166 if (g_number_of_processors <= 1)
169 spinCount = max_spins / 2;
178 if (g_number_of_processors <= 1)
181 spinCount = max_spins;
190 VOGL_MEMORY_IMPORT_BARRIER
193 void spinlock::unlock()
195 VOGL_MEMORY_EXPORT_BARRIER
197 InterlockedExchange((volatile long *)&m_flag, FALSE);
200 semaphore::semaphore(int32 initialCount, int32 maximumCount, const char *pName)
202 m_handle = CreateSemaphoreA(NULL, initialCount, maximumCount, pName);
203 if (NULL == m_handle)
205 VOGL_FAIL("semaphore: CreateSemaphore() failed");
209 semaphore::~semaphore()
213 CloseHandle(m_handle);
218 void semaphore::release(int32 releaseCount, int32 *pPreviousCount)
220 VOGL_ASSUME(sizeof(LONG) == sizeof(int32));
221 if (0 == ReleaseSemaphore(m_handle, releaseCount, (LPLONG)pPreviousCount))
223 VOGL_FAIL("semaphore: ReleaseSemaphore() failed");
227 bool semaphore::try_release(int32 releaseCount, int32 *pPreviousCount)
229 VOGL_ASSUME(sizeof(LONG) == sizeof(int32));
230 return ReleaseSemaphore(m_handle, releaseCount, (LPLONG)pPreviousCount) != 0;
233 bool semaphore::wait(uint32 milliseconds)
235 uint32 result = WaitForSingleObject(m_handle, milliseconds);
237 if (WAIT_FAILED == result)
239 VOGL_FAIL("semaphore: WaitForSingleObject() failed");
242 return WAIT_OBJECT_0 == result;
245 task_pool::task_pool()
246 : m_pTask_stack(vogl_new<ts_task_stack_t>()),
248 m_tasks_available(0, 32767),
249 m_all_tasks_completed(0, 1),
250 m_total_submitted_tasks(0),
251 m_total_completed_tasks(0),
254 utils::zero_object(m_threads);
257 task_pool::task_pool(uint num_threads)
258 : m_pTask_stack(vogl_new<ts_task_stack_t>()),
260 m_tasks_available(0, 32767),
261 m_all_tasks_completed(0, 1),
262 m_total_submitted_tasks(0),
263 m_total_completed_tasks(0),
266 utils::zero_object(m_threads);
268 bool status = init(num_threads);
272 task_pool::~task_pool()
275 vogl_delete(m_pTask_stack);
278 bool task_pool::init(uint num_threads)
280 VOGL_ASSERT(num_threads <= cMaxThreads);
281 num_threads = math::minimum<uint>(num_threads, cMaxThreads);
285 bool succeeded = true;
288 while (m_num_threads < num_threads)
290 m_threads[m_num_threads] = (HANDLE)_beginthreadex(NULL, 32768, thread_func, this, 0, NULL);
291 VOGL_ASSERT(m_threads[m_num_threads] != 0);
293 if (!m_threads[m_num_threads])
311 void task_pool::deinit()
317 // Set exit flag, then release all threads. Each should wakeup and exit.
318 atomic_exchange32(&m_exit_flag, true);
320 m_tasks_available.release(m_num_threads);
322 // Now wait for each thread to exit.
323 for (uint i = 0; i < m_num_threads; i++)
329 // Can be an INFINITE delay, but set at 30 seconds so this function always provably exits.
330 DWORD result = WaitForSingleObject(m_threads[i], 30000);
331 if ((result == WAIT_OBJECT_0) || (result == WAIT_ABANDONED))
335 CloseHandle(m_threads[i]);
342 atomic_exchange32(&m_exit_flag, false);
346 m_pTask_stack->clear();
347 m_total_submitted_tasks = 0;
348 m_total_completed_tasks = 0;
351 bool task_pool::queue_task(task_callback_func pFunc, uint64_t data, void *pData_ptr)
356 tsk.m_callback = pFunc;
358 tsk.m_pData_ptr = pData_ptr;
361 atomic_increment32(&m_total_submitted_tasks);
363 if (!m_pTask_stack->try_push(tsk))
365 atomic_increment32(&m_total_completed_tasks);
369 m_tasks_available.release(1);
374 // It's the object's responsibility to delete pObj within the execute_task() method, if needed!
375 bool task_pool::queue_task(executable_task *pObj, uint64_t data, void *pData_ptr)
382 tsk.m_pData_ptr = pData_ptr;
383 tsk.m_flags = cTaskFlagObject;
385 atomic_increment32(&m_total_submitted_tasks);
387 if (!m_pTask_stack->try_push(tsk))
389 atomic_increment32(&m_total_completed_tasks);
393 m_tasks_available.release(1);
398 void task_pool::process_task(task &tsk)
400 if (tsk.m_flags & cTaskFlagObject)
401 tsk.m_pObj->execute_task(tsk.m_data, tsk.m_pData_ptr);
403 tsk.m_callback(tsk.m_data, tsk.m_pData_ptr);
405 if (atomic_increment32(&m_total_completed_tasks) == m_total_submitted_tasks)
407 // Try to signal the semaphore (the max count is 1 so this may actually fail).
408 m_all_tasks_completed.try_release();
412 void task_pool::join()
414 // Try to steal any outstanding tasks. This could cause one or more worker threads to wake up and immediately go back to sleep, which is wasteful but should be harmless.
416 while (m_pTask_stack->pop(tsk))
419 // At this point the task stack is empty.
420 // Now wait for all concurrent tasks to complete. The m_all_tasks_completed semaphore has a max count of 1, so it's possible it could have saturated to 1 as the tasks
421 // where issued and asynchronously completed, so this loop may iterate a few times.
422 const int total_submitted_tasks = atomic_add32(&m_total_submitted_tasks, 0);
423 while (m_total_completed_tasks != total_submitted_tasks)
425 // If the previous (m_total_completed_tasks != total_submitted_tasks) check failed the semaphore MUST be eventually signalled once the last task completes.
426 // So I think this can actually be an INFINITE delay, but it shouldn't really matter if it's 1ms.
427 m_all_tasks_completed.wait(1);
431 unsigned __stdcall task_pool::thread_func(void *pContext)
433 task_pool *pPool = static_cast<task_pool *>(pContext);
437 if (!pPool->m_tasks_available.wait())
440 if (pPool->m_exit_flag)
444 if (pPool->m_pTask_stack->pop(tsk))
445 pPool->process_task(tsk);