Coverage Report

Created: 2026-11-23 00:10

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/hdf5/src/H5TSpool.h
Line
Count
Source
1
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
2
 * Copyright by The HDF Group.                                               *
3
 * All rights reserved.                                                      *
4
 *                                                                           *
5
 * This file is part of HDF5.  The full HDF5 copyright notice, including     *
6
 * terms governing use, modification, and redistribution, is contained in    *
7
 * the LICENSE file, which can be found at the root of the source code       *
8
 * distribution tree, or in https://www.hdfgroup.org/licenses.               *
9
 * If you do not have access to either file, you may request a copy from     *
10
 * help@hdfgroup.org.                                                        *
11
 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
12
13
/*
14
 * Purpose: This file contains an implementation of a simple thread-pool,
15
 *        using the H5TS package's portable objects and operations.
16
 *
17
 * Note:  Because this threadsafety framework operates outside the library,
18
 *        it does not use the error stack (although it does use error macros
19
 *        that don't push errors on a stack) and only uses the "namecheck only"
20
 *        FUNC_ENTER_* / FUNC_LEAVE_* macros.
21
 */
22
23
/****************/
24
/* Module Setup */
25
/****************/
26
27
/***********/
28
/* Headers */
29
/***********/
30
31
/****************/
32
/* Local Macros */
33
/****************/
34
35
/******************/
36
/* Local Typedefs */
37
/******************/
38
39
/* Thread pool task */
40
typedef struct H5TS_pool_task_t {
41
    H5TS_thread_start_func_t func; /* Function to invoke with thread */
42
    void                    *ctx;  /* Context for task's function */
43
    struct H5TS_pool_task_t *next; /* Pointer to next task */
44
} H5TS_pool_task_t;
45
46
/* Thread pool */
47
struct H5TS_pool_t {
48
    /* Semaphore */
49
    H5TS_semaphore_t sem;
50
51
    /* Task queue */
52
    H5TS_mutex_t      queue_mutex; /* Mutex to control access to task queue */
53
    bool              shutdown;    /* Pool is shutting down */
54
    H5TS_pool_task_t *head, *tail; /* Task queue */
55
56
    /* Threads */
57
    unsigned       num_threads; /* # of threads in pool */
58
    H5TS_thread_t *threads;     /* Array of worker threads in pool */
59
};
60
61
/********************/
62
/* Local Prototypes */
63
/********************/
64
65
/*********************/
66
/* Package Variables */
67
/*********************/
68
69
/*****************************/
70
/* Library Private Variables */
71
/*****************************/
72
73
/*******************/
74
/* Local Variables */
75
/*******************/
76
77
/*--------------------------------------------------------------------------
78
 * Function:    H5TS_pool_add_task
79
 *
80
 * Purpose:     Add a new task to a pool
81
 *
82
 * Return:      Non-negative on success / Negative on failure
83
 *
84
 *--------------------------------------------------------------------------
85
 */
86
static inline herr_t
87
H5TS_pool_add_task(H5TS_pool_t *pool, H5TS_thread_start_func_t func, void *ctx)
88
0
{
89
0
    H5TS_pool_task_t *task             = NULL;  /* Task for function to invoke */
90
0
    bool              have_queue_mutex = false; /* Whether we're holding the task queue mutex */
91
0
    herr_t            ret_value        = SUCCEED;
92
0
93
0
    /* Sanity checks */
94
0
    if (H5_UNLIKELY(NULL == pool || NULL == func))
95
0
        return FAIL;
96
0
97
0
    /* Allocate & initialize new task */
98
0
    if (H5_UNLIKELY(NULL == (task = (H5TS_pool_task_t *)malloc(sizeof(H5TS_pool_task_t)))))
99
0
        return FAIL;
100
0
    task->func = func;
101
0
    task->ctx  = ctx;
102
0
    task->next = NULL;
103
0
104
0
    /* Acquire the mutex for the task queue */
105
0
    if (H5_UNLIKELY(H5TS_mutex_lock(&pool->queue_mutex) < 0)) {
106
0
        ret_value = FAIL;
107
0
        goto done;
108
0
    }
109
0
    have_queue_mutex = true;
110
0
111
0
    /* Is pool shutting down? */
112
0
    if (H5_UNLIKELY(pool->shutdown)) {
113
0
        ret_value = FAIL;
114
0
        goto done;
115
0
    }
116
0
117
0
    /* Add task to pool's queue */
118
0
    if (NULL != pool->tail) {
119
0
        pool->tail->next = task;
120
0
        pool->tail       = task;
121
0
    }
122
0
    else
123
0
        pool->head = pool->tail = task;
124
0
125
0
    /* Avoid freeing the task on error, now */
126
0
    task = NULL;
127
0
128
0
    /* Release the task queue's mutex, if we're holding it */
129
0
    if (H5_UNLIKELY(H5TS_mutex_unlock(&pool->queue_mutex) < 0)) {
130
0
        ret_value = FAIL;
131
0
        goto done;
132
0
    }
133
0
    have_queue_mutex = false;
134
0
135
0
    /* Signal the semaphore */
136
0
    if (H5_UNLIKELY(H5TS_semaphore_signal(&pool->sem) < 0))
137
0
        ret_value = FAIL;
138
0
139
0
done:
140
0
    /* Free the task, on failure */
141
0
    if (H5_UNLIKELY(ret_value < 0)) {
142
0
        /* Release the task queue's mutex, if we're still holding it */
143
0
        /* (Can only happen on failure) */
144
0
        if (H5_UNLIKELY(have_queue_mutex))
145
0
            H5TS_mutex_unlock(&pool->queue_mutex);
146
0
        if (task)
147
0
            free(task);
148
0
    }
149
0
150
0
    return (ret_value);
151
0
} /* end H5TS_pool_add_task() */
Unexecuted instantiation: H5.c:H5TS_pool_add_task
Unexecuted instantiation: H5Eint.c:H5TS_pool_add_task