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 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383
|
/* Unit tests for GCond
* Copyright (C) 2011 Red Hat, Inc
* Author: Matthias Clasen
*
* SPDX-License-Identifier: LicenseRef-old-glib-tests
*
* This work is provided "as is"; redistribution and modification
* in whole or in part, in any medium, physical or electronic is
* permitted without restriction.
*
* This work 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.
*
* In no event shall the authors or contributors be liable for any
* direct, indirect, incidental, special, exemplary, or consequential
* damages (including, but not limited to, procurement of substitute
* goods or services; loss of use, data, or profits; or business
* interruption) however caused and on any theory of liability, whether
* in contract, strict liability, or tort (including negligence or
* otherwise) arising in any way out of the use of this software, even
* if advised of the possibility of such damage.
*/
/* We are testing some deprecated APIs here */
#ifndef GLIB_DISABLE_DEPRECATION_WARNINGS
#define GLIB_DISABLE_DEPRECATION_WARNINGS
#endif
#include <glib.h>
static GCond cond;
static GMutex mutex;
static gint next; /* locked by @mutex */
static void
push_value (gint value)
{
g_mutex_lock (&mutex);
while (next != 0)
g_cond_wait (&cond, &mutex);
next = value;
if (g_test_verbose ())
g_printerr ("Thread %p producing next value: %d\n", g_thread_self (), value);
if (value % 10 == 0)
g_cond_broadcast (&cond);
else
g_cond_signal (&cond);
g_mutex_unlock (&mutex);
}
static gint
pop_value (void)
{
gint value;
g_mutex_lock (&mutex);
while (next == 0)
{
if (g_test_verbose ())
g_printerr ("Thread %p waiting for cond\n", g_thread_self ());
g_cond_wait (&cond, &mutex);
}
value = next;
next = 0;
g_cond_broadcast (&cond);
if (g_test_verbose ())
g_printerr ("Thread %p consuming value %d\n", g_thread_self (), value);
g_mutex_unlock (&mutex);
return value;
}
static gpointer
produce_values (gpointer data)
{
gint total;
gint i;
total = 0;
for (i = 1; i < 100; i++)
{
total += i;
push_value (i);
}
push_value (-1);
push_value (-1);
if (g_test_verbose ())
g_printerr ("Thread %p produced %d altogether\n", g_thread_self (), total);
return GINT_TO_POINTER (total);
}
static gpointer
consume_values (gpointer data)
{
gint accum = 0;
gint value;
while (TRUE)
{
value = pop_value ();
if (value == -1)
break;
accum += value;
}
if (g_test_verbose ())
g_printerr ("Thread %p accumulated %d\n", g_thread_self (), accum);
return GINT_TO_POINTER (accum);
}
static GThread *producer, *consumer1, *consumer2;
static void
test_cond1 (void)
{
gint total, acc1, acc2;
producer = g_thread_create (produce_values, NULL, TRUE, NULL);
consumer1 = g_thread_create (consume_values, NULL, TRUE, NULL);
consumer2 = g_thread_create (consume_values, NULL, TRUE, NULL);
total = GPOINTER_TO_INT (g_thread_join (producer));
acc1 = GPOINTER_TO_INT (g_thread_join (consumer1));
acc2 = GPOINTER_TO_INT (g_thread_join (consumer2));
g_assert_cmpint (total, ==, acc1 + acc2);
}
typedef struct
{
GMutex mutex;
GCond cond;
gint limit;
gint count;
} Barrier;
static void
barrier_init (Barrier *barrier,
gint limit)
{
g_mutex_init (&barrier->mutex);
g_cond_init (&barrier->cond);
barrier->limit = limit;
barrier->count = limit;
}
static gint
barrier_wait (Barrier *barrier)
{
gint ret;
g_mutex_lock (&barrier->mutex);
barrier->count--;
if (barrier->count == 0)
{
ret = -1;
barrier->count = barrier->limit;
g_cond_broadcast (&barrier->cond);
}
else
{
ret = 0;
while (barrier->count != barrier->limit)
g_cond_wait (&barrier->cond, &barrier->mutex);
}
g_mutex_unlock (&barrier->mutex);
return ret;
}
static void
barrier_clear (Barrier *barrier)
{
g_mutex_clear (&barrier->mutex);
g_cond_clear (&barrier->cond);
}
static Barrier b;
static gint check;
static gpointer
cond2_func (gpointer data)
{
gint value = GPOINTER_TO_INT (data);
gint ret;
g_atomic_int_inc (&check);
if (g_test_verbose ())
g_printerr ("thread %d starting, check %d\n", value, g_atomic_int_get (&check));
g_usleep (10000 * value);
g_atomic_int_inc (&check);
if (g_test_verbose ())
g_printerr ("thread %d reaching barrier, check %d\n", value, g_atomic_int_get (&check));
ret = barrier_wait (&b);
g_assert_cmpint (g_atomic_int_get (&check), ==, 10);
if (g_test_verbose ())
g_printerr ("thread %d leaving barrier (%d), check %d\n", value, ret, g_atomic_int_get (&check));
return NULL;
}
/* this test demonstrates how to use a condition variable
* to implement a barrier
*/
static void
test_cond2 (void)
{
gint i;
GThread *threads[5];
g_atomic_int_set (&check, 0);
barrier_init (&b, 5);
for (i = 0; i < 5; i++)
threads[i] = g_thread_create (cond2_func, GINT_TO_POINTER (i), TRUE, NULL);
for (i = 0; i < 5; i++)
g_thread_join (threads[i]);
g_assert_cmpint (g_atomic_int_get (&check), ==, 10);
barrier_clear (&b);
}
static void
test_wait_until (void)
{
gint64 until;
GMutex lock;
GCond local_cond;
/* This test will make sure we don't wait too much or too little.
*
* We check the 'too long' with a timeout of 60 seconds.
*
* We check the 'too short' by verifying a guarantee of the API: we
* should not wake up until the specified time has passed.
*/
g_mutex_init (&lock);
g_cond_init (&local_cond);
until = g_get_monotonic_time () + G_TIME_SPAN_SECOND;
/* Could still have spurious wakeups, so we must loop... */
g_mutex_lock (&lock);
while (g_cond_wait_until (&local_cond, &lock, until))
;
g_mutex_unlock (&lock);
/* Make sure it's after the until time */
g_assert_cmpint (until, <=, g_get_monotonic_time ());
/* Make sure it returns FALSE on timeout */
until = g_get_monotonic_time () + G_TIME_SPAN_SECOND / 50;
g_mutex_lock (&lock);
g_assert (g_cond_wait_until (&local_cond, &lock, until) == FALSE);
g_mutex_unlock (&lock);
g_mutex_clear (&lock);
g_cond_clear (&local_cond);
}
#ifdef __linux__
#include <pthread.h>
#include <signal.h>
#include <unistd.h>
static pthread_t main_thread;
static void *
mutex_holder (void *data)
{
GMutex *lock = data;
g_mutex_lock (lock);
/* Let the lock become contended */
g_usleep (G_TIME_SPAN_SECOND);
/* Interrupt the wait on the other thread */
pthread_kill (main_thread, SIGHUP);
/* If we don't sleep here, then the g_mutex_unlock() below will clear
* the mutex, causing the interrupted futex call in the other thread
* to return success (which is not what we want).
*
* The other thread needs to have time to wake up and see that the
* lock is still contended.
*/
g_usleep (G_TIME_SPAN_SECOND / 10);
g_mutex_unlock (lock);
return NULL;
}
static void
signal_handler (int sig)
{
}
static void
test_wait_until_errno (void)
{
gboolean result;
GMutex lock;
GCond cond;
struct sigaction act = { };
/* important: no SA_RESTART (we want EINTR) */
act.sa_handler = signal_handler;
g_test_summary ("Check proper handling of errno in g_cond_wait_until with a contended mutex");
g_test_bug ("https://gitlab.gnome.org/GNOME/glib/merge_requests/957");
g_mutex_init (&lock);
g_cond_init (&cond);
main_thread = pthread_self ();
sigaction (SIGHUP, &act, NULL);
g_mutex_lock (&lock);
/* We create an annoying worker thread that will do two things:
*
* 1) hold the lock that we want to reacquire after returning from
* the condition variable wait
*
* 2) send us a signal to cause our wait on the contended lock to
* return EINTR, clobbering the errno return from the condition
* variable
*/
g_thread_unref (g_thread_new ("mutex-holder", mutex_holder, &lock));
result = g_cond_wait_until (&cond, &lock,
g_get_monotonic_time () + G_TIME_SPAN_SECOND / 50);
/* Even after all that disruption, we should still successfully return
* 'timed out'.
*/
g_assert_false (result);
g_mutex_unlock (&lock);
g_cond_clear (&cond);
g_mutex_clear (&lock);
}
#else
static void
test_wait_until_errno (void)
{
g_test_skip ("We only test this on Linux");
}
#endif
int
main (int argc, char *argv[])
{
g_test_init (&argc, &argv, NULL);
g_test_add_func ("/thread/cond1", test_cond1);
g_test_add_func ("/thread/cond2", test_cond2);
g_test_add_func ("/thread/cond/wait-until", test_wait_until);
g_test_add_func ("/thread/cond/wait-until/contended-and-interrupted", test_wait_until_errno);
return g_test_run ();
}
|