1ASYNC_START_JOB(3ossl)              OpenSSL             ASYNC_START_JOB(3ossl)
2
3
4

NAME

6       ASYNC_get_wait_ctx, ASYNC_init_thread, ASYNC_cleanup_thread,
7       ASYNC_start_job, ASYNC_pause_job, ASYNC_get_current_job,
8       ASYNC_block_pause, ASYNC_unblock_pause, ASYNC_is_capable - asynchronous
9       job management functions
10

SYNOPSIS

12        #include <openssl/async.h>
13
14        int ASYNC_init_thread(size_t max_size, size_t init_size);
15        void ASYNC_cleanup_thread(void);
16
17        int ASYNC_start_job(ASYNC_JOB **job, ASYNC_WAIT_CTX *ctx, int *ret,
18                            int (*func)(void *), void *args, size_t size);
19        int ASYNC_pause_job(void);
20
21        ASYNC_JOB *ASYNC_get_current_job(void);
22        ASYNC_WAIT_CTX *ASYNC_get_wait_ctx(ASYNC_JOB *job);
23        void ASYNC_block_pause(void);
24        void ASYNC_unblock_pause(void);
25
26        int ASYNC_is_capable(void);
27

DESCRIPTION

29       OpenSSL implements asynchronous capabilities through an ASYNC_JOB. This
30       represents code that can be started and executes until some event
31       occurs. At that point the code can be paused and control returns to
32       user code until some subsequent event indicates that the job can be
33       resumed.
34
35       The creation of an ASYNC_JOB is a relatively expensive operation.
36       Therefore, for efficiency reasons, jobs can be created up front and
37       reused many times. They are held in a pool until they are needed, at
38       which point they are removed from the pool, used, and then returned to
39       the pool when the job completes. If the user application is multi-
40       threaded, then ASYNC_init_thread() may be called for each thread that
41       will initiate asynchronous jobs. Before user code exits per-thread
42       resources need to be cleaned up. This will normally occur automatically
43       (see OPENSSL_init_crypto(3)) but may be explicitly initiated by using
44       ASYNC_cleanup_thread(). No asynchronous jobs must be outstanding for
45       the thread when ASYNC_cleanup_thread() is called. Failing to ensure
46       this will result in memory leaks.
47
48       The max_size argument limits the number of ASYNC_JOBs that will be held
49       in the pool. If max_size is set to 0 then no upper limit is set. When
50       an ASYNC_JOB is needed but there are none available in the pool already
51       then one will be automatically created, as long as the total of
52       ASYNC_JOBs managed by the pool does not exceed max_size. When the pool
53       is first initialised init_size ASYNC_JOBs will be created immediately.
54       If ASYNC_init_thread() is not called before the pool is first used then
55       it will be called automatically with a max_size of 0 (no upper limit)
56       and an init_size of 0 (no ASYNC_JOBs created up front).
57
58       An asynchronous job is started by calling the ASYNC_start_job()
59       function.  Initially *job should be NULL. ctx should point to an
60       ASYNC_WAIT_CTX object created through the ASYNC_WAIT_CTX_new(3)
61       function. ret should point to a location where the return value of the
62       asynchronous function should be stored on completion of the job. func
63       represents the function that should be started asynchronously. The data
64       pointed to by args and of size size will be copied and then passed as
65       an argument to func when the job starts.  ASYNC_start_job will return
66       one of the following values:
67
68       ASYNC_ERR
69           An error occurred trying to start the job. Check the OpenSSL error
70           queue (e.g.  see ERR_print_errors(3)) for more details.
71
72       ASYNC_NO_JOBS
73           There are no jobs currently available in the pool. This call can be
74           retried again at a later time.
75
76       ASYNC_PAUSE
77           The job was successfully started but was "paused" before it
78           completed (see ASYNC_pause_job() below). A handle to the job is
79           placed in *job. Other work can be performed (if desired) and the
80           job restarted at a later time. To restart a job call
81           ASYNC_start_job() again passing the job handle in *job. The func,
82           args and size parameters will be ignored when restarting a job.
83           When restarting a job ASYNC_start_job() must be called from the
84           same thread that the job was originally started from.
85
86       ASYNC_FINISH
87           The job completed. *job will be NULL and the return value from func
88           will be placed in *ret.
89
90       At any one time there can be a maximum of one job actively running per
91       thread (you can have many that are paused). ASYNC_get_current_job() can
92       be used to get a pointer to the currently executing ASYNC_JOB. If no
93       job is currently executing then this will return NULL.
94
95       If executing within the context of a job (i.e. having been called
96       directly or indirectly by the function "func" passed as an argument to
97       ASYNC_start_job()) then ASYNC_pause_job() will immediately return
98       control to the calling application with ASYNC_PAUSE returned from the
99       ASYNC_start_job() call. A subsequent call to ASYNC_start_job passing in
100       the relevant ASYNC_JOB in the *job parameter will resume execution from
101       the ASYNC_pause_job() call. If ASYNC_pause_job() is called whilst not
102       within the context of a job then no action is taken and
103       ASYNC_pause_job() returns immediately.
104
105       ASYNC_get_wait_ctx() can be used to get a pointer to the ASYNC_WAIT_CTX
106       for the job. ASYNC_WAIT_CTXs contain two different ways to notify
107       applications that a job is ready to be resumed. One is a "wait" file
108       descriptor, and the other is a "callback" mechanism.
109
110       The "wait" file descriptor associated with ASYNC_WAIT_CTX is used for
111       applications to wait for the file descriptor to be ready for "read"
112       using a system function call such as select or poll (being ready for
113       "read" indicates that the job should be resumed). If no file descriptor
114       is made available then an application will have to periodically "poll"
115       the job by attempting to restart it to see if it is ready to continue.
116
117       ASYNC_WAIT_CTXs also have a "callback" mechanism to notify
118       applications. The callback is set by an application, and it will be
119       automatically called when an engine completes a cryptography operation,
120       so that the application can resume the paused work flow without
121       polling. An engine could be written to look whether the callback has
122       been set. If it has then it would use the callback mechanism in
123       preference to the file descriptor notifications. If a callback is not
124       set then the engine may use file descriptor based notifications. Please
125       note that not all engines may support the callback mechanism, so the
126       callback may not be used even if it has been set. See
127       ASYNC_WAIT_CTX_new() for more details.
128
129       The ASYNC_block_pause() function will prevent the currently active job
130       from pausing. The block will remain in place until a subsequent call to
131       ASYNC_unblock_pause(). These functions can be nested, e.g. if you call
132       ASYNC_block_pause() twice then you must call ASYNC_unblock_pause()
133       twice in order to re-enable pausing. If these functions are called
134       while there is no currently active job then they have no effect. This
135       functionality can be useful to avoid deadlock scenarios. For example
136       during the execution of an ASYNC_JOB an application acquires a lock. It
137       then calls some cryptographic function which invokes ASYNC_pause_job().
138       This returns control back to the code that created the ASYNC_JOB. If
139       that code then attempts to acquire the same lock before resuming the
140       original job then a deadlock can occur. By calling ASYNC_block_pause()
141       immediately after acquiring the lock and ASYNC_unblock_pause()
142       immediately before releasing it then this situation cannot occur.
143
144       Some platforms cannot support async operations. The ASYNC_is_capable()
145       function can be used to detect whether the current platform is async
146       capable or not.
147

