Lumiera  0.pre.03
»edit your freedom«
test-locking.c
Go to the documentation of this file.
1 /*
2  TEST-LOCKING - test locking functions
3 
4  Copyright (C) Lumiera.org
5  2008, Christian Thaeter <ct@pipapo.org>
6 
7  This program is free software; you can redistribute it and/or
8  modify it under the terms of the GNU General Public License as
9  published by the Free Software Foundation; either version 2 of
10  the License, or (at your option) any later version.
11 
12  This program is distributed in the hope that it will be useful,
13  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  GNU General Public License for more details.
16 
17  You should have received a copy of the GNU General Public License
18  along with this program; if not, write to the Free Software
19  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 
21 * *****************************************************/
22 
32 #include "lib/test/test.h"
33 #include "lib/mutex.h"
34 #include "lib/recmutex.h"
35 
36 #include <stdio.h>
37 #include <string.h>
38 
39 TESTS_BEGIN
40 
41 
42 TEST (mutexsection)
43 {
44  lumiera_mutex m;
45  lumiera_mutex_init (&m, "mutexsection", &NOBUG_FLAG(NOBUG_ON), NOBUG_CONTEXT);
46 
47  LUMIERA_MUTEX_SECTION (NOBUG_ON, &m)
48  {
49  printf ("mutex locked section 1\n");
50  }
51 
52  LUMIERA_MUTEX_SECTION (NOBUG_ON, &m)
53  {
54  printf ("mutex locked section 2\n");
55  }
56 
57  lumiera_mutex_destroy (&m, &NOBUG_FLAG(NOBUG_ON), NOBUG_CONTEXT);
58 }
59 
60 
61 TEST (mutexforgotunlock)
62 {
63  lumiera_mutex m;
64  lumiera_mutex_init (&m, "mutexforgotunlock", &NOBUG_FLAG(NOBUG_ON), NOBUG_CONTEXT);
65 
66  LUMIERA_MUTEX_SECTION (NOBUG_ON, &m)
67  {
68  break; // MUTEX_SECTIONS must not be left by a jump
69  }
70 
71  lumiera_mutex_destroy (&m, &NOBUG_FLAG(NOBUG_ON), NOBUG_CONTEXT);
72 }
73 
74 
75 TEST (mutexexplicitunlock)
76 {
77  lumiera_mutex m;
78  lumiera_mutex_init (&m, "mutexforgotunlock", &NOBUG_FLAG(NOBUG_ON), NOBUG_CONTEXT);
79 
80  LUMIERA_MUTEX_SECTION (NOBUG_ON, &m)
81  {
82  ECHO("mutex locked section");
83  LUMIERA_MUTEX_SECTION_UNLOCK;
84  break;
85  }
86 
87  lumiera_mutex_destroy (&m, &NOBUG_FLAG(NOBUG_ON), NOBUG_CONTEXT);
88 }
89 
90 
91 TEST (nestedmutexsection)
92 {
93  lumiera_mutex m;
94  lumiera_mutex_init (&m, "m_mutexsection", &NOBUG_FLAG(NOBUG_ON), NOBUG_CONTEXT);
95 
96  lumiera_mutex n;
97  lumiera_mutex_init (&n, "n_mutexsection", &NOBUG_FLAG(NOBUG_ON), NOBUG_CONTEXT);
98 
99  LUMIERA_MUTEX_SECTION (NOBUG_ON, &m)
100  {
101  printf ("outer mutex locked section\n");
102 
103  LUMIERA_MUTEX_SECTION (NOBUG_ON, &n)
104  {
105  printf ("inner mutex locked section\n");
106  }
107  }
108 
109  lumiera_mutex_destroy (&n, &NOBUG_FLAG(NOBUG_ON), NOBUG_CONTEXT);
110  lumiera_mutex_destroy (&m, &NOBUG_FLAG(NOBUG_ON), NOBUG_CONTEXT);
111 }
112 
113 TEST (chainedmutexsection)
114 {
115  lumiera_mutex m;
116  lumiera_mutex_init (&m, "m_mutexsection", &NOBUG_FLAG(NOBUG_ON), NOBUG_CONTEXT);
117 
118  lumiera_mutex n;
119  lumiera_mutex_init (&n, "n_mutexsection", &NOBUG_FLAG(NOBUG_ON), NOBUG_CONTEXT);
120 
121  LUMIERA_MUTEX_SECTION (NOBUG_ON, &m)
122  {
123  printf ("outer mutex locked section\n");
124 
125  LUMIERA_MUTEX_SECTION_CHAIN (NOBUG_ON, &n)
126  {
127  printf ("inner but not outer mutex locked section\n");
128  }
129  }
130 
131  lumiera_mutex_destroy (&n, &NOBUG_FLAG(NOBUG_ON), NOBUG_CONTEXT);
132  lumiera_mutex_destroy (&m, &NOBUG_FLAG(NOBUG_ON), NOBUG_CONTEXT);
133 }
134 
135 
136 
137 TEST (recursivemutexsection)
138 {
139  lumiera_recmutex m;
140  lumiera_recmutex_init (&m, "m_recmutexsection", &NOBUG_FLAG(NOBUG_ON), NOBUG_CONTEXT);
141 
142 
143  LUMIERA_RECMUTEX_SECTION (NOBUG_ON, &m)
144  {
145  printf ("recmutex locked once\n");
146 
147  LUMIERA_RECMUTEX_SECTION (NOBUG_ON, &m)
148  {
149  printf ("recmutex locked twice\n");
150  }
151  }
152 
153  lumiera_recmutex_destroy (&m, &NOBUG_FLAG(NOBUG_ON), NOBUG_CONTEXT);
154 }
155 
156 
157 /* ====== 10/2023 : partially dismantled
158  *
159  * After switching to C++14 Threads and Locking (#1279),
160  * some backend-services are no longer used...
161  * - rwlocksection
162  * - rwlockforgotunlock
163  * - rwdeadlockwr
164  * - rwdeadlockrw
165  * - rwlockdeadlockwr
166  * - rwlockdeadlockrw
167  * - conditionops
168  * - conditionsection
169  * - conditionforgotunlock
170  * - condition signaling (planned)
171  * - condition broadcasting (planned)
172  * - recconditionops
173  * - recconditionsection
174  * - recconditionforgotunlock
175  * - chainedrecconditionsection
176  * - nestedrecconditionsection
177  * - reccondition signaling (planned)
178  * - reccondition broadcasting (planned)
179  *
180  */
181 
182 TESTS_END
#define LUMIERA_RECMUTEX_SECTION(nobugflag, mtx)
Recursive Mutual exclusive section.
Definition: recmutex.h:41
LumieraRecmutex lumiera_recmutex_destroy(LumieraRecmutex self, struct nobug_flag *flag, const struct nobug_context ctx)
Destroy a recursive mutex variable.
Definition: recmutex.c:62
Helpers and support macros for defining test executables in C.
#define LUMIERA_MUTEX_SECTION(nobugflag, mtx)
Mutual exclusive section.
Definition: mutex.h:41
Mutual exclusion locking, header.
LumieraRecmutex lumiera_recmutex_init(LumieraRecmutex self, const char *purpose, struct nobug_flag *flag, const struct nobug_context ctx)
Initialise a recursive mutex variable Initialises a &#39;recursive&#39; mutex which might be locked by the sa...
Definition: recmutex.c:41
Mutual exclusion locking, header.
#define LUMIERA_MUTEX_SECTION_CHAIN(nobugflag, mtx)
Mutual exclusion chainbuilder section.
Definition: mutex.h:65
LumieraMutex lumiera_mutex_init(LumieraMutex self, const char *purpose, struct nobug_flag *flag, const struct nobug_context ctx)
Initialise a mutex variable This initialises a &#39;fast&#39; default mutex which must not be locked recursiv...
Definition: mutex.c:30
LumieraMutex lumiera_mutex_destroy(LumieraMutex self, struct nobug_flag *flag, const struct nobug_context ctx)
Destroy a mutex variable.
Definition: mutex.c:48