https://github.com/rochus-keller/CspChan Skip to content Toggle navigation Sign in * Product + Actions Automate any workflow + Packages Host and manage packages + Security Find and fix vulnerabilities + Codespaces Instant dev environments + Copilot Write better code with AI + Code review Manage code changes + Issues Plan and track work + Discussions Collaborate outside of code Explore + All features + Documentation + GitHub Skills + Blog * Solutions For + Enterprise + Teams + Startups + Education By Solution + CI/CD & Automation + DevOps + DevSecOps Resources + Learning Pathways + White papers, Ebooks, Webinars + Customer Stories + Partners * Open Source + GitHub Sponsors Fund open source developers + The ReadME Project GitHub community articles Repositories + Topics + Trending + Collections * Pricing Search or jump to... Search code, repositories, users, issues, pull requests... Search [ ] Clear Search syntax tips Provide feedback We read every piece of feedback, and take your input very seriously. [ ] [ ] Include my email address so I can be contacted Cancel Submit feedback Saved searches Use saved searches to filter your results more quickly Name [ ] Query [ ] To see all available qualifiers, see our documentation. Cancel Create saved search Sign in Sign up You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session. You switched accounts on another tab or window. Reload to refresh your session. Dismiss alert {{ message }} rochus-keller / CspChan Public * Notifications * Fork 1 * Star 90 A pure C89 implementation of Go channels, including blocking and non-blocking selects. License Unknown, MPL-2.0 licenses found Licenses found Unknown LICENSE.LGPLv21 MPL-2.0 LICENSE.MPLv2 90 stars 1 fork Activity Star Notifications * Code * Issues 0 * Pull requests 0 * Actions * Projects 0 * Security * Insights Additional navigation options * Code * Issues * Pull requests * Actions * Projects * Security * Insights rochus-keller/CspChan This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository. main Switch branches/tags [ ] Branches Tags Could not load branches Nothing to show {{ refName }} default View all branches Could not load tags Nothing to show {{ refName }} default View all tags Name already in use A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch? Cancel Create 2 branches 0 tags Code * Local * Codespaces * Clone HTTPS GitHub CLI [https://github.com/r] Use Git or checkout with SVN using the web URL. [gh repo clone rochus] Work fast with our official CLI. Learn more about the CLI. * Open with GitHub Desktop * Download ZIP Sign In Required Please sign in to use Codespaces. Launching GitHub Desktop If nothing happens, download GitHub Desktop and try again. Launching GitHub Desktop If nothing happens, download GitHub Desktop and try again. Launching Xcode If nothing happens, download Xcode and try again. Launching Visual Studio Code Your codespace will open once ready. There was a problem preparing your codespace, please try again. Latest commit @rochus-keller rochus-keller readme ... e082d79 Dec 13, 2023 readme e082d79 Git stats * 7 commits Files Permalink Failed to load latest commit information. Type Name Latest commit message Commit time CspChan.c removed CspChan_join, create detached, check pthread returns, sieve2,... December 13, 2023 19:57 CspChan.h removed CspChan_join, create detached, check pthread returns, sieve2,... December 13, 2023 19:57 CspChan.pro Unbuffered channels incl. select, for review December 12, 2023 18:24 LICENSE.LGPLv21 initial publication December 11, 2023 18:15 LICENSE.MPLv2 initial publication December 11, 2023 18:15 Readme.md readme December 13, 2023 20:16 test.c removed CspChan_join, create detached, check pthread returns, sieve2,... December 13, 2023 19:57 View code How to use Example Planned or work-in-progress features Related work Support Readme.md This C library implements channels, as introduced by Hoare (Communicating Sequential Processes, 1985) and popularized by the Go programming language. Buffered and unbuffered channels are supported (unbuffered are WIP), and also the select statement in its blocking and non-blocking variants, as in the Go programming language. The library currently works with Ptreads; support for Win32 threads is work in progress. How to use Just include the CspChan.h and CspChan.c files in your project, or build a shared library with the CspChan.c file. More information can be found in the source code. Example #include #include #include "CspChan.h" static void* senderA(void* arg) { CspChan_t* out = (CspChan_t*)arg; int i = 0; while(!CspChan_closed(out)) { CspChan_send(out,&i); i++; CspChan_sleep(1000); } return 0; } static void* senderB(void* arg) { CspChan_t* out = (CspChan_t*)arg; int i = -1; while(!CspChan_closed(out)) { CspChan_sleep(1000); CspChan_send(out,&i); i--; CspChan_sleep(1000); } return 0; } typedef struct receiverAB_arg { CspChan_t* a; CspChan_t* b; } receiverAB_arg; static void* receiverAB(void* arg) { receiverAB_arg* ra = (receiverAB_arg*)arg; while( !CspChan_closed(ra->a) && !CspChan_closed(ra->b) ) { int a,b; CspChan_t* receivers[2] = { ra->a, ra->b }; void* rData[2] = { &a, &b }; switch( CspChan_select(receivers,rData,2, 0, 0, 0) ) { case 0: printf("a: %d\n",a); fflush(stdout); break; case 1: printf("b: %d\n",b); fflush(stdout); break; } } free(arg); return 0; } int main() { CspChan_t* a = CspChan_create(0,4); /* unbuffered channel */ CspChan_t* b = CspChan_create(1,4); /* buffered channel */ CspChan_fork(senderA,a); CspChan_fork(senderB,b); receiverAB_arg* arg = (receiverAB_arg*)malloc(sizeof(receiverAB_arg)); arg->a = a; arg->b = b; CspChan_fork(receiverAB,arg); CspChan_sleep(9000); CspChan_close(a); CspChan_close(b); CspChan_dispose(a); CspChan_dispose(b); } In addition, test.c includes some of the examples from Birch Hansen, Per (1987): Joyce - A Programming Language for Distributed Systems. Planned or work-in-progress features * [*] Unix version with buffered channels and blocking and non-blocking select * [*] Unix version with unbuffered channels * [ ] Windows version * [ ] Implement a thread-pool to re-use threads instead of starting a new one with each call to CspChan_fork to improve performance Related work There are a couple of C++ implementations of CSP channels, e.g. https://www.cs.kent.ac.uk/projects/ofa/c++csp/ or https://github.com/ atollk/copper, but the intention of the present library is a C89 implementation of Go channels. For C there are also some libraries, partially with similar goals as the present one. Pipe (https://github.com/cgaebel/pipe) is a C99 implementation of a thread-safe FIFO. The library is very well documented, but also more complex than the present one, and with more dynamic allocations (CspChan uses a simple fixed size ring buffer instead, like Go). The Pipe library has no select implementation, and adding one seems pretty complicated and requires changes to the library. The Chan library (https://github.com/tylertreat/chan) is a C implementation of Go channels and shares the same goals as the present library. There is even an implementation of Go select. The implementation is rather complex (with separate implementations for buffered and unbuffered channels) and also with more dynamic allocations than I would hope for; apparently only non-blocking selects are supported (i.e. only Go selects with default); adding blocking selects would require significant changes to the library. Support If you need support or would like to post issues or feature requests please use the Github issue list at https://github.com/rochus-keller/ CspChan/issues or send an email to the author. About A pure C89 implementation of Go channels, including blocking and non-blocking selects. Resources Readme License Unknown, MPL-2.0 licenses found Licenses found Unknown LICENSE.LGPLv21 MPL-2.0 LICENSE.MPLv2 Activity Stars 90 stars Watchers 3 watching Forks 1 fork Report repository Releases No releases published Packages 0 No packages published Languages * C 99.4% * QMake 0.6% Footer (c) 2023 GitHub, Inc. Footer navigation * Terms * Privacy * Security * Status * Docs * Contact * You can't perform that action at this time.