Skip to content

Commit fd4bb7e

Browse files
authored
Merge pull request #6132 from mattip/windows-threading
fix thread queue allocation on windows
2 parents 0fee0eb + 0815a5b commit fd4bb7e

2 files changed

Lines changed: 23 additions & 6 deletions

File tree

‎common_thread.h‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,14 @@ typedef struct blas_queue {
119119
struct blas_queue *next;
120120

121121
#if defined( __WIN32__) || defined(__CYGWIN32__) || defined(_WIN32) || defined(__CYGWIN__)
122+
/*
123+
* Link used exclusively by the Windows thread pool to chain the jobs that
124+
* are waiting to be picked up. It is deliberately kept separate from
125+
* "next" above, which is owned (and rebuilt on every call) by whoever
126+
* submitted the job: appending to the pool must never write into the
127+
* queue nodes of another caller.
128+
*/
129+
struct blas_queue *pool_next;
122130
CRITICAL_SECTION lock;
123131
HANDLE finish;
124132
volatile int finished;

‎driver/others/blas_server_win32.c‎

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ typedef struct{
5757
HANDLE killed;
5858

5959
blas_queue_t *queue; /* Parameter Pointer */
60+
blas_queue_t *tail; /* Last job waiting in "queue", for O(1) append */
6061
int shutdown; /* server shutdown flag */
6162

6263
} blas_pool_t;
@@ -242,7 +243,10 @@ static DWORD WINAPI blas_thread_server(void *arg){
242243
EnterCriticalSection(&pool.lock);
243244

244245
queue = pool.queue;
245-
if (queue) pool.queue = queue->next;
246+
if (queue) {
247+
pool.queue = queue->pool_next;
248+
if (pool.queue == NULL) pool.tail = NULL;
249+
}
246250

247251
LeaveCriticalSection(&pool.lock);
248252

@@ -380,6 +384,7 @@ int blas_thread_init(void){
380384

381385
pool.shutdown = 0;
382386
pool.queue = NULL;
387+
pool.tail = NULL;
383388

384389
for(i = 0; i < blas_cpu_number - 1; i++){
385390
blas_threads[i] = CreateThread(NULL, 0,
@@ -411,7 +416,7 @@ int exec_blas_async(BLASLONG pos, blas_queue_t *queue){
411416
if (unlikely(blas_server_avail == 0)) blas_thread_init();
412417
#endif
413418

414-
blas_queue_t *current;
419+
blas_queue_t *current, *last = NULL;
415420

416421
current = queue;
417422

@@ -425,19 +430,22 @@ int exec_blas_async(BLASLONG pos, blas_queue_t *queue){
425430
__asm__ __volatile__ ("stmxcsr %0" : "=m" (current -> sse_mode));
426431
#endif
427432

433+
/* Build the pool's own chain over this caller's jobs */
434+
current -> pool_next = current -> next;
435+
last = current;
436+
428437
current = current -> next;
429438
pos ++;
430439
}
431440

432441
EnterCriticalSection(&pool.lock);
433442

434-
if (pool.queue) {
435-
current = pool.queue;
436-
while (current -> next) current = current -> next;
437-
current -> next = queue;
443+
if (pool.tail) {
444+
pool.tail -> pool_next = queue;
438445
} else {
439446
pool.queue = queue;
440447
}
448+
pool.tail = last;
441449

442450
LeaveCriticalSection(&pool.lock);
443451

@@ -574,6 +582,7 @@ void goto_set_num_threads(int num_threads)
574582

575583
pool.shutdown = 0;
576584
pool.queue = NULL;
585+
pool.tail = NULL;
577586
blas_server_avail = 1;
578587
}
579588

0 commit comments

Comments
 (0)