root/maint/gnulib/tests/test-cnd.c

/* [previous][next][first][last][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. cnd_wait_routine
  2. test_cnd_wait
  3. get_ts
  4. cnd_timedwait_routine
  5. test_cnd_timedwait
  6. main

   1 /* Test of condition variables in multithreaded situations.
   2    Copyright (C) 2008-2021 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 #include <config.h>
  18 
  19 /* Which tests to perform.
  20    Uncomment some of these, to verify that all tests crash if no locking
  21    is enabled.  */
  22 #define DO_TEST_COND 1
  23 #define DO_TEST_TIMEDCOND 1
  24 
  25 
  26 /* Whether to help the scheduler through explicit thrd_yield().
  27    Uncomment this to see if the operating system has a fair scheduler.  */
  28 #define EXPLICIT_YIELD 1
  29 
  30 /* Whether to print debugging messages.  */
  31 #define ENABLE_DEBUGGING 0
  32 
  33 #include <threads.h>
  34 #include <stdio.h>
  35 #include <stdlib.h>
  36 #include <string.h>
  37 #include <sys/time.h>
  38 
  39 #if HAVE_DECL_ALARM
  40 # include <signal.h>
  41 # include <unistd.h>
  42 #endif
  43 
  44 #include "macros.h"
  45 
  46 #if ENABLE_DEBUGGING
  47 # define dbgprintf printf
  48 #else
  49 # define dbgprintf if (0) printf
  50 #endif
  51 
  52 #if EXPLICIT_YIELD
  53 # define yield() thrd_yield ()
  54 #else
  55 # define yield()
  56 #endif
  57 
  58 
  59 /*
  60  * Condition check
  61  */
  62 static int cond_value = 0;
  63 static cnd_t condtest;
  64 static mtx_t lockcond;
  65 
  66 static int
  67 cnd_wait_routine (void *arg)
     /* [previous][next][first][last][top][bottom][index][help] */
  68 {
  69   ASSERT (mtx_lock (&lockcond) == thrd_success);
  70   while (!cond_value)
  71     {
  72       ASSERT (cnd_wait (&condtest, &lockcond) == thrd_success);
  73     }
  74   ASSERT (mtx_unlock (&lockcond) == thrd_success);
  75 
  76   cond_value = 2;
  77 
  78   return 0;
  79 }
  80 
  81 static void
  82 test_cnd_wait ()
     /* [previous][next][first][last][top][bottom][index][help] */
  83 {
  84   struct timespec remain;
  85   thrd_t thread;
  86   int ret;
  87 
  88   remain.tv_sec = 2;
  89   remain.tv_nsec = 0;
  90 
  91   cond_value = 0;
  92 
  93   ASSERT (thrd_create (&thread, cnd_wait_routine, NULL) == thrd_success);
  94   do
  95     {
  96       yield ();
  97       ret = thrd_sleep (&remain, &remain);
  98       ASSERT (ret >= -1);
  99     }
 100   while (ret == -1 && (remain.tv_sec != 0 || remain.tv_nsec != 0));
 101 
 102   /* signal condition */
 103   ASSERT (mtx_lock (&lockcond) == thrd_success);
 104   cond_value = 1;
 105   ASSERT (cnd_signal (&condtest) == thrd_success);
 106   ASSERT (mtx_unlock (&lockcond) == thrd_success);
 107 
 108   ASSERT (thrd_join (thread, NULL) == thrd_success);
 109 
 110   if (cond_value != 2)
 111     abort ();
 112 }
 113 
 114 
 115 /*
 116  * Timed Condition check
 117  */
 118 static int cond_timeout;
 119 
 120 static void
 121 get_ts (struct timespec *ts)
     /* [previous][next][first][last][top][bottom][index][help] */
 122 {
 123   struct timeval now;
 124 
 125   gettimeofday (&now, NULL);
 126 
 127   ts->tv_sec = now.tv_sec + 1;
 128   ts->tv_nsec = now.tv_usec * 1000;
 129 }
 130 
 131 static int
 132 cnd_timedwait_routine (void *arg)
     /* [previous][next][first][last][top][bottom][index][help] */
 133 {
 134   int ret;
 135   struct timespec ts;
 136 
 137   ASSERT (mtx_lock (&lockcond) == thrd_success);
 138   while (!cond_value)
 139     {
 140       get_ts (&ts);
 141       ret = cnd_timedwait (&condtest, &lockcond, &ts);
 142       if (ret == thrd_timedout)
 143         cond_timeout = 1;
 144     }
 145   ASSERT (mtx_unlock (&lockcond) == thrd_success);
 146 
 147   return 0;
 148 }
 149 
 150 static void
 151 test_cnd_timedwait (void)
     /* [previous][next][first][last][top][bottom][index][help] */
 152 {
 153   struct timespec remain;
 154   thrd_t thread;
 155   int ret;
 156 
 157   remain.tv_sec = 2;
 158   remain.tv_nsec = 0;
 159 
 160   cond_value = cond_timeout = 0;
 161 
 162   ASSERT (thrd_create (&thread, cnd_timedwait_routine, NULL) == thrd_success);
 163   do
 164     {
 165       yield ();
 166       ret = thrd_sleep (&remain, &remain);
 167       ASSERT (ret >= -1);
 168     }
 169   while (ret == -1 && (remain.tv_sec != 0 || remain.tv_nsec != 0));
 170 
 171   /* signal condition */
 172   ASSERT (mtx_lock (&lockcond) == thrd_success);
 173   cond_value = 1;
 174   ASSERT (cnd_signal (&condtest) == thrd_success);
 175   ASSERT (mtx_unlock (&lockcond) == thrd_success);
 176 
 177   ASSERT (thrd_join (thread, NULL) == thrd_success);
 178 
 179   if (!cond_timeout)
 180     abort ();
 181 }
 182 
 183 int
 184 main ()
     /* [previous][next][first][last][top][bottom][index][help] */
 185 {
 186 #if HAVE_DECL_ALARM
 187   /* Declare failure if test takes too long, by using default abort
 188      caused by SIGALRM.  */
 189   int alarm_value = 600;
 190   signal (SIGALRM, SIG_DFL);
 191   alarm (alarm_value);
 192 #endif
 193 
 194   ASSERT (cnd_init (&condtest) == thrd_success);
 195   ASSERT (mtx_init (&lockcond, mtx_plain) == thrd_success);
 196 
 197 #if DO_TEST_COND
 198   printf ("Starting test_cnd_wait ..."); fflush (stdout);
 199   test_cnd_wait ();
 200   printf (" OK\n"); fflush (stdout);
 201 #endif
 202 #if DO_TEST_TIMEDCOND
 203   printf ("Starting test_cnd_timedwait ..."); fflush (stdout);
 204   test_cnd_timedwait ();
 205   printf (" OK\n"); fflush (stdout);
 206 #endif
 207 
 208   return 0;
 209 }

/* [previous][next][first][last][top][bottom][index][help] */