intprops: new macro INT_PROMOTE
[gnulib.git] / tests / test-pthread-mutex-type.c
blobe23b9adf88614c5236faebe86510d712d3628aba
1 /* Test of locking in multithreaded situations.
2 Copyright (C) 2024-2025 Free Software Foundation, Inc.
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.
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.
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/>. */
17 /* Written by Bruno Haible <bruno@clisp.org>, 2024. */
19 #include <config.h>
21 /* Specification. */
22 #include <pthread.h>
24 #include <errno.h>
25 #include <stdio.h>
26 #include <string.h>
28 #include "macros.h"
30 /* Returns the effective type of a lock. */
31 static const char *
32 get_effective_type (pthread_mutex_t *lock)
34 /* Lock once. */
35 ASSERT (pthread_mutex_lock (lock) == 0);
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";
44 /* We can't really check whether the lock is effectively ERRORCHECK, without
45 risking a deadlock. */
47 return "unknown!";
50 int
51 main ()
53 /* Find the effective type of a NORMAL lock. */
54 const char *type_normal;
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);
65 /* Find the effective type of an ERRORCHECK lock. */
66 const char *type_errorcheck;
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);
77 /* Find the effective type of a RECURSIVE lock. */
78 const char *type_recursive;
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);
89 /* Find the effective type of a DEFAULT lock. */
90 const char *type_default;
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);
101 /* Find the effective type of a default-initialized lock. */
102 const char *type_def;
104 pthread_mutex_t lock;
105 ASSERT (pthread_mutex_init (&lock, NULL) == 0);
106 type_def = get_effective_type (&lock);
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);
115 ASSERT (strcmp (type_normal, "NORMAL") == 0);
116 ASSERT (strcmp (type_errorcheck, "NORMAL") == 0);
117 ASSERT (strcmp (type_recursive, "RECURSIVE") == 0);
119 ASSERT (strcmp (type_default, type_def) == 0);
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);
126 return test_exit_status;