Optimise ASYNC_CTX handling
[openssl.git] / crypto / async / async.c
1 /* crypto/async/async.c */
2 /*
3  * Written by Matt Caswell (matt@openssl.org) for the OpenSSL project.
4  */
5 /* ====================================================================
6  * Copyright (c) 2015 The OpenSSL Project.  All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  *
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in
17  *    the documentation and/or other materials provided with the
18  *    distribution.
19  *
20  * 3. All advertising materials mentioning features or use of this
21  *    software must display the following acknowledgment:
22  *    "This product includes software developed by the OpenSSL Project
23  *    for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
24  *
25  * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
26  *    endorse or promote products derived from this software without
27  *    prior written permission. For written permission, please contact
28  *    licensing@OpenSSL.org.
29  *
30  * 5. Products derived from this software may not be called "OpenSSL"
31  *    nor may "OpenSSL" appear in their names without prior written
32  *    permission of the OpenSSL Project.
33  *
34  * 6. Redistributions of any form whatsoever must retain the following
35  *    acknowledgment:
36  *    "This product includes software developed by the OpenSSL Project
37  *    for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
38  *
39  * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
40  * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
41  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
42  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
43  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
44  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
45  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
46  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
48  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
49  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
50  * OF THE POSSIBILITY OF SUCH DAMAGE.
51  * ====================================================================
52  */
53
54 #include <openssl/async.h>
55 #include <string.h>
56 #include "async_locl.h"
57
58 #define ASYNC_JOB_RUNNING   0
59 #define ASYNC_JOB_PAUSING   1
60 #define ASYNC_JOB_PAUSED    2
61 #define ASYNC_JOB_STOPPING  3
62
63 static ASYNC_CTX *ASYNC_CTX_new(void)
64 {
65     ASYNC_CTX *nctx = NULL;
66
67     if(!(nctx = OPENSSL_malloc(sizeof (ASYNC_CTX)))) {
68         /* Error here */
69         goto err;
70     }
71
72     ASYNC_FIBRE_init_dispatcher(&nctx->dispatcher);
73     nctx->currjob = NULL;
74     if(!ASYNC_set_ctx(nctx))
75         goto err;
76
77     return nctx;
78 err:
79     if(nctx) {
80         OPENSSL_free(nctx);
81     }
82
83     return NULL;
84 }
85
86 static int ASYNC_CTX_free(void)
87 {
88     if(ASYNC_get_ctx()) {
89         OPENSSL_free(ASYNC_get_ctx());
90     }
91
92     if(!ASYNC_set_ctx(NULL))
93         return 0;
94
95     return 1;
96 }
97
98 static ASYNC_JOB *ASYNC_JOB_new(void)
99 {
100     ASYNC_JOB *job = NULL;
101     int pipefds[2];
102
103     if(!(job = OPENSSL_malloc(sizeof (ASYNC_JOB)))) {
104         return NULL;
105     }
106
107     if(!async_pipe(pipefds)) {
108         OPENSSL_free(job);
109         return NULL;
110     }
111
112     job->wake_set = 0;
113     job->wait_fd = pipefds[0];
114     job->wake_fd = pipefds[1];
115
116     job->status = ASYNC_JOB_RUNNING;
117     job->funcargs = NULL;
118
119     return job;
120 }
121
122 static void ASYNC_JOB_free(ASYNC_JOB *job)
123 {
124     if(job) {
125         if(job->funcargs)
126             OPENSSL_free(job->funcargs);
127         ASYNC_FIBRE_free(&job->fibrectx);
128         OPENSSL_free(job);
129     }
130 }
131
132 static ASYNC_JOB *async_get_pool_job(void) {
133     ASYNC_JOB *job;
134     STACK_OF(ASYNC_JOB) *pool;
135
136     pool = async_get_pool();
137     if (pool == NULL) {
138         /*
139          * Pool has not been initialised, so init with the defaults, i.e.
140          * no max size and no pre-created jobs
141          */
142         if (ASYNC_init_pool(0, 0) == 0)
143             return NULL;
144         pool = async_get_pool();
145     }
146
147     job = sk_ASYNC_JOB_pop(pool);
148     if (job == NULL) {
149         /* Pool is empty */
150         if (!async_pool_can_grow())
151             return NULL;
152
153         job = ASYNC_JOB_new();
154         if (job) {
155             ASYNC_FIBRE_makecontext(&job->fibrectx);
156             async_increment_pool_size();
157         }
158     }
159     return job;
160 }
161
162 static void async_release_job(ASYNC_JOB *job) {
163     if(job->funcargs)
164         OPENSSL_free(job->funcargs);
165     job->funcargs = NULL;
166     /* Ignore error return */
167     async_release_job_to_pool(job);
168 }
169
170 void ASYNC_start_func(void)
171 {
172     ASYNC_JOB *job;
173
174     while (1) {
175         /* Run the job */
176         job = ASYNC_get_ctx()->currjob;
177         job->ret = job->func(job->funcargs);
178
179         /* Stop the job */
180         job->status = ASYNC_JOB_STOPPING;
181         if(!ASYNC_FIBRE_swapcontext(&job->fibrectx,
182                                     &ASYNC_get_ctx()->dispatcher, 1)) {
183             /*
184              * Should not happen. Getting here will close the thread...can't do much
185              * about it
186              */
187         }
188     }
189 }
190
191 int ASYNC_start_job(ASYNC_JOB **job, int *ret, int (*func)(void *),
192                          void *args, size_t size)
193 {
194     if(!ASYNC_get_ctx() && !ASYNC_CTX_new()) {
195         return ASYNC_ERR;
196     }
197
198     if(*job) {
199         ASYNC_get_ctx()->currjob = *job;
200     }
201
202     for (;;) {
203         if(ASYNC_get_ctx()->currjob) {
204             if(ASYNC_get_ctx()->currjob->status == ASYNC_JOB_STOPPING) {
205                 *ret = ASYNC_get_ctx()->currjob->ret;
206                 async_release_job(ASYNC_get_ctx()->currjob);
207                 ASYNC_get_ctx()->currjob = NULL;
208                 *job = NULL;
209                 return ASYNC_FINISH;
210             }
211
212             if(ASYNC_get_ctx()->currjob->status == ASYNC_JOB_PAUSING) {
213                 *job = ASYNC_get_ctx()->currjob;
214                 ASYNC_get_ctx()->currjob->status = ASYNC_JOB_PAUSED;
215                 ASYNC_get_ctx()->currjob = NULL;
216                 return ASYNC_PAUSE;
217             }
218
219             if(ASYNC_get_ctx()->currjob->status == ASYNC_JOB_PAUSED) {
220                 ASYNC_get_ctx()->currjob = *job;
221                 /* Resume previous job */
222                 if(!ASYNC_FIBRE_swapcontext(&ASYNC_get_ctx()->dispatcher,
223                     &ASYNC_get_ctx()->currjob->fibrectx, 1))
224                     goto err;
225                 continue;
226             }
227
228             /* Should not happen */
229             async_release_job(ASYNC_get_ctx()->currjob);
230             ASYNC_get_ctx()->currjob = NULL;
231             *job = NULL;
232             return ASYNC_ERR;
233         }
234
235         /* Start a new job */
236         if(!(ASYNC_get_ctx()->currjob = async_get_pool_job())) {
237             return ASYNC_NO_JOBS;
238         }
239
240         if(args != NULL) {
241             ASYNC_get_ctx()->currjob->funcargs = OPENSSL_malloc(size);
242             if(!ASYNC_get_ctx()->currjob->funcargs) {
243                 async_release_job(ASYNC_get_ctx()->currjob);
244                 ASYNC_get_ctx()->currjob = NULL;
245                 return ASYNC_ERR;
246             }
247             memcpy(ASYNC_get_ctx()->currjob->funcargs, args, size);
248         } else {
249             ASYNC_get_ctx()->currjob->funcargs = NULL;
250         }
251
252         ASYNC_get_ctx()->currjob->func = func;
253         if(!ASYNC_FIBRE_swapcontext(&ASYNC_get_ctx()->dispatcher,
254             &ASYNC_get_ctx()->currjob->fibrectx, 1))
255             goto err;
256     }
257
258 err:
259     async_release_job(ASYNC_get_ctx()->currjob);
260     ASYNC_get_ctx()->currjob = NULL;
261     *job = NULL;
262     return ASYNC_ERR;
263 }
264
265
266 int ASYNC_pause_job(void)
267 {
268     ASYNC_JOB *job;
269
270     if(!ASYNC_get_ctx() || !ASYNC_get_ctx()->currjob)
271         return 0;
272
273     job = ASYNC_get_ctx()->currjob;
274     job->status = ASYNC_JOB_PAUSING;
275
276     if(!ASYNC_FIBRE_swapcontext(&job->fibrectx,
277                                &ASYNC_get_ctx()->dispatcher, 1)) {
278         /* Error */
279         return 0;
280     }
281
282     return 1;
283 }
284
285 static void async_empty_pool(STACK_OF(ASYNC_JOB) *pool)
286 {
287     ASYNC_JOB *job;
288
289     do {
290         job = sk_ASYNC_JOB_pop(pool);
291         ASYNC_JOB_free(job);
292     } while (job);
293 }
294
295 int ASYNC_init_pool(size_t max_size, size_t init_size)
296 {
297     STACK_OF(ASYNC_JOB) *pool;
298     size_t curr_size = 0;
299
300     if (init_size > max_size)
301         return 0;
302
303     pool = sk_ASYNC_JOB_new_null();
304     if (pool == NULL) {
305         return 0;
306     }
307     /* Pre-create jobs as required */
308     while (init_size) {
309         ASYNC_JOB *job;
310         job = ASYNC_JOB_new();
311         if (job) {
312             ASYNC_FIBRE_makecontext(&job->fibrectx);
313             job->funcargs = NULL;
314             sk_ASYNC_JOB_push(pool, job);
315             curr_size++;
316             init_size--;
317         } else {
318             /*
319              * Not actually fatal because we already created the pool, just skip
320              * creation of any more jobs
321              */
322             init_size = 0;
323         }
324     }
325
326     if (!async_set_pool(pool, curr_size, max_size)) {
327         async_empty_pool(pool);
328         sk_ASYNC_JOB_free(pool);
329         return 0;
330     }
331
332     return 1;
333 }
334
335 void ASYNC_free_pool(void)
336 {
337     STACK_OF(ASYNC_JOB) *pool;
338
339     pool = async_get_pool();
340     if (pool == NULL)
341         return;
342
343     async_empty_pool(pool);
344     async_release_pool();
345     ASYNC_CTX_free();
346 }
347
348 ASYNC_JOB *ASYNC_get_current_job(void)
349 {
350     ASYNC_CTX *ctx;
351     if((ctx = ASYNC_get_ctx()) == NULL)
352         return NULL;
353
354     return ctx->currjob;
355 }
356
357 int ASYNC_get_wait_fd(ASYNC_JOB *job)
358 {
359     return job->wait_fd;
360 }
361
362 void ASYNC_wake(ASYNC_JOB *job)
363 {
364     char dummy = 0;
365
366     if (job->wake_set)
367         return;
368     async_write1(job->wake_fd, &dummy);
369     job->wake_set = 1;
370 }
371
372 void ASYNC_clear_wake(ASYNC_JOB *job)
373 {
374     char dummy = 0;
375     if (!job->wake_set)
376         return;
377     async_read1(job->wait_fd, &dummy);
378     job->wake_set = 0;
379 }