Skip to content

Instantly share code, notes, and snippets.

@belm0
belm0 / ws_mask.ipynb
Created June 15, 2019 12:41
websocket data masking implementations
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@belm0
belm0 / task_perf_counter.py
Last active June 24, 2019 14:19
perf counter for Trio tasks
from collections import defaultdict
from time import perf_counter
import trio
from attr import attrs, attr
@attrs(slots=True)
class TimeInfo:
deschedule_start = attr(type=float, default=0)
@belm0
belm0 / async_friendly_contextmanager.py
Last active July 25, 2019 06:58
Python decorator for creating *non-async* context managers which can decorate *async* functions
from contextlib import _GeneratorContextManager
from functools import wraps
from inspect import iscoroutinefunction
class _AsyncFriendlyGeneratorContextManager(_GeneratorContextManager):
"""
Hack contextlib._GeneratorContextManager so that resulting context
manager can properly decorate async functions.
"""
@belm0
belm0 / mcas.cpp
Last active December 17, 2022 11:33
C++ MCAS (multi-word compare and swap)
// C++ MCAS implementation, based on "Efficient Multi-word Compare and Swap"
// (https://arxiv.org/pdf/2008.02527.pdf)
//
// TODO: API header
// TODO: reclamation. Currently the implementation leaks descriptor records
// and leaves them in place indefinitely. Short list of options:
// 1) reuse thread-local descriptors (https://arxiv.org/pdf/1708.01797.pdf)
// 2) use "record manager" abstraction and plugins (DEBRA, etc.)
// (https://bitbucket.org/trbot86/implementations/src/master/cpp/debra)
// TODO: generalization: 32-bit support, user-defined word size and
@belm0
belm0 / article_sc_and_lua_2.md
Last active February 6, 2023 15:10
Structured concurrency and Lua (part 2)

Structured concurrency and Lua (part 2)

John Belmonte, 2022-Sep

In the previous installment, we introduced some basics of structured concurrency, and considered how this paradigm might fit within the Lua programming language.

An important point was encapsulation: the caller of a function shouldn't be concerned whether the implementation employs concurrency or not. We shouldn't have to give up features of the language or runtime just because our program contains concurrency.

A primary example is exception handling. In a basic Lua program without concurrency, we have the following expectations about error propagation:

@belm0
belm0 / article_sc_and_lua_3.md
Last active September 20, 2023 07:42
Structured concurrency and Lua (part 3)

Structured concurrency and Lua (part 3)

John Belmonte, 2022-Oct

In the previous installment, we demonstrated how structured concurrency enables magical tracebacks, spanning both stack frames and task hierarchy. Not only do we regain the ability to diagnose errors, but since exceptions propagate through the nursery of each task, we also have Lua's full protected-call facility at our disposal—just as if the code was not using coroutines.

Nurseries are powerful because they let us orchestrate a set of tasks, ensuring that their lifetime is well-understood, and allowing every task to properly finalize on cancellation of the group. Cancellation can happen if one of the child tasks has an exception, or is otherwise explicitly requested. Explicit cancellation turns out to be fairly common—done just as often as exiting imperative code loops.

Here, we'll try to gain a

@belm0
belm0 / article_sc_and_lua_1.md
Last active September 21, 2023 06:37
Structured concurrency and Lua (part 1)

Structured concurrency and Lua (part 1)

John Belmonte, 2022-Sep

I've started writing a toy structured concurrency implementation for the Lua programming language. Some motivations:

  • use it as a simple introduction to structured concurrency from the perspective of Lua (this article)
  • learn the fundamental properties of structured concurrency and how to implement them
  • share code that could become the starting point for a real Lua library and framework

So what is structured concurrency? For now, I'll just say that it's a programming paradigm that makes managing concurrency (arguably the hardest problem of computer science) an order of magnitude easier in many contexts. It achieves this in ways that seem subtle to us—clearly so, since its utility didn't reach critical mass until around 2018[^sc_birth] (just as control structures like functions, if, and while weren't introduced to languages until long after the first compu