Back to index

glibc  2.9
tst-kill3.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 <unistd.h>
00026 #include <sys/time.h>
00027 
00028 
00029 static pthread_cond_t c = PTHREAD_COND_INITIALIZER;
00030 static pthread_mutex_t m = PTHREAD_MUTEX_INITIALIZER;
00031 static pthread_barrier_t b;
00032 
00033 
00034 static void
00035 handler (int sig)
00036 {
00037   write (1, "handler called\n", 15);
00038   _exit (1);
00039 }
00040 
00041 
00042 static void *
00043 tf (void *a)
00044 {
00045   /* Block SIGUSR1.  */
00046   sigset_t ss;
00047 
00048   sigemptyset (&ss);
00049   sigaddset (&ss, SIGUSR1);
00050   if (pthread_sigmask (SIG_BLOCK, &ss, NULL) != 0)
00051     {
00052       puts ("child: sigmask failed");
00053       exit (1);
00054     }
00055 
00056   if (pthread_mutex_lock (&m) != 0)
00057     {
00058       puts ("child: mutex_lock failed");
00059       exit (1);
00060     }
00061 
00062   int e = pthread_barrier_wait (&b);
00063   if (e != 0 && e != PTHREAD_BARRIER_SERIAL_THREAD)
00064     {
00065       puts ("child: barrier_wait failed");
00066       exit (1);
00067     }
00068 
00069   /* Compute timeout.  */
00070   struct timeval tv;
00071   (void) gettimeofday (&tv, NULL);
00072   struct timespec ts;
00073   TIMEVAL_TO_TIMESPEC (&tv, &ts);
00074   /* Timeout: 1sec.  */
00075   ts.tv_sec += 1;
00076 
00077   /* This call should never return.  */
00078   if (pthread_cond_timedwait (&c, &m, &ts) != ETIMEDOUT)
00079     {
00080       puts ("cond_timedwait didn't time out");
00081       exit (1);
00082     }
00083 
00084   return NULL;
00085 }
00086 
00087 
00088 int
00089 do_test (void)
00090 {
00091   pthread_t th;
00092 
00093   struct sigaction sa;
00094   sigemptyset (&sa.sa_mask);
00095   sa.sa_flags = 0;
00096   sa.sa_handler = handler;
00097   if (sigaction (SIGUSR1, &sa, NULL) != 0)
00098     {
00099       puts ("sigaction failed");
00100       exit (1);
00101     }
00102 
00103   if (pthread_barrier_init (&b, NULL, 2) != 0)
00104     {
00105       puts ("barrier_init failed");
00106       exit (1);
00107     }
00108 
00109   if (pthread_create (&th, NULL, tf, NULL) != 0)
00110     {
00111       puts ("create failed");
00112       exit (1);
00113     }
00114 
00115   int e = pthread_barrier_wait (&b);
00116   if (e != 0 && e != PTHREAD_BARRIER_SERIAL_THREAD)
00117     {
00118       puts ("barrier_wait failed");
00119       exit (1);
00120     }
00121 
00122   if (pthread_mutex_lock (&m) != 0)
00123     {
00124       puts ("mutex_lock failed");
00125       exit (1);
00126     }
00127 
00128   /* Send the thread a signal which it has blocked.  */
00129   if (pthread_kill (th, SIGUSR1) != 0)
00130     {
00131       puts ("kill failed");
00132       exit (1);
00133     }
00134 
00135   if (pthread_mutex_unlock (&m) != 0)
00136     {
00137       puts ("mutex_unlock failed");
00138       exit (1);
00139     }
00140 
00141   void *r;
00142   if (pthread_join (th, &r) != 0)
00143     {
00144       puts ("join failed");
00145       exit (1);
00146     }
00147   if (r != NULL)
00148     {
00149       puts ("return value wrong");
00150       exit (1);
00151     }
00152 
00153   return 0;
00154 }
00155 
00156 
00157 #define TIMEOUT 5
00158 #define TEST_FUNCTION do_test ()
00159 #include "../test-skeleton.c"