Lecture: concurrency Q&A

Preparation

Spinlocks in xv6

More on locks

struct lock { _Atomic int next, now_serving; };

void acquire(struct lock *l)
{
  int ticket = atomic_fetch_add(&l->next, 1);
  while (l->now_serving != ticket);
}

void release(struct lock *l)
{
  ++l->now_serving;
}
#include <immintrin.h>
...
if (_xbegin() == _XBEGIN_STARTED) {
  // do a few things
  _xend();
} else {
  // plan B (e.g., fail or retry)
}