Blame nptl/tst-spin4.c

Packit 6c4009
#include <pthread.h>
Packit 6c4009
#include <stdio.h>
Packit 6c4009
#include <unistd.h>
Packit 6c4009
Packit 6c4009
static int count = 0;
Packit 6c4009
Packit 6c4009
static void *
Packit 6c4009
thread_add_one (void *arg)
Packit 6c4009
{
Packit 6c4009
  int tmp;
Packit 6c4009
  pthread_spinlock_t *lock = (pthread_spinlock_t *) arg;
Packit 6c4009
Packit 6c4009
  /* When do_test holds the lock for 1 sec, the two thread will be
Packit 6c4009
     in contention for the lock. */
Packit 6c4009
  if (pthread_spin_lock (lock) != 0)
Packit 6c4009
    {
Packit 6c4009
      puts ("thread_add_one(): spin_lock failed");
Packit 6c4009
      pthread_exit ((void *) 1l);
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  /* sleep 1s before modifying count */
Packit 6c4009
  tmp = count;
Packit 6c4009
  sleep (1);
Packit 6c4009
  count = tmp + 1;
Packit 6c4009
Packit 6c4009
  if (pthread_spin_unlock (lock) != 0)
Packit 6c4009
    {
Packit 6c4009
      puts ("thread_add_one(): spin_unlock failed");
Packit 6c4009
      pthread_exit ((void *) 1l);
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  return NULL;
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
static int
Packit 6c4009
do_test (void)
Packit 6c4009
{
Packit 6c4009
  pthread_t thr1, thr2;
Packit 6c4009
  pthread_spinlock_t lock;
Packit 6c4009
  int tmp;
Packit 6c4009
Packit 6c4009
  if (pthread_spin_init (&lock, PTHREAD_PROCESS_PRIVATE) != 0)
Packit 6c4009
    {
Packit 6c4009
      puts ("spin_init failed");
Packit 6c4009
      return 1;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  if (pthread_spin_lock (&lock) != 0)
Packit 6c4009
    {
Packit 6c4009
      puts ("1st spin_lock failed");
Packit 6c4009
      return 1;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  if (pthread_create (&thr1, NULL, thread_add_one, (void *) &lock) != 0)
Packit 6c4009
    {
Packit 6c4009
      puts ("1st pthread_create failed");
Packit 6c4009
      return 1;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  if (pthread_create (&thr2, NULL, thread_add_one, (void *) &lock) != 0)
Packit 6c4009
    {
Packit 6c4009
      puts ("2nd pthread_create failed");
Packit 6c4009
      return 1;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  /* sleep 1s before modifying count */
Packit 6c4009
  tmp = count;
Packit 6c4009
  sleep (1);
Packit 6c4009
  count = tmp + 1;
Packit 6c4009
Packit 6c4009
  if (pthread_spin_unlock (&lock) != 0)
Packit 6c4009
    {
Packit 6c4009
      puts ("1st spin_unlock failed");
Packit 6c4009
      return 1;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  void *status;
Packit 6c4009
  if (pthread_join (thr1, &status) != 0)
Packit 6c4009
    {
Packit 6c4009
      puts ("1st pthread_join failed");
Packit 6c4009
      return 1;
Packit 6c4009
    }
Packit 6c4009
  if (status != NULL)
Packit 6c4009
    {
Packit 6c4009
      puts ("failure in the 1st thread");
Packit 6c4009
      return 1;
Packit 6c4009
    }
Packit 6c4009
  if (pthread_join (thr2, &status) != 0)
Packit 6c4009
    {
Packit 6c4009
      puts ("2nd pthread_join failed");
Packit 6c4009
      return 1;
Packit 6c4009
    }
Packit 6c4009
  if (status != NULL)
Packit 6c4009
    {
Packit 6c4009
      puts ("failure in the 2nd thread");
Packit 6c4009
      return 1;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  if (count != 3)
Packit 6c4009
    {
Packit 6c4009
      printf ("count is %d, should be 3\n", count);
Packit 6c4009
      return 1;
Packit 6c4009
    }
Packit 6c4009
  return 0;
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
#define TIMEOUT 5
Packit 6c4009
#define TEST_FUNCTION do_test ()
Packit 6c4009
#include "../test-skeleton.c"