Back to index

glibc  2.9
tst-kill2.c
Go to the documentation of this file.
00001 /* Copyright (C) 2003 Free Software Foundation, Inc.
00002    This file is part of the GNU C Library.
00003    Contributed by Ulrich Drepper <drepper@redhat.com>, 2003.
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 <signal.h>
00023 #include <stdio.h>
00024 #include <stdlib.h>
00025 #include <sys/time.h>
00026 
00027 
00028 static pthread_cond_t c = PTHREAD_COND_INITIALIZER;
00029 static pthread_mutex_t m = PTHREAD_MUTEX_INITIALIZER;
00030 static pthread_barrier_t b;
00031 
00032 static void *
00033 tf (void *a)
00034 {
00035   /* Block SIGUSR1.  */
00036   sigset_t ss;
00037 
00038   sigemptyset (&ss);
00039   sigaddset (&ss, SIGUSR1);
00040   if (pthread_sigmask (SIG_BLOCK, &ss, NULL) != 0)
00041     {
00042       puts ("child: sigmask failed");
00043       exit (1);
00044     }
00045 
00046   if (pthread_mutex_lock (&m) != 0)
00047     {
00048       puts ("child: mutex_lock failed");
00049       exit (1);
00050     }
00051 
00052   int e = pthread_barrier_wait (&b);
00053   if (e != 0 && e != PTHREAD_BARRIER_SERIAL_THREAD)
00054     {
00055       puts ("child: barrier_wait failed");
00056       exit (1);
00057     }
00058 
00059   /* Compute timeout.  */
00060   struct timeval tv;
00061   (void) gettimeofday (&tv, NULL);
00062   struct timespec ts;
00063   TIMEVAL_TO_TIMESPEC (&tv, &ts);
00064   /* Timeout: 1sec.  */
00065   ts.tv_sec += 1;
00066 
00067   /* This call should never return.  */
00068   if (pthread_cond_timedwait (&c, &m, &ts) != ETIMEDOUT)
00069     {
00070       puts ("cond_timedwait didn't time out");
00071       exit (1);
00072     }
00073 
00074   return NULL;
00075 }
00076 
00077 
00078 int
00079 do_test (void)
00080 {
00081   pthread_t th;
00082 
00083   if (pthread_barrier_init (&b, NULL, 2) != 0)
00084     {
00085       puts ("barrier_init failed");
00086       exit (1);
00087     }
00088 
00089   if (pthread_create (&th, NULL, tf, NULL) != 0)
00090     {
00091       puts ("create failed");
00092       exit (1);
00093     }
00094 
00095   int e = pthread_barrier_wait (&b);
00096   if (e != 0 && e != PTHREAD_BARRIER_SERIAL_THREAD)
00097     {
00098       puts ("barrier_wait failed");
00099       exit (1);
00100     }
00101 
00102   if (pthread_mutex_lock (&m) != 0)
00103     {
00104       puts ("mutex_lock failed");
00105       exit (1);
00106     }
00107 
00108   /* Send the thread a signal which it has blocked.  */
00109   if (pthread_kill (th, SIGUSR1) != 0)
00110     {
00111       puts ("kill failed");
00112       exit (1);
00113     }
00114 
00115   if (pthread_mutex_unlock (&m) != 0)
00116     {
00117       puts ("mutex_unlock failed");
00118       exit (1);
00119     }
00120 
00121   void *r;
00122   if (pthread_join (th, &r) != 0)
00123     {
00124       puts ("join failed");
00125       exit (1);
00126     }
00127   if (r != NULL)
00128     {
00129       puts ("return value wrong");
00130       exit (1);
00131     }
00132 
00133   return 0;
00134 }
00135 
00136 
00137 #define TIMEOUT 5
00138 #define TEST_FUNCTION do_test ()
00139 #include "../test-skeleton.c"