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