Add clarification to docs on ASYNC_free_pool()
[openssl.git] / doc / crypto / ASYNC_start_job.pod
1 =pod
2
3 =head1 NAME
4
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
8
9 =head1 SYNOPSIS
10
11  #include <openssl/async.h>
12
13  int ASYNC_init_pool(size_t max_size, size_t init_size);
14  void ASYNC_free_pool(void);
15
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);
19
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);
24
25 =head1 DESCRIPTION
26
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.
31
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
42 leaks.
43
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().
54
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
61 the following values:
62
63 =over 4
64
65 =item B<ASYNC_ERR>
66
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.
69
70 =item B<ASYNC_NO_JOBS>
71
72 There are no jobs currently available in the pool. This call can be retried
73 again at a later time.
74
75 =item B<ASYNC_PAUSE>
76
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.
84
85 =item B<ASYNC_FINISH>
86
87 The job completed. B<*job> will be NULL and the return value from B<func> will
88 be placed in B<*ret>.
89
90 =back
91
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.
96
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.
105
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().
114
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
122 ASYNC_clear_wake().
123
124
125 =head1 RETURN VALUES
126
127 ASYNC_init_pool returns 1 on success or 0 otherwise.
128
129 ASYNC_start_job returns one of ASYNC_ERR, ASYNC_NO_JOBS, ASYNC_PAUSE or
130 ASYNC_FINISH as described above.
131
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
134 returned.
135
136 ASYNC_get_wait_fd returns the "wait" file descriptor associated with the
137 ASYNC_JOB provided as an argument.
138
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.
141
142 =head1 EXAMPLE
143
144 The following example demonstrates how to use most of the core async APIs:
145
146  #include <stdio.h>
147  #include <openssl/async.h>
148
149  int jobfunc(void *arg)
150  {
151      ASYNC_JOB *currjob;
152      unsigned char *msg;
153
154      currjob = ASYNC_get_current_job();
155      if (currjob != NULL) {
156          printf("Executing within a job\n");
157      } else {
158          printf("Not executing within a job - should not happen\n");
159          return 0;
160      }
161
162      msg = (unsigned char *)arg;
163      printf("Passed in message is: %s\n", msg);
164
165      /*
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().
170       */
171      ASYNC_wake(currjob);
172
173      /* Return control back to main */
174      ASYNC_pause_job();
175
176      /* Clear the wake signal */
177      ASYNC_clear_wake(currjob);
178
179      printf ("Resumed the job after a pause\n");
180
181      return 1;
182  }
183
184  int main(void)
185  {
186      ASYNC_JOB *job = NULL;
187      int ret, waitfd;
188      fd_set waitfdset;
189      unsigned char msg[13] = "Hello world!";
190
191      /*
192       * We're only expecting 1 job to be used here so we're only creating
193       * a pool of 1
194       */
195      if (!ASYNC_init_pool(1, 1)) {
196          printf("Error creating pool\n");
197          goto end;
198      }
199
200      printf("Starting...\n");
201
202      for (;;) {
203          switch(ASYNC_start_job(&job, &ret, jobfunc, msg, sizeof(msg))) {
204          case ASYNC_ERR:
205          case ASYNC_NO_JOBS:
206                  printf("An error occurred\n");
207                  goto end;
208          case ASYNC_PAUSE:
209                  printf("Job was paused\n");
210                  break;
211          case ASYNC_FINISH:
212                  printf("Job finished with return value %d\n", ret);
213                  goto end;
214          }
215
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);
219          FD_ZERO(&waitfdset);
220          FD_SET(waitfd, &waitfdset);
221          select(waitfd + 1, &waitfdset, NULL, NULL, NULL);
222      }
223
224  end:
225      printf("Finishing\n");
226      ASYNC_free_pool();
227
228      return 0;
229  }
230
231 The expected output from executing the above example program is:
232
233  Starting...
234  Executing within a job
235  Passed in message is: Hello world!
236  Job was paused
237  Waiting for the job to be woken up
238  Resumed the job after a pause
239  Job finished with return value 1
240  Finishing
241
242 =head1 SEE ALSO
243
244 L<crypto(3)>, L<ERR_print_errors(3)>
245
246 =head1 HISTORY
247
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.
251
252 =cut