Lely core libraries
2.3.4
|
This file is part of the utilities library; it contains the implementation of the fiber functions. More...
#include "util.h"
#include <lely/libc/stddef.h>
#include <lely/util/errnum.h>
#include <lely/util/fiber.h>
#include <lely/util/mkjmp.h>
#include <lely/util/util.h>
#include <assert.h>
#include <fenv.h>
#include <stdlib.h>
#include <sys/mman.h>
#include <unistd.h>
Go to the source code of this file.
Data Structures | |
struct | fiber |
A fiber. More... | |
struct | fiber_thrd |
A thread-local struct containing the fiber associated with the thread and a pointer to the fiber currently running in the thread. More... | |
Macros | |
#define | LELY_FIBER_MINSTKSZ 8192 |
The minimum size (in bytes) of a fiber stack frame. | |
#define | LELY_FIBER_STKSZ 131072 |
The default size (in bytes) of a fiber stack frame. | |
Functions | |
static void | sigjmpto (sigjmp_buf from, sigjmp_buf to, int savemask) |
Saves the from calling environment with sigsetjmp(from, savemask) and restores the to calling environment with siglongjmp(to, 1) . | |
static _Noreturn void | fiber_start (void *arg) |
The function running in a fiber. More... | |
int | fiber_thrd_init (int flags) |
Initializes the fiber associated with the calling thread. More... | |
void | fiber_thrd_fini (void) |
Finalizes the fiber associated with the calling thread. More... | |
fiber_t * | fiber_create (fiber_func_t *func, void *arg, int flags, size_t data_size, size_t stack_size) |
Creates a new fiber, allocates a stack and sets up a calling environment to begin executing the specified function. More... | |
void | fiber_destroy (fiber_t *fiber) |
Destroys the specified fiber. More... | |
void * | fiber_data (const fiber_t *fiber) |
Returns a pointer to the data region of the specified fiber, or of the calling fiber if fiber is NULL. More... | |
fiber_t * | fiber_resume (fiber_t *fiber) |
Equivalent to fiber_resume_with(fiber, NULL, NULL) . | |
fiber_t * | fiber_resume_with (fiber_t *fiber, fiber_func_t *func, void *arg) |
Suspends the calling fiber and resumes the specified fiber, optionally executing a function before resuming the suspended function. More... | |
This file is part of the utilities library; it contains the implementation of the fiber functions.
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
Definition in file fiber.c.
|
static |
int fiber_thrd_init | ( | int | flags | ) |
Initializes the fiber associated with the calling thread.
This is necessary to start a fiber, since fiber_resume() and fiber_resume_with() can only be called from within a valid fiber. This function can be invoked more than once by the same thread. Only the first invocation initializes the fiber.
flags | any supported combination of FIBER_SAVE_MASK, FIBER_SAVE_FENV and FIBER_SAVE_ERROR. If the calling thread already has an associated fiber, this parameter is ignored. |
void fiber_thrd_fini | ( | void | ) |
Finalizes the fiber associated with the calling thread.
This function MUST be called once for each successful call to fiber_thrd_init(). Only the last invocation finalizes the fiber.
fiber_t* fiber_create | ( | fiber_func_t * | func, |
void * | arg, | ||
int | flags, | ||
size_t | data_size, | ||
size_t | stack_size | ||
) |
Creates a new fiber, allocates a stack and sets up a calling environment to begin executing the specified function.
The function is not executed until the fiber is resumed with fiber_resume() or fiber_resume_with().
func | the function to be executed by the fiber (can be NULL). |
arg | the second argument supplied to func. |
flags | any supported combination of FIBER_SAVE_MASK, FIBER_SAVE_FENV, FIBER_SAVE_ERROR and FIBER_GUARD_STACK. |
data_size | the size of the data region to be allocated for the fiber. A pointer to this region can be obtained with fiber_data(). |
stack_size | the size (in bytes) of the stack frame to be allocated for the fiber. If 0, the default size (LELY_FIBER_STKSZ) is used. The size of the allocated stack is always at least LELY_FIBER_MINSTKSZ bytes. |
void fiber_destroy | ( | fiber_t * | fiber | ) |
void* fiber_data | ( | const fiber_t * | fiber | ) |
fiber_t* fiber_resume_with | ( | fiber_t * | fiber, |
fiber_func_t * | func, | ||
void * | arg | ||
) |
Suspends the calling fiber and resumes the specified fiber, optionally executing a function before resuming the suspended function.
Note that this function MUST be called from a valid fiber created by fiber_create() or fiber_thrd_init().
fiber | a pointer to the fiber to be resumed. If fiber is NULL, the fiber associated with the calling thread is resumed. |
func | a pointer to the function to be executed in the context of fiber before the suspended function resumes. If not NULL, a pointer to the calling fiber is supplied as the first argument to func and the result of func is returned to the suspended function. |
arg | the argument to be supplied to func. |