about summary refs log tree commit diff
path: root/htl/tests/test-11.c
blob: 441e0ef14f13d3bd5794618bcb80149d98048a3d (plain) (blame)
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
/* Test rwlocks.
   Copyright (C) 2000-2021 Free Software Foundation, Inc.
   This file is part of the GNU C Library.

   The GNU C Library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Lesser General Public
   License as published by the Free Software Foundation; either
   version 2.1 of the License, or (at your option) any later version.

   The GNU C Library is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   Lesser General Public License for more details.

   You should have received a copy of the GNU Lesser General Public
   License along with the GNU C Library;  if not, see
   <https://www.gnu.org/licenses/>.  */

#define _GNU_SOURCE

#include <pthread.h>
#include <assert.h>
#include <error.h>
#include <errno.h>

#define THREADS 1

int a;
int b;

/* Get a read lock and assert that a == b.  */
void *
test1 (void *arg)
{
  error_t err;
  pthread_rwlock_t *lock = arg;
  int i;

  for (i = 0; i < 200; i++)
    {
      err = pthread_rwlock_rdlock (lock);
      assert (err == 0);

      assert (a == b);

      sched_yield ();

      assert (a == b);

      err = pthread_rwlock_unlock (lock);
      assert (err == 0);
    }

  return 0;
}

int
main (int argc, char **argv)
{
  error_t err;
  pthread_rwlockattr_t attr;
  pthread_rwlock_t lock;
  int pshared;

  int i;
  pthread_t tid[THREADS];
  void *ret;

  err = pthread_rwlockattr_init (&attr);
  if (err)
    error (1, err, "pthread_rwlockattr_init");

  err = pthread_rwlockattr_getpshared (&attr, &pshared);
  if (err)
    error (1, err, "pthread_rwlockattr_getpshared");

  /* Assert the default state as mandated by POSIX.  */
  assert (pshared == PTHREAD_PROCESS_PRIVATE);

  err = pthread_rwlockattr_setpshared (&attr, pshared);
  if (err)
    error (1, err, "pthread_rwlockattr_setpshared");

  err = pthread_rwlock_init (&lock, &attr);
  if (err)
    error (1, err, "pthread_rwlock_init");

  err = pthread_rwlockattr_destroy (&attr);
  if (err)
    error (1, err, "pthread_rwlockattr_destroy");

  /* Now test the lock.  */

  for (i = 0; i < THREADS; i++)
    {
      err = pthread_create (&tid[i], 0, test1, &lock);
      if (err)
	error (1, err, "pthread_create");
    }

  for (i = 0; i < 10; i++)
    {
      sched_yield ();

      /* Get a write lock.  */
      pthread_rwlock_wrlock (&lock);
      /* Increment a and b giving other threads a chance to run in
         between.  */
      sched_yield ();
      a++;
      sched_yield ();
      b++;
      sched_yield ();
      /* Unlock.  */
      pthread_rwlock_unlock (&lock);
    }

  for (i = 0; i < THREADS; i++)
    {
      err = pthread_join (tid[i], &ret);
      if (err)
	error (1, err, "pthread_join");
    }

  /* Read lock it.  */
  err = pthread_rwlock_tryrdlock (&lock);
  assert (err == 0);

  /* Try to write lock it.  It should fail with EBUSY.  */
  err = pthread_rwlock_trywrlock (&lock);
  assert (err == EBUSY);

  /* Drop the read lock.  */
  err = pthread_rwlock_unlock (&lock);
  assert (err == 0);

  /* Get a write lock.  */
  err = pthread_rwlock_trywrlock (&lock);
  assert (err == 0);

  /* Fail trying to acquire another write lock.  */
  err = pthread_rwlock_trywrlock (&lock);
  assert (err == EBUSY);

  /* Try to get a read lock which should also fail.  */
  err = pthread_rwlock_tryrdlock (&lock);
  assert (err == EBUSY);

  /* Unlock it.  */
  err = pthread_rwlock_unlock (&lock);
  assert (err == 0);


  err = pthread_rwlock_destroy (&lock);
  if (err)
    error (1, err, "pthread_rwlock_destroy");

  return 0;
}