-
Notifications
You must be signed in to change notification settings - Fork 2
Add switch to fiber interface, support fiber_switch in asyncify implementation
#15
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 5 commits
Commits
Show all changes
24 commits
Select commit
Hold shift + click to select a range
a1f8811
Created hello_switch.c (hello_.c but using fiber_switch)
yinamy 542f29c
Revert "Created hello_switch.c (hello_.c but using fiber_switch)"
yinamy abdffba
Reapply "Created hello_switch.c (hello_.c but using fiber_switch)"
yinamy 7fe2296
Updated hello_switch.c to working version
yinamy f8d0a0b
Implemented fiber-switch interface with asyncify, it works with hello…
yinamy 0d22c5f
Apply suggestions from code review
yinamy 80e4fee
Renamed fiber-switch.h to fiber_switch.h
yinamy 43ab195
Created `fiber_main` in `asyncify_switch_impl.c` as per Daniel's sugg…
yinamy a0c1475
Removed fiber-switch.h
yinamy 83f8a28
Update inc/fiber_switch.h
yinamy 8b9f0c5
Update src/asyncify/asyncify_switch_impl.c
yinamy 190d183
Update src/asyncify/asyncify_switch_impl.c
yinamy 8ababac
Update src/asyncify/asyncify_switch_impl.c
yinamy 4158af5
Merge branch 'switch' of github.com:yinamy/fiber-c into switch
yinamy 400ba86
Changed get_active_fiber() to get_main_fiber(), added a check to fibe…
yinamy d583ef1
Added/implemented a function `fiber_return_switch` to interface, whic…
yinamy 8e225e6
Changed `fiber_entry_point_t` to take one more argument
yinamy 1266cad
Added itersum_switch.c
yinamy 322bc02
Added treesum_switch.c
yinamy 401859c
Update src/asyncify/asyncify_switch_impl.c
yinamy 536fd58
Update src/asyncify/asyncify_switch_impl.c
yinamy 4b95ee9
Update src/asyncify/asyncify_switch_impl.c
yinamy 7078500
Update src/asyncify/asyncify_switch_impl.c
yinamy b956688
Update inc/fiber_switch.h
yinamy File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,70 @@ | ||
| // Cooperative printing of "hello world" | ||
| #include <assert.h> | ||
| #include <stdbool.h> | ||
| #include <stdint.h> | ||
| #include <stdio.h> | ||
| #include <stdlib.h> | ||
| #include <string.h> | ||
| #include <fiber-switch.h> | ||
|
|
||
| static volatile fiber_t hello_fiber; | ||
| static volatile fiber_t world_fiber; | ||
|
|
||
| fiber_result_t status; | ||
|
|
||
| bool hello_done; | ||
| bool world_done; | ||
|
|
||
|
|
||
| void* hello(void *arg) { | ||
| uint32_t i = (uint32_t)(uintptr_t)arg; | ||
|
|
||
| static const char s[] = "hlowrd"; | ||
|
|
||
| if (!hello_done){ | ||
| while (i < strlen(s)) { | ||
| putc(s[i], stdout); | ||
| i = (uint32_t)(uintptr_t)fiber_switch(world_fiber, (void*)(uintptr_t)i, &status); | ||
| if (status == FIBER_OK) hello_done = true; | ||
| } | ||
| } | ||
|
|
||
| return NULL; | ||
| } | ||
|
|
||
| void* world(void *arg) { | ||
| uint32_t i = (uint32_t)(uintptr_t)arg; | ||
| static const char s[] = "el ol"; | ||
|
|
||
| if (!world_done) { | ||
| putc(s[i], stdout); | ||
| i++; | ||
| i = (uint32_t)(uintptr_t)fiber_switch(hello_fiber, (void*)(uintptr_t)i, &status); | ||
| if (status == FIBER_OK) world_done = true; | ||
| } | ||
|
|
||
| return NULL; | ||
| } | ||
|
|
||
| int main(void) { | ||
| fiber_init(); | ||
|
|
||
| hello_fiber = fiber_alloc(hello); | ||
| world_fiber = fiber_alloc(world); | ||
|
|
||
| hello_done = false; | ||
| world_done = false; | ||
|
|
||
| uint32_t i = 0; | ||
|
|
||
| (void)fiber_resume(hello_fiber, (void*)(uintptr_t)i, &status); | ||
|
|
||
| putc('\n', stdout); | ||
| fflush(stdout); | ||
|
|
||
| fiber_free(hello_fiber); | ||
| fiber_free(world_fiber); | ||
|
|
||
| fiber_finalize(); | ||
| return 0; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,56 @@ | ||
| /** A basic fiber interface. **/ | ||
yinamy marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| #ifndef WASMFX_FIBER_C_H | ||
| #define WASMFX_FIBER_C_H | ||
|
|
||
| #define export(NAME) __attribute__((export_name(NAME))) | ||
|
|
||
| /** The signature of a fiber entry point. **/ | ||
| typedef void* (*fiber_entry_point_t)(void*); | ||
|
|
||
| /** The abstract type of a fiber object. **/ | ||
| typedef struct fiber* fiber_t; | ||
|
|
||
| /** Allocates a new fiber with the default stack size. **/ | ||
| export("fiber_alloc") | ||
| fiber_t fiber_alloc(fiber_entry_point_t entry); | ||
|
|
||
| /** Reclaims the memory occupied by the fiber object. **/ | ||
| export("fiber_free") | ||
| void fiber_free(fiber_t fiber); | ||
|
|
||
| /** Yields control to its parent context. This function must be called | ||
| from within a fiber context. **/ | ||
| export("fiber_yield") | ||
| void* fiber_yield(void *arg); | ||
yinamy marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| /** Possible status codes for `fiber_switch`. **/ | ||
| typedef enum { FIBER_OK = 0, FIBER_YIELD = 1, FIBER_ERROR = 2 } fiber_result_t; | ||
yinamy marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| /** Resumes a given `fiber` with argument `arg`, returning some value | ||
| of type `void*`. The output parameter `status` indicates whether | ||
| the fiber ran to completion (`FIBER_OK`), yielded control | ||
| (`FIBER_YIELD`), or failed (`FIBER_ERROR`), in the latter case the | ||
| return value will always be `NULL`. **/ | ||
| export("fiber_resume") | ||
| void* fiber_resume(fiber_t fiber, void *arg, fiber_result_t *status); | ||
yinamy marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| /** Switches to a given `fiber` with argument `arg`, returning some value | ||
| of type `void*`. The output parameter `status` indicates whether | ||
| the fiber ran to completion (`FIBER_OK`), yielded control | ||
| (`FIBER_YIELD`), or failed (`FIBER_ERROR`), in the latter case the | ||
| return value will always be `NULL`. **/ | ||
yinamy marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| export("fiber_switch") | ||
| void* fiber_switch(fiber_t fiber, void *arg, fiber_result_t *status); | ||
yinamy marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| /** Initialises the fiber runtime. It must be called exactly once | ||
| before using any of the other fiber_* functions. **/ | ||
| export("fiber_init") | ||
| void fiber_init(void); | ||
|
|
||
| /** Tears down the fiber runtime. It must be called after the final | ||
| use of any of the other fiber_* functions. **/ | ||
| export("fiber_finalize") | ||
| void fiber_finalize(void); | ||
|
|
||
| #undef export | ||
| #endif | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.