c0f362e2c8b0d469fd5ce8854081257486d84c2a
[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/crypto.h>
55 #include <openssl/async.h>
56 #include <string.h>
57 #include "async_locl.h"
58
59 #define ASYNC_JOB_RUNNING   0
60 #define ASYNC_JOB_PAUSING   1
61 #define ASYNC_JOB_PAUSED    2
62 #define ASYNC_JOB_STOPPING  3
63
64
65 static ASYNC_CTX *ASYNC_CTX_new(void)
66 {
67     ASYNC_CTX *nctx = NULL;
68
69     if(!(nctx = OPENSSL_malloc(sizeof (ASYNC_CTX)))) {
70         /* Error here */
71         goto err;
72     }
73
74     ASYNC_FIBRE_init_dispatcher(&nctx->dispatcher);
75     nctx->currjob = NULL;
76     if(!ASYNC_set_ctx(nctx))
77         goto err;
78
79     return nctx;
80 err:
81     if(nctx) {
82         OPENSSL_free(nctx);
83     }
84
85     return NULL;
86 }
87
88 static int ASYNC_CTX_free(void)
89 {
90     if(ASYNC_get_ctx()) {
91         OPENSSL_free(ASYNC_get_ctx());
92     }
93
94     if(!ASYNC_set_ctx(NULL))
95         return 0;
96
97     return 1;
98 }
99
100 static ASYNC_JOB *ASYNC_JOB_new(void)
101 {
102     ASYNC_JOB *job = NULL;
103
104     if(!(job = OPENSSL_malloc(sizeof (ASYNC_JOB)))) {
105         return NULL;
106     }
107
108     job->status = ASYNC_JOB_RUNNING;
109     job->funcargs = NULL;
110
111     return job;
112 }
113
114 static void ASYNC_JOB_free(ASYNC_JOB *job)
115 {
116     if(job) {
117         if(job->funcargs)
118             OPENSSL_free(job->funcargs);
119         ASYNC_FIBRE_free(&job->fibrectx);
120         OPENSSL_free(job);
121     }
122 }
123
124 void ASYNC_start_func(void)
125 {
126     ASYNC_JOB *job;
127
128     /* Run the job */
129     job = ASYNC_get_ctx()->currjob;
130     job->ret = job->func(job->funcargs);
131
132     /* Stop the job */
133     job->status = ASYNC_JOB_STOPPING;
134     if(!ASYNC_FIBRE_swapcontext(&job->fibrectx,
135                                 &ASYNC_get_ctx()->dispatcher, 0)) {
136         /*
137          * Should not happen. Getting here will close the thread...can't do much
138          * about it
139          */
140     }
141 }
142
143 int ASYNC_start_job(ASYNC_JOB **job, int *ret, int (*func)(void *),
144                          void *args, size_t size)
145 {
146     if(ASYNC_get_ctx() || !ASYNC_CTX_new()) {
147         return ASYNC_ERR;
148     }
149
150     if(*job) {
151         ASYNC_get_ctx()->currjob = *job;
152     }
153
154     for (;;) {
155         if(ASYNC_get_ctx()->currjob) {
156             if(ASYNC_get_ctx()->currjob->status == ASYNC_JOB_STOPPING) {
157                 *ret = ASYNC_get_ctx()->currjob->ret;
158                 ASYNC_JOB_free(ASYNC_get_ctx()->currjob);
159                 ASYNC_get_ctx()->currjob = NULL;
160                 *job = NULL;
161                 ASYNC_CTX_free();
162                 return ASYNC_FINISH;
163             }
164
165             if(ASYNC_get_ctx()->currjob->status == ASYNC_JOB_PAUSING) {
166                 *job = ASYNC_get_ctx()->currjob;
167                 ASYNC_get_ctx()->currjob->status = ASYNC_JOB_PAUSED;
168                 ASYNC_CTX_free();
169                 return ASYNC_PAUSE;
170             }
171
172             if(ASYNC_get_ctx()->currjob->status == ASYNC_JOB_PAUSED) {
173                 ASYNC_get_ctx()->currjob = *job;
174                 /* Resume previous job */
175                 if(!ASYNC_FIBRE_swapcontext(&ASYNC_get_ctx()->dispatcher,
176                     &ASYNC_get_ctx()->currjob->fibrectx, 1))
177                     goto err;
178                 continue;
179             }
180
181             /* Should not happen */
182             ASYNC_JOB_free(ASYNC_get_ctx()->currjob);
183             ASYNC_get_ctx()->currjob = NULL;
184             *job = NULL;
185             ASYNC_CTX_free();
186             return ASYNC_ERR;
187         }
188
189         /* Start a new job */
190         if(!(ASYNC_get_ctx()->currjob = ASYNC_JOB_new())) {
191             ASYNC_CTX_free();
192             return ASYNC_ERR;
193         }
194
195         if(args != NULL) {
196             ASYNC_get_ctx()->currjob->funcargs = OPENSSL_malloc(size);
197             if(!ASYNC_get_ctx()->currjob->funcargs) {
198                 ASYNC_JOB_free(ASYNC_get_ctx()->currjob);
199                 ASYNC_get_ctx()->currjob = NULL;
200                 ASYNC_CTX_free();
201                 return ASYNC_ERR;
202             }
203             memcpy(ASYNC_get_ctx()->currjob->funcargs, args, size);
204         } else {
205             ASYNC_get_ctx()->currjob->funcargs = NULL;
206         }
207
208         ASYNC_get_ctx()->currjob->func = func;
209         ASYNC_FIBRE_makecontext(&ASYNC_get_ctx()->currjob->fibrectx);
210         if(!ASYNC_FIBRE_swapcontext(&ASYNC_get_ctx()->dispatcher,
211             &ASYNC_get_ctx()->currjob->fibrectx, 1))
212             goto err;
213     }
214
215 err:
216     ASYNC_JOB_free(ASYNC_get_ctx()->currjob);
217     ASYNC_get_ctx()->currjob = NULL;
218     *job = NULL;
219     ASYNC_CTX_free();
220     return ASYNC_ERR;
221 }
222
223
224 int ASYNC_pause_job(void)
225 {
226     ASYNC_JOB *job;
227
228     if(!ASYNC_get_ctx() || !ASYNC_get_ctx()->currjob)
229         return 0;
230
231     job = ASYNC_get_ctx()->currjob;
232     job->status = ASYNC_JOB_PAUSING;
233
234     if(!ASYNC_FIBRE_swapcontext(&job->fibrectx,
235                                &ASYNC_get_ctx()->dispatcher, 1)) {
236         /* Error */
237         return 0;
238     }
239
240     return 1;
241 }
242
243 int ASYNC_in_job(void)
244 {
245     if(ASYNC_get_ctx())
246         return 1;
247
248     return 0;
249 }