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, ASYNC_block_pause, ASYNC_unblock_pause - asynchronous job
12 #include <openssl/async.h>
14 int ASYNC_init_pool(size_t max_size, size_t init_size);
15 void ASYNC_free_pool(void);
17 int ASYNC_start_job(ASYNC_JOB **job, int *ret, int (*func)(void *),
18 void *args, size_t size);
19 int ASYNC_pause_job(void);
21 int ASYNC_get_wait_fd(ASYNC_JOB *job);
22 ASYNC_JOB *ASYNC_get_current_job(void);
23 void ASYNC_wake(ASYNC_JOB *job);
24 void ASYNC_clear_wake(ASYNC_JOB *job);
25 void ASYNC_block_pause(void);
26 void ASYNC_unblock_pause(void);
30 OpenSSL implements asynchronous capabilities through an ASYNC_JOB. This
31 represents code that can be started and executes until some event occurs. At
32 that point the code can be paused and control returns to user code until some
33 subsequent event indicates that the job can be resumed.
35 The creation of an ASYNC_JOB is a relatively expensive operation. Therefore, for
36 efficiency reasons, jobs can be created up front and reused many times. They are
37 held in a pool until they are needed, at which point they are removed from the
38 pool, used, and then returned to the pool when the job completes. Before using
39 any of the asynchronous job functions, user code should first call
40 ASYNC_init_pool(). If the user application is multi-threaded, then this should
41 be done for each thread that will initiate asynchronous jobs. Before user code
42 exits it should free the pool up (for each thread where a pool was initialised)
43 using ASYNC_free_pool(). No asynchronous jobs must be outstanding for the thread
44 when ASYNC_free_pool() is called. Failing to ensure this will result in memory
47 The B<max_size> argument limits the number of ASYNC_JOBs that will be held in
48 the pool. If B<max_size> is set to 0 then no upper limit is set. When an
49 ASYNC_JOB is needed but there are none available in the pool already then one
50 will be automatically created, as long as the total of ASYNC_JOBs managed by the
51 pool does not exceed B<max_size>. When the pool is first initialised
52 B<init_size> ASYNC_JOBs will be created immediately. If ASYNC_init_pool() is not
53 called before the pool is first used then it will be called automatically with a
54 B<max_size> of 0 (no upper limit) and an B<init_size> of 0 (no ASYNC_JOBs
55 created up front). If a pool is created in this way it must still be cleaned up
56 with an explicit call to ASYNC_free_pool().
58 An asynchronous job is started by calling the ASYNC_start_job() function.
59 Initially B<*job> should be NULL. B<ret> should point to a location where the
60 return value of the asynchronous function should be stored on completion of the
61 job. B<func> represents the function that should be started asynchronously. The
62 data pointed to by B<args> and of size B<size> will be copied and then passed as
63 an argument to B<func> when the job starts. ASYNC_start_job will return one of
70 An error occurred trying to start the job. Check the OpenSSL error queue (e.g.
71 see L<ERR_print_errors(3)>) for more details.
73 =item B<ASYNC_NO_JOBS>
75 There are no jobs currently available in the pool. This call can be retried
76 again at a later time.
80 The job was successfully started but was "paused" before it completed (see
81 ASYNC_pause_job() below). A handle to the job is placed in B<*job>. Other work
82 can be performed (if desired) and the job restarted at a later time. To restart
83 a job call ASYNC_start_job() again passing the job handle in B<*job>. The
84 B<func>, B<args> and B<size> parameters will be ignored when restarting a job.
85 When restarting a job ASYNC_start_job() B<must> be called from the same thread
86 that the job was originally started from.
90 The job completed. B<*job> will be NULL and the return value from B<func> will
95 At any one time there can be a maximum of one job actively running per thread
96 (you can have many that are paused). ASYNC_get_current_job() can be used to get
97 a pointer to the currently executing ASYNC_JOB. If no job is currently executing
98 then this will return NULL.
100 If executing within the context of a job (i.e. having been called directly or
101 indirectly by the function "func" passed as an argument to ASYNC_start_job())
102 then ASYNC_pause_job() will immediately return control to the calling
103 application with ASYNC_PAUSE returned from the ASYNC_start_job() call. A
104 subsequent call to ASYNC_start_job passing in the relevant ASYNC_JOB in the
105 B<*job> parameter will resume execution from the ASYNC_pause_job() call. If
106 ASYNC_pause_job() is called whilst not within the context of a job then no
107 action is taken and ASYNC_pause_job() returns immediately.
109 Every ASYNC_JOB has a "wait" file descriptor associated with it. Calling
110 ASYNC_get_wait_fd() and passing in a pointer to an ASYNC_JOB in the B<job>
111 parameter will return the wait file descriptor associated with that job. This
112 file descriptor can be used to signal that the job should be resumed.
113 Applications can wait for the file descriptor to be ready for "read" using a
114 system function call such as select or poll (being ready for "read" indicates
115 that the job should be resumed). Applications can signal that a job is ready to
116 resume using ASYNC_wake() or clear an existing signal using ASYNC_clear_wake().
118 An example of typical usage might be an async capable engine. User code would
119 initiate cryptographic operations. The engine would initiate those operations
120 asynchronously and then call ASYNC_pause_job() to return control to the user
121 code. The user code can then perform other tasks or wait for the job to be ready
122 by calling "select" or other similar function on the wait file descriptor. The
123 engine can signal to the user code that the job should be resumed using
124 ASYNC_wake(). Once resumed the engine would clear the wake signal by calling
127 The ASYNC_block_pause() function will prevent the currently active job from
128 pausing. The block will remain in place until a subsequent call to
129 ASYNC_unblock_pause(). These functions can be nested, e.g. if you call
130 ASYNC_block_pause() twice then you must call ASYNC_unblock_pause() twice in
131 order to reenable pausing. If these functions are called while there is no
132 currently active job then they have no effect. This functionality can be useful
133 to avoid deadlock scenarios. For example during the execution of an ASYNC_JOB an
134 application aquires a lock. It then calls some cryptographic function which
135 invokes ASYNC_pause_job(). This returns control back to the code that created
136 the ASYNC_JOB. If that code then attempts to aquire the same lock before
137 resuming the original job then a deadlock can occur. By calling
138 ASYNC_block_pause() immediately after aquiring the lock and
139 ASYNC_unblock_pause() immediately before releasing it then this situation cannot
144 ASYNC_init_pool returns 1 on success or 0 otherwise.
146 ASYNC_start_job returns one of ASYNC_ERR, ASYNC_NO_JOBS, ASYNC_PAUSE or
147 ASYNC_FINISH as described above.
149 ASYNC_pause_job returns 0 if an error occured or 1 on success. If called when
150 not within the context of an ASYNC_JOB then this is counted as success so 1 is
153 ASYNC_get_wait_fd returns the "wait" file descriptor associated with the
154 ASYNC_JOB provided as an argument.
156 ASYNC_get_current_job returns a pointer to the currently executing ASYNC_JOB or
157 NULL if not within the context of a job.
161 The following example demonstrates how to use most of the core async APIs:
164 #include <openssl/async.h>
166 int jobfunc(void *arg)
171 currjob = ASYNC_get_current_job();
172 if (currjob != NULL) {
173 printf("Executing within a job\n");
175 printf("Not executing within a job - should not happen\n");
179 msg = (unsigned char *)arg;
180 printf("Passed in message is: %s\n", msg);
183 * Normally some external event would cause this to happen at some
184 * later point - but we do it here for demo purposes, i.e.
185 * immediately signalling that the job is ready to be woken up after
186 * we return to main via ASYNC_pause_job().
190 /* Return control back to main */
193 /* Clear the wake signal */
194 ASYNC_clear_wake(currjob);
196 printf ("Resumed the job after a pause\n");
203 ASYNC_JOB *job = NULL;
206 unsigned char msg[13] = "Hello world!";
209 * We're only expecting 1 job to be used here so we're only creating
212 if (!ASYNC_init_pool(1, 1)) {
213 printf("Error creating pool\n");
217 printf("Starting...\n");
220 switch(ASYNC_start_job(&job, &ret, jobfunc, msg, sizeof(msg))) {
223 printf("An error occurred\n");
226 printf("Job was paused\n");
229 printf("Job finished with return value %d\n", ret);
233 /* Wait for the job to be woken */
234 printf("Waiting for the job to be woken up\n");
235 waitfd = ASYNC_get_wait_fd(job);
237 FD_SET(waitfd, &waitfdset);
238 select(waitfd + 1, &waitfdset, NULL, NULL, NULL);
242 printf("Finishing\n");
248 The expected output from executing the above example program is:
251 Executing within a job
252 Passed in message is: Hello world!
254 Waiting for the job to be woken up
255 Resumed the job after a pause
256 Job finished with return value 1
261 L<crypto(3)>, L<ERR_print_errors(3)>
265 ASYNC_init_pool, ASYNC_free_pool, ASYNC_start_job, ASYNC_pause_job,
266 ASYNC_get_wait_fd, ASYNC_get_current_job, ASYNC_wake, ASYNC_clear_wake were
267 first added to OpenSSL 1.1.0.