Check chain extensions also for trusted certificates
[openssl.git] / doc / crypto / ASYNC_start_job.pod
index ec896340f8c11902f31f98f9089b8f9b322097f7..256cc4372ef6cd877001b3460a27a56a5aa578e6 100644 (file)
@@ -2,16 +2,20 @@
 
 =head1 NAME
 
-ASYNC_init_pool, ASYNC_free_pool, ASYNC_start_job, ASYNC_pause_job,
-ASYNC_in_job, ASYNC_get_wait_fd, ASYNC_get_current_job, ASYNC_wake,
-ASYNC_clear_wake - asynchronous job management functions
+ASYNC_init, ASYNC_cleanup, ASYNC_init_thread, ASYNC_cleanup_thread,
+ASYNC_start_job, ASYNC_pause_job, ASYNC_in_job, ASYNC_get_wait_fd,
+ASYNC_get_current_job, ASYNC_wake, ASYNC_clear_wake, ASYNC_block_pause,
+ASYNC_unblock_pause - asynchronous job management functions
 
 =head1 SYNOPSIS
 
  #include <openssl/async.h>
 
- int ASYNC_init_pool(size_t max_size, size_t init_size);
- void ASYNC_free_pool(void);
+ int ASYNC_init(int init_thread, size_t max_size, size_t init_size);
+ void ASYNC_cleanup(int cleanupthread);
+
+ int ASYNC_init_thread(size_t max_size, size_t init_size);
+ void ASYNC_cleanup_thread(void);
 
  int ASYNC_start_job(ASYNC_JOB **job, int *ret, int (*func)(void *),
                      void *args, size_t size);
@@ -21,6 +25,8 @@ ASYNC_clear_wake - asynchronous job management functions
  ASYNC_JOB *ASYNC_get_current_job(void);
  void ASYNC_wake(ASYNC_JOB *job);
  void ASYNC_clear_wake(ASYNC_JOB *job);
+ void ASYNC_block_pause(void);
+ void ASYNC_unblock_pause(void);
 
 =head1 DESCRIPTION
 
@@ -34,19 +40,28 @@ efficiency reasons, jobs can be created up front and reused many times. They are
 held in a pool until they are needed, at which point they are removed from the
 pool, used, and then returned to the pool when the job completes. Before using
 any of the asynchronous job functions, user code should first call
-ASYNC_init_pool(). If the user application is multi-threaded, then this should
-be done for each thread that will initiate asynchronous jobs. Before user code
-exits it should free the pool up (for each thread where a pool was initialised)
-using ASYNC_free_pool(). The B<max_size> argument limits the number of
-ASYNC_JOBs that will be held in the pool. If B<max_size> is set to 0 then no
-upper limit is set. When an ASYNC_JOB is needed but there are none available in
-the pool already then one will be automatically created, as long as the total
-of ASYNC_JOBs managed by the pool does not exceed B<max_size>. When the pool is
-first initialised B<init_size> ASYNC_JOBs will be created immediately. If
-ASYNC_init_pool() is not called before the pool is first used then it will be
-called automatically with a B<max_size> of 0 (no upper limit) and an
-B<init_size> of 0 (no ASYNC_JOBs created up front). If a pool is created in this
-way it must still be cleaned up with an explicit call to ASYNC_free_pool().
+ASYNC_init(). If the user application is multi-threaded, then
+ASYNC_init_thread() should be called for each thread that will initiate
+asynchronous jobs. If the B<init_thread> parameter to ASYNC_init() is non-zero
+then ASYNC_init_thread is automatically called for the current thread. Before
+user code exits it should free up resources for each thread that was initialised
+using ASYNC_cleanup_thread(). No asynchronous jobs must be outstanding for the thread
+when ASYNC_cleanup_thread() is called. Failing to ensure this will result in memory
+leaks. Additionally an application should call ASYNC_cleanup() when all
+asynchronous work is complete across all threads. If B<cleanupthread> is
+non-zero then ASYNC_cleanup_thread() is automatically called for the current
+thread.
+
+The B<max_size> argument limits the number of ASYNC_JOBs that will be held in
+the pool. If B<max_size> is set to 0 then no upper limit is set. When an
+ASYNC_JOB is needed but there are none available in the pool already then one
+will be automatically created, as long as the total of ASYNC_JOBs managed by the
+pool does not exceed B<max_size>. When the pool is first initialised
+B<init_size> ASYNC_JOBs will be created immediately. If ASYNC_init_thread() is
+not called before the pool is first used then it will be called automatically
+with a B<max_size> of 0 (no upper limit) and an B<init_size> of 0 (no ASYNC_JOBs
+created up front). If a pool is created in this way it must still be cleaned up
+with an explicit call to ASYNC_cleanup_thread().
 
 An asynchronous job is started by calling the ASYNC_start_job() function.
 Initially B<*job> should be NULL. B<ret> should point to a location where the
@@ -75,6 +90,8 @@ ASYNC_pause_job() below). A handle to the job is placed in B<*job>. Other work
 can be performed (if desired) and the job restarted at a later time. To restart
 a job call ASYNC_start_job() again passing the job handle in B<*job>. The
 B<func>, B<args> and B<size> parameters will be ignored when restarting a job.
