Merge remote-tracking branch 'origin/tpool-nospinlock' into integration_2025_w03 (!3193)

Remove spinlock from threadpool task return

The idea to use atomic aligned to cache line for counting finished jobs
is reused, but instead of polling a semaphore was added.
This commit is contained in:
Robert Schmidt
2025-01-15 15:04:37 +01:00
13 changed files with 137 additions and 100 deletions
+33 -5
View File
@@ -21,7 +21,7 @@
#ifndef TASK_ANSWER_THREAD_POOL_H
#define TASK_ANSWER_THREAD_POOL_H
#include "pthread_utils.h"
#ifdef __cplusplus
extern "C" {
#endif
@@ -39,6 +39,7 @@ extern "C" {
#include <stddef.h>
#include <stdint.h>
#include <semaphore.h>
#if defined(__i386__) || defined(__x86_64__)
#define LEVEL1_DCACHE_LINESIZE 64
@@ -52,9 +53,17 @@ extern "C" {
#error Unknown CPU architecture
#endif
/** @brief
* A multi-producer - single-consumer synchronization mechanism built for efficiency under
* contention.
*
* @param sem semaphore to wait on
* @param counter atomic counter to keep track of the number of tasks completed. Atomic counter
* is used for efficiency under contention.
*/
typedef struct {
// Avoid false sharing
_Alignas(LEVEL1_DCACHE_LINESIZE) _Atomic(int) status;
sem_t sem;
_Alignas(LEVEL1_DCACHE_LINESIZE) _Atomic(int) counter;
} task_ans_t;
typedef struct {
@@ -64,10 +73,29 @@ typedef struct {
task_ans_t* ans;
} thread_info_tm_t;
/// @brief Initialize a task_ans_t struct
///
/// @param ans task_ans_t struct
/// @param num_jobs number of tasks to wait for
void init_task_ans(task_ans_t* ans, unsigned int num_jobs);
void join_task_ans(task_ans_t* arr, size_t len);
/// @brief Wait for all tasks to complete
/// @param ans task_ans_t struct
void join_task_ans(task_ans_t* arr);
void completed_task_ans(task_ans_t* task);
/// @brief Mark a number of tasks as completed.
///
/// @param ans task_ans_t struct
/// @param num_completed_jobs number of tasks to mark as completed
void completed_many_task_ans(task_ans_t* ans, uint num_completed_jobs);
/// @brief Mark 1 tasks as completed.
///
/// @param ans task_ans_t struct
static inline void completed_task_ans(task_ans_t* ans)
{
completed_many_task_ans(ans, 1);
}
#ifdef __cplusplus
}