ASYNC_START_JOB(3ossl)
πΈ NAME πΈ
okay bestie, welcome to ASYNC_START_JOB(3ossl) β the OpenSSL page for asynchronous job management functions. This is the whole squad:
ASYNC_get_wait_ctxASYNC_init_threadASYNC_cleanup_threadASYNC_start_jobASYNC_pause_jobASYNC_get_current_jobASYNC_block_pauseASYNC_unblock_pauseASYNC_is_capableASYNC_stack_alloc_fnASYNC_stack_free_fnASYNC_set_mem_functionsASYNC_get_mem_functions
These are the asynchronous job management functions. Periodt. π
β¨ SYNOPSIS β¨
#include <openssl/async.h>
int ASYNC_init_thread(size_t max_size, size_t init_size);
void ASYNC_cleanup_thread(void);
int ASYNC_start_job(ASYNC_JOB **job, ASYNC_WAIT_CTX *ctx, int *ret,
int (*func)(void *), void *args, size_t size);
int ASYNC_pause_job(void);
ASYNC_JOB *ASYNC_get_current_job(void);
ASYNC_WAIT_CTX *ASYNC_get_wait_ctx(ASYNC_JOB *job);
void ASYNC_block_pause(void);
void ASYNC_unblock_pause(void);
int ASYNC_is_capable(void);
typedef void *(*ASYNC_stack_alloc_fn)(size_t *num);
typedef void (*ASYNC_stack_free_fn)(void *addr);
int ASYNC_set_mem_functions(ASYNC_stack_alloc_fn alloc_fn,
ASYNC_stack_free_fn free_fn);
void ASYNC_get_mem_functions(ASYNC_stack_alloc_fn *alloc_fn,
ASYNC_stack_free_fn *free_fn);
π DESCRIPTION π
okay so OpenSSL does its async thing through an ASYNC_JOB. Think of it as code that can be started and run until some event occurs. At that point the code can pause, and control returns to user code until some later event says βbestie, you can resume now.β Itβs OpenSSLβs specific implementation of cooperative multitasking. Slay. β¨
The creation of an ASYNC_JOB is a relatively expensive operation. So, for efficiency reasons, jobs can be created up front and reused many times. Theyβre held in a pool until theyβre needed, at which point theyβre 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. like, do not do that, bestie π
The max_size argument limits the number of ASYNC_JOBs that will be held in the pool. If 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 max_size. When the pool is first initialised, 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 max_size of 0 (no upper limit) and an init_size of 0 (no ASYNC_JOBs created up front).
An asynchronous job is started by calling the ASYNC_start_job() function. Initially *job should be NULL. ctx should point to an ASYNC_WAIT_CTX 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:
ASYNC_ERRβ an error occurred trying to start the job. Check the OpenSSL error queue (e.g. seeERR_print_errors(3)) for more details. πASYNC_NO_JOBSβ there are no jobs currently available in the pool. This call can be retried again at a later time. β³ASYNC_PAUSEβ the job was successfully started but was βpausedβ before it completed (seeASYNC_pause_job()below). A handle to the job is placed in*job. Other work can be performed (if desired) and the job restarted at a later time. To restart a job, callASYNC_start_job()again passing the job handle in*job. Thefunc,argsandsizeparameters will be ignored when restarting a job. When restarting a job,ASYNC_start_job()must be called from the same thread that the job was originally started from.ASYNC_WAIT_CTXis used to know when a job is ready to be restarted. π¦ASYNC_FINISHβ the job completed.*jobwill beNULLand the return value fromfuncwill be placed in*ret. π
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()) then ASYNC_pause_job() will immediately return control to the calling application with ASYNC_PAUSE returned from the ASYNC_start_job() call. A subsequent call to ASYNC_start_job passing in the relevant ASYNC_JOB in the *job parameter will resume execution from the ASYNC_pause_job() call. If ASYNC_pause_job() is called whilst not within the context of a job then no action is taken and ASYNC_pause_job() returns immediately.
ASYNC_get_wait_ctx() can be used to get a pointer to the ASYNC_WAIT_CTX for the job (see ASYNC_WAIT_CTX_new(3)). ASYNC_WAIT_CTXs contain two different ways to notify applications that a job is ready to be resumed. One is a βwaitβ file descriptor, and the other is a βcallbackβ mechanism. π
The βwaitβ file descriptor associated with ASYNC_WAIT_CTX is used for applications to wait for the file descriptor to be ready for βreadβ using a system function call such as select(2) or poll(2) (being ready for βreadβ indicates that the job should be resumed). If no file descriptor is made available then an application will have to periodically βpollβ the job by attempting to restart it to see if it is ready to continue. π
ASYNC_WAIT_CTXs also have a βcallbackβ 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 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 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 ASYNC_JOB 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 ASYNC_JOB. If that code then attempts to acquire the same lock before resuming the original job then a deadlock can occur. By calling ASYNC_block_pause() immediately after acquiring the lock and ASYNC_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. π
Custom memory allocation functions are supported for the POSIX platform. Custom memory allocation functions allow alternative methods of allocating stack memory such as mmap, or using stack memory from the current thread. Using an ASYNC_stack_alloc_fn callback also allows manipulation of the stack size, which defaults to 32k. The stack size can be altered by allocating a stack of a size different to the requested size, and passing back the new stack size in the callbackβs *num parameter. π§
π«Ά RETURN VALUES π«Ά
ASYNC_init_threadreturns1on success or0otherwise.ASYNC_start_jobreturns one ofASYNC_ERR,ASYNC_NO_JOBS,ASYNC_PAUSEorASYNC_FINISHas described above.ASYNC_pause_jobreturns0if an error occurred or1on success. If called when not within the context of anASYNC_JOBthen this is counted as success so1is returned.ASYNC_get_current_jobreturns a pointer to the currently executingASYNC_JOBorNULLif not within the context of a job.ASYNC_get_wait_ctx()returns a pointer to theASYNC_WAIT_CTXfor the job.ASYNC_is_capable()returns1if the current platform is async capable or0otherwise.ASYNC_set_mem_functionsreturns1if custom stack allocators are supported by the current platform and no allocations have already occurred or0otherwise.
π NOTES π
On Windows platforms the <openssl/async.h> header is dependent on some of the types customarily made available by including <windows.h>. The application developer is likely to require control over when the latter is included, commonly as one of the first included headers. Therefore, it is defined as an application developerβs responsibility to include <windows.h> prior to <openssl/async.h>. πͺβ¨
β¨ EXAMPLES β¨
ok bestie this is the part you actually came for π here's a whole working program that flexes basically all the core async APIs at once. like, it's giving "show don't tell" and honestly? it ate.
#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 = 'X';
currjob = ASYNC_get_current_job();
if (currjob != NULL) {
printf("Executing within a job\n");
} else {
printf("Not executing within a job - should not happen\n");
return 0;
}
msg = (unsigned char *)arg;
printf("Passed in message is: %s\n", msg);
/*
* Create a way to inform the calling thread when this job is ready
* to resume, in this example we're using file descriptors.
* For offloading the task to an asynchronous ENGINE it's not necessary,
* the ENGINE should handle that internally.
*/
if (pipe(pipefds) != 0) {
printf("Failed to create pipe\n");
return 0;
}
wptr = OPENSSL_malloc(sizeof(OSSL_ASYNC_FD));
if (wptr == NULL) {
printf("Failed to malloc\n");
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 (like a network read being ready,
* disk access being finished, or some hardware offload operation
* completing) 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 just before calling a blocking
* method. The main thread will wait until pipefds[0] is ready
* for reading before returning control to this thread.
*/
ASYNC_pause_job();
/* Perform the blocking call (it won't block with this example code) */
read(pipefds[0], &buf, 1);
printf ("Resumed the job after a pause\n");
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...\n");
ctx = ASYNC_WAIT_CTX_new();
if (ctx == NULL) {
printf("Failed to create ASYNC_WAIT_CTX\n");
abort();
}
for (;;) {
switch (ASYNC_start_job(&job, ctx, &ret, jobfunc, msg, sizeof(msg))) {
case ASYNC_ERR:
case ASYNC_NO_JOBS:
printf("An error occurred\n");
goto end;
case ASYNC_PAUSE:
printf("Job was paused\n");
break;
case ASYNC_FINISH:
printf("Job finished with return value %d\n", ret);
goto end;
}
/* Get the file descriptor we can use to wait for the job
* to be ready to be woken up
*/
printf("Waiting for the job to be woken up\n");
if (!ASYNC_WAIT_CTX_get_all_fds(ctx, NULL, &numfds)
|| numfds > 1) {
printf("Unexpected number of fds\n");
abort();
}
ASYNC_WAIT_CTX_get_all_fds(ctx, &waitfd, &numfds);
FD_ZERO(&waitfdset);
FD_SET(waitfd, &waitfdset);
/* Wait for the job to be ready for wakeup */
select(waitfd + 1, &waitfdset, NULL, NULL, NULL);
}
end:
ASYNC_WAIT_CTX_free(ctx);
printf("Finishing\n");
return 0;
}
and like, if you run it, this is exactly what you should see π no cap, this is the expected output:
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
π SEE ALSO π
crypto(7), ERR_print_errors(3)
like go read those too, iykyk π«Ά
π¦ HISTORY π¦
ok so timeline check bestie π β ASYNC_init_thread, ASYNC_cleanup_thread, ASYNC_start_job, ASYNC_pause_job, ASYNC_get_current_job, ASYNC_get_wait_ctx(), ASYNC_block_pause(), ASYNC_unblock_pause() and ASYNC_is_capable() all first showed up in OpenSSL 1.1.0. then much later, ASYNC_set_mem_functions() and ASYNC_get_mem_functions() joined the party in OpenSSL 3.2. late to the group chat but we love them anyway π
πΈ COPYRIGHT πΈ
Copyright 2015-2024 The OpenSSL Project Authors. All Rights Reserved.
Licensed under the Apache License 2.0 (the "License"). You may not use this file except in compliance with the License. You can obtain a copy in the file LICENSE in the source distribution or at https://www.openssl.org/source/license.html.
and that's the whole page, bestie π§β¨ you made it to the end!! this one's from version 3.5.6, dated 2026-04-07 β go forth and pause some jobs responsibly ππ
π¦ where this page comes from
- freebsd 15.1: base 15.1
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms