Standard preamble:
========================================================================
..
.... Set up some character translations and predefined strings. \*(-- will
give an unbreakable dash, \*(PI will give pi, \*(L" will give a left
double quote, and \*(R" will give a right double quote. \*(C+ will
give a nicer C++. Capital omega is used to do unbreakable dashes and
therefore won't be available. \*(C` and \*(C' expand to `' in nroff,
nothing in troff, for use with C<>.
.tr \(*W- . ds -- \(*W- . ds PI pi . if (\n(.H=4u)&(1m=24u) .ds -- \(*W\h'-12u'\(*W\h'-12u'-\" diablo 10 pitch . if (\n(.H=4u)&(1m=20u) .ds -- \(*W\h'-12u'\(*W\h'-8u'-\" diablo 12 pitch . ds L" "" . ds R" "" . ds C` "" . ds C' "" 'br\} . ds -- \|\(em\| . ds PI \(*p . ds L" `` . ds R" '' . ds C` . ds C' 'br\}
Escape single quotes in literal strings from groff's Unicode transform.
If the F register is >0, we'll generate index entries on stderr for
titles (.TH), headers (.SH), subsections (.SS), items (.Ip), and index
entries marked with X<> in POD. Of course, you'll have to process the
output yourself in some meaningful fashion.
Avoid warning from groff about undefined register 'F'.
.. .nr rF 0 . if \nF \{\ . de IX . tm Index:\\$1\t\\n%\t"\\$2" .. . if !\nF==2 \{\ . nr % 0 . nr F 2 . \} . \} .\} .rr rF
Accent mark definitions (@(#)ms.acc 1.5 88/02/08 SMI; from UCB 4.2).
Fear. Run. Save yourself. No user-serviceable parts.
. \" fudge factors for nroff and troff . ds #H 0 . ds #V .8m . ds #F .3m . ds #[ \f1 . ds #] .\} . ds #H ((1u-(\\\\n(.fu%2u))*.13m) . ds #V .6m . ds #F 0 . ds #[ \& . ds #] \& .\} . \" simple accents for nroff and troff . ds ' \& . ds ` \& . ds ^ \& . ds , \& . ds ~ ~ . ds / .\} . ds ' \\k:\h'-(\\n(.wu*8/10-\*(#H)'\'\h"|\\n:u" . ds ` \\k:\h'-(\\n(.wu*8/10-\*(#H)'\`\h'|\\n:u' . ds ^ \\k:\h'-(\\n(.wu*10/11-\*(#H)'^\h'|\\n:u' . ds , \\k:\h'-(\\n(.wu*8/10)',\h'|\\n:u' . ds ~ \\k:\h'-(\\n(.wu-\*(#H-.1m)'~\h'|\\n:u' . ds / \\k:\h'-(\\n(.wu*8/10-\*(#H)'\z\(sl\h'|\\n:u' .\} . \" troff and (daisy-wheel) nroff accents . \" corrections for vroff . \" for low resolution devices (crt and lpr) \{\ . ds : e . ds 8 ss . ds o a . ds d- d\h'-1'\(ga . ds D- D\h'-1'\(hy . ds th \o'bp' . ds Th \o'LP' . ds ae ae . ds Ae AE .\} ========================================================================
Title "ASYNC_START_JOB 3ossl"
way too many mistakes in technical documents.
The creation of an \s-1ASYNC_JOB\s0 is a relatively expensive operation. Therefore, for 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. If the user application is multi-threaded, then ASYNC_init_thread() may be called for each thread that will initiate asynchronous jobs. Before user code exits per-thread resources need to be cleaned up. This will normally occur automatically (see OPENSSL_init_crypto\|(3)) but may be explicitly initiated by 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.
The max_size argument limits the number of \s-1ASYNC_JOB\s0s that will be held in the pool. If max_size is set to 0 then no upper limit is set. When an \fB\s-1ASYNC_JOB\s0 is needed but there are none available in the pool already then one will be automatically created, as long as the total of \s-1ASYNC_JOB\s0s managed by the pool does not exceed max_size. When the pool is first initialised \fIinit_size \s-1ASYNC_JOB\s0s 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 max_size of 0 (no upper limit) and an init_size of 0 (no \fB\s-1ASYNC_JOB\s0s created up front).
An asynchronous job is started by calling the ASYNC_start_job() function. Initially *job should be \s-1NULL.\s0 ctx should point to an \s-1ASYNC_WAIT_CTX\s0 object created through the ASYNC_WAIT_CTX_new\|(3) function. ret should point to a location where the return value of the asynchronous function should be stored on completion of the job. func represents the function that should be started asynchronously. The data pointed to by args and of size size will be copied and then passed as an argument to func when the job starts. ASYNC_start_job will return one of the following values:
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 \s-1ASYNC_JOB\s0. If no job is currently executing then this will return \s-1NULL.\s0
If executing within the context of a job (i.e. having been called directly or indirectly by the function \*(L"func\*(R" passed as an argument to ASYNC_start_job()) then ASYNC_pause_job() will immediately return control to the calling application with \s-1ASYNC_PAUSE\s0 returned from the ASYNC_start_job() call. A subsequent call to ASYNC_start_job passing in the relevant \s-1ASYNC_JOB\s0 in the \fI*job parameter will resume execution from the ASYNC_pause_job() call. If \fBASYNC_pause_job() is called whilst not within the context of a job then no action is taken and ASYNC_pause_job() returns immediately.
\fBASYNC_get_wait_ctx() can be used to get a pointer to the \s-1ASYNC_WAIT_CTX\s0 for the job. \s-1ASYNC_WAIT_CTX\s0s contain two different ways to notify applications that a job is ready to be resumed. One is a \*(L"wait\*(R" file descriptor, and the other is a \*(L"callback\*(R" mechanism.
The \*(L"wait\*(R" file descriptor associated with \s-1ASYNC_WAIT_CTX\s0 is used for applications to wait for the file descriptor to be ready for \*(L"read\*(R" using a system function call such as select or poll (being ready for \*(L"read\*(R" indicates that the job should be resumed). If no file descriptor is made available then an application will have to periodically \*(L"poll\*(R" the job by attempting to restart it to see if it is ready to continue.
\fB\s-1ASYNC_WAIT_CTX\s0s also have a \*(L"callback\*(R" mechanism to notify applications. The callback is set by an application, and it will be automatically called when an engine completes a cryptography operation, so that the application can resume the paused work flow without polling. An engine could be written to look whether the callback has been set. If it has then it would use the callback mechanism in preference to the file descriptor notifications. If a callback is not set then the engine may use file descriptor based notifications. Please note that not all engines may support the callback mechanism, so the callback may not be used even if it has been set. See ASYNC_WAIT_CTX_new() for more details.
The ASYNC_block_pause() function will prevent the currently active job from pausing. The block will remain in place until a subsequent call to \fBASYNC_unblock_pause(). These functions can be nested, e.g. if you call \fBASYNC_block_pause() twice then you must call ASYNC_unblock_pause() twice in order to re-enable 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 \s-1ASYNC_JOB\s0 an application acquires a lock. It then calls some cryptographic function which invokes ASYNC_pause_job(). This returns control back to the code that created the \s-1ASYNC_JOB\s0. If that code then attempts to acquire the same lock before resuming the original job then a deadlock can occur. By calling \fBASYNC_block_pause() immediately after acquiring the lock and \fBASYNC_unblock_pause() immediately before releasing it then this situation cannot occur.
Some platforms cannot support async operations. The ASYNC_is_capable() function can be used to detect whether the current platform is async capable or not.
ASYNC_start_job returns one of \s-1ASYNC_ERR\s0, \s-1ASYNC_NO_JOBS\s0, \s-1ASYNC_PAUSE\s0 or \fB\s-1ASYNC_FINISH\s0 as described above.
ASYNC_pause_job returns 0 if an error occurred or 1 on success. If called when not within the context of an \s-1ASYNC_JOB\s0 then this is counted as success so 1 is returned.
ASYNC_get_current_job returns a pointer to the currently executing \s-1ASYNC_JOB\s0 or \s-1NULL\s0 if not within the context of a job.
\fBASYNC_get_wait_ctx() returns a pointer to the \s-1ASYNC_WAIT_CTX\s0 for the job.
\fBASYNC_is_capable() returns 1 if the current platform is async capable or 0 otherwise.
.Vb 7 #ifdef _WIN32 # include <windows.h> #endif #include <stdio.h> #include <unistd.h> #include <openssl/async.h> #include <openssl/crypto.h> \& int unique = 0; \& void cleanup(ASYNC_WAIT_CTX *ctx, const void *key, OSSL_ASYNC_FD r, void *vw) { OSSL_ASYNC_FD *w = (OSSL_ASYNC_FD *)vw; \& close(r); close(*w); OPENSSL_free(w); } \& int jobfunc(void *arg) { ASYNC_JOB *currjob; unsigned char *msg; int pipefds[2] = {0, 0}; OSSL_ASYNC_FD *wptr; char buf = \*(AqX\*(Aq; \& currjob = ASYNC_get_current_job(); if (currjob != NULL) { printf("Executing within a job\en"); } else { printf("Not executing within a job - should not happen\en"); return 0; } \& msg = (unsigned char *)arg; printf("Passed in message is: %s\en", msg); \& if (pipe(pipefds) != 0) { printf("Failed to create pipe\en"); return 0; } wptr = OPENSSL_malloc(sizeof(OSSL_ASYNC_FD)); if (wptr == NULL) { printf("Failed to malloc\en"); return 0; } *wptr = pipefds[1]; ASYNC_WAIT_CTX_set_wait_fd(ASYNC_get_wait_ctx(currjob), &unique, pipefds[0], wptr, cleanup); \& /* * Normally some external event would cause this to happen at some * later point - but we do it here for demo purposes, i.e. * immediately signalling that the job is ready to be woken up after * we return to main via ASYNC_pause_job(). */ write(pipefds[1], &buf, 1); \& /* Return control back to main */ ASYNC_pause_job(); \& /* Clear the wake signal */ read(pipefds[0], &buf, 1); \& printf ("Resumed the job after a pause\en"); \& return 1; } \& int main(void) { ASYNC_JOB *job = NULL; ASYNC_WAIT_CTX *ctx = NULL; int ret; OSSL_ASYNC_FD waitfd; fd_set waitfdset; size_t numfds; unsigned char msg[13] = "Hello world!"; \& printf("Starting...\en"); \& ctx = ASYNC_WAIT_CTX_new(); if (ctx == NULL) { printf("Failed to create ASYNC_WAIT_CTX\en"); abort(); } \& for (;;) { switch (ASYNC_start_job(&job, ctx, &ret, jobfunc, msg, sizeof(msg))) { case ASYNC_ERR: case ASYNC_NO_JOBS: printf("An error occurred\en"); goto end; case ASYNC_PAUSE: printf("Job was paused\en"); break; case ASYNC_FINISH: printf("Job finished with return value %d\en", ret); goto end; } \& /* Wait for the job to be woken */ printf("Waiting for the job to be woken up\en"); \& if (!ASYNC_WAIT_CTX_get_all_fds(ctx, NULL, &numfds) || numfds > 1) { printf("Unexpected number of fds\en"); abort(); } ASYNC_WAIT_CTX_get_all_fds(ctx, &waitfd, &numfds); FD_ZERO(&waitfdset); FD_SET(waitfd, &waitfdset); select(waitfd + 1, &waitfdset, NULL, NULL, NULL); } \& end: ASYNC_WAIT_CTX_free(ctx); printf("Finishing\en"); \& return 0; } .Ve
The expected output from executing the above example program is:
.Vb 8 Starting... Executing within a job Passed in message is: Hello world! Job was paused Waiting for the job to be woken up Resumed the job after a pause Job finished with return value 1 Finishing .Ve
Licensed under the Apache License 2.0 (the \*(L"License\*(R"). You may not use this file except in compliance with the License. You can obtain a copy in the file \s-1LICENSE\s0 in the source distribution or at <https://www.openssl.org/source/license.html>.