1 /* crypto/async/async.c */
3 * Written by Matt Caswell (matt@openssl.org) for the OpenSSL project.
5 /* ====================================================================
6 * Copyright (c) 2015 The OpenSSL Project. All rights reserved.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
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
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/)"
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.
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.
34 * 6. Redistributions of any form whatsoever must retain the following
36 * "This product includes software developed by the OpenSSL Project
37 * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
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 * ====================================================================
55 * Without this we start getting longjmp crashes because it thinks we're jumping
56 * up the stack when in fact we are jumping to an entirely different stack. The
57 * cost of this is not having certain buffer overrun/underrun checks etc for
58 * this source file :-(
60 #undef _FORTIFY_SOURCE
62 #include <openssl/err.h>
63 #include <openssl/async.h>
65 #include "async_locl.h"
67 #define ASYNC_JOB_RUNNING 0
68 #define ASYNC_JOB_PAUSING 1
69 #define ASYNC_JOB_PAUSED 2
70 #define ASYNC_JOB_STOPPING 3
72 static async_ctx *async_ctx_new(void)
74 async_ctx *nctx = NULL;
76 if(!(nctx = OPENSSL_malloc(sizeof (async_ctx)))) {
77 ASYNCerr(ASYNC_F_ASYNC_CTX_NEW, ERR_R_MALLOC_FAILURE);
81 async_fibre_init_dispatcher(&nctx->dispatcher);
83 if(!async_set_ctx(nctx))
95 static int async_ctx_free(void)
98 OPENSSL_free(async_get_ctx());
101 if(!async_set_ctx(NULL))
107 static ASYNC_JOB *async_job_new(void)
109 ASYNC_JOB *job = NULL;
112 if(!(job = OPENSSL_malloc(sizeof (ASYNC_JOB)))) {
113 ASYNCerr(ASYNC_F_ASYNC_JOB_NEW, ERR_R_MALLOC_FAILURE);
117 if(!async_pipe(pipefds)) {
119 ASYNCerr(ASYNC_F_ASYNC_JOB_NEW, ASYNC_R_CANNOT_CREATE_WAIT_PIPE);
124 job->wait_fd = pipefds[0];
125 job->wake_fd = pipefds[1];
127 job->status = ASYNC_JOB_RUNNING;
128 job->funcargs = NULL;
133 static void async_job_free(ASYNC_JOB *job)
137 OPENSSL_free(job->funcargs);
138 async_fibre_free(&job->fibrectx);
143 static ASYNC_JOB *async_get_pool_job(void) {
145 STACK_OF(ASYNC_JOB) *pool;
147 pool = async_get_pool();
150 * Pool has not been initialised, so init with the defaults, i.e.
151 * no max size and no pre-created jobs
153 if (ASYNC_init_pool(0, 0) == 0)
155 pool = async_get_pool();
158 job = sk_ASYNC_JOB_pop(pool);
161 if (!async_pool_can_grow())
164 job = async_job_new();
166 async_fibre_makecontext(&job->fibrectx);
167 async_increment_pool_size();
173 static void async_release_job(ASYNC_JOB *job) {
175 OPENSSL_free(job->funcargs);
176 job->funcargs = NULL;
177 /* Ignore error return */
178 async_release_job_to_pool(job);
181 void async_start_func(void)
187 job = async_get_ctx()->currjob;
188 job->ret = job->func(job->funcargs);
191 job->status = ASYNC_JOB_STOPPING;
192 if(!async_fibre_swapcontext(&job->fibrectx,
193 &async_get_ctx()->dispatcher, 1)) {
195 * Should not happen. Getting here will close the thread...can't do
198 ASYNCerr(ASYNC_F_ASYNC_START_FUNC, ASYNC_R_FAILED_TO_SWAP_CONTEXT);
203 int ASYNC_start_job(ASYNC_JOB **job, int *ret, int (*func)(void *),
204 void *args, size_t size)
206 if(!async_get_ctx() && !async_ctx_new()) {
211 async_get_ctx()->currjob = *job;
215 if(async_get_ctx()->currjob) {
216 if(async_get_ctx()->currjob->status == ASYNC_JOB_STOPPING) {
217 *ret = async_get_ctx()->currjob->ret;
218 async_release_job(async_get_ctx()->currjob);
219 async_get_ctx()->currjob = NULL;
224 if(async_get_ctx()->currjob->status == ASYNC_JOB_PAUSING) {
225 *job = async_get_ctx()->currjob;
226 async_get_ctx()->currjob->status = ASYNC_JOB_PAUSED;
227 async_get_ctx()->currjob = NULL;
231 if(async_get_ctx()->currjob->status == ASYNC_JOB_PAUSED) {
232 async_get_ctx()->currjob = *job;
233 /* Resume previous job */
234 if(!async_fibre_swapcontext(&async_get_ctx()->dispatcher,
235 &async_get_ctx()->currjob->fibrectx, 1)) {
236 ASYNCerr(ASYNC_F_ASYNC_START_JOB,
237 ASYNC_R_FAILED_TO_SWAP_CONTEXT);
243 /* Should not happen */
244 ASYNCerr(ASYNC_F_ASYNC_START_JOB, ERR_R_INTERNAL_ERROR);
245 async_release_job(async_get_ctx()->currjob);
246 async_get_ctx()->currjob = NULL;
251 /* Start a new job */
252 if(!(async_get_ctx()->currjob = async_get_pool_job())) {
253 return ASYNC_NO_JOBS;
257 async_get_ctx()->currjob->funcargs = OPENSSL_malloc(size);
258 if(!async_get_ctx()->currjob->funcargs) {
259 ASYNCerr(ASYNC_F_ASYNC_START_JOB, ERR_R_MALLOC_FAILURE);
260 async_release_job(async_get_ctx()->currjob);
261 async_get_ctx()->currjob = NULL;
264 memcpy(async_get_ctx()->currjob->funcargs, args, size);
266 async_get_ctx()->currjob->funcargs = NULL;
269 async_get_ctx()->currjob->func = func;
270 if(!async_fibre_swapcontext(&async_get_ctx()->dispatcher,
271 &async_get_ctx()->currjob->fibrectx, 1)) {
272 ASYNCerr(ASYNC_F_ASYNC_START_JOB, ASYNC_R_FAILED_TO_SWAP_CONTEXT);
278 async_release_job(async_get_ctx()->currjob);
279 async_get_ctx()->currjob = NULL;
285 int ASYNC_pause_job(void)
289 if(!async_get_ctx() || !async_get_ctx()->currjob) {
291 * Could be we've deliberately not been started within a job so this is
292 * counted as success.
297 job = async_get_ctx()->currjob;
298 job->status = ASYNC_JOB_PAUSING;
300 if(!async_fibre_swapcontext(&job->fibrectx,
301 &async_get_ctx()->dispatcher, 1)) {
302 ASYNCerr(ASYNC_F_ASYNC_PAUSE_JOB, ASYNC_R_FAILED_TO_SWAP_CONTEXT);
309 static void async_empty_pool(STACK_OF(ASYNC_JOB) *pool)
314 job = sk_ASYNC_JOB_pop(pool);
319 int ASYNC_init_pool(size_t max_size, size_t init_size)
321 STACK_OF(ASYNC_JOB) *pool;
322 size_t curr_size = 0;
324 if (init_size > max_size) {
325 ASYNCerr(ASYNC_F_ASYNC_INIT_POOL, ASYNC_R_INVALID_POOL_SIZE);
329 pool = sk_ASYNC_JOB_new_null();
331 ASYNCerr(ASYNC_F_ASYNC_INIT_POOL, ERR_R_MALLOC_FAILURE);
334 /* Pre-create jobs as required */
337 job = async_job_new();
339 async_fibre_makecontext(&job->fibrectx);
340 job->funcargs = NULL;
341 sk_ASYNC_JOB_push(pool, job);
346 * Not actually fatal because we already created the pool, just skip
347 * creation of any more jobs
353 if (!async_set_pool(pool, curr_size, max_size)) {
354 ASYNCerr(ASYNC_F_ASYNC_INIT_POOL, ASYNC_R_FAILED_TO_SET_POOL);
355 async_empty_pool(pool);
356 sk_ASYNC_JOB_free(pool);
363 void ASYNC_free_pool(void)
365 STACK_OF(ASYNC_JOB) *pool;
367 pool = async_get_pool();
371 async_empty_pool(pool);
372 async_release_pool();
376 ASYNC_JOB *ASYNC_get_current_job(void)
379 if((ctx = async_get_ctx()) == NULL)
385 int ASYNC_get_wait_fd(ASYNC_JOB *job)
390 void ASYNC_wake(ASYNC_JOB *job)
396 async_write1(job->wake_fd, &dummy);
400 void ASYNC_clear_wake(ASYNC_JOB *job)
405 async_read1(job->wait_fd, &dummy);