Implement windows async thread local variable support
authorMatt Caswell <matt@openssl.org>
Thu, 19 Nov 2015 21:44:13 +0000 (21:44 +0000)
committerMatt Caswell <matt@openssl.org>
Fri, 20 Nov 2015 23:40:23 +0000 (23:40 +0000)
Implements Thread Local Storage in the windows async port. This also has
some knock on effects to the posix and null implementations.

Reviewed-by: Rich Salz <rsalz@openssl.org>
crypto/async/arch/async_null.c
crypto/async/arch/async_posix.c
crypto/async/arch/async_win.c
crypto/async/arch/async_win.h
crypto/async/async.c
crypto/async/async_err.c
crypto/async/async_locl.h
include/openssl/async.h
util/libeay.num

index dba159f309624615cfae4a93e75e8d6ee19bc9da..b2dbfee7ec21d538fa360fba525eb15548e0f85d 100644 (file)
@@ -76,10 +76,23 @@ int async_read1(OSSL_ASYNC_FD fd, void *buf)
     return -1;
 }
 
-int async_thread_local_init(void)
+int async_global_init(void)
 {
     return 0;
 }
 
+int async_local_init(void)
+{
+    return 0;
+}
+
+void async_local_cleanup(void)
+{
+}
+
+void async_global_cleanup(void)
+{
+}
+
 #endif
 
index bd4b0c2f1b62cb5b70d1d457f5cab6ddc1365f30..77a2c33de607dbc9f6b32e61e7f8ac1e5d839b87 100644 (file)
@@ -66,7 +66,7 @@ pthread_key_t posixpool;
 
 #define STACKSIZE       32768
 
-int async_thread_local_init(void)
+int async_global_init(void)
 {
     if (pthread_key_create(&posixctx, NULL) != 0
             || pthread_key_create(&posixpool, NULL) != 0)
@@ -75,6 +75,22 @@ int async_thread_local_init(void)
     return 1;
 }
 
+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_init(async_fibre *fibre)
 {
     void *stack = NULL;
index 4eb449d34f4f00967370f307566c31846939a464..20c8a09bc4067835ea38186eb671cfbf92198883 100644 (file)
@@ -64,18 +64,80 @@ struct winpool {
     size_t max_size;
 };
 
+static DWORD asyncwinpool = 0;
+static DWORD asyncwinctx = 0;
+static DWORD asyncwindispatch = 0;
+
+
 void async_start_func(void);
 
+int async_global_init(void)
+{
+    asyncwinpool = TlsAlloc();
+    asyncwinctx = TlsAlloc();
+    asyncwindispatch = TlsAlloc();
+    if (asyncwinpool == TLS_OUT_OF_INDEXES || asyncwinctx == TLS_OUT_OF_INDEXES
+            || asyncwindispatch == TLS_OUT_OF_INDEXES) {
+        if (asyncwinpool != TLS_OUT_OF_INDEXES) {
+            TlsFree(asyncwinpool);
+        }
+        if (asyncwinctx != TLS_OUT_OF_INDEXES) {
+            TlsFree(asyncwinctx);
+        }
+        if (asyncwindispatch != TLS_OUT_OF_INDEXES) {
+            TlsFree(asyncwindispatch);
+        }
+        return 0;
+    }
+    return 1;
+}
+
+int async_local_init(void)
+{
+    return (TlsSetValue(asyncwinpool, NULL) != 0)
+        && (TlsSetValue(asyncwinctx, NULL) != 0)
+        && (TlsSetValue(asyncwindispatch, NULL) != 0);
+}
+
+void async_local_cleanup(void)
+{
+    async_ctx *ctx = async_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);
+    TlsFree(asyncwindispatch);
+    asyncwinpool = 0;
+    asyncwinctx = 0;
+    asyncwindispatch = 0;
+}
+
 int async_fibre_init_dispatcher(async_fibre *fibre)
 {
     LPVOID dispatcher;
 
-    dispatcher =
-        (LPVOID) CRYPTO_get_thread_local(CRYPTO_THREAD_LOCAL_ASYNC_DISPATCH);
+    dispatcher = (LPVOID)TlsGetValue(asyncwindispatch);
     if (dispatcher == NULL) {
         fibre->fibre = ConvertThreadToFiber(NULL);
-        CRYPTO_set_thread_local(CRYPTO_THREAD_LOCAL_ASYNC_DISPATCH,
-                                (void *)fibre->fibre);
+        if (fibre->fibre == NULL) {
+            fibre->converted = 0;
+            fibre->fibre = GetCurrentFiber();
+            if (fibre->fibre == NULL)
+                return 0;
+        } else {
+            fibre->converted = 1;
+        }
+        if (TlsSetValue(asyncwindispatch, (LPVOID)fibre->fibre) == 0)
+            return 0;
     } else {
         fibre->fibre = dispatcher;
     }
@@ -125,15 +187,23 @@ int async_read1(OSSL_ASYNC_FD fd, void *buf)
 
 async_pool *async_get_pool(void)
 {
-    return (async_pool *)
-            CRYPTO_get_thread_local(CRYPTO_THREAD_LOCAL_ASYNC_POOL);
+    return (async_pool *)TlsGetValue(asyncwinpool);
 }
 
 
 int async_set_pool(async_pool *pool)
 {
-    CRYPTO_set_thread_local(CRYPTO_THREAD_LOCAL_ASYNC_POOL, (void *)pool);
-    return 1;
+    return TlsSetValue(asyncwinpool, (LPVOID)pool) != 0;
+}
+
+async_ctx *async_get_ctx(void)
+{
+    return (async_ctx *)TlsGetValue(asyncwinctx);
+}
+
+int async_set_ctx(async_ctx *ctx)
+{
+    return TlsSetValue(asyncwinctx, (LPVOID)ctx) != 0;
 }
 
 #endif
index 5e91732e8722fef34b3003e699257a7754527083..77e41e405b9e8486465ae845030ea8b2b9f0b085 100644 (file)
 
 typedef struct async_fibre_st {
     LPVOID fibre;
+    int converted;
 } async_fibre;
 
-# define async_set_ctx(nctx) \
-        (CRYPTO_set_thread_local(CRYPTO_THREAD_LOCAL_ASYNC_CTX, (void *)(nctx)))
-# define async_get_ctx() \
-        ((async_ctx *)CRYPTO_get_thread_local(CRYPTO_THREAD_LOCAL_ASYNC_CTX))
 # define async_fibre_swapcontext(o,n,r) \
         (SwitchToFiber((n)->fibre), 1)
 # define async_fibre_makecontext(c) \
         ((c)->fibre = CreateFiber(0, async_start_func_win, 0))
 # define async_fibre_free(f)             (DeleteFiber((f)->fibre))
 
+async_ctx *async_get_ctx(void);
+int async_set_ctx(async_ctx *ctx);
+
 int async_fibre_init_dispatcher(async_fibre *fibre);
 VOID CALLBACK async_start_func_win(PVOID unused);
 
index c18c5c4517b3567bcd10bf46e2452d55e61fc3d8..5664d990b6909706094af927d11a33b9c231ad35 100644 (file)
@@ -330,7 +330,7 @@ static void async_empty_pool(async_pool *pool)
 
 int ASYNC_init(int init_thread, size_t max_size, size_t init_size)
 {
-    if (!async_thread_local_init())
+    if (!async_global_init())
         return 0;
 
     if (init_thread)
@@ -349,6 +349,10 @@ int ASYNC_init_thread(size_t max_size, size_t init_size)
         return 0;
     }
 
+    if (!async_local_init()) {
+        ASYNCerr(ASYNC_F_ASYNC_INIT_THREAD, ASYNC_R_INIT_FAILED);
+        return 0;
+    }
     pool = OPENSSL_zalloc(sizeof *pool);
     if (pool == NULL) {
         ASYNCerr(ASYNC_F_ASYNC_INIT_THREAD, ERR_R_MALLOC_FAILURE);
@@ -383,7 +387,6 @@ int ASYNC_init_thread(size_t max_size, size_t init_size)
         }
     }
     pool->curr_size = curr_size;
