Lely core libraries
2.2.5
|
This file is part of the utilities library; it contains the setjmp()/longjmp() implementation of the fiber functions. More...
#include "fiber.h"
#include <lely/libc/stddef.h>
#include <lely/util/errnum.h>
#include <lely/util/fiber.h>
#include <lely/util/mkjmp.h>
#include <assert.h>
#include <errno.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 |
struct | fiber_thrd |
Functions | |
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 setjmp()/longjmp() 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-sjlj.c.
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. |
Definition at line 108 of file fiber-sjlj.c.
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.
Definition at line 135 of file fiber-sjlj.c.
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. |
Definition at line 152 of file fiber-sjlj.c.
void fiber_destroy | ( | fiber_t * | fiber | ) |
Destroys the specified fiber.
If fiber is NULL or points to the fiber associated with the calling thread, this function has no effect. Destroying the calling fiber or a fiber running in another thread is undefined behavior.
Definition at line 299 of file fiber-sjlj.c.
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.
If fiber points to the fiber associated with the calling thread, this function returns NULL.
Definition at line 314 of file fiber-sjlj.c.
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. |
Definition at line 334 of file fiber-sjlj.c.