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
|
/* Test of locking in multithreaded situations.
Copyright (C) 2024 Free Software Foundation, Inc.
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program 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 General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>. */
/* Written by Bruno Haible <bruno@clisp.org>, 2024. */
#include <config.h>
/* Specification. */
#include <pthread.h>
#include <errno.h>
#include <stdio.h>
#include <string.h>
#include "macros.h"
/* Returns the effective type of a lock. */
static const char *
get_effective_type (pthread_mutex_t *lock)
{
/* Lock once. */
ASSERT (pthread_mutex_lock (lock) == 0);
/* Try to lock a second time. */
int err = pthread_mutex_trylock (lock);
if (err == 0)
return "RECURSIVE";
if (err == EBUSY)
return "NORMAL";
/* We can't really check whether the lock is effectively ERRORCHECK, without
risking a deadlock. */
return "unknown!";
}
int
main ()
{
/* Find the effective type of a NORMAL lock. */
const char *type_normal;
{
pthread_mutex_t lock;
pthread_mutexattr_t attr;
ASSERT (pthread_mutexattr_init (&attr) == 0);
ASSERT (pthread_mutexattr_settype (&attr, PTHREAD_MUTEX_NORMAL) == 0);
ASSERT (pthread_mutex_init (&lock, &attr) == 0);
ASSERT (pthread_mutexattr_destroy (&attr) == 0);
type_normal = get_effective_type (&lock);
}
/* Find the effective type of an ERRORCHECK lock. */
const char *type_errorcheck;
{
pthread_mutex_t lock;
pthread_mutexattr_t attr;
ASSERT (pthread_mutexattr_init (&attr) == 0);
ASSERT (pthread_mutexattr_settype (&attr, PTHREAD_MUTEX_ERRORCHECK) == 0);
ASSERT (pthread_mutex_init (&lock, &attr) == 0);
ASSERT (pthread_mutexattr_destroy (&attr) == 0);
type_errorcheck = get_effective_type (&lock);
}
/* Find the effective type of a RECURSIVE lock. */
const char *type_recursive;
{
pthread_mutex_t lock;
pthread_mutexattr_t attr;
ASSERT (pthread_mutexattr_init (&attr) == 0);
ASSERT (pthread_mutexattr_settype (&attr, PTHREAD_MUTEX_RECURSIVE) == 0);
ASSERT (pthread_mutex_init (&lock, &attr) == 0);
ASSERT (pthread_mutexattr_destroy (&attr) == 0);
type_recursive = get_effective_type (&lock);
}
/* Find the effective type of a DEFAULT lock. */
const char *type_default;
{
pthread_mutex_t lock;
pthread_mutexattr_t attr;
ASSERT (pthread_mutexattr_init (&attr) == 0);
ASSERT (pthread_mutexattr_settype (&attr, PTHREAD_MUTEX_DEFAULT) == 0);
ASSERT (pthread_mutex_init (&lock, &attr) == 0);
ASSERT (pthread_mutexattr_destroy (&attr) == 0);
type_default = get_effective_type (&lock);
}
/* Find the effective type of a default-initialized lock. */
const char *type_def;
{
pthread_mutex_t lock;
ASSERT (pthread_mutex_init (&lock, NULL) == 0);
type_def = get_effective_type (&lock);
}
printf ("PTHREAD_MUTEX_NORMAL -> type = %s\n", type_normal);
printf ("PTHREAD_MUTEX_ERRORCHECK -> type = %s\n", type_errorcheck);
printf ("PTHREAD_MUTEX_RECURSIVE -> type = %s\n", type_recursive);
printf ("PTHREAD_MUTEX_DEFAULT -> type = %s\n", type_default);
printf ("Default -> type = %s\n", type_def);
ASSERT (strcmp (type_normal, "NORMAL") == 0);
ASSERT (strcmp (type_errorcheck, "NORMAL") == 0);
ASSERT (strcmp (type_recursive, "RECURSIVE") == 0);
ASSERT (strcmp (type_default, type_def) == 0);
/* This is not required by POSIX, but happens to be the case on all
platforms. */
ASSERT (strcmp (type_default, "NORMAL") == 0);
ASSERT (strcmp (type_def, "NORMAL") == 0);
return test_exit_status;
}
|