6086260ff53f5f78a2dd611a893031051b8d8333
[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(). 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().
50
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
57 the following values:
58
59 =over 4
60
61 =item B<ASYNC_ERR>
62
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.
65
66 =item B<ASYNC_NO_JOBS>
67
68 There are no jobs currently available in the pool. This call can be retried
69 again at a later time.
70
71 =item B<ASYNC_PAUSE>
72
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.
78 When restarting a job ASYNC_start_job() B<must> be called from the same thread
79 that the job was originally started from.
80
81 =item B<ASYNC_FINISH>
82
83 The job completed. B<*job> will be NULL and the return value from B<func> will
84 be placed in B<*ret>.
85
86 =back
87
88 At any one time there can be a maximum of one job actively running per thread
89 (you can have many that are paused). ASYNC_get_current_job() can be used to get
90 a pointer to the currently executing ASYNC_JOB. If no job is currently executing
91 then this will return NULL.
92
93 If executing within the context of a job (i.e. having been called directly or
94 indirectly by the function "func" passed as an argument to ASYNC_start_job())
95 then ASYNC_pause_job() will immediately return control to the calling
96 application with ASYNC_PAUSE returned from the ASYNC_start_job() call. A
97 subsequent call to ASYNC_start_job passing in the relevant ASYNC_JOB in the
98 B<*job> parameter will resume execution from the ASYNC_pause_job() call. If
99 ASYNC_pause_job() is called whilst not within the context of a job then no
100 action is taken and ASYNC_pause_job() returns immediately.
101
102 Every ASYNC_JOB has a "wait" file descriptor associated with it. Calling
103 ASYNC_get_wait_fd() and passing in a pointer to an ASYNC_JOB in the B<job>
104 parameter will return the wait file descriptor associated with that job. This
105 file descriptor can be used to signal that the job should be resumed.
106 Applications can wait for the file descriptor to be ready for "read" using a
107 system function call such as select or poll (being ready for "read" indicates
108 that the job should be resumed). Applications can signal that a job is ready to
109 resume using ASYNC_wake() or clear an existing signal using ASYNC_clear_wake().
110
111 An example of typical usage might be an async capable engine. User code would
112 initiate cryptographic operations. The engine would initiate those operations
113 asynchronously and then call ASYNC_pause_job() to return control to the user
114 code. The user code can then perform other tasks or wait for the job to be ready
115 by calling "select" or other similar function on the wait file descriptor. The
116 engine can signal to the user code that the job should be resumed using
117 ASYNC_wake(). Once resumed the engine would clear the wake signal by calling
118 ASYNC_clear_wake().
119
120
121 =head1 RETURN VALUES
122
123 ASYNC_init_pool returns 1 on success or 0 otherwise.
124
125 ASYNC_start_job returns one of ASYNC_ERR, ASYNC_NO_JOBS, ASYNC_PAUSE or
126 ASYNC_FINISH as described above.
127
128 ASYNC_pause_job returns 0 if an error occured or 1 on success. If called when
129 not within the context of an ASYNC_JOB then this is counted as success so 1 is
130 returned.
131
132 ASYNC_get_wait_fd returns the "wait" file descriptor associated with the
133 ASYNC_JOB provided as an argument.
134
135 ASYNC_get_current_job returns a pointer to the currently executing ASYNC_JOB or
136 NULL if not within the context of a job.
137
138 =head1 EXAMPLE
139
140 The following example demonstrates how to use most of the core async APIs:
141
142  #include <stdio.h>
143  #include <openssl/async.h>
144
145  int jobfunc(void *arg)
146  {
147      ASYNC_JOB *currjob;
148      unsigned char *msg;
149
150      currjob = ASYNC_get_current_job();
151      if (currjob != NULL) {
152          printf("Executing within a job\n");
153      } else {
154          printf("Not executing within a job - should not happen\n");
155          return 0;
156      }
157
158      msg = (unsigned char *)arg;
159      printf("Passed in message is: %s\n", msg);
160
161      /*
162       * Normally some external event would cause this to happen at some
163       * later point - but we do it here for demo purposes, i.e.
164       * immediately signalling that the job is ready to be woken up after
165       * we return to main via ASYNC_pause_job().
166       */
167      ASYNC_wake(currjob);
168
169      /* Return control back to main */
170      ASYNC_pause_job();
171
172      /* Clear the wake signal */
173      ASYNC_clear_wake(currjob);
174
175      printf ("Resumed the job after a pause\n");
176
177      return 1;
178  }
179
180  int main(void)
181  {
182      ASYNC_JOB *job = NULL;
183      int ret, waitfd;
184      fd_set waitfdset;
185      unsigned char msg[13] = "Hello world!";
186
187      /*
188       * We're only expecting 1 job to be used here so we're only creating
189       * a pool of 1
190       */
191      if (!ASYNC_init_pool(1, 1)) {
192          printf("Error creating pool\n");
193          goto end;
194      }
195
196      printf("Starting...\n");
197
198      for (;;) {
199          switch(ASYNC_start_job(&job, &ret, jobfunc, msg, sizeof(msg))) {
200          case ASYNC_ERR:
201          case ASYNC_NO_JOBS:
202                  printf("An error occurred\n");
203                  goto end;
204          case ASYNC_PAUSE:
205                  printf("Job was paused\n");
206                  break;
207          case ASYNC_FINISH:
208                  printf("Job finished with return value %d\n", ret);
209                  goto end;
210          }
211
212          /* Wait for the job to be woken */
213          printf("Waiting for the job to be woken up\n");
214          waitfd = ASYNC_get_wait_fd(job);
215          FD_ZERO(&waitfdset);
216          FD_SET(waitfd, &waitfdset);
217          select(waitfd + 1, &waitfdset, NULL, NULL, NULL);
218      }
219
220  end:
221      printf("Finishing\n");
222      ASYNC_free_pool();
223
224      return 0;
225  }
226
227 The expected output from executing the above example program is:
228
229  Starting...
230  Executing within a job
231  Passed in message is: Hello world!
232  Job was paused
233  Waiting for the job to be woken up
234  Resumed the job after a pause
235  Job finished with return value 1
236  Finishing
237
238 =head1 SEE ALSO
239
240 L<crypto(3)>, L<ERR_print_errors(3)>
241
242 =head1 HISTORY
243
244 ASYNC_init_pool, ASYNC_free_pool, ASYNC_start_job, ASYNC_pause_job,
245 ASYNC_get_wait_fd, ASYNC_get_current_job, ASYNC_wake, ASYNC_clear_wake were
246 first added to OpenSSL 1.1.0.
247
248 =cut