Checkpoint is a new(?) synchronization primitive. Idea is to mark parts of program that must be executed sequentially with checkpoints.
Checkpoint is a point in program where thread awaits until some condition will hold.
Checkpoints are implemented through method pass(checkp_get, checkp_set, name) of global object of type 'checkpoint'.
Threads wait till checkp_get() condition will hold and then execute checkp_set() function.
Depending on checkp_get() and checkp_set() arguments to pass(), checkpoints can implement
any kind of synchronization, and might be used instead of mutexes, rwlocks or condition variables.
Method pass() has following arguments:
- checkp_get() function is a condition that checks values of flags. If checkp_get() returns zero,
thread will sleep in kernel until condition will hold and checkp_get() return non-zero.
- checkp_set() function is to update values of flags. Function check_set() returns integer value
that can be used by calling code.
- optional string 'name' to identify checkpoint for debugging.
Execution of program with checkpoints depends on values of flags for checkp_get(). These flags must be
updated in checkp_set(). After each checkp_set() call, all awaiting checkp_get() are checked
to resume execution of awaiting threads. Functions checkp_get() and checkp_set() are executed
under global checkpoints lock.
Checkpoint interface:
typedef int (*checkp_get)();
typedef int (*checkp_set)();
class checkpoint {
public:
int pass(checkp_get get, checkp_set set, std::string name = "default");
int try_pass(checkp_get get, checkp_set set);
std::string print();
};
For example:
#1 int locked = 0;
...
#2 chkp.pass([]()->int {return !locked;}, []()->int {locked = 1; return 0;});
...
#3 chkp.pass([]()->int {return locked;}, []()->int {locked = 0; return 0;});
Line #2 is equivalent to locking a mutex, and line #3 is equivalent to unlocking.
Implementation for Linux and some test examples are at:
https://github.com/raschupkin/Checkpoints
Probably any type of synchronization can be implemented with mutexes and condition variables, but it might be easier to split program on logically dependent parts with checkpoints.