+When restarting a job ASYNC_start_job() B<must> be called from the same thread
+that the job was originally started from.
 
 =item B<ASYNC_FINISH>
 
@@ -83,8 +100,10 @@ be placed in B<*ret>.
 
 =back
 
-ASYNC_get_current_job() can be used to get a pointer to the currently executing
-ASYNC_JOB. If no job is currently executing then this will return NULL.
+At any one time there can be a maximum of one job actively running per thread
+(you can have many that are paused). ASYNC_get_current_job() can be used to get
+a pointer to the currently executing ASYNC_JOB. If no job is currently executing
+then this will return NULL.
 
 If executing within the context of a job (i.e. having been called directly or
 indirectly by the function "func" passed as an argument to ASYNC_start_job())
@@ -99,9 +118,10 @@ Every ASYNC_JOB has a "wait" file descriptor associated with it. Calling
 ASYNC_get_wait_fd() and passing in a pointer to an ASYNC_JOB in the B<job>
 parameter will return the wait file descriptor associated with that job. This
 file descriptor can be used to signal that the job should be resumed.
-Applications can wait on the file descriptor using a system function call
-such as select or poll. Applications can signal that a job is ready to resume
-using ASYNC_wake() or clear an existing signal using ASYNC_clear_wake().
+Applications can wait for the file descriptor to be ready for "read" using a
+system function call such as select or poll (being ready for "read" indicates
+that the job should be resumed). Applications can signal that a job is ready to
+resume using ASYNC_wake() or clear an existing signal using ASYNC_clear_wake().
 
 An example of typical usage might be an async capable engine. User code would
 initiate cryptographic operations. The engine would initiate those operations
@@ -109,19 +129,34 @@ asynchronously and then call ASYNC_pause_job() to return control to the user
 code. The user code can then perform other tasks or wait for the job to be ready
 by calling "select" or other similar function on the wait file descriptor. The
 engine can signal to the user code that the job should be resumed using
-ASYNC_wait(). Once resumed the engine would clear the wake signal by calling
+ASYNC_wake(). Once resumed the engine would clear the wake signal by calling
 ASYNC_clear_wake().
 
+The ASYNC_block_pause() function will prevent the currently active job from
+pausing. The block will remain in place until a subsequent call to
+ASYNC_unblock_pause(). These functions can be nested, e.g. if you call
+ASYNC_block_pause() twice then you must call ASYNC_unblock_pause() twice in
+order to reenable pausing. If these functions are called while there is no
+currently active job then they have no effect. This functionality can be useful
+to avoid deadlock scenarios. For example during the execution of an ASYNC_JOB an
+application aquires a lock. It then calls some cryptographic function which
+invokes ASYNC_pause_job(). This returns control back to the code that created
+the ASYNC_JOB. If that code then attempts to aquire the same lock before
+resuming the original job then a deadlock can occur. By calling
+ASYNC_block_pause() immediately after aquiring the lock and
+ASYNC_unblock_pause() immediately before releasing it then this situation cannot
+occur.
 
 =head1 RETURN VALUES
 
-ASYNC_init_pool returns 1 on success or 0 otherwise.
+ASYNC_init and ASYNC_init_thread return 1 on success or 0 otherwise.
 
 ASYNC_start_job returns one of ASYNC_ERR, ASYNC_NO_JOBS, ASYNC_PAUSE or
 ASYNC_FINISH as described above.
 
-ASYNC_pause_job returns 0 if an error occured (including if called when not
-within the context of an ASYNC_JOB), or 1 on success.
+ASYNC_pause_job returns 0 if an error occured or 1 on success. If called when
+not within the context of an ASYNC_JOB then this is counted as success so 1 is
+returned.
 
 ASYNC_get_wait_fd returns the "wait" file descriptor associated with the
 ASYNC_JOB provided as an argument.
@@ -182,7 +217,7 @@ The following example demonstrates how to use most of the core async APIs:
       * We're only expecting 1 job to be used here so we're only creating
       * a pool of 1
       */
-     if (!ASYNC_init_pool(1, 1)) {
+     if (!ASYNC_init(1, 1, 1)) {
          printf("Error creating pool\n");
          goto end;
      }
@@ -213,7 +248,7 @@ The following example demonstrates how to use most of the core async APIs:
 
  end:
      printf("Finishing\n");
-     ASYNC_free_pool();
+     ASYNC_cleanup(1);
 
      return 0;
  }
@@ -235,8 +270,8 @@ L<crypto(3)>, L<ERR_print_errors(3)>
 
 =head1 HISTORY
 
-ASYNC_init_pool, ASYNC_free_pool, ASYNC_start_job, ASYNC_pause_job,
-ASYNC_get_wait_fd, ASYNC_get_current_job, ASYNC_wake, ASYNC_clear_wake were
-first added to OpenSSL 1.1.0.
+ASYNC_init, ASYNC_init_thread, ASYNC_cleanup, ASYNC_cleanup_thread,
+ASYNC_start_job, ASYNC_pause_job, ASYNC_get_wait_fd, ASYNC_get_current_job,
+ASYNC_wake, ASYNC_clear_wake were first added to OpenSSL 1.1.0.
 
 =cut