-/* crypto/async/arch/async_posix.c */
/*
* Written by Matt Caswell (matt@openssl.org) for the OpenSSL project.
*/
* ====================================================================
*/
+/* This must be the first #include file */
#include "../async_locl.h"
-#include <openssl/async.h>
-#ifdef ASYNC_SYSV
+#ifdef ASYNC_POSIX
+
# include <stddef.h>
-# include <ucontext.h>
# include <unistd.h>
-# include <openssl/crypto.h>
-# include <openssl/async.h>
-__thread ASYNC_CTX *sysvctx;
+pthread_key_t posixctx;
+pthread_key_t posixpool;
#define STACKSIZE 32768
-__thread size_t pool_max_size = 0;
-__thread size_t pool_curr_size = 0;
-__thread STACK_OF(ASYNC_JOB) *pool = NULL;
-
-int ASYNC_FIBRE_init(ASYNC_FIBRE *fibre)
+int async_global_init(void)
{
- void *stack = NULL;
-
- if (!(stack = OPENSSL_malloc(STACKSIZE))) {
+ if (pthread_key_create(&posixctx, NULL) != 0
+ || pthread_key_create(&posixpool, NULL) != 0)
return 0;
- }
-
- fibre->fibre.uc_stack.ss_sp = stack;
- fibre->fibre.uc_stack.ss_size = STACKSIZE;
- fibre->fibre.uc_link = NULL;
- fibre->env_init = 0;
return 1;
}
-void ASYNC_FIBRE_free(ASYNC_FIBRE *fibre)
+int async_local_init(void)
{
- if (fibre->fibre.uc_stack.ss_sp)
- OPENSSL_free(fibre->fibre.uc_stack.ss_sp);
+ if (!async_set_ctx(NULL) || ! async_set_pool(NULL))
+ return 0;
+
+ return 1;
}
-int async_pipe(int *pipefds)
+void async_local_cleanup(void)
{
- if (pipe(pipefds) == 0)
- return 1;
-
- return 0;
}
-int async_write1(int fd, const void *buf)
+void async_global_cleanup(void)
{
- if (write(fd, buf, 1) > 0)
- return 1;
-
- return 0;
}
-int async_read1(int fd, void *buf)
+int async_fibre_makecontext(async_fibre *fibre)
{
- if (read(fd, buf, 1) > 0)
- return 1;
-
+ fibre->env_init = 0;
+ if (getcontext(&fibre->fibre) == 0) {
+ fibre->fibre.uc_stack.ss_sp = OPENSSL_malloc(STACKSIZE);
+ if (fibre->fibre.uc_stack.ss_sp != NULL) {
+ fibre->fibre.uc_stack.ss_size = STACKSIZE;
+ fibre->fibre.uc_link = NULL;
+ makecontext(&fibre->fibre, async_start_func, 0);
+ return 1;
+ }
+ } else {
+ fibre->fibre.uc_stack.ss_sp = NULL;
+ }
return 0;
}
-STACK_OF(ASYNC_JOB) *async_get_pool(void)
+void async_fibre_free(async_fibre *fibre)
{
- return pool;
+ OPENSSL_free(fibre->fibre.uc_stack.ss_sp);
+ fibre->fibre.uc_stack.ss_sp = NULL;
}
-void async_set_pool(STACK_OF(ASYNC_JOB) *poolin, size_t curr_size,
- size_t max_size)
+int async_pipe(OSSL_ASYNC_FD *pipefds)
{
- pool = poolin;
- pool_curr_size = curr_size;
- pool_max_size = max_size;
-}
+ if (pipe(pipefds) == 0)
+ return 1;
-void async_increment_pool_size(void)
-{
- pool_curr_size++;
+ return 0;
}
-void async_release_job_to_pool(ASYNC_JOB *job)
+int async_close_fd(OSSL_ASYNC_FD fd)
{
- sk_ASYNC_JOB_push(pool, job);
-}
+ if (close(fd) != 0)
+ return 0;
-size_t async_pool_max_size(void)
-{
- return pool_max_size;
+ return 1;
}
-void async_release_pool(void)
+int async_write1(OSSL_ASYNC_FD fd, const void *buf)
{
- sk_ASYNC_JOB_free(pool);
+ if (write(fd, buf, 1) > 0)
+ return 1;
+
+ return 0;
}
-int async_pool_can_grow(void)
+int async_read1(OSSL_ASYNC_FD fd, void *buf)
{
- return (pool_max_size == 0) || (pool_curr_size < pool_max_size);
+ if (read(fd, buf, 1) > 0)
+ return 1;
+
+ return 0;
}
#endif