#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#include "lock.h"

static void perror_exit (char *s)
{
  perror (s);
  exit (-1);
}

Lock *make_lock ()
{
  int n;

  Lock *lock = (Lock *) malloc (sizeof(Lock));
  if (lock == NULL) perror_exit ("malloc failed");

  n = pthread_mutex_init (lock->mutex, NULL);
  if (n != 0) perror_exit ("make_lock failed");

  return lock;
}

void lock_acquire (Lock *lock)
{
  int n = pthread_mutex_lock (lock->mutex);
  if (n != 0) perror_exit ("acquire failed");
}

void lock_release (Lock *lock)
{
  int n = pthread_mutex_unlock (lock->mutex);
  if (n != 0) perror_exit ("release failed");
}
