{"id":28000660,"url":"https://github.com/sysprog21/fiber","last_synced_at":"2025-05-08T23:54:06.182Z","repository":{"id":72389245,"uuid":"251482008","full_name":"sysprog21/fiber","owner":"sysprog21","description":"A User Space Threading Library","archived":false,"fork":false,"pushed_at":"2024-05-09T18:56:50.000Z","size":49,"stargazers_count":19,"open_issues_count":0,"forks_count":11,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-05-08T23:54:00.908Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"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/sysprog21.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":"2020-03-31T02:37:56.000Z","updated_at":"2025-04-03T15:01:31.000Z","dependencies_parsed_at":"2024-05-08T18:42:37.104Z","dependency_job_id":null,"html_url":"https://github.com/sysprog21/fiber","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/sysprog21%2Ffiber","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sysprog21%2Ffiber/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sysprog21%2Ffiber/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sysprog21%2Ffiber/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/sysprog21","download_url":"https://codeload.github.com/sysprog21/fiber/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":253166485,"owners_count":21864471,"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":[],"created_at":"2025-05-08T23:54:05.668Z","updated_at":"2025-05-08T23:54:06.157Z","avatar_url":"https://github.com/sysprog21.png","language":"C","funding_links":[],"categories":[],"sub_categories":[],"readme":"# fiber: A User Space Threading Library\n\nFiber is a lightweight thread library with M:N mapping between user-level\nthread (ULT) and Linux native thread (or kernel-level thread, KLT).\n\n## Features\n* Preemptive user-level threads\n* Familiar threading concepts are available\n  - Mutexes\n  - Condition variables\n\n## Implementation Details\n\nThe preemptive scheduler is implemented through timer and signal functions.\nIn `k_thread_exec_func()` function, a timer is initiated through the following:\n```c\nsetitimer(ITIMER_PROF, \u0026timeslice, NULL)\n```\n\nWhen the timer expires, signal `SIGPROF` is sent to the process.\n`sigaction()` would invoke the scheduling routine `schedule()` to run, which\nchooses a thread from a run queue to run. The scheduler maintains a run queue.\nThe new created threads are pushed into the end of the queue. The first thread\nin the head of the queue is the thread currently running. Each time the\nscheduler receives signal `SIGPROF`, it interrupts the running thread at the\nhead by pushing this thread into the end of the queue. In addition, it swaps\nthe context between this thread and next thread which is the new head of the\nqueue.\n\nA userspace program/process may not create a kernel thread. Instead, it could\ncreate a *native* thread using `pthread_create`, which invokes the `clone`\nsystem call to do so. Inside Fiber, `clone` system call is used for creating\nkernel-level threads. With a kernel that understands threads, we use `clone`,\nbut we still have to create the new thread's stack. The kernel does not\ncreate/assign a stack for a new thread. The `clone` system call accepts a\n`child_stack` argument. Therefore, `fiber_create` must allocate a stack for\nthe new thread and pass that to clone:\n```c\n    /* invoke the clone system call to create a native thread */\n    if (-1 == clone((int (*)(void *)) k_thread_exec_func,\n                    (char *) stack + _THREAD_STACK,\n                    SIGCHLD | CLONE_SIGHAND | CLONE_VM | CLONE_PTRACE, NULL)) {\n        perror(\"Failed to invoke clone system call.\");\n        ...\n    }\n```\n\nOnly a process or main thread is assigned its initial stack by the kernel,\nusually at a high memory address. Thus, if the process does not use threads,\nnormally, it just uses that pre-assigned stack. But, if a thread is created,\ni.e., the  native thread, the starting process/thread must pre-allocate the\narea for the proposed thread with malloc.\n\n## License\n`fiber` is released under the MIT License. Use of this source code is governed\nby a MIT License that can be found in the LICENSE file.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsysprog21%2Ffiber","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsysprog21%2Ffiber","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsysprog21%2Ffiber/lists"}