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)
5  2008, Christian Thaeter <ct@pipapo.org>
6 
7   **Lumiera** is free software; you can redistribute it and/or modify it
8   under the terms of the GNU General Public License as published by the
9   Free Software Foundation; either version 2 of the License, or (at your
10   option) any later version. See the file COPYING for further details.
11 
12 * *****************************************************************/
13 
23 #include "lib/test/test.h"
24 #include "lib/mutex.h"
25 #include "lib/recmutex.h"
26 
27 #include <stdio.h>
28 #include <string.h>
29 
30 TESTS_BEGIN
31 
32 
33 TEST (mutexsection)
34 {
35  lumiera_mutex m;
36  lumiera_mutex_init (&m, "mutexsection", &NOBUG_FLAG(NOBUG_ON), NOBUG_CONTEXT);
37 
38  LUMIERA_MUTEX_SECTION (NOBUG_ON, &m)
39  {
40  printf ("mutex locked section 1\n");
41  }
42 
43  LUMIERA_MUTEX_SECTION (NOBUG_ON, &m)
44  {
45  printf ("mutex locked section 2\n");
46  }
47 
48  lumiera_mutex_destroy (&m, &NOBUG_FLAG(NOBUG_ON), NOBUG_CONTEXT);
49 }
50 
51 
52 TEST (mutexforgotunlock)
53 {
54  lumiera_mutex m;
55  lumiera_mutex_init (&m, "mutexforgotunlock", &NOBUG_FLAG(NOBUG_ON), NOBUG_CONTEXT);
56 
57  LUMIERA_MUTEX_SECTION (NOBUG_ON, &m)
58  {
59  break; // MUTEX_SECTIONS must not be left by a jump
60  }
61 
62  lumiera_mutex_destroy (&m, &NOBUG_FLAG(NOBUG_ON), NOBUG_CONTEXT);
63 }
64 
65 
66 TEST (mutexexplicitunlock)
67 {
68  lumiera_mutex m;
69  lumiera_mutex_init (&m, "mutexforgotunlock", &NOBUG_FLAG(NOBUG_ON), NOBUG_CONTEXT);
70 
71  LUMIERA_MUTEX_SECTION (NOBUG_ON, &m)
72  {
73  ECHO("mutex locked section");
74  LUMIERA_MUTEX_SECTION_UNLOCK;
75  break;
76  }
77 
78  lumiera_mutex_destroy (&m, &NOBUG_FLAG(NOBUG_ON), NOBUG_CONTEXT);
79 }
80 
81 
82 TEST (nestedmutexsection)
83 {
84  lumiera_mutex m;
85  lumiera_mutex_init (&m, "m_mutexsection", &NOBUG_FLAG(NOBUG_ON), NOBUG_CONTEXT);
86 
87  lumiera_mutex n;
88  lumiera_mutex_init (&n, "n_mutexsection", &NOBUG_FLAG(NOBUG_ON), NOBUG_CONTEXT);
89 
90  LUMIERA_MUTEX_SECTION (NOBUG_ON, &m)
91  {
92  printf ("outer mutex locked section\n");
93 
94  LUMIERA_MUTEX_SECTION (NOBUG_ON, &n)
95  {
96  printf ("inner mutex locked section\n");
97  }
98  }
99 
100  lumiera_mutex_destroy (&n, &NOBUG_FLAG(NOBUG_ON), NOBUG_CONTEXT);
101  lumiera_mutex_destroy (&m, &NOBUG_FLAG(NOBUG_ON), NOBUG_CONTEXT);
102 }
103 
104 TEST (chainedmutexsection)
105 {
106  lumiera_mutex m;
107  lumiera_mutex_init (&m, "m_mutexsection", &NOBUG_FLAG(NOBUG_ON), NOBUG_CONTEXT);
108 
109  lumiera_mutex n;
110  lumiera_mutex_init (&n, "n_mutexsection", &NOBUG_FLAG(NOBUG_ON), NOBUG_CONTEXT);
111 
112  LUMIERA_MUTEX_SECTION (NOBUG_ON, &m)
113  {
114  printf ("outer mutex locked section\n");
115 
116  LUMIERA_MUTEX_SECTION_CHAIN (NOBUG_ON, &n)
117  {
118  printf ("inner but not outer mutex locked section\n");
119  }
120  }
121 
122  lumiera_mutex_destroy (&n, &NOBUG_FLAG(NOBUG_ON), NOBUG_CONTEXT);
123  lumiera_mutex_destroy (&m, &NOBUG_FLAG(NOBUG_ON), NOBUG_CONTEXT);
124 }
125 
126 
127 
128 TEST (recursivemutexsection)
129 {
130  lumiera_recmutex m;
131  lumiera_recmutex_init (&m, "m_recmutexsection", &NOBUG_FLAG(NOBUG_ON), NOBUG_CONTEXT);
132 
133 
134  LUMIERA_RECMUTEX_SECTION (NOBUG_ON, &m)
135  {
136  printf ("recmutex locked once\n");
137 
138  LUMIERA_RECMUTEX_SECTION (NOBUG_ON, &m)
139  {
140  printf ("recmutex locked twice\n");
141  }
142  }
143 
144  lumiera_recmutex_destroy (&m, &NOBUG_FLAG(NOBUG_ON), NOBUG_CONTEXT);
145 }
146 
147 
148 /* ====== 10/2023 : partially dismantled
149  *
150  * After switching to C++14 Threads and Locking (#1279),
151  * some backend-services are no longer used...
152  * - rwlocksection
153  * - rwlockforgotunlock
154  * - rwdeadlockwr
155  * - rwdeadlockrw
156  * - rwlockdeadlockwr
157  * - rwlockdeadlockrw
158  * - conditionops
159  * - conditionsection
160  * - conditionforgotunlock
161  * - condition signaling (planned)
162  * - condition broadcasting (planned)
163  * - recconditionops
164  * - recconditionsection
165  * - recconditionforgotunlock
166  * - chainedrecconditionsection
167  * - nestedrecconditionsection
168  * - reccondition signaling (planned)
169  * - reccondition broadcasting (planned)
170  *
171  */
172 
173 TESTS_END
#define LUMIERA_RECMUTEX_SECTION(nobugflag, mtx)
Recursive Mutual exclusive section.
Definition: recmutex.h:32
LumieraRecmutex lumiera_recmutex_destroy(LumieraRecmutex self, struct nobug_flag *flag, const struct nobug_context ctx)
Destroy a recursive mutex variable.
Definition: recmutex.c:53
Helpers and support macros for defining test executables in C.
#define LUMIERA_MUTEX_SECTION(nobugflag, mtx)
Mutual exclusive section.
Definition: mutex.h:32
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:32
Mutual exclusion locking, header.
#define LUMIERA_MUTEX_SECTION_CHAIN(nobugflag, mtx)
Mutual exclusion chainbuilder section.
Definition: mutex.h:56
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:21
LumieraMutex lumiera_mutex_destroy(LumieraMutex self, struct nobug_flag *flag, const struct nobug_context ctx)
Destroy a mutex variable.
Definition: mutex.c:39