{"id":18428654,"url":"https://github.com/ascpixi/cosmos-coroutines","last_synced_at":"2025-09-04T20:38:00.177Z","repository":{"id":65176030,"uuid":"585703672","full_name":"ascpixi/cosmos-coroutines","owner":"ascpixi","description":"⏱ A simple, non-preemptive coroutine scheduler that allows for cooperative multitasking within Cosmos kernels","archived":false,"fork":false,"pushed_at":"2024-02-11T18:49:53.000Z","size":82,"stargazers_count":21,"open_issues_count":0,"forks_count":2,"subscribers_count":4,"default_branch":"main","last_synced_at":"2025-08-15T15:38:29.979Z","etag":null,"topics":["cooperative-multitasking","coroutines","cosmos","cosmos-os","cosmosos","csharp","multitasking"],"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/ascpixi.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":"2023-01-05T21:17:11.000Z","updated_at":"2024-12-26T02:13:28.000Z","dependencies_parsed_at":"2024-02-11T20:14:48.265Z","dependency_job_id":"68ed4710-de83-4988-89e4-5ba1b1eb501f","html_url":"https://github.com/ascpixi/cosmos-coroutines","commit_stats":{"total_commits":7,"total_committers":2,"mean_commits":3.5,"dds":0.2857142857142857,"last_synced_commit":"e6c1431dfe1cc23619cf4801b0fa1a5fe40f0001"},"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/ascpixi/cosmos-coroutines","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ascpixi%2Fcosmos-coroutines","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ascpixi%2Fcosmos-coroutines/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ascpixi%2Fcosmos-coroutines/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ascpixi%2Fcosmos-coroutines/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/ascpixi","download_url":"https://codeload.github.com/ascpixi/cosmos-coroutines/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ascpixi%2Fcosmos-coroutines/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":271454842,"owners_count":24762698,"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","status":"online","status_checked_at":"2025-08-21T02:00:08.990Z","response_time":74,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"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":["cooperative-multitasking","coroutines","cosmos","cosmos-os","cosmosos","csharp","multitasking"],"created_at":"2024-11-06T05:14:19.710Z","updated_at":"2025-08-21T09:04:42.321Z","avatar_url":"https://github.com/ascpixi.png","language":"C#","funding_links":[],"categories":[],"sub_categories":[],"readme":"# cosmos-coroutines\nA simple, non-preemptive coroutine scheduler that allows for cooperative multitasking within Cosmos kernels.\n\nThis project was created to demonstrate the ability to use C#'s iterator support to achieve cooperative multitasking.\n\n## Limitations\nCosmos.Coroutines has the following limitations:\n- non-preemptive; you need to do a `yield return` to hand back control to the coroutine scheduler\n- basic round-robin; no priority system\n- CPU halting - which occurs in many parts of Cosmos - will also halt the coroutine pool scheduler\n\nOther than the caveats mentioned above, the coroutine system can act like a cooperative kernel task scheduler.\n\n## Installing\nCosmos.Coroutines is available on [NuGet](https://www.nuget.org/packages/Cosmos.Coroutines); either use the NuGet package manager in your IDE of choice, or, in a package manager terminal, type in:\n```powershell\nNuGet\\Install-Package Cosmos.Coroutines -Version 1.0.1\n```\n\n## Usage\nThe following classes are included in the `Cosmos.System.Coroutines` namespace:\n- `Coroutine` - represents a coroutine, which can belong to only one `CoroutinePool`.\n- `CoroutinePool` - manages multiple coroutines. A global `CoroutinePool` is allocated on startup and can be accessed using `CoroutinePool.Main`. This pool will not affect the execution of the OS in any way until the `StartPool` instance method is called.\n- `CoroutineControlPoint` - an object returned by valid coroutine implementations of the `IEnumerator` interface, and accepted by the `Coroutine` constructor. Specifies whether the coroutine should be ticked at a given time.\n- `WaitFor` - a `CoroutineControlPoint` that waits for the specified amount of nanoseconds.\n- `WaitUntil` - a `CoroutineControlPoint` that waits until a given condition is met.\n- `WaitIndefinetly` - a `CoroutineControlPoint` that halts the coroutine until it's explicitly un-halted through said control point.\n\nTo use the main `CoroutinePool`, simply do:\n```cs\nCoroutinePool.Main.StartPool();\n```\n\nTo create a coroutine:\n```cs\nvar coroutine = new Coroutine(MyCoroutine1());\ncoroutine.Start(); // will run the coroutine on the main pool; to run it in another, use CoroutinePool.AddCoroutine\n\n// ...\n\nIEnumerator\u003cCoroutineControlPoint\u003e MyCoroutine1()\n{\n    while(true) {\n        Console.WriteLine(\"This prints every second.\");\n        yield return WaitFor.Seconds(1);\n    }\n}\n```\n\nYou can start as many coroutines as you want, however, please note that with more coroutines, the slower the operating system gets.\n\n\u003e [!WARNING]\n\u003e A coroutine is not the same as a traditional C# thread, and you should not mistake the two. A C# thread is **preempted**; that is, if the thread encounters, for example, an infinite loop, the kernel will still continue to execute, as the thread will be automatically switched from (preempted) after a time quantum. A coroutine relies on the method to voluntarily give back control to the pool; if a software bug appears that would make the coroutine refrain from giving back control to the pool, the kernel would halt.\n\n### Creating a \"main\" function\nAfter performing a cycle over all coroutines, you may want to execute kernel code, to perform e.g. maintanance tasks. This can be easily achieved using the `CoroutinePool.OnCoroutineCycle` delegate list:\n```cs\nCoroutinePool.Main.OnCoroutineCycle.Add(Main);\nCoroutinePool.Main.StartPool();\n\n// ...\n\nvoid Main() {\n    // everything in this method will be executed after a pool cycle\n}\n```\n\nThis is a list of delegates instead of a standard C# event, as these are currently non-functional on Cosmos. [See this issue for more details.](https://github.com/CosmosOS/Cosmos/issues/2765)\n\n## Coroutines and memory management\n`CoroutinePool`s can be set to automatically collect all unused objects on the heap after the executor finishes a cycle - that is, when all coroutines in its internal list have been ticked. This is enabled by default for the main pool, but disabled for user-created pools. It's strongly recommended to enable periodic heap collection if you're running the pool on your main thread (as is most likely the case with Cosmos).\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fascpixi%2Fcosmos-coroutines","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fascpixi%2Fcosmos-coroutines","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fascpixi%2Fcosmos-coroutines/lists"}