5 ASYNC_init_pool, ASYNC_free_pool, ASYNC_start_job, ASYNC_pause_job,
6 ASYNC_in_job, ASYNC_get_wait_fd, ASYNC_get_current_job, ASYNC_wake,
7 ASYNC_clear_wake - asynchronous job management functions
11 #include <openssl/async.h>
13 int ASYNC_init_pool(size_t max_size, size_t init_size);
14 void ASYNC_free_pool(void);
16 int ASYNC_start_job(ASYNC_JOB **job, int *ret, int (*func)(void *),
17 void *args, size_t size);
18 int ASYNC_pause_job(void);
20 int ASYNC_get_wait_fd(ASYNC_JOB *job);
21 ASYNC_JOB *ASYNC_get_current_job(void);
22 void ASYNC_wake(ASYNC_JOB *job);
23 void ASYNC_clear_wake(ASYNC_JOB *job);
27 OpenSSL implements asynchronous capabilities through an ASYNC_JOB. This
28 represents code that can be started and executes until some event occurs. At
29 that point the code can be paused and control returns to user code until some
30 subsequent event indicates that the job can be resumed.
32 The creation of an ASYNC_JOB is a relatively expensive operation. Therefore, for
33 efficiency reasons, jobs can be created up front and reused many times. They are
34 held in a pool until they are needed, at which point they are removed from the
35 pool, used, and then returned to the pool when the job completes. Before using
36 any of the asynchronous job functions, user code should first call
37 ASYNC_init_pool(). If the user application is multi-threaded, then this should
38 be done for each thread that will initiate asynchronous jobs. Before user code
39 exits it should free the pool up (for each thread where a pool was initialised)
40 using ASYNC_free_pool(). The B<max_size> argument limits the number of
41 ASYNC_JOBs that will be held in the pool. If B<max_size> is set to 0 then no
42 upper limit is set. When an ASYNC_JOB is needed but there are none available in
43 the pool already then one will be automatically created, as long as the total
44 of ASYNC_JOBs managed by the pool does not exceed B<max_size>. When the pool is
45 first initialised B<init_size> ASYNC_JOBs will be created immediately. If
46 ASYNC_init_pool() is not called before the pool is first used then it will be
47 called automatically with a B<max_size> of 0 (no upper limit) and an
48 B<init_size> of 0 (no ASYNC_JOBs created up front). If a pool is created in this
49 way it must still be cleaned up with an explicit call to ASYNC_free_pool().
51 An asynchronous job is started by calling the ASYNC_start_job() function.
52 Initially B<*job> should be NULL. B<ret> should point to a location where the
53 return value of the asynchronous function should be stored on completion of the
54 job. B<func> represents the function that should be started asynchronously. The
55 data pointed to by B<args> and of size B<size> will be copied and then passed as
56 an argument to B<func> when the job starts. ASYNC_start_job will return one of
63 An error occurred trying to start the job. Check the OpenSSL error queue (e.g.
64 see L<ERR_print_errors(3)>) for more details.
66 =item B<ASYNC_NO_JOBS>
68 There are no jobs currently available in the pool. This call can be retried
69 again at a later time.
73 The job was successfully started but was "paused" before it completed (see
74 ASYNC_pause_job() below). A handle to the job is placed in B<*job>. Other work
75 can be performed (if desired) and the job restarted at a later time. To restart
76 a job call ASYNC_start_job() again passing the job handle in B<*job>. The
77 B<func>, B<args> and B<size> parameters will be ignored when restarting a job.
81 The job completed. B<*job> will be NULL and the return value from B<func> will
86 ASYNC_get_current_job() can be used to get a pointer to the currently executing
87 ASYNC_JOB. If no job is currently executing then this will return NULL.
89 If executing within the context of a job (i.e. having been called directly or
90 indirectly by the function "func" passed as an argument to ASYNC_start_job())
91 then ASYNC_pause_job() will immediately return control to the calling
92 application with ASYNC_PAUSE returned from the ASYNC_start_job() call. A
93 subsequent call to ASYNC_start_job passing in the relevant ASYNC_JOB in the
94 B<*job> parameter will resume execution from the ASYNC_pause_job() call. If
95 ASYNC_pause_job() is called whilst not within the context of a job then no
96 action is taken and ASYNC_pause_job() returns immediately.
98 Every ASYNC_JOB has a "wait" file descriptor associated with it. Calling
99 ASYNC_get_wait_fd() and passing in a pointer to an ASYNC_JOB in the B<job>
100 parameter will return the wait file descriptor associated with that job. This
101 file descriptor can be used to signal that the job should be resumed.
102 Applications can wait on the file descriptor using a system function call
103 such as select or poll. Applications can signal that a job is ready to resume
104 using ASYNC_wake() or clear an existing signal using ASYNC_clear_wake().
106 An example of typical usage might be an async capable engine. User code would
107 initiate cryptographic operations. The engine would initiate those operations
108 asynchronously and then call ASYNC_pause_job() to return control to the user
109 code. The user code can then perform other tasks or wait for the job to be ready
110 by calling "select" or other similar function on the wait file descriptor. The
111 engine can signal to the user code that the job should be resumed using
112 ASYNC_wait(). Once resumed the engine would clear the wake signal by calling
118 ASYNC_init_pool returns 1 on success or 0 otherwise.
120 ASYNC_start_job returns one of ASYNC_ERR, ASYNC_NO_JOBS, ASYNC_PAUSE or
121 ASYNC_FINISH as described above.
123 ASYNC_pause_job returns 0 if an error occured (including if called when not
124 within the context of an ASYNC_JOB), or 1 on success.
126 ASYNC_get_wait_fd returns the "wait" file descriptor associated with the
127 ASYNC_JOB provided as an argument.
129 ASYNC_get_current_job returns a pointer to the currently executing ASYNC_JOB or
130 NULL if not within the context of a job.
134 The following example demonstrates how to use most of the core async APIs:
137 #include <openssl/async.h>
139 int jobfunc(void *arg)
144 currjob = ASYNC_get_current_job();
145 if (currjob != NULL) {
146 printf("Executing within a job\n");
148 printf("Not executing within a job - should not happen\n");
152 msg = (unsigned char *)arg;
153 printf("Passed in message is: %s\n", msg);
156 * Normally some external event would cause this to happen at some
157 * later point - but we do it here for demo purposes, i.e.
158 * immediately signalling that the job is ready to be woken up after
159 * we return to main via ASYNC_pause_job().
163 /* Return control back to main */
166 /* Clear the wake signal */
167 ASYNC_clear_wake(currjob);
169 printf ("Resumed the job after a pause\n");
176 ASYNC_JOB *job = NULL;
179 unsigned char msg[13] = "Hello world!";
182 * We're only expecting 1 job to be used here so we're only creating
185 if (!ASYNC_init_pool(1, 1)) {
186 printf("Error creating pool\n");
190 printf("Starting...\n");
193 switch(ASYNC_start_job(&job, &ret, jobfunc, msg, sizeof(msg))) {
196 printf("An error occurred\n");
199 printf("Job was paused\n");
202 printf("Job finished with return value %d\n", ret);
206 /* Wait for the job to be woken */
207 printf("Waiting for the job to be woken up\n");
208 waitfd = ASYNC_get_wait_fd(job);
210 FD_SET(waitfd, &waitfdset);
211 select(waitfd + 1, &waitfdset, NULL, NULL, NULL);
215 printf("Finishing\n");
221 The expected output from executing the above example program is:
224 Executing within a job
225 Passed in message is: Hello world!
227 Waiting for the job to be woken up
228 Resumed the job after a pause
229 Job finished with return value 1
234 L<crypto(3)>, L<ERR_print_errors(3)>
238 ASYNC_init_pool, ASYNC_free_pool, ASYNC_start_job, ASYNC_pause_job,
239 ASYNC_get_wait_fd, ASYNC_get_current_job, ASYNC_wake, ASYNC_clear_wake were
240 first added to OpenSSL 1.1.0.