Various RT doc fixes
[openssl.git] / doc / crypto / ASYNC_start_job.pod
1 =pod
2
3 =head1 NAME
4
5 ASYNC_init, ASYNC_cleanup, ASYNC_init_thread, ASYNC_cleanup_thread,
6 ASYNC_start_job, ASYNC_pause_job, ASYNC_in_job, ASYNC_get_wait_fd,
7 ASYNC_get_current_job, ASYNC_wake, ASYNC_clear_wake, ASYNC_block_pause,
8 ASYNC_unblock_pause - asynchronous job management functions
9
10 =head1 SYNOPSIS
11
12  #include <openssl/async.h>
13
14  int ASYNC_init(int init_thread, size_t max_size, size_t init_size);
15  void ASYNC_cleanup(int cleanupthread);
16
17  int ASYNC_init_thread(size_t max_size, size_t init_size);
18  void ASYNC_cleanup_thread(void);
19
20  int ASYNC_start_job(ASYNC_JOB **job, int *ret, int (*func)(void *),
21                      void *args, size_t size);
22  int ASYNC_pause_job(void);
23
24  int ASYNC_get_wait_fd(ASYNC_JOB *job);
25  ASYNC_JOB *ASYNC_get_current_job(void);
26  void ASYNC_wake(ASYNC_JOB *job);
27  void ASYNC_clear_wake(ASYNC_JOB *job);
28  void ASYNC_block_pause(void);
29  void ASYNC_unblock_pause(void);
30
31 =head1 DESCRIPTION
32
33 OpenSSL implements asynchronous capabilities through an ASYNC_JOB. This
34 represents code that can be started and executes until some event occurs. At
35 that point the code can be paused and control returns to user code until some
36 subsequent event indicates that the job can be resumed.
37
38 The creation of an ASYNC_JOB is a relatively expensive operation. Therefore, for
39 efficiency reasons, jobs can be created up front and reused many times. They are
40 held in a pool until they are needed, at which point they are removed from the
41 pool, used, and then returned to the pool when the job completes. Before using
42 any of the asynchronous job functions, user code should first call
43 ASYNC_init(). If the user application is multi-threaded, then
44 ASYNC_init_thread() should be called for each thread that will initiate
45 asynchronous jobs. If the B<init_thread> parameter to ASYNC_init() is non-zero
46 then ASYNC_init_thread is automatically called for the current thread. Before
47 user code exits it should free up resources for each thread that was initialised
48 using ASYNC_cleanup_thread(). No asynchronous jobs must be outstanding for the thread
49 when ASYNC_cleanup_thread() is called. Failing to ensure this will result in memory
50 leaks. Additionally an application should call ASYNC_cleanup() when all
51 asynchronous work is complete across all threads. If B<cleanupthread> is
52 non-zero then ASYNC_cleanup_thread() is automatically called for the current
53 thread.
54
55 The B<max_size> argument limits the number of ASYNC_JOBs that will be held in
56 the pool. If B<max_size> is set to 0 then no upper limit is set. When an
57 ASYNC_JOB is needed but there are none available in the pool already then one
58 will be automatically created, as long as the total of ASYNC_JOBs managed by the
59 pool does not exceed B<max_size>. When the pool is first initialised
60 B<init_size> ASYNC_JOBs will be created immediately. If ASYNC_init_thread() is
61 not called before the pool is first used then it will be called automatically
62 with a B<max_size> of 0 (no upper limit) and an B<init_size> of 0 (no ASYNC_JOBs
63 created up front). If a pool is created in this way it must still be cleaned up
64 with an explicit call to ASYNC_cleanup_thread().
65
66 An asynchronous job is started by calling the ASYNC_start_job() function.
67 Initially B<*job> should be NULL. B<ret> should point to a location where the
68 return value of the asynchronous function should be stored on completion of the
69 job. B<func> represents the function that should be started asynchronously. The
70 data pointed to by B<args> and of size B<size> will be copied and then passed as
71 an argument to B<func> when the job starts. ASYNC_start_job will return one of
72 the following values:
73
74 =over 4
75
76 =item B<ASYNC_ERR>
77
78 An error occurred trying to start the job. Check the OpenSSL error queue (e.g.
79 see L<ERR_print_errors(3)>) for more details.
80
81 =item B<ASYNC_NO_JOBS>
82
83 There are no jobs currently available in the pool. This call can be retried
84 again at a later time.
85
86 =item B<ASYNC_PAUSE>
87
88 The job was successfully started but was "paused" before it completed (see
89 ASYNC_pause_job() below). A handle to the job is placed in B<*job>. Other work
90 can be performed (if desired) and the job restarted at a later time. To restart
91 a job call ASYNC_start_job() again passing the job handle in B<*job>. The
92 B<func>, B<args> and B<size> parameters will be ignored when restarting a job.
93 When restarting a job ASYNC_start_job() B<must> be called from the same thread
94 that the job was originally started from.
95
96 =item B<ASYNC_FINISH>
97
98 The job completed. B<*job> will be NULL and the return value from B<func> will
99 be placed in B<*ret>.
100
101 =back
102
103 At any one time there can be a maximum of one job actively running per thread
104 (you can have many that are paused). ASYNC_get_current_job() can be used to get
105 a pointer to the currently executing ASYNC_JOB. If no job is currently executing
106 then this will return NULL.
107
108 If executing within the context of a job (i.e. having been called directly or
109 indirectly by the function "func" passed as an argument to ASYNC_start_job())
110 then ASYNC_pause_job() will immediately return control to the calling
111 application with ASYNC_PAUSE returned from the ASYNC_start_job() call. A
112 subsequent call to ASYNC_start_job passing in the relevant ASYNC_JOB in the
113 B<*job> parameter will resume execution from the ASYNC_pause_job() call. If
114 ASYNC_pause_job() is called whilst not within the context of a job then no
115 action is taken and ASYNC_pause_job() returns immediately.
116
117 Every ASYNC_JOB has a "wait" file descriptor associated with it. Calling
118 ASYNC_get_wait_fd() and passing in a pointer to an ASYNC_JOB in the B<job>
119 parameter will return the wait file descriptor associated with that job. This
120 file descriptor can be used to signal that the job should be resumed.
121 Applications can wait for the file descriptor to be ready for "read" using a
122 system function call such as select or poll (being ready for "read" indicates
123 that the job should be resumed). Applications can signal that a job is ready to
124 resume using ASYNC_wake() or clear an existing signal using ASYNC_clear_wake().
125
126 An example of typical usage might be an async capable engine. User code would
127 initiate cryptographic operations. The engine would initiate those operations
128 asynchronously and then call ASYNC_pause_job() to return control to the user
129 code. The user code can then perform other tasks or wait for the job to be ready
130 by calling "select" or other similar function on the wait file descriptor. The
131 engine can signal to the user code that the job should be resumed using
132 ASYNC_wake(). Once resumed the engine would clear the wake signal by calling
133 ASYNC_clear_wake().
134
135 The ASYNC_block_pause() function will prevent the currently active job from
136 pausing. The block will remain in place until a subsequent call to
137 ASYNC_unblock_pause(). These functions can be nested, e.g. if you call
138 ASYNC_block_pause() twice then you must call ASYNC_unblock_pause() twice in
139 order to reenable pausing. If these functions are called while there is no
140 currently active job then they have no effect. This functionality can be useful
141 to avoid deadlock scenarios. For example during the execution of an ASYNC_JOB an
142 application aquires a lock. It then calls some cryptographic function which
143 invokes ASYNC_pause_job(). This returns control back to the code that created
144 the ASYNC_JOB. If that code then attempts to aquire the same lock before
145 resuming the original job then a deadlock can occur. By calling
146 ASYNC_block_pause() immediately after aquiring the lock and
147 ASYNC_unblock_pause() immediately before releasing it then this situation cannot
148 occur.
149
150 =head1 RETURN VALUES
151
152 ASYNC_init and ASYNC_init_thread return 1 on success or 0 otherwise.
153
154 ASYNC_start_job returns one of ASYNC_ERR, ASYNC_NO_JOBS, ASYNC_PAUSE or
155 ASYNC_FINISH as described above.
156
157 ASYNC_pause_job returns 0 if an error occured or 1 on success. If called when
158 not within the context of an ASYNC_JOB then this is counted as success so 1 is
159 returned.
160
161 ASYNC_get_wait_fd returns the "wait" file descriptor associated with the
162 ASYNC_JOB provided as an argument.
163
164 ASYNC_get_current_job returns a pointer to the currently executing ASYNC_JOB or
165 NULL if not within the context of a job.
166
167 =head1 EXAMPLE
168
169 The following example demonstrates how to use most of the core async APIs:
170
171  #include <stdio.h>
172  #include <openssl/async.h>
173
174  int jobfunc(void *arg)
175  {
176      ASYNC_JOB *currjob;
177      unsigned char *msg;
178
179      currjob = ASYNC_get_current_job();
180      if (currjob != NULL) {
181          printf("Executing within a job\n");
182      } else {
183          printf("Not executing within a job - should not happen\n");
184          return 0;
185      }
186
187      msg = (unsigned char *)arg;
188      printf("Passed in message is: %s\n", msg);
189
190      /*
191       * Normally some external event would cause this to happen at some
192       * later point - but we do it here for demo purposes, i.e.
193       * immediately signalling that the job is ready to be woken up after
194       * we return to main via ASYNC_pause_job().
195       */
196      ASYNC_wake(currjob);
197
198      /* Return control back to main */
199      ASYNC_pause_job();
200
201      /* Clear the wake signal */
202      ASYNC_clear_wake(currjob);
203
204      printf ("Resumed the job after a pause\n");
205
206      return 1;
207  }
208
209  int main(void)
210  {
211      ASYNC_JOB *job = NULL;
212      int ret, waitfd;
213      fd_set waitfdset;
214      unsigned char msg[13] = "Hello world!";
215
216      /*
217       * We're only expecting 1 job to be used here so we're only creating
218       * a pool of 1
219       */
220      if (!ASYNC_init(1, 1, 1)) {
221          printf("Error creating pool\n");
222          goto end;
223      }
224
225      printf("Starting...\n");
226
227      for (;;) {
228          switch(ASYNC_start_job(&job, &ret, jobfunc, msg, sizeof(msg))) {
229          case ASYNC_ERR:
230          case ASYNC_NO_JOBS:
231                  printf("An error occurred\n");
232                  goto end;
233          case ASYNC_PAUSE:
234                  printf("Job was paused\n");
235                  break;
236          case ASYNC_FINISH:
237                  printf("Job finished with return value %d\n", ret);
238                  goto end;
239          }
240
241          /* Wait for the job to be woken */
242          printf("Waiting for the job to be woken up\n");
243          waitfd = ASYNC_get_wait_fd(job);
244          FD_ZERO(&waitfdset);
245          FD_SET(waitfd, &waitfdset);
246          select(waitfd + 1, &waitfdset, NULL, NULL, NULL);
247      }
248
249  end:
250      printf("Finishing\n");
251      ASYNC_cleanup(1);
252
253      return 0;
254  }
255
256 The expected output from executing the above example program is:
257
258  Starting...
259  Executing within a job
260  Passed in message is: Hello world!
261  Job was paused
262  Waiting for the job to be woken up
263  Resumed the job after a pause
264  Job finished with return value 1
265  Finishing
266
267 =head1 SEE ALSO
268
269 L<crypto(3)>, L<ERR_print_errors(3)>
270
271 =head1 HISTORY
272
273 ASYNC_init, ASYNC_init_thread, ASYNC_cleanup, ASYNC_cleanup_thread,
274 ASYNC_start_job, ASYNC_pause_job, ASYNC_get_wait_fd, ASYNC_get_current_job,
275 ASYNC_wake, ASYNC_clear_wake were first added to OpenSSL 1.1.0.
276
277 =cut