-
     if (!async_set_pool(pool)) {
         ASYNCerr(ASYNC_F_ASYNC_INIT_THREAD, ASYNC_R_FAILED_TO_SET_POOL);
         goto err;
@@ -404,6 +407,7 @@ static void async_free_pool_internal(async_pool *pool)
     sk_ASYNC_JOB_free(pool->jobs);
     OPENSSL_free(pool);
     (void)async_set_pool(NULL);
+    async_local_cleanup();
     async_ctx_free();
 }
 
index 07a169ba863af8148af0b677eadabeb21b770395..6fe1f85dbb0d8b74b9251c644eba7a14ea5e008e 100644 (file)
@@ -83,6 +83,7 @@ static ERR_STRING_DATA ASYNC_str_reasons[] = {
     {ERR_REASON(ASYNC_R_CANNOT_CREATE_WAIT_PIPE), "cannot create wait pipe"},
     {ERR_REASON(ASYNC_R_FAILED_TO_SET_POOL), "failed to set pool"},
     {ERR_REASON(ASYNC_R_FAILED_TO_SWAP_CONTEXT), "failed to swap context"},
+    {ERR_REASON(ASYNC_R_INIT_FAILED), "init failed"},
     {ERR_REASON(ASYNC_R_INVALID_POOL_SIZE), "invalid pool size"},
     {ERR_REASON(ASYNC_R_POOL_ALREADY_INITED), "pool already inited"},
     {0, NULL}
index 0a9c59fcfbc4ebae3f95da450c6f4f060f528366..a463bf1c229e9b49c1716f26c42269011ebd9879 100644 (file)
@@ -86,7 +86,10 @@ struct async_pool_st {
     size_t max_size;
 };
 
-int async_thread_local_init(void);
+int async_global_init(void);
+int async_local_init(void);
+void async_local_cleanup(void);
+void async_global_cleanup(void);
 void async_start_func(void);
 int async_pipe(OSSL_ASYNC_FD *pipefds);
 int async_close_fd(OSSL_ASYNC_FD fd);
index 83bde16ebad59b6496a7d03e6bedd9bd8bd96e7e..de5ef89644aa2ee21f043e61ddbd6bbc8f99a9f6 100644 (file)
@@ -112,6 +112,7 @@ void ERR_load_ASYNC_strings(void);
 # define ASYNC_R_CANNOT_CREATE_WAIT_PIPE                  100
 # define ASYNC_R_FAILED_TO_SET_POOL                       101
 # define ASYNC_R_FAILED_TO_SWAP_CONTEXT                   102
+# define ASYNC_R_INIT_FAILED                              105
 # define ASYNC_R_INVALID_POOL_SIZE                        103
 # define ASYNC_R_POOL_ALREADY_INITED                      104
 
index c61d83d249c6756e940b05925aac44d916b40aca..0ff302c78f000bdb79fbb91bda2ce977ad03a82c 100755 (executable)
@@ -4661,3 +4661,5 @@ ASYNC_get_wait_fd                       5020      EXIST::FUNCTION:
 ERR_load_ASYNC_strings                  5021   EXIST::FUNCTION:
 ASYNC_unblock_pause                     5022   EXIST::FUNCTION:
 ASYNC_block_pause                       5023   EXIST::FUNCTION:
+ASYNC_cleanup                           5024   EXIST::FUNCTION:
+ASYNC_init                              5025   EXIST::FUNCTION: