Some platforms provide getcontext() but it does not work
[openssl.git] / crypto / async / arch / async_posix.c
index 3f6cb6274aa19bc403f8542d10a8b30eca7b5bc4..33f2a3fa1ea21f7f42237c589e088bca19904676 100644 (file)
@@ -1,4 +1,3 @@
-/* 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_POSIX
+
 # include <stddef.h>
-# include <ucontext.h>
 # include <unistd.h>
-# include <openssl/crypto.h>
-# include <openssl/async.h>
-
-__thread async_ctx *sysvctx;
 
 #define STACKSIZE       32768
 
-extern __thread size_t posixpool_max_size;
-extern __thread size_t posixpool_curr_size;
-extern __thread STACK_OF(ASYNC_JOB) *posixpool;
-__thread size_t posixpool_max_size = 0;
-__thread size_t posixpool_curr_size = 0;
-__thread STACK_OF(ASYNC_JOB) *posixpool = NULL;
-
-int async_fibre_init(async_fibre *fibre)
-{
-    void *stack = NULL;
-
-    stack = OPENSSL_malloc(STACKSIZE);
-    if (stack == NULL) {
-        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)
-{
-    if (fibre->fibre.uc_stack.ss_sp)
-        OPENSSL_free(fibre->fibre.uc_stack.ss_sp);
-}
-
-int async_pipe(OSSL_ASYNC_FD *pipefds)
+int ASYNC_is_capable(void)
 {
-    if (pipe(pipefds) == 0)
-        return 1;
+    ucontext_t ctx;
 
-    return 0;
+    /*
+     * Some platforms provide getcontext() but it does not work (notably
+     * MacOSX PPC64). Check for a working getcontext();
+     */
+    return getcontext(&ctx) == 0;
 }
 
-int async_write1(OSSL_ASYNC_FD fd, const void *buf)
+void async_local_cleanup(void)
 {
-    if (write(fd, buf, 1) > 0)
-        return 1;
-
-    return 0;
 }
 
-int async_read1(OSSL_ASYNC_FD 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)
-{
-    return posixpool;
-}
-
-int async_set_pool(STACK_OF(ASYNC_JOB) *poolin, size_t curr_size,
-                    size_t max_size)
-{
-    posixpool = poolin;
-    posixpool_curr_size = curr_size;
-    posixpool_max_size = max_size;
-    return 1;
-}
-
-void async_increment_pool_size(void)
-{
-    posixpool_curr_size++;
-}
-
-void async_release_job_to_pool(ASYNC_JOB *job)
-{
-    sk_ASYNC_JOB_push(posixpool, job);
-}
-
-size_t async_pool_max_size(void)
-{
-    return posixpool_max_size;
-}
-
-void async_release_pool(void)
-{
-    sk_ASYNC_JOB_free(posixpool);
-    posixpool = NULL;
-}
-
-int async_pool_can_grow(void)
+void async_fibre_free(async_fibre *fibre)
 {
-    return (posixpool_max_size == 0)
-        || (posixpool_curr_size < posixpool_max_size);
+    OPENSSL_free(fibre->fibre.uc_stack.ss_sp);
+    fibre->fibre.uc_stack.ss_sp = NULL;
 }
 
 #endif