Back to index

glibc  2.9
tst-mutex7.c
Go to the documentation of this file.
00001 /* Copyright (C) 2002, 2004, 2006 Free Software Foundation, Inc.
00002    This file is part of the GNU C Library.
00003    Contributed by Ulrich Drepper <drepper@redhat.com>, 2002.
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 <stdio.h>
00023 #include <stdlib.h>
00024 #include <time.h>
00025 
00026 
00027 #ifndef TYPE
00028 # define TYPE PTHREAD_MUTEX_DEFAULT
00029 #endif
00030 
00031 
00032 static pthread_mutex_t lock;
00033 
00034 
00035 #define ROUNDS 1000
00036 #define N 100
00037 
00038 
00039 static void *
00040 tf (void *arg)
00041 {
00042   int nr = (long int) arg;
00043   int cnt;
00044   struct timespec ts = { .tv_sec = 0, .tv_nsec = 11000 };
00045 
00046   for (cnt = 0; cnt < ROUNDS; ++cnt)
00047     {
00048       if (pthread_mutex_lock (&lock) != 0)
00049        {
00050          printf ("thread %d: failed to get the lock\n", nr);
00051          return (void *) 1l;
00052        }
00053 
00054       if (pthread_mutex_unlock (&lock) != 0)
00055        {
00056          printf ("thread %d: failed to release the lock\n", nr);
00057          return (void *) 1l;
00058        }
00059 
00060       nanosleep (&ts, NULL);
00061     }
00062 
00063   return NULL;
00064 }
00065 
00066 
00067 static int
00068 do_test (void)
00069 {
00070   pthread_mutexattr_t a;
00071 
00072   if (pthread_mutexattr_init (&a) != 0)
00073     {
00074       puts ("mutexattr_init failed");
00075       exit (1);
00076     }
00077 
00078   if (pthread_mutexattr_settype (&a, TYPE) != 0)
00079     {
00080       puts ("mutexattr_settype failed");
00081       exit (1);
00082     }
00083 
00084 #ifdef ENABLE_PI
00085   if (pthread_mutexattr_setprotocol (&a, PTHREAD_PRIO_INHERIT) != 0)
00086     {
00087       puts ("pthread_mutexattr_setprotocol failed");
00088       return 1;
00089     }
00090 #endif
00091 
00092   int e = pthread_mutex_init (&lock, &a);
00093   if (e != 0)
00094     {
00095 #ifdef ENABLE_PI
00096       if (e == ENOTSUP)
00097        {
00098          puts ("PI mutexes unsupported");
00099          return 0;
00100        }
00101 #endif
00102       puts ("mutex_init failed");
00103       return 1;
00104     }
00105 
00106   if (pthread_mutexattr_destroy (&a) != 0)
00107     {
00108       puts ("mutexattr_destroy failed");
00109       return 1;
00110     }
00111 
00112   pthread_attr_t at;
00113   pthread_t th[N];
00114   int cnt;
00115 
00116   if (pthread_attr_init (&at) != 0)
00117     {
00118       puts ("attr_init failed");
00119       return 1;
00120     }
00121 
00122   if (pthread_attr_setstacksize (&at, 1 * 1024 * 1024) != 0)
00123     {
00124       puts ("attr_setstacksize failed");
00125       return 1;
00126     }
00127 
00128   if (pthread_mutex_lock (&lock) != 0)
00129     {
00130       puts ("locking in parent failed");
00131       return 1;
00132     }
00133 
00134   for (cnt = 0; cnt < N; ++cnt)
00135     if (pthread_create (&th[cnt], &at, tf, (void *) (long int) cnt) != 0)
00136       {
00137        printf ("creating thread %d failed\n", cnt);
00138        return 1;
00139       }
00140 
00141   if (pthread_attr_destroy (&at) != 0)
00142     {
00143       puts ("attr_destroy failed");
00144       return 1;
00145     }
00146 
00147   if (pthread_mutex_unlock (&lock) != 0)
00148     {
00149       puts ("unlocking in parent failed");
00150       return 1;
00151     }
00152 
00153   for (cnt = 0; cnt < N; ++cnt)
00154     if (pthread_join (th[cnt], NULL) != 0)
00155       {
00156        printf ("joining thread %d failed\n", cnt);
00157        return 1;
00158       }
00159 
00160   return 0;
00161 }
00162 
00163 #define TIMEOUT 60
00164 #define TEST_FUNCTION do_test ()
00165 #include "../test-skeleton.c"