{"id":13532043,"url":"https://github.com/codehz/ctxco","last_synced_at":"2025-04-23T06:24:21.708Z","repository":{"id":98090957,"uuid":"229776162","full_name":"codehz/ctxco","owner":"codehz","description":"Minimal (and intuitive to use) coroutine implement in c","archived":false,"fork":false,"pushed_at":"2020-01-09T11:35:19.000Z","size":42,"stargazers_count":18,"open_issues_count":0,"forks_count":1,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-03-29T22:31:33.871Z","etag":null,"topics":["c","coroutine","epoll","iouring","library","linux","mit-license","xmake"],"latest_commit_sha":null,"homepage":"","language":"C","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/codehz.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2019-12-23T15:17:18.000Z","updated_at":"2023-12-18T11:49:35.000Z","dependencies_parsed_at":"2023-03-24T15:10:46.721Z","dependency_job_id":null,"html_url":"https://github.com/codehz/ctxco","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/codehz%2Fctxco","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/codehz%2Fctxco/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/codehz%2Fctxco/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/codehz%2Fctxco/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/codehz","download_url":"https://codeload.github.com/codehz/ctxco/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":250381440,"owners_count":21421147,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["c","coroutine","epoll","iouring","library","linux","mit-license","xmake"],"created_at":"2024-08-01T07:01:07.783Z","updated_at":"2025-04-23T06:24:21.691Z","avatar_url":"https://github.com/codehz.png","language":"C","funding_links":[],"categories":["Projects"],"sub_categories":[],"readme":"# CTXCO\n\nMinimal coroutine implemented in c.\n\nOnly support linux for now!\n\nExperiment project, **DO NOT** use it in production.\n\n## API\n\n```c\ntypedef void (*ctxco_func_t)(void *priv);\ntypedef void (*ctxco_poller_func_t)(void *priv, ctxco_request_ref_t co);\n\nvoid ctxco_init(ctxco_poller_func_t poller, void *priv);\nvoid ctxco_deinit();\nvoid ctxco_start(ctxco_func_t func, void *priv, size_t stacksize);\nvoid ctxco_loop();\nbool ctxco_yield();\nvoid *ctxco_invoke(int op, ...);\nvoid ctxco_resume(ctxco_impl_t co, void *ret);\n```\n\n* ctxco_init: initialize the library (per thread)\n\nyou can pass an ad-hoc poller function for it, see src/demo/epoll.c (*PS: Not an efficient implementation*)\n\n* ctxco_deinit: clean up\n* ctxco_start: start coroutine\n* ctxco_loop: start main loop (will return when all coroutine exit)\n* ctxco_yield: switch to another coroutine\n* ctxco_invoke: sending custom poll request to your poller function\n* ctxco_resume: send the coroutine to ready queue\n\n## Build\n\nThis project use [xmake](https://xmake.io) as build system. You can just install xmake and run `xmake build -a` to build.\n\n## Example\n\n```c\n// coroutine entry 01\nvoid co1(void *priv) {\n    printf(\"co1\\n\");\n    // switch to other coroutine\n    ctxco_yield();\n    printf(\"co1 end\\n\");\n}\n// coroutine entry 02\nvoid co2(void *priv) {\n    printf(\"co2\\n\");\n    // switch to other coroutine\n    ctxco_yield();\n    for (int i = 0; i \u003c 10; i++) {\n        printf(\"co2 spawn co1\\n\");\n        // spawn a new coroutine in co2 coroutine\n        ctxco_start(co1, NULL, 0);\n        // switch to other coroutine\n        ctxco_yield();\n    }\n    printf(\"co2 end\\n\");\n}\n\nint main() {\n    printf(\"main start\\n\");\n    ctxco_init(NULL, NULL);\n\n    ctxco_start(co1, NULL, 0);\n    ctxco_start(co2, NULL, 0);\n\n    ctxco_loop();\n\n    ctxco_deinit();\n    printf(\"main end\\n\");\n}\n```\n\n## License\n\nMIT License\nCopyright (c) 2019 Code Hz\n\n## Thanks\n\n* Boost.context\n* tbox","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcodehz%2Fctxco","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fcodehz%2Fctxco","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcodehz%2Fctxco/lists"}