Back to index

glibc  2.9
tst-cond16.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 <pthread.h>
00022 #include <stdbool.h>
00023 #include <stdio.h>
00024 #include <stdlib.h>
00025 #include <unistd.h>
00026 
00027 pthread_cond_t cv = PTHREAD_COND_INITIALIZER;
00028 pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;
00029 bool n, exiting;
00030 FILE *f;
00031 int count;
00032 
00033 void *
00034 tf (void *dummy)
00035 {
00036   bool loop = true;
00037 
00038   while (loop)
00039     {
00040       pthread_mutex_lock (&lock);
00041       while (n && !exiting)
00042        pthread_cond_wait (&cv, &lock);
00043       n = true;
00044       pthread_mutex_unlock (&lock);
00045 
00046       fputs (".", f);
00047 
00048       pthread_mutex_lock (&lock);
00049       n = false;
00050       if (exiting)
00051        loop = false;
00052 #ifdef UNLOCK_AFTER_BROADCAST
00053       pthread_cond_broadcast (&cv);
00054       pthread_mutex_unlock (&lock);
00055 #else
00056       pthread_mutex_unlock (&lock);
00057       pthread_cond_broadcast (&cv);
00058 #endif
00059     }
00060 
00061   return NULL;
00062 }
00063 
00064 int
00065 do_test (void)
00066 {
00067   f = fopen ("/dev/null", "w");
00068   if (f == NULL)
00069     {
00070       printf ("couldn't open /dev/null, %m\n");
00071       return 1;
00072     }
00073 
00074   count = sysconf (_SC_NPROCESSORS_ONLN);
00075   if (count <= 0)
00076     count = 1;
00077   count *= 4;
00078 
00079   pthread_t th[count];
00080   int i, ret;
00081   for (i = 0; i < count; ++i)
00082     if ((ret = pthread_create (&th[i], NULL, tf, NULL)) != 0)
00083       {
00084        errno = ret;
00085        printf ("pthread_create %d failed: %m\n", i);
00086        return 1;
00087       }
00088 
00089   struct timespec ts = { .tv_sec = 20, .tv_nsec = 0 };
00090   while (nanosleep (&ts, &ts) != 0);
00091 
00092   pthread_mutex_lock (&lock);
00093   exiting = true;
00094   pthread_mutex_unlock (&lock);
00095 
00096   for (i = 0; i < count; ++i)
00097     pthread_join (th[i], NULL);
00098 
00099   fclose (f);
00100   return 0;
00101 }
00102 
00103 #define TEST_FUNCTION do_test ()
00104 #define TIMEOUT 40
00105 #include "../test-skeleton.c"