Commit 27949c35 authored by Matt Caswell's avatar Matt Caswell
Browse files

Simplify async pool handling



A lot of the pool handling code was in the arch specific files, but was
actually boiler plate and the same across the implementations. This commit
moves as much code as possible out of the arch specific files.

Reviewed-by: default avatarRich Salz <rsalz@openssl.org>
parent 2b2c78d4
Loading
Loading
Loading
Loading
+0 −35
Original line number Diff line number Diff line
@@ -56,41 +56,6 @@

#ifdef ASYNC_NULL

STACK_OF(ASYNC_JOB) *async_get_pool(void)
{
    return NULL;
}

int async_set_pool(STACK_OF(ASYNC_JOB) *poolin, size_t curr_size,
                   size_t max_size)
{
    return 0;
}

void async_increment_pool_size(void)
{
    return;
}

void async_release_job_to_pool(ASYNC_JOB *job)
{
    return;
}

size_t async_pool_max_size(void)
{
    return 0;
}

void async_release_pool(void)
{
    return;
}

int async_pool_can_grow(void) {
    return 0;
}

int async_pipe(OSSL_ASYNC_FD *pipefds)
{
    return -1;
+2 −0
Original line number Diff line number Diff line
@@ -72,5 +72,7 @@ typedef struct async_fibre_st {
# define async_fibre_makecontext(c)
# define async_fibre_free(f)
# define async_fibre_init_dispatcher(f)
# define async_get_pool()                       NULL
# define async_set_pool(p)                      0

#endif
+10 −49
Original line number Diff line number Diff line
@@ -61,17 +61,11 @@
# include <openssl/crypto.h>
# include <openssl/async.h>

__thread async_ctx *sysvctx;
__thread async_ctx *posixctx;
__thread async_pool *posixpool;

#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;
@@ -103,6 +97,14 @@ int async_pipe(OSSL_ASYNC_FD *pipefds)
    return 0;
}

int async_close_fd(OSSL_ASYNC_FD fd)
{
    if (close(fd) != 0)
        return 0;

    return 1;
}

int async_write1(OSSL_ASYNC_FD fd, const void *buf)
{
    if (write(fd, buf, 1) > 0)
@@ -119,45 +121,4 @@ int async_read1(OSSL_ASYNC_FD fd, void *buf)
    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)
{
    return (posixpool_max_size == 0)
        || (posixpool_curr_size < posixpool_max_size);
}

#endif
+6 −3
Original line number Diff line number Diff line
@@ -73,7 +73,8 @@
#  include <setjmp.h>
#  include "e_os.h"

extern __thread async_ctx *sysvctx;
extern __thread async_ctx *posixctx;
extern __thread async_pool *posixpool;

typedef struct async_fibre_st {
    ucontext_t fibre;
@@ -81,8 +82,10 @@ typedef struct async_fibre_st {
    int env_init;
} async_fibre;

#  define async_set_ctx(nctx)             (sysvctx = (nctx))
#  define async_get_ctx()                 (sysvctx)
#  define async_set_ctx(nctx)             (posixctx = (nctx))
#  define async_get_ctx()                 (posixctx)
#  define async_set_pool(p)               (posixpool = (p))
#  define async_get_pool()                (posixpool)

static inline int async_fibre_swapcontext(async_fibre *o, async_fibre *n, int r)
{
+12 −57
Original line number Diff line number Diff line
@@ -51,7 +51,7 @@
 * ====================================================================
 */

#include "async_win.h"
#include "../async_locl.h"

#ifdef ASYNC_WIN

@@ -95,6 +95,14 @@ int async_pipe(OSSL_ASYNC_FD *pipefds)
    return 1;
}

int async_close_fd(OSSL_ASYNC_FD fd)
{
    if (CloseHandle(fd) == 0)
        return 0;

    return 1;
}

int async_write1(OSSL_ASYNC_FD fd, const void *buf)
{
    DWORD numwritten = 0;
@@ -115,70 +123,17 @@ int async_read1(OSSL_ASYNC_FD fd, void *buf)
    return 0;
}

STACK_OF(ASYNC_JOB) *async_get_pool(void)
async_pool *async_get_pool(void)
{
    struct winpool *pool;
    pool = (struct winpool *)
    return (async_pool *)
            CRYPTO_get_thread_local(CRYPTO_THREAD_LOCAL_ASYNC_POOL);
    return pool->pool;
}


int async_set_pool(STACK_OF(ASYNC_JOB) *poolin, size_t curr_size,
                    size_t max_size)
int async_set_pool(async_pool *pool)
{
    struct winpool *pool;
    pool = OPENSSL_malloc(sizeof *pool);
    if (pool == NULL)
        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;
}

void async_increment_pool_size(void)
{
    struct winpool *pool;
    pool = (struct winpool *)
            CRYPTO_get_thread_local(CRYPTO_THREAD_LOCAL_ASYNC_POOL);
    pool->curr_size++;
}

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)
{
    struct winpool *pool;
    pool = (struct winpool *)
            CRYPTO_get_thread_local(CRYPTO_THREAD_LOCAL_ASYNC_POOL);
    return pool->max_size;
}

void async_release_pool(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);
}

int async_pool_can_grow(void)
{
    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);
}

#endif
Loading