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(). No asynchronous jobs must be outstanding for the thread
41 when ASYNC_free_pool() is called. Failing to ensure this will result in memory
44 The B<max_size> argument limits the number of ASYNC_JOBs that will be held in
45 the pool. If B<max_size> is set to 0 then no upper limit is set. When an
46 ASYNC_JOB is needed but there are none available in the pool already then one
47 will be automatically created, as long as the total of ASYNC_JOBs managed by the
48 pool does not exceed B<max_size>. When the pool is first initialised
49 B<init_size> ASYNC_JOBs will be created immediately. If ASYNC_init_pool() is not
50 called before the pool is first used then it will be called automatically with a
51 B<max_size> of 0 (no upper limit) and an B<init_size> of 0 (no ASYNC_JOBs
52 created up front). If a pool is created in this way it must still be cleaned up
53 with an explicit call to ASYNC_free_pool().
55 An asynchronous job is started by calling the ASYNC_start_job() function.
56 Initially B<*job> should be NULL. B<ret> should point to a location where the
57 return value of the asynchronous function should be stored on completion of the
58 job. B<func> represents the function that should be started asynchronously. The
59 data pointed to by B<args> and of size B<size> will be copied and then passed as
60 an argument to B<func> when the job starts. ASYNC_start_job will return one of
67 An error occurred trying to start the job. Check the OpenSSL error queue (e.g.
68 see L<ERR_print_errors(3)>) for more details.
70 =item B<ASYNC_NO_JOBS>
72 There are no jobs currently available in the pool. This call can be retried
73 again at a later time.
77 The job was successfully started but was "paused" before it completed (see
78 ASYNC_pause_job() below). A handle to the job is placed in B<*job>. Other work
79 can be performed (if desired) and the job restarted at a later time. To restart
80 a job call ASYNC_start_job() again passing the job handle in B<*job>. The
81 B<func>, B<args> and B<size> parameters will be ignored when restarting a job.
82 When restarting a job ASYNC_start_job() B<must> be called from the same thread
83 that the job was originally started from.
87 The job completed. B<*job> will be NULL and the return value from B<func> will
92 At any one time there can be a maximum of one job actively running per thread
93 (you can have many that are paused). ASYNC_get_current_job() can be used to get
94 a pointer to the currently executing ASYNC_JOB. If no job is currently executing
95 then this will return NULL.
97 If executing within the context of a job (i.e. having been called directly or
98 indirectly by the function "func" passed as an argument to ASYNC_start_job())
99 then ASYNC_pause_job() will immediately return control to the calling
100 application with ASYNC_PAUSE returned from the ASYNC_start_job() call. A
101 subsequent call to ASYNC_start_job passing in the relevant ASYNC_JOB in the
102 B<*job> parameter will resume execution from the ASYNC_pause_job() call. If
103 ASYNC_pause_job() is called whilst not within the context of a job then no
104 action is taken and ASYNC_pause_job() returns immediately.
106 Every ASYNC_JOB has a "wait" file descriptor associated with it. Calling
107 ASYNC_get_wait_fd() and passing in a pointer to an ASYNC_JOB in the B<job>
108 parameter will return the wait file descriptor associated with that job. This
109 file descriptor can be used to signal that the job should be resumed.
110 Applications can wait for the file descriptor to be ready for "read" using a
111 system function call such as select or poll (being ready for "read" indicates
112 that the job should be resumed). Applications can signal that a job is ready to
113 resume using ASYNC_wake() or clear an existing signal using ASYNC_clear_wake().
115 An example of typical usage might be an async capable engine. User code would
116 initiate cryptographic operations. The engine would initiate those operations
117 asynchronously and then call ASYNC_pause_job() to return control to the user
118 code. The user code can then perform other tasks or wait for the job to be ready
119 by calling "select" or other similar function on the wait file descriptor. The
120 engine can signal to the user code that the job should be resumed using
121 ASYNC_wake(). Once resumed the engine would clear the wake signal by calling
127 ASYNC_init_pool returns 1 on success or 0 otherwise.
129 ASYNC_start_job returns one of ASYNC_ERR, ASYNC_NO_JOBS, ASYNC_PAUSE or
130 ASYNC_FINISH as described above.
132 ASYNC_pause_job returns 0 if an error occured or 1 on success. If called when
133 not within the context of an ASYNC_JOB then this is counted as success so 1 is
136 ASYNC_get_wait_fd returns the "wait" file descriptor associated with the
137 ASYNC_JOB provided as an argument.
139 ASYNC_get_current_job returns a pointer to the currently executing ASYNC_JOB or
140 NULL if not within the context of a job.
144 The following example demonstrates how to use most of the core async APIs:
147 #include <openssl/async.h>
149 int jobfunc(void *arg)
154 currjob = ASYNC_get_current_job();
155 if (currjob != NULL) {
156 printf("Executing within a job\n");
158 printf("Not executing within a job - should not happen\n");
162 msg = (unsigned char *)arg;
163 printf("Passed in message is: %s\n", msg);
166 * Normally some external event would cause this to happen at some
167 * later point - but we do it here for demo purposes, i.e.
168 * immediately signalling that the job is ready to be woken up after
169 * we return to main via ASYNC_pause_job().
173 /* Return control back to main */
176 /* Clear the wake signal */
177 ASYNC_clear_wake(currjob);
179 printf ("Resumed the job after a pause\n");
186 ASYNC_JOB *job = NULL;
189 unsigned char msg[13] = "Hello world!";
192 * We're only expecting 1 job to be used here so we're only creating
195 if (!ASYNC_init_pool(1, 1)) {
196 printf("Error creating pool\n");
200 printf("Starting...\n");
203 switch(ASYNC_start_job(&job, &ret, jobfunc, msg, sizeof(msg))) {
206 printf("An error occurred\n");
209 printf("Job was paused\n");
212 printf("Job finished with return value %d\n", ret);
216 /* Wait for the job to be woken */
217 printf("Waiting for the job to be woken up\n");
218 waitfd = ASYNC_get_wait_fd(job);
220 FD_SET(waitfd, &waitfdset);
221 select(waitfd + 1, &waitfdset, NULL, NULL, NULL);
225 printf("Finishing\n");
231 The expected output from executing the above example program is:
234 Executing within a job
235 Passed in message is: Hello world!
237 Waiting for the job to be woken up
238 Resumed the job after a pause
239 Job finished with return value 1
244 L<crypto(3)>, L<ERR_print_errors(3)>
248 ASYNC_init_pool, ASYNC_free_pool, ASYNC_start_job, ASYNC_pause_job,
249 ASYNC_get_wait_fd, ASYNC_get_current_job, ASYNC_wake, ASYNC_clear_wake were
250 first added to OpenSSL 1.1.0.