Back to index

glibc  2.9
tst-cond18.c
Go to the documentation of this file.
00001 /* Copyright (C) 2004 Free Software Foundation, Inc.
00002    This file is part of the GNU C Library.
00003    Contributed by Jakub Jelinek <jakub@redhat.com>, 2004.
00004 
00005    The GNU C Library is free software; you can redistribute it and/or
00006    modify it under the terms of the GNU Lesser General Public
00007    License as published by the Free Software Foundation; either
00008    version 2.1 of the License, or (at your option) any later version.
00009 
00010    The GNU C Library is distributed in the hope that it will be useful,
00011    but WITHOUT ANY WARRANTY; without even the implied warranty of
00012    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013    Lesser General Public License for more details.
00014 
00015    You should have received a copy of the GNU Lesser General Public
00016    License along with the GNU C Library; if not, write to the Free
00017    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
00018    02111-1307 USA.  */
00019 
00020 #include <errno.h>
00021 #include <fcntl.h>
00022 #include <pthread.h>
00023 #include <stdbool.h>
00024 #include <stdlib.h>
00025 #include <stdio.h>
00026 #include <unistd.h>
00027 
00028 pthread_cond_t cv = PTHREAD_COND_INITIALIZER;
00029 pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;
00030 bool exiting;
00031 int fd, count, spins, nn;
00032 
00033 void *
00034 tf (void *id)
00035 {
00036   pthread_mutex_lock (&lock);
00037 
00038   if ((long) id == 0)
00039     {
00040       while (!exiting)
00041        {
00042          if ((spins++ % 1000) == 0)
00043            write (fd, ".", 1);
00044          pthread_mutex_unlock (&lock);
00045 
00046          pthread_mutex_lock (&lock);
00047          int njobs = rand () % (count + 1);
00048          nn = njobs;
00049          if ((rand () % 30) == 0)
00050            pthread_cond_broadcast (&cv);
00051          else
00052            while (njobs--)
00053              pthread_cond_signal (&cv);
00054        }
00055 
00056       pthread_cond_broadcast (&cv);
00057     }
00058   else
00059     {
00060       while (!exiting)
00061        {
00062          while (!nn && !exiting)
00063            pthread_cond_wait (&cv, &lock);
00064          --nn;
00065          pthread_mutex_unlock (&lock);
00066 
00067          pthread_mutex_lock (&lock);
00068        }
00069     }
00070 
00071   pthread_mutex_unlock (&lock);
00072   return NULL;
00073 }
00074 
00075 int
00076 do_test (void)
00077 {
00078   fd = open ("/dev/null", O_WRONLY);
00079   if (fd < 0)
00080     {
00081       printf ("couldn't open /dev/null, %m\n");
00082       return 1;
00083     }
00084 
00085   count = sysconf (_SC_NPROCESSORS_ONLN);
00086   if (count <= 0)
00087     count = 1;
00088   count *= 8;
00089 
00090   pthread_t th[count + 1];
00091   int i, ret;
00092 
00093   for (i = 0; i <= count; ++i)
00094     if ((ret = pthread_create (&th[i], NULL, tf, (void *) (long) i)) != 0)
00095       {
00096        errno = ret;
00097        printf ("pthread_create %d failed: %m\n", i);
00098        return 1;
00099       }
00100 
00101   struct timespec ts = { .tv_sec = 20, .tv_nsec = 0 };
00102   while (nanosleep (&ts, &ts) != 0);
00103 
00104   pthread_mutex_lock (&lock);
00105   exiting = true;
00106   pthread_mutex_unlock (&lock);
00107 
00108   for (i = 0; i < count; ++i)
00109     pthread_join (th[i], NULL);
00110 
00111   close (fd);
00112   return 0;
00113 }
00114 
00115 #define TEST_FUNCTION do_test ()
00116 #define TIMEOUT 40
00117 #include "../test-skeleton.c"