Skip to content

Instantly share code, notes, and snippets.

View polytypic's full-sized avatar
⚠️

Vesa Karvonen polytypic

⚠️
  • Helsinki, Finland
View GitHub Profile
@polytypic
polytypic / slides.md
Last active February 15, 2023 09:43
k-CAS for sweat-free concurrent programming
title
k-CAS for sweat-free concurrent programming

k-CAS for sweat-free concurrent programming

Vesa Karvonen

@polytypic
polytypic / gkmz-with-read-only-cmp-ops.md
Last active July 31, 2023 07:53
Extending k-CAS with efficient read-only CMP operations

Extending k-CAS with efficient read-only CMP operations

The kcas library currently uses the GKMZ algorithm for Efficient Multi-word Compare and Swap or MCAS aka k-CAS. This is a nearly optimal algorithm for MCAS as it requires only k + 1 CAS operations.

The new library API also provides a transactional API for using the algorithm. For example, suppose one would create the following shared memory locations:

@polytypic
polytypic / article.md
Last active October 5, 2023 11:51
A generalized approach to turn a functional data structure into a lock-free starvation avoiding data structure

A generalized approach to turn a functional data structure into a lock-free starvation avoiding data structure

Any functional data structure can be turned into a lock-free data structure by wrapping it inside an Atomic. For example, consider the following set implementation:

module Identity_set : sig
  type 'a t
  val empty : 'a t

Learning day: ReDiSL

Logistics team held a "learning day" on Thursday 2021-03-04 where we could spend one full day on a project or tutorial of our choosing. I worked on a little proof-of-concept project I dubbed ReDiSL or Redis DSL. This is a little writeup on the topic.

The motivation for working on the Redis DSL or ReDiSL was that it could make for a nice way to work with Redis especially when one is doing

@polytypic
polytypic / skiplist.ml
Last active November 13, 2023 09:26
A lock-free skiplist
(* Copyright (c) 2023 Vesa Karvonen
Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted, provided that the above
copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
@polytypic
polytypic / article.md
Last active January 9, 2024 12:02
A hack to implement efficient TLS (thread-local-storage)
@polytypic
polytypic / .gitignore
Last active January 20, 2024 17:08
Recursive generator using C++20 coroutines
build
@polytypic
polytypic / article.md
Last active February 11, 2024 11:32
A pattern for using GADTs to subset cases

A pattern for using GADTs to subset cases

Consider the following straightforward mutable queue implementation using two stacks:

type 'a adt_queue = {
  mutable head : 'a head;
  mutable tail : 'a tail;
}
@polytypic
polytypic / article.md
Last active February 15, 2024 09:47
Fear the `Obj.magic`

Fear the Obj.magic

Reaction of a junior OCaml programmer being suggested to use Obj.magic

Let me first quote a paragraph from the Memory Representation of Values (with my emphasis):

Obj is an undocumented module that exposes the internals of the OCaml compiler and runtime. It is very useful for

@polytypic
polytypic / article.md
Last active February 16, 2024 15:27
A lock-free starvation avoiding bag

A lock-free starvation avoiding bag

module type Bag = sig
  type 'a t
  val create : unit -> 'a t
  val to_list : 'a t -> 'a list
  type 'a handle
  val add : 'a t -> 'a -> 'a handle
  val remove : 'a t -> 'a handle -> unit