summaryrefslogtreecommitdiff
path: root/tests/test-10.c
blob: bec05c14ad799de913ae0662988acca09828cf8b (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
/* Test error checking mutexes.  */

#define _GNU_SOURCE

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

int
main (int argc, char **argv)
{
  error_t err;
  pthread_mutexattr_t mattr;
  pthread_mutex_t mutex;

  err = pthread_mutexattr_init (&mattr);
  if (err)
    error (1, err, "pthread_mutexattr_init");

  err = pthread_mutexattr_settype (&mattr, PTHREAD_MUTEX_ERRORCHECK);
  if (err)
    error (1, err, "pthread_mutexattr_settype");

  err = pthread_mutex_init (&mutex, &mattr);
  if (err)
    error (1, err, "pthread_mutex_init");

  err = pthread_mutexattr_destroy (&mattr);
  if (err)
    error (1, err, "pthread_mutexattr_destroy");

  err = pthread_mutex_lock (&mutex);
  assert (err == 0);

  err = pthread_mutex_lock (&mutex);
  assert (err == EDEADLK);

  err = pthread_mutex_unlock (&mutex);
  assert (err == 0);

  err = pthread_mutex_unlock (&mutex);
  assert (err == EPERM);

  return 0;
}