Async error handling and MacOS/X fixes
[openssl.git] / crypto / async / arch / async_posix.c
index 1b21bcc345fd3909ead766152902419367a55eee..30075810baeb33de2e1951047cc9ee4537b4949c 100644 (file)
  * ====================================================================
  */
 
+/* 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 <openssl/crypto.h>
-# include <openssl/async.h>
+# include <unistd.h>
 
-__thread ASYNC_CTX *sysvctx;
+pthread_key_t posixctx;
+pthread_key_t posixpool;
 
 #define STACKSIZE       32768
 
-int ASYNC_FIBRE_init(ASYNC_FIBRE *fibre)
+int async_global_init(void)
 {
-    void *stack = NULL;
+    if (pthread_key_create(&posixctx, NULL) != 0
+            || pthread_key_create(&posixpool, NULL) != 0)
+        return 0;
+
+    return 1;
+}
 
-    if (!(stack = OPENSSL_malloc(STACKSIZE))) {
+int async_local_init(void)
+{
+    if (!async_set_ctx(NULL) || ! async_set_pool(NULL))
         return 0;
+
+    return 1;
+}
+
+void async_local_cleanup(void)
+{
+}
+
+void async_global_cleanup(void)
+{
+}
+
+int async_fibre_makecontext(async_fibre *fibre)
+{
+    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;
+}
+
+void async_fibre_free(async_fibre *fibre)
+{
+    OPENSSL_free(fibre->fibre.uc_stack.ss_sp);
+    fibre->fibre.uc_stack.ss_sp = NULL;
+}
+
+int async_pipe(OSSL_ASYNC_FD *pipefds)
+{
+    if (pipe(pipefds) == 0)
+        return 1;
+
+    return 0;
+}
 
-    fibre->fibre.uc_stack.ss_sp = stack;
-    fibre->fibre.uc_stack.ss_size = STACKSIZE;
-    fibre->fibre.uc_link = NULL;
+int async_close_fd(OSSL_ASYNC_FD fd)
+{
+    if (close(fd) != 0)
+        return 0;
 
     return 1;
 }
 
-void ASYNC_FIBRE_free(ASYNC_FIBRE *fibre)
+int async_write1(OSSL_ASYNC_FD fd, const void *buf)
+{
+    if (write(fd, buf, 1) > 0)
+        return 1;
+
+    return 0;
+}
+
+int async_read1(OSSL_ASYNC_FD fd, void *buf)
 {
-    if (fibre->fibre.uc_stack.ss_sp)
-        OPENSSL_free(fibre->fibre.uc_stack.ss_sp);
+    if (read(fd, buf, 1) > 0)
+        return 1;
+
+    return 0;
 }
+
 #endif