Skip to content

a type of user level spin lock (does not require machine exchange instruction)

License

Notifications You must be signed in to change notification settings

keithgabryelski/spin-lock

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

spin-lock

Some time in the early 1990s a friend (Mike Ditto) and I (Keith Gabryelski) worked on this code. I've recently reconstituted the algorithm from memory. Here it is for those to use as needed.

A user level spin lock that does not require an exchange instruction.

This code is provided just to document a method I've not seen since we first thought of it. It may be useful to some -- it may make others think of better solutions.

example code

struct spin_lock_state *state = spin_allocate((key_t)42, NULL);
int pid = getpid();

spin_lock(state, pid)

// critical code here

spin_unlock(state, pid);

second example (provided)

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include "spin-lock.h"

int
main(int argc, char *argv[])
{
  key_t key;

  if (argc != 3) {
    fprintf(stderr, "usage: spin-lock-test [keyfile] [lockerid]\n");
    exit(1);
  }

  /* make the key: */
  if ((key = ftok(argv[1], 'R')) == -1) {
    perror("ftok");
    exit(1);
  }

  printf("key: 0x%lx\n", (long)key);

  int locker_id = atoi(argv[2]);
  printf("locker_id: %d\n", locker_id);

  printf("allocating\n");
  struct spin_lock_state *state = spin_allocate(key);

  printf("locking\n");
  spin_lock(state, locker_id, SPIN_WAIT_DEFAULT_CALLBACK);
  printf("locked ... sleeping for 30 seconds\n");

  sleep(30);

  printf("unlocking\n");
  spin_unlock(state, locker_id);
  printf("unlocked\n");

  exit(0);
}

compile the above code and then run in two different windows:

first window

./spin-lock-test spin-lock-test.c 1

second window

./spin-lock-test spin-lock-test.c 2

About

a type of user level spin lock (does not require machine exchange instruction)

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages