Back to index

glibc  2.9
tst-eintr2.c
Go to the documentation of this file.
00001 /* Copyright (C) 2003, 2008 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 <string.h>
00026 #include <sys/time.h>
00027 
00028 #include "eintr.c"
00029 
00030 
00031 static pthread_mutex_t m1 = PTHREAD_MUTEX_INITIALIZER;
00032 static pthread_mutex_t m2 = PTHREAD_MUTEX_INITIALIZER;
00033 
00034 
00035 static void *
00036 tf1 (void *arg)
00037 {
00038   struct timespec ts;
00039   struct timeval tv;
00040 
00041   gettimeofday (&tv, NULL);
00042   TIMEVAL_TO_TIMESPEC (&tv, &ts);
00043   ts.tv_sec += 10000;
00044 
00045   /* This call must never return.  */
00046   int e = pthread_mutex_timedlock (&m1, &ts);
00047   char buf[100];
00048   printf ("tf1: mutex_timedlock returned: %s\n",
00049          strerror_r (e, buf, sizeof (buf)));
00050 
00051   exit (1);
00052 }
00053 
00054 
00055 static void *
00056 tf2 (void *arg)
00057 {
00058   while (1)
00059     {
00060       int e = pthread_mutex_lock (&m2);
00061       if (e != 0)
00062        {
00063          puts ("tf2: mutex_lock failed");
00064          exit (1);
00065        }
00066       e = pthread_mutex_unlock (&m2);
00067       if (e != 0)
00068        {
00069          puts ("tf2: mutex_unlock failed");
00070          exit (1);
00071        }
00072       struct timespec ts = { .tv_sec = 0, .tv_nsec = 10000000 };
00073       nanosleep (&ts, NULL);
00074     }
00075 }
00076 
00077 
00078 static int
00079 do_test (void)
00080 {
00081   if (pthread_mutex_lock (&m1) != 0)
00082     {
00083       puts ("mutex_lock failed");
00084       exit (1);
00085     }
00086 
00087   setup_eintr (SIGUSR1, NULL);
00088 
00089   pthread_t th;
00090   char buf[100];
00091   int e = pthread_create (&th, NULL, tf1, NULL);
00092   if (e != 0)
00093     {
00094       printf ("main: 1st pthread_create failed: %s\n",
00095              strerror_r (e, buf, sizeof (buf)));
00096       exit (1);
00097     }
00098 
00099   e = pthread_create (&th, NULL, tf2, NULL);
00100   if (e != 0)
00101     {
00102       printf ("main: 2nd pthread_create failed: %s\n",
00103              strerror_r (e, buf, sizeof (buf)));
00104       exit (1);
00105     }
00106 
00107   /* This call must never return.  */
00108   e = pthread_mutex_lock (&m1);
00109   printf ("main: mutex_lock returned: %s\n",
00110          strerror_r (e, buf, sizeof (buf)));
00111 
00112   return 1;
00113 }
00114 
00115 #define EXPECTED_SIGNAL SIGALRM
00116 #define TIMEOUT 3
00117 #define TEST_FUNCTION do_test ()
00118 #include "../test-skeleton.c"