1e1051a39Sopenharmony_ci/*
2e1051a39Sopenharmony_ci * Copyright 2015-2020 The OpenSSL Project Authors. All Rights Reserved.
3e1051a39Sopenharmony_ci *
4e1051a39Sopenharmony_ci * Licensed under the Apache License 2.0 (the "License").  You may not use
5e1051a39Sopenharmony_ci * this file except in compliance with the License.  You can obtain a copy
6e1051a39Sopenharmony_ci * in the file LICENSE in the source distribution or at
7e1051a39Sopenharmony_ci * https://www.openssl.org/source/license.html
8e1051a39Sopenharmony_ci */
9e1051a39Sopenharmony_ci
10e1051a39Sopenharmony_ci/* This must be the first #include file */
11e1051a39Sopenharmony_ci#include "../async_local.h"
12e1051a39Sopenharmony_ci
13e1051a39Sopenharmony_ci#ifdef ASYNC_POSIX
14e1051a39Sopenharmony_ci
15e1051a39Sopenharmony_ci# include <stddef.h>
16e1051a39Sopenharmony_ci# include <unistd.h>
17e1051a39Sopenharmony_ci
18e1051a39Sopenharmony_ci#define STACKSIZE       32768
19e1051a39Sopenharmony_ci
20e1051a39Sopenharmony_ciint ASYNC_is_capable(void)
21e1051a39Sopenharmony_ci{
22e1051a39Sopenharmony_ci    ucontext_t ctx;
23e1051a39Sopenharmony_ci
24e1051a39Sopenharmony_ci    /*
25e1051a39Sopenharmony_ci     * Some platforms provide getcontext() but it does not work (notably
26e1051a39Sopenharmony_ci     * MacOSX PPC64). Check for a working getcontext();
27e1051a39Sopenharmony_ci     */
28e1051a39Sopenharmony_ci    return getcontext(&ctx) == 0;
29e1051a39Sopenharmony_ci}
30e1051a39Sopenharmony_ci
31e1051a39Sopenharmony_civoid async_local_cleanup(void)
32e1051a39Sopenharmony_ci{
33e1051a39Sopenharmony_ci}
34e1051a39Sopenharmony_ci
35e1051a39Sopenharmony_ciint async_fibre_makecontext(async_fibre *fibre)
36e1051a39Sopenharmony_ci{
37e1051a39Sopenharmony_ci#ifndef USE_SWAPCONTEXT
38e1051a39Sopenharmony_ci    fibre->env_init = 0;
39e1051a39Sopenharmony_ci#endif
40e1051a39Sopenharmony_ci    if (getcontext(&fibre->fibre) == 0) {
41e1051a39Sopenharmony_ci        fibre->fibre.uc_stack.ss_sp = OPENSSL_malloc(STACKSIZE);
42e1051a39Sopenharmony_ci        if (fibre->fibre.uc_stack.ss_sp != NULL) {
43e1051a39Sopenharmony_ci            fibre->fibre.uc_stack.ss_size = STACKSIZE;
44e1051a39Sopenharmony_ci            fibre->fibre.uc_link = NULL;
45e1051a39Sopenharmony_ci            makecontext(&fibre->fibre, async_start_func, 0);
46e1051a39Sopenharmony_ci            return 1;
47e1051a39Sopenharmony_ci        }
48e1051a39Sopenharmony_ci    } else {
49e1051a39Sopenharmony_ci        fibre->fibre.uc_stack.ss_sp = NULL;
50e1051a39Sopenharmony_ci    }
51e1051a39Sopenharmony_ci    return 0;
52e1051a39Sopenharmony_ci}
53e1051a39Sopenharmony_ci
54e1051a39Sopenharmony_civoid async_fibre_free(async_fibre *fibre)
55e1051a39Sopenharmony_ci{
56e1051a39Sopenharmony_ci    OPENSSL_free(fibre->fibre.uc_stack.ss_sp);
57e1051a39Sopenharmony_ci    fibre->fibre.uc_stack.ss_sp = NULL;
58e1051a39Sopenharmony_ci}
59e1051a39Sopenharmony_ci
60e1051a39Sopenharmony_ci#endif
61