-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfuture.c
280 lines (224 loc) · 7.81 KB
/
future.c
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
#include <pthread.h>
#include "future.h"
#include <stdlib.h>
#include <unistd.h>
typedef void *(*function_t)(void *, size_t, size_t *);
int future_init(future_t *future);
void future_set(future_t *future, void *result, size_t resultSz);
void *future_get(future_t *future);
void future_destroy(future_t *future);
typedef struct wrap {
callable_t callable;
future_t *future;
runnable_t *runnable;
} wrap_t;
typedef struct map_wrap {
future_t *future_from;
function_t func;
future_t *new_future;
runnable_t *runnable;
} map_wrap_t;
/**
* The function is used as a runnable function in map function
* to create a new value for a future from another future.
* @param arg - argument of the runnable;
* @param argsz - size of the argument of runnable.
*/
void map_runnable(void *arg, size_t argsz __attribute__ ((unused))) {
map_wrap_t *wrapper = arg;
future_t *fut = wrapper->future_from;
/* Thread may stop here to wait until future_value is done. */
void *futResult = future_get(fut);
/* When future_value is done its size is also set. */
size_t futResultSz = fut->resultSz;
function_t func = wrapper->func;
size_t resultSz = 0;
void *result = func(futResult, futResultSz, &resultSz);
future_set(wrapper->new_future, result, resultSz);
/* Destroys the mutex and condition in the future but not the result of it. */
future_destroy(fut);
free(wrapper->runnable);
free(wrapper);
}
/**
* The function is used as a runnable function for async function
* to wrap callable into runnable function and submit runnable task
* instead of callable task to the thread_pool.
* @param arg - argument of the runnable;
* @param argsz - size of the argument of the runnable.
*/
void runnable_function(void *arg, size_t argsz __attribute__ ((unused))) {
wrap_t *wrapper = arg;
size_t result_size = 0;
void *(*callFunc)(void *, size_t, size_t *) = wrapper->callable.function;
void *result = callFunc(wrapper->callable.arg, wrapper->callable.argsz, &result_size);
future_set(wrapper->future, result, result_size);
free(wrapper->runnable);
free(wrapper);
}
/**
* Function creates a new runnable out of a wrapper struct.
* @param wrapper - pointer to a wrapper struct.
* @return - returns a pointer to a new runnable, otherwise null.
*/
runnable_t *callable_to_runnable(wrap_t *wrapper) {
runnable_t *new_runnable = malloc(sizeof(runnable_t));
if (new_runnable == NULL) {
err("callable_to_runnable(): malloc failed for runnable.\n");
return NULL;
}
new_runnable->function = runnable_function;
new_runnable->arg = wrapper;
new_runnable->argsz = sizeof(wrap_t);
return new_runnable;
}
/**
* Submits new task/callable to thread_pool jobqueue.
* Result is an initialised future with the result and result size.
* @param pool - pointer on the thread_pool
* @param future - pointer on the future which carries the result of the callable task.
* @param callable - callable task to be submitted to thread_pool.
* @return 0 on success, otherwise -1 if some failures happened.
*/
int async(thread_pool_t *pool, future_t *future, callable_t callable) {
if (future_init(future) == -1) {
err("async(): future_init() failed as future is NULL.\n");
return -1;
}
wrap_t *wrapper = malloc(sizeof(wrap_t));
if (wrapper == NULL) {
err("async(): malloc failed for creating wrapper.\n");
return -1;
}
wrapper->callable = callable;
wrapper->future = future;
wrapper->runnable = NULL;
runnable_t *my_runnable = callable_to_runnable(wrapper);
if (my_runnable == NULL) {
err("async(): malloc failed for creating new runnable.\n");
return -1;
}
wrapper->runnable = my_runnable;
if (defer(pool, *my_runnable) != 0) {
err("async(): Submitting new callable task failed.\n");
free(wrapper);
free(my_runnable);
return -1;
}
return 0;
}
/**
* Function uses 'from' future and 'function' to map a new future.
* Multiple maps on the same future is an undefined behaviour, as
* after mapping future 'from' is destroyed.
* @param pool - pointer on thread_pool
* @param future - pointer on future to be mapped
* @param from - pointer on base future to map another future
* @param function - function pointer to map the new future
* @return 0 on success, otherwise -1 if some failures happened.
*/
int map(thread_pool_t *pool, future_t *future, future_t *from,
void *(*function)(void *, size_t, size_t *)) {
if (future_init(future) == -1) {
err("map(): future_init() failed as future is NULL.\n");
return -1;
}
map_wrap_t *wrapper = malloc(sizeof(map_wrap_t));
if (wrapper == NULL) {
err("map(): malloc failed for creating map_wrapper.\n");
return -1;
}
wrapper->func = function;
wrapper->future_from = from;
wrapper->new_future = future;
wrapper->runnable = NULL;
runnable_t *my_runnable = malloc(sizeof(runnable_t));
if (my_runnable == NULL) {
err("map(): malloc failed for creating runnable.\n");
return -1;
}
my_runnable->function = map_runnable;
my_runnable->arg = wrapper;
my_runnable->argsz = sizeof(map_wrap_t);
wrapper->runnable = my_runnable;
if (defer(pool, *my_runnable) != 0) {
err("map(): Submitting new task failed.\n");
free(wrapper);
free(my_runnable);
return -1;
}
return 0;
}
/**
* Blocking function. Calling thread waits until future result
* will be ready to take. After await, future is destroyed, so
* multiple awaits on the same future is undefined behaviour.
* @param future - pointer to the future
* @return pointer to the result of future, which may also be NULL.
*/
void *await(future_t *future) {
void *res = future_get(future);
future_destroy(future);
return res;
}
/**
* Initializes future parameters.
* @param future - pointer to a future.
* @return - returns 0 on success, otherwise -1;
*/
int future_init(future_t *future) {
if (future == NULL) {
err("future_init(): future is a null pointer.\n");
return -1;
}
future->result = NULL;
future->resultSz = 0;
future->done = false;
if (pthread_mutex_init(&future->mutex, NULL) != 0) {
err("future_init(): mutex initialisation failed.\n");
return -1;
}
if (pthread_cond_init(&future->cond, NULL) != 0) {
err("future_init(): condition initialisation failed.\n");
return -1;
}
return 0;
}
/**
* Sets the result and result size of the future.
* @param future - pointer on the future.
* @param result - pointer on the result.
* @param resultSz - size of the result of the future.
*/
void future_set(future_t *future, void *result, size_t resultSz) {
pthread_mutex_lock(&future->mutex);
future->result = result;
future->resultSz = resultSz;
future->done = true;
/* Broadcast as other futures may wait to be created out of this future. */
pthread_cond_broadcast(&future->cond);
pthread_mutex_unlock(&future->mutex);
}
/**
* Returns the future value.
* Function is a blocking function as future may be not ready to get its result.
* @param future - pointer on the future.
* @return returns future value as it gets ready.
*/
void *future_get(future_t *future) {
pthread_mutex_lock(&future->mutex);
while (!future->done) {
pthread_cond_wait(&future->cond, &future->mutex);
}
pthread_mutex_unlock(&future->mutex);
return future->result;
}
/**
* Destroys the future by destroying its mutex and condition.
* Note that result cannot be free-d as it is the responsibility of the user.
* @param future - pointer on the future.
*/
void future_destroy(future_t *future) {
pthread_mutex_destroy(&future->mutex);
pthread_cond_destroy(&future->cond);
}