RETURN VALUES

149       ASYNC_init_thread returns 1 on success or 0 otherwise.
150
151       ASYNC_start_job returns one of ASYNC_ERR, ASYNC_NO_JOBS, ASYNC_PAUSE or
152       ASYNC_FINISH as described above.
153
154       ASYNC_pause_job returns 0 if an error occurred or 1 on success. If
155       called when not within the context of an ASYNC_JOB then this is counted
156       as success so 1 is returned.
157
158       ASYNC_get_current_job returns a pointer to the currently executing
159       ASYNC_JOB or NULL if not within the context of a job.
160
161       ASYNC_get_wait_ctx() returns a pointer to the ASYNC_WAIT_CTX for the
162       job.
163
164       ASYNC_is_capable() returns 1 if the current platform is async capable
165       or 0 otherwise.
166

NOTES

168       On Windows platforms the <openssl/async.h> header is dependent on some
169       of the types customarily made available by including <windows.h>. The
170       application developer is likely to require control over when the latter
171       is included, commonly as one of the first included headers. Therefore,
172       it is defined as an application developer's responsibility to include
173       <windows.h> prior to <openssl/async.h>.
174

EXAMPLES

176       The following example demonstrates how to use most of the core async
177       APIs:
178
179        #ifdef _WIN32
180        # include <windows.h>
181        #endif
182        #include <stdio.h>
183        #include <unistd.h>
184        #include <openssl/async.h>
185        #include <openssl/crypto.h>
186
187        int unique = 0;
188
189        void cleanup(ASYNC_WAIT_CTX *ctx, const void *key, OSSL_ASYNC_FD r, void *vw)
190        {
191            OSSL_ASYNC_FD *w = (OSSL_ASYNC_FD *)vw;
192
193            close(r);
194            close(*w);
195            OPENSSL_free(w);
196        }
197
198        int jobfunc(void *arg)
199        {
200            ASYNC_JOB *currjob;
201            unsigned char *msg;
202            int pipefds[2] = {0, 0};
203            OSSL_ASYNC_FD *wptr;
204            char buf = 'X';
205
206            currjob = ASYNC_get_current_job();
207            if (currjob != NULL) {
208                printf("Executing within a job\n");
209            } else {
210                printf("Not executing within a job - should not happen\n");
211                return 0;
212            }
213
214            msg = (unsigned char *)arg;
215            printf("Passed in message is: %s\n", msg);
216
217            if (pipe(pipefds) != 0) {
218                printf("Failed to create pipe\n");
219                return 0;
220            }
221            wptr = OPENSSL_malloc(sizeof(OSSL_ASYNC_FD));
222            if (wptr == NULL) {
223                printf("Failed to malloc\n");
224                return 0;
225            }
226            *wptr = pipefds[1];
227            ASYNC_WAIT_CTX_set_wait_fd(ASYNC_get_wait_ctx(currjob), &unique,
228                                       pipefds[0], wptr, cleanup);
229
230            /*
231             * Normally some external event would cause this to happen at some
232             * later point - but we do it here for demo purposes, i.e.
233             * immediately signalling that the job is ready to be woken up after
234             * we return to main via ASYNC_pause_job().
235             */
236            write(pipefds[1], &buf, 1);
237
238            /* Return control back to main */
239            ASYNC_pause_job();
240
241            /* Clear the wake signal */
242            read(pipefds[0], &buf, 1);
243
244            printf ("Resumed the job after a pause\n");
245
246            return 1;
247        }
248
249        int main(void)
250        {
251            ASYNC_JOB *job = NULL;
252            ASYNC_WAIT_CTX *ctx = NULL;
253            int ret;
254            OSSL_ASYNC_FD waitfd;
255            fd_set waitfdset;
256            size_t numfds;
257            unsigned char msg[13] = "Hello world!";
258
259            printf("Starting...\n");
260
261            ctx = ASYNC_WAIT_CTX_new();
262            if (ctx == NULL) {
263                printf("Failed to create ASYNC_WAIT_CTX\n");
264                abort();
265            }
266
267            for (;;) {
268                switch (ASYNC_start_job(&job, ctx, &ret, jobfunc, msg, sizeof(msg))) {
269                case ASYNC_ERR:
270                case ASYNC_NO_JOBS:
271                    printf("An error occurred\n");
272                    goto end;
273                case ASYNC_PAUSE:
274                    printf("Job was paused\n");
275                    break;
276                case ASYNC_FINISH:
277                    printf("Job finished with return value %d\n", ret);
278                    goto end;
279                }
280
281                /* Wait for the job to be woken */
282                printf("Waiting for the job to be woken up\n");
283
284                if (!ASYNC_WAIT_CTX_get_all_fds(ctx, NULL, &numfds)
285                        || numfds > 1) {
286                    printf("Unexpected number of fds\n");
287                    abort();
288                }
289                ASYNC_WAIT_CTX_get_all_fds(ctx, &waitfd, &numfds);
290                FD_ZERO(&waitfdset);
291                FD_SET(waitfd, &waitfdset);
292                select(waitfd + 1, &waitfdset, NULL, NULL, NULL);
293            }
294
295        end:
296            ASYNC_WAIT_CTX_free(ctx);
297            printf("Finishing\n");
298
299            return 0;
300        }
301
302       The expected output from executing the above example program is:
303
304        Starting...
305        Executing within a job
306        Passed in message is: Hello world!
307        Job was paused
308        Waiting for the job to be woken up
309        Resumed the job after a pause
310        Job finished with return value 1
311        Finishing
312

SEE ALSO

314       crypto(7), ERR_print_errors(3)
315

HISTORY

317       ASYNC_init_thread, ASYNC_cleanup_thread, ASYNC_start_job,
318       ASYNC_pause_job, ASYNC_get_current_job, ASYNC_get_wait_ctx(),
319       ASYNC_block_pause(), ASYNC_unblock_pause() and ASYNC_is_capable() were
320       first added in OpenSSL 1.1.0.
321
323       Copyright 2015-2021 The OpenSSL Project Authors. All Rights Reserved.
324
325       Licensed under the Apache License 2.0 (the "License").  You may not use
326       this file except in compliance with the License.  You can obtain a copy
327       in the file LICENSE in the source distribution or at
328       <https://www.openssl.org/source/license.html>.
329
330
331
3323.0.5                             2022-11-01            ASYNC_START_JOB(3ossl)
Impressum