File size: 12,715 Bytes
8b7c501 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 |
/* Standard C headers */
#include <assert.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
/* Configuration header */
#include "threadpool-common.h"
/* Windows headers */
#ifndef WIN32_LEAN_AND_MEAN
#define WIN32_LEAN_AND_MEAN
#endif
#include <windows.h>
/* Public library header */
#include <pthreadpool.h>
/* Internal library headers */
#include "threadpool-atomics.h"
#include "threadpool-object.h"
#include "threadpool-utils.h"
static void checkin_worker_thread(struct pthreadpool* threadpool, uint32_t event_index) {
if (pthreadpool_decrement_fetch_acquire_release_size_t(&threadpool->active_threads) == 0) {
SetEvent(threadpool->completion_event[event_index]);
}
}
static void wait_worker_threads(struct pthreadpool* threadpool, uint32_t event_index) {
/* Initial check */
size_t active_threads = pthreadpool_load_acquire_size_t(&threadpool->active_threads);
if (active_threads == 0) {
return;
}
/* Spin-wait */
for (uint32_t i = PTHREADPOOL_SPIN_WAIT_ITERATIONS; i != 0; i--) {
pthreadpool_yield();
active_threads = pthreadpool_load_acquire_size_t(&threadpool->active_threads);
if (active_threads == 0) {
return;
}
}
/* Fall-back to event wait */
const DWORD wait_status = WaitForSingleObject(threadpool->completion_event[event_index], INFINITE);
assert(wait_status == WAIT_OBJECT_0);
assert(pthreadpool_load_relaxed_size_t(&threadpool->active_threads) == 0);
}
static uint32_t wait_for_new_command(
struct pthreadpool* threadpool,
uint32_t last_command,
uint32_t last_flags)
{
uint32_t command = pthreadpool_load_acquire_uint32_t(&threadpool->command);
if (command != last_command) {
return command;
}
if ((last_flags & PTHREADPOOL_FLAG_YIELD_WORKERS) == 0) {
/* Spin-wait loop */
for (uint32_t i = PTHREADPOOL_SPIN_WAIT_ITERATIONS; i != 0; i--) {
pthreadpool_yield();
command = pthreadpool_load_acquire_uint32_t(&threadpool->command);
if (command != last_command) {
return command;
}
}
}
/* Spin-wait disabled or timed out, fall back to event wait */
const uint32_t event_index = (last_command >> 31);
const DWORD wait_status = WaitForSingleObject(threadpool->command_event[event_index], INFINITE);
assert(wait_status == WAIT_OBJECT_0);
command = pthreadpool_load_relaxed_uint32_t(&threadpool->command);
assert(command != last_command);
return command;
}
static DWORD WINAPI thread_main(LPVOID arg) {
struct thread_info* thread = (struct thread_info*) arg;
struct pthreadpool* threadpool = thread->threadpool;
uint32_t last_command = threadpool_command_init;
struct fpu_state saved_fpu_state = { 0 };
uint32_t flags = 0;
/* Check in */
checkin_worker_thread(threadpool, 0);
/* Monitor new commands and act accordingly */
for (;;) {
uint32_t command = wait_for_new_command(threadpool, last_command, flags);
pthreadpool_fence_acquire();
flags = pthreadpool_load_relaxed_uint32_t(&threadpool->flags);
/* Process command */
switch (command & THREADPOOL_COMMAND_MASK) {
case threadpool_command_parallelize:
{
const thread_function_t thread_function =
(thread_function_t) pthreadpool_load_relaxed_void_p(&threadpool->thread_function);
if (flags & PTHREADPOOL_FLAG_DISABLE_DENORMALS) {
saved_fpu_state = get_fpu_state();
disable_fpu_denormals();
}
thread_function(threadpool, thread);
if (flags & PTHREADPOOL_FLAG_DISABLE_DENORMALS) {
set_fpu_state(saved_fpu_state);
}
break;
}
case threadpool_command_shutdown:
/* Exit immediately: the master thread is waiting on pthread_join */
return 0;
case threadpool_command_init:
/* To inhibit compiler warning */
break;
}
/* Notify the master thread that we finished processing */
const uint32_t event_index = command >> 31;
checkin_worker_thread(threadpool, event_index);
/* Update last command */
last_command = command;
};
return 0;
}
struct pthreadpool* pthreadpool_create(size_t threads_count) {
if (threads_count == 0) {
SYSTEM_INFO system_info;
ZeroMemory(&system_info, sizeof(system_info));
GetSystemInfo(&system_info);
threads_count = (size_t) system_info.dwNumberOfProcessors;
}
struct pthreadpool* threadpool = pthreadpool_allocate(threads_count);
if (threadpool == NULL) {
return NULL;
}
threadpool->threads_count = fxdiv_init_size_t(threads_count);
for (size_t tid = 0; tid < threads_count; tid++) {
threadpool->threads[tid].thread_number = tid;
threadpool->threads[tid].threadpool = threadpool;
}
/* Thread pool with a single thread computes everything on the caller thread. */
if (threads_count > 1) {
threadpool->execution_mutex = CreateMutexW(
NULL /* mutex attributes */,
FALSE /* initially owned */,
NULL /* name */);
for (size_t i = 0; i < 2; i++) {
threadpool->completion_event[i] = CreateEventW(
NULL /* event attributes */,
TRUE /* manual-reset event: yes */,
FALSE /* initial state: nonsignaled */,
NULL /* name */);
threadpool->command_event[i] = CreateEventW(
NULL /* event attributes */,
TRUE /* manual-reset event: yes */,
FALSE /* initial state: nonsignaled */,
NULL /* name */);
}
pthreadpool_store_relaxed_size_t(&threadpool->active_threads, threads_count - 1 /* caller thread */);
/* Caller thread serves as worker #0. Thus, we create system threads starting with worker #1. */
for (size_t tid = 1; tid < threads_count; tid++) {
threadpool->threads[tid].thread_handle = CreateThread(
NULL /* thread attributes */,
0 /* stack size: default */,
&thread_main,
&threadpool->threads[tid],
0 /* creation flags */,
NULL /* thread id */);
}
/* Wait until all threads initialize */
wait_worker_threads(threadpool, 0);
}
return threadpool;
}
PTHREADPOOL_INTERNAL void pthreadpool_parallelize(
struct pthreadpool* threadpool,
thread_function_t thread_function,
const void* params,
size_t params_size,
void* task,
void* context,
size_t linear_range,
uint32_t flags)
{
assert(threadpool != NULL);
assert(thread_function != NULL);
assert(task != NULL);
assert(linear_range > 1);
/* Protect the global threadpool structures */
const DWORD wait_status = WaitForSingleObject(threadpool->execution_mutex, INFINITE);
assert(wait_status == WAIT_OBJECT_0);
/* Setup global arguments */
pthreadpool_store_relaxed_void_p(&threadpool->thread_function, (void*) thread_function);
pthreadpool_store_relaxed_void_p(&threadpool->task, task);
pthreadpool_store_relaxed_void_p(&threadpool->argument, context);
pthreadpool_store_relaxed_uint32_t(&threadpool->flags, flags);
const struct fxdiv_divisor_size_t threads_count = threadpool->threads_count;
pthreadpool_store_relaxed_size_t(&threadpool->active_threads, threads_count.value - 1 /* caller thread */);
if (params_size != 0) {
CopyMemory(&threadpool->params, params, params_size);
pthreadpool_fence_release();
}
/* Spread the work between threads */
const struct fxdiv_result_size_t range_params = fxdiv_divide_size_t(linear_range, threads_count);
size_t range_start = 0;
for (size_t tid = 0; tid < threads_count.value; tid++) {
struct thread_info* thread = &threadpool->threads[tid];
const size_t range_length = range_params.quotient + (size_t) (tid < range_params.remainder);
const size_t range_end = range_start + range_length;
pthreadpool_store_relaxed_size_t(&thread->range_start, range_start);
pthreadpool_store_relaxed_size_t(&thread->range_end, range_end);
pthreadpool_store_relaxed_size_t(&thread->range_length, range_length);
/* The next subrange starts where the previous ended */
range_start = range_end;
}
/*
* Update the threadpool command.
* Imporantly, do it after initializing command parameters (range, task, argument, flags)
* ~(threadpool->command | THREADPOOL_COMMAND_MASK) flips the bits not in command mask
* to ensure the unmasked command is different then the last command, because worker threads
* monitor for change in the unmasked command.
*/
const uint32_t old_command = pthreadpool_load_relaxed_uint32_t(&threadpool->command);
const uint32_t new_command = ~(old_command | THREADPOOL_COMMAND_MASK) | threadpool_command_parallelize;
/*
* Reset the command event for the next command.
* It is important to reset the event before writing out the new command, because as soon as the worker threads
* observe the new command, they may process it and switch to waiting on the next command event.
*
* Note: the event is different from the command event signalled in this update.
*/
const uint32_t event_index = (old_command >> 31);
BOOL reset_event_status = ResetEvent(threadpool->command_event[event_index ^ 1]);
assert(reset_event_status != FALSE);
/*
* Store the command with release semantics to guarantee that if a worker thread observes
* the new command value, it also observes the updated command parameters.
*
* Note: release semantics is necessary, because the workers might be waiting in a spin-loop
* rather than on the event object.
*/
pthreadpool_store_release_uint32_t(&threadpool->command, new_command);
/*
* Signal the event to wake up the threads.
* Event in use must be switched after every submitted command to avoid race conditions.
* Choose the event based on the high bit of the command, which is flipped on every update.
*/
const BOOL set_event_status = SetEvent(threadpool->command_event[event_index]);
assert(set_event_status != FALSE);
/* Save and modify FPU denormals control, if needed */
struct fpu_state saved_fpu_state = { 0 };
if (flags & PTHREADPOOL_FLAG_DISABLE_DENORMALS) {
saved_fpu_state = get_fpu_state();
disable_fpu_denormals();
}
/* Do computations as worker #0 */
thread_function(threadpool, &threadpool->threads[0]);
/* Restore FPU denormals control, if needed */
if (flags & PTHREADPOOL_FLAG_DISABLE_DENORMALS) {
set_fpu_state(saved_fpu_state);
}
/*
* Wait until the threads finish computation
* Use the complementary event because it corresponds to the new command.
*/
wait_worker_threads(threadpool, event_index ^ 1);
/*
* Reset the completion event for the next command.
* Note: the event is different from the one used for waiting in this update.
*/
reset_event_status = ResetEvent(threadpool->completion_event[event_index]);
assert(reset_event_status != FALSE);
/* Make changes by other threads visible to this thread */
pthreadpool_fence_acquire();
/* Unprotect the global threadpool structures */
const BOOL release_mutex_status = ReleaseMutex(threadpool->execution_mutex);
assert(release_mutex_status != FALSE);
}
void pthreadpool_destroy(struct pthreadpool* threadpool) {
if (threadpool != NULL) {
const size_t threads_count = threadpool->threads_count.value;
if (threads_count > 1) {
pthreadpool_store_relaxed_size_t(&threadpool->active_threads, threads_count - 1 /* caller thread */);
/*
* Store the command with release semantics to guarantee that if a worker thread observes
* the new command value, it also observes the updated active_threads values.
*/
const uint32_t old_command = pthreadpool_load_relaxed_uint32_t(&threadpool->command);
pthreadpool_store_release_uint32_t(&threadpool->command, threadpool_command_shutdown);
/*
* Signal the event to wake up the threads.
* Event in use must be switched after every submitted command to avoid race conditions.
* Choose the event based on the high bit of the command, which is flipped on every update.
*/
const uint32_t event_index = (old_command >> 31);
const BOOL set_event_status = SetEvent(threadpool->command_event[event_index]);
assert(set_event_status != FALSE);
/* Wait until all threads return */
for (size_t tid = 1; tid < threads_count; tid++) {
const HANDLE thread_handle = threadpool->threads[tid].thread_handle;
if (thread_handle != NULL) {
const DWORD wait_status = WaitForSingleObject(thread_handle, INFINITE);
assert(wait_status == WAIT_OBJECT_0);
const BOOL close_status = CloseHandle(thread_handle);
assert(close_status != FALSE);
}
}
/* Release resources */
if (threadpool->execution_mutex != NULL) {
const BOOL close_status = CloseHandle(threadpool->execution_mutex);
assert(close_status != FALSE);
}
for (size_t i = 0; i < 2; i++) {
if (threadpool->command_event[i] != NULL) {
const BOOL close_status = CloseHandle(threadpool->command_event[i]);
assert(close_status != FALSE);
}
if (threadpool->completion_event[i] != NULL) {
const BOOL close_status = CloseHandle(threadpool->completion_event[i]);
assert(close_status != FALSE);
}
}
}
pthreadpool_deallocate(threadpool);
}
}
|