{"id":18014655,"url":"https://github.com/dhil/pthandlers","last_synced_at":"2025-03-26T18:30:45.780Z","repository":{"id":76216566,"uuid":"287977271","full_name":"dhil/pthandlers","owner":"dhil","description":"an encoding of affine effect handlers using pthreads","archived":false,"fork":false,"pushed_at":"2022-11-15T17:52:39.000Z","size":98,"stargazers_count":14,"open_issues_count":0,"forks_count":1,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-03-22T06:11:18.276Z","etag":null,"topics":["computational-effects","delimited-continuations","effect-handlers","fibers","pthreads"],"latest_commit_sha":null,"homepage":"","language":"C","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"gpl-3.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/dhil.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-08-16T16:03:23.000Z","updated_at":"2025-01-22T22:39:36.000Z","dependencies_parsed_at":null,"dependency_job_id":"d17a4aca-df33-4bfb-aa5c-6b07d73be6f3","html_url":"https://github.com/dhil/pthandlers","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/dhil%2Fpthandlers","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dhil%2Fpthandlers/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dhil%2Fpthandlers/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dhil%2Fpthandlers/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/dhil","download_url":"https://codeload.github.com/dhil/pthandlers/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":245712485,"owners_count":20660248,"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":["computational-effects","delimited-continuations","effect-handlers","fibers","pthreads"],"created_at":"2024-10-30T04:10:34.434Z","updated_at":"2025-03-26T18:30:45.765Z","avatar_url":"https://github.com/dhil.png","language":"C","funding_links":[],"categories":[],"sub_categories":[],"readme":"# PtFX: affine effect handling via POSIX threads\n\nThis C library provides a proof-of-concept encoding of affine effect\nhandlers using POSIX threads (pthreads).\n\n## Encoding affine effect handlers with pthreads\n\nThe encoding strategy utilises the insights of Kumar *et al.* (1998),\nwho implement one-shot delimited continuations using threads, as the\nbasis for simulating the implementation of effect handlers in\nMulticore OCaml (Dolan *et al.* (2015)), which uses a variation of\nsegmented stacks á la Bruggeman *et al.* (1996).\n\n### Simulating delimited control with threads\n\nThe key insight from Kumar *et al.* (1998) is that each thread has its\nown stack and by establishing a parent-child relationship between the\nthread-stacks, we can simulate delimited control. For all intents and\npurposes we can treat thread and stack as synonyms as the simulation\ndo not take advantage of the concurrency or parallel aspects of\n(p)threads. In fact the control flow of any encoded program is\nintended to be sequential and entirely deterministic. As depicted in\nFigure 1 below, to run some computation `f` under some delimiter `del`\nthe idea is to install the delimiter on top of the current\nthread/stack, `s1`, and subsequently spawn a new thread/stack, `s2`,\nto execute the computation. Conceptually, in terms of single-threaded\nprograms, the stack pointer (`sp`) moves to the top of the new stack\n`s2`.\n\n```\n  s1         s2\n+-----+    +-----+\n|     |    |     |\n|     |    | f() |\n| del |\u003c---|     |\u003c- stack pointer (sp)\n+-----+    +-----+\n---------------------------------------\n      Fig 1. Execution stacks\n```\n\nAfter spawning the new thread the parent thread `s1` blocks and awaits\neither the completion of `s2` or a continuation capture within\n`s2`. Capturing the continuation within `f` amounts to synchronising\nthe threads `s2` and `s1`, as the thread `s2` has to unblock `s1` and\nsubsequently block itself to await being resumed. As depicted in Figure 2\nbelow, a continuation capture conceptually replaces the delimiter\n`del` with a reference, `k`, to the child stack, `s2`, and sets the\nstack pointer to point to the top of the parent stack,\n`s1`. Similarly, to resume `s2`, the thread `s1` simply has to unblock\n`s2` and subsequently block itself --- whether `del` gets reinstalled\ndepends on the exact nature of the control operator of\nchoice. Following the resumption, the stack pointer is, conceptually,\nset to point to the top of the child stack `s2`.\n\n```\nAfter stack suspension\n  s1             s2\n+-----+        +-----+\n|     |        |     |\n|     |        | f() |\n|  k  |-------\u003e|     |\n|     |\u003c- sp   +-----+\n+-----+\n\nAfter stack resumption\n  s1         s2\n+-----+    +-----+\n|     |    |     |\n|     |    | f() |\n| del |\u003c---|     |\u003c- stack pointer (sp)\n+-----+    +-----+\n---------------------------------------------\n   Fig 2. Suspending and resuming stacks\n```\n\nStack synchronisation can readily be realised by using mutexes and\ncondition variables.\n\n### Simulating effect handlers\n\nThe novelty of this encoding does not lie in how delimited control is\nsimulated, but rather, how the technique outlined in the previous\nsection is adapted to encode the structured interface of effect\nhandlers.\n\nTODO...\n\n## Building\n\nThe `Makefile` provides rules for building static and shared\nvariations of this library, simply type\n\n```shell\n$ make lib\n```\n\nThis rule assembles the static library `libpthandlers.a` and the\nshared library `libpthandlers.so` and outputs both in the `lib/`\ndirectory. To build either library variant alone invoke `make static`\nor `make shared` respectively.\n\n### Examples\n\nThis repository contains some example programs. To build them all\nsimply type `make examples`. Alternatively, they can be built individually\n\n```shell\n$ make examples/{coop,divzero,dobedobe,state}\n```\n\nThe resulting binaries are placed in the root of this repository.\n\n## Caveats\n\nThis encoding relies heavily on tail-call optimisation (i.e. gcc's\n`-foptimize-sibling-calls`), which seems to be rather fragile, or\nunreliable, in C compilers. In addition this implementation also\ndepends on one or more flags from the `-O1` optimisation bundle that I\nhave yet to discover. Currently, without passing `-O1` it appears that\n`gcc` (version 7.5.0) fails to tail-call optimise some functions.\n\n## Future work\n\nThe current implementation only supports unary deep handlers. A\nnatural extension would be to support unary shallow handlers. Going\neven further, it would be interesting to encode both deep and shallow\nvariations of multi-handlers.\n\n## Acknowledgements\n\nThis work was done whilst I was being supported by\n[EPSRC](https://www.epsrc.ac.uk/) grant\n[EP/L01503X/1](http://pervasiveparallelism.inf.ed.ac.uk) (EPSRC Centre\nfor Doctoral Training in Pervasive Parallelism) and by ERC\nConsolidator Grant\n[Skye](https://homepages.inf.ed.ac.uk/jcheney/group/skye.html) (grant\nnumber 682315).\n\n## References\n\n* Sanjeev Kumar, Carl Bruggeman, and R. Kent Dybvig. 1998. Threads Yield Continuations. Higher-Order and Symbolic Computation 10, 223–236 (1998).  \n  [(DOI)](https://doi.org/10.1023/A:1007782300874)\n* Carl Bruggeman, Oscar Waddell, and R. Kent Dybvig. 1996. Representing control in the presence of one-shot continuations. SIGPLAN Not. 31, 5 (May 1996), 99–107.  \n  [(DOI)](https://doi.org/10.1145/249069.231395)\n* Stephen Dolan, Leo White, KC Sivaramakrishnan, Jeremy Yallop and Anil Madhavapeddy. 2015. Effective Concurrency with Algebraic Effects. OCaml Workshop.  \n  [(PDF)](https://kcsrk.info/papers/effects_ocaml15.pdf)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdhil%2Fpthandlers","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdhil%2Fpthandlers","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdhil%2Fpthandlers/lists"}