Debian Codegraph @

gnulib-tests/test-pthread-mutex-type.c

raw
1 /* Test of locking in multithreaded situations.
2 Copyright (C) 2024-2025 Free Software Foundation, Inc.
3
4 This program is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation, either version 3 of the License, or
7 (at your option) any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see <https://www.gnu.org/licenses/>. */
16
17 /* Written by Bruno Haible <[email protected]>, 2024. */
18
19 #include <config.h>
20
21 /* Specification. */
22 #include <pthread.h>
23
24 #include <errno.h>
25 #include <stdio.h>
26 #include <string.h>
27
28 #include "macros.h"
29
30 /* Returns the effective type of a lock. */
31 static const char *
32 get_effective_type (pthread_mutex_t *lock)
33 {
34 /* Lock once. */
35 ASSERT (pthread_mutex_lock (lock) == 0);
36
37 /* Try to lock a second time. */
38 int err = pthread_mutex_trylock (lock);
39 if (err == 0)
40 return "RECURSIVE";
41 if (err == EBUSY)
42 return "NORMAL";
43
44 /* We can't really check whether the lock is effectively ERRORCHECK, without
45 risking a deadlock. */
46
47 return "unknown!";
48 }
49
50 int
51 main ()
52 {
53 /* Find the effective type of a NORMAL lock. */
54 const char *type_normal;
55 {
56 pthread_mutex_t lock;
57 pthread_mutexattr_t attr;
58 ASSERT (pthread_mutexattr_init (&attr) == 0);
59 ASSERT (pthread_mutexattr_settype (&attr, PTHREAD_MUTEX_NORMAL) == 0);
60 ASSERT (pthread_mutex_init (&lock, &attr) == 0);
61 ASSERT (pthread_mutexattr_destroy (&attr) == 0);
62 type_normal = get_effective_type (&lock);
63 }
64
65 /* Find the effective type of an ERRORCHECK lock. */
66 const char *type_errorcheck;
67 {
68 pthread_mutex_t lock;
69 pthread_mutexattr_t attr;
70 ASSERT (pthread_mutexattr_init (&attr) == 0);
71 ASSERT (pthread_mutexattr_settype (&attr, PTHREAD_MUTEX_ERRORCHECK) == 0);
72 ASSERT (pthread_mutex_init (&lock, &attr) == 0);
73 ASSERT (pthread_mutexattr_destroy (&attr) == 0);
74 type_errorcheck = get_effective_type (&lock);
75 }
76
77 /* Find the effective type of a RECURSIVE lock. */
78 const char *type_recursive;
79 {
80 pthread_mutex_t lock;
81 pthread_mutexattr_t attr;
82 ASSERT (pthread_mutexattr_init (&attr) == 0);
83 ASSERT (pthread_mutexattr_settype (&attr, PTHREAD_MUTEX_RECURSIVE) == 0);
84 ASSERT (pthread_mutex_init (&lock, &attr) == 0);
85 ASSERT (pthread_mutexattr_destroy (&attr) == 0);
86 type_recursive = get_effective_type (&lock);
87 }
88
89 /* Find the effective type of a DEFAULT lock. */
90 const char *type_default;
91 {
92 pthread_mutex_t lock;
93 pthread_mutexattr_t attr;
94 ASSERT (pthread_mutexattr_init (&attr) == 0);
95 ASSERT (pthread_mutexattr_settype (&attr, PTHREAD_MUTEX_DEFAULT) == 0);
96 ASSERT (pthread_mutex_init (&lock, &attr) == 0);
97 ASSERT (pthread_mutexattr_destroy (&attr) == 0);
98 type_default = get_effective_type (&lock);
99 }
100
101 /* Find the effective type of a default-initialized lock. */
102 const char *type_def;
103 {
104 pthread_mutex_t lock;
105 ASSERT (pthread_mutex_init (&lock, NULL) == 0);
106 type_def = get_effective_type (&lock);
107 }
108
109 printf ("PTHREAD_MUTEX_NORMAL -> type = %s\n", type_normal);
110 printf ("PTHREAD_MUTEX_ERRORCHECK -> type = %s\n", type_errorcheck);
111 printf ("PTHREAD_MUTEX_RECURSIVE -> type = %s\n", type_recursive);
112 printf ("PTHREAD_MUTEX_DEFAULT -> type = %s\n", type_default);
113 printf ("Default -> type = %s\n", type_def);
114
115 ASSERT (strcmp (type_normal, "NORMAL") == 0);
116 ASSERT (strcmp (type_errorcheck, "NORMAL") == 0);
117 ASSERT (strcmp (type_recursive, "RECURSIVE") == 0);
118
119 ASSERT (strcmp (type_default, type_def) == 0);
120
121 /* This is not required by POSIX, but happens to be the case on all
122 platforms. */
123 ASSERT (strcmp (type_default, "NORMAL") == 0);
124 ASSERT (strcmp (type_def, "NORMAL") == 0);
125
126 return test_exit_status;
127 }