Skip to content

Instantly share code, notes, and snippets.

View jensengrey's full-sized avatar
💭
Popping and Locking

jensengrey

💭
Popping and Locking
View GitHub Profile
@jensengrey
jensengrey / a-main.ml
Created April 12, 2023 00:49 — forked from hirrolot/a-main.ml
A simple CPS conversion as in "Compiling with Continuations", Andrew W. Appel
(* A variable identifier of the lambda language [term]. *)
type var = string [@@deriving eq]
(* The lambda language; direct style. *)
type term =
| Var of var
| Fix of (var * var list * term) list * term
| Appl of term * term list
| Record of term list
| Select of term * int

Creating atomic get/set 128 function using InterlockedCompareExchange128

The code provided below highlights a potential issue with using the InterlockedCompareExchange128 function in C++. The problem is that there is no atomic get/set 128 function available, but they can be synthesized from InterlockedCompareExchange128.

However, there are incorrect forms that can work in non-race conditions, which can mislead developers into thinking their code is correct. The first incorrect form is the Get function, which uses a non-atomic read of *p and can cause problems if p happens to match the input value. The second incorrect form is the Set function, which writes the desired value through the last parameter, which is not atomic.

To solve these issues, the correct forms of Get and Set functions have been provided in the code. In the correct form of the Get function, the compare will match, and the same value will be written back atomically. In the correct form of the Set function, the code