aboutsummaryrefslogtreecommitdiff
path: root/nptl/tst-spin4.c
blob: 5b23a172cad46cab551bb713086ccec72a8c398b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
#include <pthread.h>
#include <stdio.h>
#include <unistd.h>

static int count = 0;

static void *
thread_add_one (void *arg)
{
  int tmp;
  pthread_spinlock_t *lock = (pthread_spinlock_t *) arg;

  /* When do_test holds the lock for 1 sec, the two thread will be
     in contention for the lock. */
  if (pthread_spin_lock (lock) != 0)
    {
      puts ("thread_add_one(): spin_lock failed");
      pthread_exit ((void *) 1l);
    }

  /* sleep 1s before modifying count */
  tmp = count;
  sleep (1);
  count = tmp + 1;

  if (pthread_spin_unlock (lock) != 0)
    {
      puts ("thread_add_one(): spin_unlock failed");
      pthread_exit ((void *) 1l);
    }

  return NULL;
}

static int
do_test (void)
{
  pthread_t thr1, thr2;
  pthread_spinlock_t lock;
  int tmp;

  if (pthread_spin_init (&lock, PTHREAD_PROCESS_PRIVATE) != 0)
    {
      puts ("spin_init failed");
      return 1;
    }

  if (pthread_spin_lock (&lock) != 0)
    {
      puts ("1st spin_lock failed");
      return 1;
    }

  if (pthread_create (&thr1, NULL, thread_add_one, (void *) &lock) != 0)
    {
      puts ("1st pthread_create failed");
      return 1;
    }

  if (pthread_create (&thr2, NULL, thread_add_one, (void *) &lock) != 0)
    {
      puts ("2nd pthread_create failed");
      return 1;
    }

  /* sleep 1s before modifying count */
  tmp = count;
  sleep (1);
  count = tmp + 1;

  if (pthread_spin_unlock (&lock) != 0)
    {
      puts ("1st spin_unlock failed");
      return 1;
    }

  void *status;
  if (pthread_join (thr1, &status) != 0)
    {
      puts ("1st pthread_join failed");
      return 1;
    }
  if (status != NULL)
    {
      puts ("failure in the 1st thread");
      return 1;
    }
  if (pthread_join (thr2, &status) != 0)
    {
      puts ("2nd pthread_join failed");
      return 1;
    }
  if (status != NULL)
    {
      puts ("failure in the 2nd thread");
      return 1;
    }

  if (count != 3)
    {
      printf ("count is %d, should be 3\n", count);
      return 1;
    }
  return 0;
}

#define TIMEOUT 5
#define TEST_FUNCTION do_test ()
#include "../test-skeleton.c"