]>
git.ipfire.org Git - thirdparty/openssl.git/blob - crypto/async/async.c
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 * ====================================================================
54 #include <openssl/async.h>
56 #include "async_locl.h"
58 #define ASYNC_JOB_RUNNING 0
59 #define ASYNC_JOB_PAUSING 1
60 #define ASYNC_JOB_PAUSED 2
61 #define ASYNC_JOB_STOPPING 3
63 static async_ctx
*async_ctx_new(void)
65 async_ctx
*nctx
= NULL
;
67 if(!(nctx
= OPENSSL_malloc(sizeof (async_ctx
)))) {
72 async_fibre_init_dispatcher(&nctx
->dispatcher
);
74 if(!async_set_ctx(nctx
))
86 static int async_ctx_free(void)
89 OPENSSL_free(async_get_ctx());
92 if(!async_set_ctx(NULL
))
98 static ASYNC_JOB
*async_job_new(void)
100 ASYNC_JOB
*job
= NULL
;
103 if(!(job
= OPENSSL_malloc(sizeof (ASYNC_JOB
)))) {
107 if(!async_pipe(pipefds
)) {
113 job
->wait_fd
= pipefds
[0];
114 job
->wake_fd
= pipefds
[1];
116 job
->status
= ASYNC_JOB_RUNNING
;
117 job
->funcargs
= NULL
;
122 static void async_job_free(ASYNC_JOB
*job
)
126 OPENSSL_free(job
->funcargs
);
127 async_fibre_free(&job
->fibrectx
);
132 static ASYNC_JOB
*async_get_pool_job(void) {
134 STACK_OF(ASYNC_JOB
) *pool
;
136 pool
= async_get_pool();
139 * Pool has not been initialised, so init with the defaults, i.e.
140 * no max size and no pre-created jobs
142 if (ASYNC_init_pool(0, 0) == 0)
144 pool
= async_get_pool();
147 job
= sk_ASYNC_JOB_pop(pool
);
150 if (!async_pool_can_grow())
153 job
= async_job_new();
155 async_fibre_makecontext(&job
->fibrectx
);
156 async_increment_pool_size();
162 static void async_release_job(ASYNC_JOB
*job
) {
164 OPENSSL_free(job
->funcargs
);
165 job
->funcargs
= NULL
;
166 /* Ignore error return */
167 async_release_job_to_pool(job
);
170 void async_start_func(void)
176 job
= async_get_ctx()->currjob
;
177 job
->ret
= job
->func(job
->funcargs
);
180 job
->status
= ASYNC_JOB_STOPPING
;
181 if(!async_fibre_swapcontext(&job
->fibrectx
,
182 &async_get_ctx()->dispatcher
, 1)) {
184 * Should not happen. Getting here will close the thread...can't do much
191 int ASYNC_start_job(ASYNC_JOB
**job
, int *ret
, int (*func
)(void *),
192 void *args
, size_t size
)
194 if(!async_get_ctx() && !async_ctx_new()) {
199 async_get_ctx()->currjob
= *job
;
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
;
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
;
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))
228 /* Should not happen */
229 async_release_job(async_get_ctx()->currjob
);
230 async_get_ctx()->currjob
= NULL
;
235 /* Start a new job */
236 if(!(async_get_ctx()->currjob
= async_get_pool_job())) {
237 return ASYNC_NO_JOBS
;
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
;
247 memcpy(async_get_ctx()->currjob
->funcargs
, args
, size
);
249 async_get_ctx()->currjob
->funcargs
= NULL
;
252 async_get_ctx()->currjob
->func
= func
;
253 if(!async_fibre_swapcontext(&async_get_ctx()->dispatcher
,
254 &async_get_ctx()->currjob
->fibrectx
, 1))
259 async_release_job(async_get_ctx()->currjob
);
260 async_get_ctx()->currjob
= NULL
;
266 int ASYNC_pause_job(void)
270 if(!async_get_ctx() || !async_get_ctx()->currjob
)
273 job
= async_get_ctx()->currjob
;
274 job
->status
= ASYNC_JOB_PAUSING
;
276 if(!async_fibre_swapcontext(&job
->fibrectx
,
277 &async_get_ctx()->dispatcher
, 1)) {
285 static void async_empty_pool(STACK_OF(ASYNC_JOB
) *pool
)
290 job
= sk_ASYNC_JOB_pop(pool
);
295 int ASYNC_init_pool(size_t max_size
, size_t init_size
)
297 STACK_OF(ASYNC_JOB
) *pool
;
298 size_t curr_size
= 0;
300 if (init_size
> max_size
)
303 pool
= sk_ASYNC_JOB_new_null();
307 /* Pre-create jobs as required */
310 job
= async_job_new();
312 async_fibre_makecontext(&job
->fibrectx
);
313 job
->funcargs
= NULL
;
314 sk_ASYNC_JOB_push(pool
, job
);
319 * Not actually fatal because we already created the pool, just skip
320 * creation of any more jobs
326 if (!async_set_pool(pool
, curr_size
, max_size
)) {
327 async_empty_pool(pool
);
328 sk_ASYNC_JOB_free(pool
);
335 void ASYNC_free_pool(void)
337 STACK_OF(ASYNC_JOB
) *pool
;
339 pool
= async_get_pool();
343 async_empty_pool(pool
);
344 async_release_pool();
348 ASYNC_JOB
*ASYNC_get_current_job(void)
351 if((ctx
= async_get_ctx()) == NULL
)
357 int ASYNC_get_wait_fd(ASYNC_JOB
*job
)
362 void ASYNC_wake(ASYNC_JOB
*job
)
368 async_write1(job
->wake_fd
, &dummy
);
372 void ASYNC_clear_wake(ASYNC_JOB
*job
)
377 async_read1(job
->wait_fd
, &dummy
);