async_win.c: remove unused variable
[openssl.git] / crypto / async / arch / async_win.c
index 5cb14ce9867b43d5fda68f6c3ed69f6a8123846a..e862e2548f5a592a3cfcdddca7f5ca0ab3c779f0 100644 (file)
@@ -1,4 +1,3 @@
-/* crypto/async/arch/async_win.c */
 /*
  * Written by Matt Caswell (matt@openssl.org) for the OpenSSL project.
  */
@@ -51,7 +50,8 @@
  * ====================================================================
  */
 
-#include "async_win.h"
+/* This must be the first #include file */
+#include "../async_locl.h"
 
 #ifdef ASYNC_WIN
 
@@ -64,21 +64,61 @@ struct winpool {
     size_t max_size;
 };
 
+static DWORD asyncwinpool = 0;
+static DWORD asyncwinctx = 0;
+
+
 void async_start_func(void);
 
+int async_global_init(void)
+{
+    asyncwinpool = TlsAlloc();
+    asyncwinctx = TlsAlloc();
+    if (asyncwinpool == TLS_OUT_OF_INDEXES
+            || asyncwinctx == TLS_OUT_OF_INDEXES) {
+        if (asyncwinpool != TLS_OUT_OF_INDEXES) {
+            TlsFree(asyncwinpool);
+        }
+        if (asyncwinctx != TLS_OUT_OF_INDEXES) {
+            TlsFree(asyncwinctx);
+        }
+        return 0;
+    }
+    return 1;
+}
+
+void async_local_cleanup(void)
+{
+    async_ctx *ctx = async_arch_get_ctx();
+    if (ctx != NULL) {
+        async_fibre *fibre = &ctx->dispatcher;
+        if(fibre != NULL && fibre->fibre != NULL && fibre->converted) {
+            ConvertFiberToThread();
+            fibre->fibre = NULL;
+        }
+    }
+}
+
+void async_global_cleanup(void)
+{
+    TlsFree(asyncwinpool);
+    TlsFree(asyncwinctx);
+    asyncwinpool = 0;
+    asyncwinctx = 0;
+}
+
 int async_fibre_init_dispatcher(async_fibre *fibre)
 {
-    LPVOID dispatcher;
-
-    dispatcher =
-        (LPVOID) CRYPTO_get_thread_local(CRYPTO_THREAD_LOCAL_ASYNC_DISPATCH);
-    if (!dispatcher) {
-        fibre->fibre = ConvertThreadToFiber(NULL);
-        CRYPTO_set_thread_local(CRYPTO_THREAD_LOCAL_ASYNC_DISPATCH,
-                                (void *)fibre->fibre);
+    fibre->fibre = ConvertThreadToFiber(NULL);
+    if (fibre->fibre == NULL) {
+        fibre->converted = 0;
+        fibre->fibre = GetCurrentFiber();
+        if (fibre->fibre == NULL)
+            return 0;
     } else {
-        fibre->fibre = dispatcher;
+        fibre->converted = 1;
     }
+
     return 1;
 }
 
@@ -87,94 +127,61 @@ VOID CALLBACK async_start_func_win(PVOID unused)
     async_start_func();
 }
 
-int async_pipe(int *pipefds)
+int async_pipe(OSSL_ASYNC_FD *pipefds)
 {
-    if (_pipe(pipefds, 256, _O_BINARY) == 0)
-        return 1;
+    if (CreatePipe(&pipefds[0], &pipefds[1], NULL, 256) == 0)
+        return 0;
 
-    return 0;
+    return 1;
 }
 
-int async_write1(int fd, const void *buf)
+int async_close_fd(OSSL_ASYNC_FD fd)
 {
-    if (_write(fd, buf, 1) > 0)
-        return 1;
+    if (CloseHandle(fd) == 0)
+        return 0;
 
-    return 0;
+    return 1;
 }
 
-int async_read1(int fd, void *buf)
+int async_write1(OSSL_ASYNC_FD fd, const void *buf)
 {
-    if (_read(fd, buf, 1) > 0)
+    DWORD numwritten = 0;
+
+    if (WriteFile(fd, buf, 1, &numwritten, NULL) && numwritten == 1)
         return 1;
 
     return 0;
 }
 
-STACK_OF(ASYNC_JOB) *async_get_pool(void)
+int async_read1(OSSL_ASYNC_FD fd, void *buf)
 {
-    struct winpool *pool;
-    pool = (struct winpool *)
-            CRYPTO_get_thread_local(CRYPTO_THREAD_LOCAL_ASYNC_POOL);
-    return pool->pool;
-}
+    DWORD numread = 0;
 
+    if (ReadFile(fd, buf, 1, &numread, NULL) && numread == 1)
+        return 1;
 
-int async_set_pool(STACK_OF(ASYNC_JOB) *poolin, size_t curr_size,
-                    size_t max_size)
-{
-    struct winpool *pool;
-    pool = OPENSSL_malloc(sizeof *pool);
-    if (!pool)
-        return 0;
-
-    pool->pool = poolin;
-    pool->curr_size = curr_size;
-    pool->max_size = max_size;
-    CRYPTO_set_thread_local(CRYPTO_THREAD_LOCAL_ASYNC_POOL, (void *)pool);
-    return 1;
+    return 0;
 }
 
-void async_increment_pool_size(void)
+async_pool *async_get_pool(void)
 {
-    struct winpool *pool;
-    pool = (struct winpool *)
-            CRYPTO_get_thread_local(CRYPTO_THREAD_LOCAL_ASYNC_POOL);
-    pool->curr_size++;
+    return (async_pool *)TlsGetValue(asyncwinpool);
 }
 
-void async_release_job_to_pool(ASYNC_JOB *job)
-{
-    struct winpool *pool;
-    pool = (struct winpool *)
-            CRYPTO_get_thread_local(CRYPTO_THREAD_LOCAL_ASYNC_POOL);
-    sk_ASYNC_JOB_push(pool->pool, job);
-}
 
-size_t async_pool_max_size(void)
+int async_set_pool(async_pool *pool)
 {
-    struct winpool *pool;
-    pool = (struct winpool *)
-            CRYPTO_get_thread_local(CRYPTO_THREAD_LOCAL_ASYNC_POOL);
-    return pool->max_size;
+    return TlsSetValue(asyncwinpool, (LPVOID)pool) != 0;
 }
 
-void async_release_pool(void)
+async_ctx *async_arch_get_ctx(void)
 {
-    struct winpool *pool;
-    pool = (struct winpool *)
-            CRYPTO_get_thread_local(CRYPTO_THREAD_LOCAL_ASYNC_POOL);
-    sk_ASYNC_JOB_free(pool->pool);
-    OPENSSL_free(pool);
-    CRYPTO_set_thread_local(CRYPTO_THREAD_LOCAL_ASYNC_POOL, NULL);
+    return (async_ctx *)TlsGetValue(asyncwinctx);
 }
 
-int async_pool_can_grow(void)
+int async_set_ctx(async_ctx *ctx)
 {
-    struct winpool *pool;
-    pool = (struct winpool *)
-            CRYPTO_get_thread_local(CRYPTO_THREAD_LOCAL_ASYNC_POOL);
-    return (pool->max_size == 0) || (pool->curr_size < pool->max_size);
+    return TlsSetValue(asyncwinctx, (LPVOID)ctx) != 0;
 }
 
 #endif