ASYNC_start_job.pod 12 KB

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