blob: 86eade856a28f738517dd8055bf025877fca6156 (
plain)
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
|
/* SPDX-License-Identifier: LGPL-3.0-or-later */
/*
* threadpool_serial.c
*
* Copyright (C) 2021 David Oberhollenzer <goliath@infraroot.at>
*/
#include "threadpool.h"
#include "util.h"
#include <stdlib.h>
#include <string.h>
typedef struct work_item_t {
struct work_item_t *next;
void *data;
} work_item_t;
typedef struct {
thread_pool_t base;
work_item_t *queue;
work_item_t *queue_last;
work_item_t *recycle;
thread_pool_worker_t fun;
void *user;
int status;
} thread_pool_impl_t;
static void destroy(thread_pool_t *interface)
{
thread_pool_impl_t *pool = (thread_pool_impl_t *)interface;
while (pool->queue != NULL) {
work_item_t *item = pool->queue;
pool->queue = item->next;
free(item);
}
while (pool->recycle != NULL) {
work_item_t *item = pool->recycle;
pool->recycle = item->next;
free(item);
}
free(pool);
}
static size_t get_worker_count(thread_pool_t *pool)
{
(void)pool;
return 1;
}
static void set_worker_ptr(thread_pool_t *interface, size_t idx, void *ptr)
{
thread_pool_impl_t *pool = (thread_pool_impl_t *)interface;
if (idx >= 1)
return;
pool->user = ptr;
}
static int submit(thread_pool_t *interface, void *ptr)
{
thread_pool_impl_t *pool = (thread_pool_impl_t *)interface;
work_item_t *item = NULL;
if (pool->status != 0)
return pool->status;
if (pool->recycle != NULL) {
item = pool->recycle;
pool->recycle = item->next;
item->next = NULL;
}
if (item == NULL) {
item = calloc(1, sizeof(*item));
if (item == NULL)
return -1;
}
item->data = ptr;
if (pool->queue_last == NULL) {
pool->queue = item;
} else {
pool->queue_last->next = item;
}
pool->queue_last = item;
return 0;
}
static void *dequeue(thread_pool_t *interface)
{
thread_pool_impl_t *pool = (thread_pool_impl_t *)interface;
work_item_t *item;
void *ptr;
int ret;
if (pool->queue == NULL)
return NULL;
item = pool->queue;
pool->queue = item->next;
if (pool->queue == NULL)
pool->queue_last = NULL;
ptr = item->data;
memset(item, 0, sizeof(*item));
item->next = pool->recycle;
pool->recycle = item;
ret = pool->fun(pool->user, ptr);
if (ret != 0 && pool->status == 0)
pool->status = ret;
return ptr;
}
static int get_status(thread_pool_t *interface)
{
thread_pool_impl_t *pool = (thread_pool_impl_t *)interface;
return pool->status;
}
thread_pool_t *thread_pool_create_serial(thread_pool_worker_t worker)
{
thread_pool_impl_t *pool = calloc(1, sizeof(*pool));
thread_pool_t *interface = (thread_pool_t *)pool;
if (pool == NULL)
return NULL;
pool->fun = worker;
interface = (thread_pool_t *)pool;
interface->destroy = destroy;
interface->get_worker_count = get_worker_count;
interface->set_worker_ptr = set_worker_ptr;
interface->submit = submit;
interface->dequeue = dequeue;
interface->get_status = get_status;
return interface;
}
#ifdef NO_THREAD_IMPL
thread_pool_t *thread_pool_create(size_t num_jobs, thread_pool_worker_t worker)
{
(void)num_jobs;
return thread_pool_create_serial(worker);
}
#endif
|