Back to index

glibc  2.9
ex10.c
Go to the documentation of this file.
00001 /* Tests for pthread_mutex_timedlock function.
00002    Copyright (C) 2000, 2001, 2002 Free Software Foundation, Inc.
00003    Contributed by Kaz Kylheku <kaz@ashi.footprints.net>, 2000.
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 License as
00007    published by the Free Software Foundation; either version 2.1 of the
00008    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; see the file COPYING.LIB.  If not,
00017    write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
00018    Boston, MA 02111-1307, USA.  */
00019 
00020 #include <errno.h>
00021 #include <error.h>
00022 #include <stdio.h>
00023 #include <stdlib.h>
00024 #include <pthread.h>
00025 #include <time.h>
00026 
00027 #define NUM_THREADS 10
00028 #define NUM_ITERS   50
00029 #define TIMEOUT_NS  100000000L
00030 
00031 static void *thread (void *)  __attribute__ ((__noreturn__));
00032 static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
00033 
00034 int
00035 main (void)
00036 {
00037   pthread_t th;
00038   int i;
00039 
00040   for (i = 0; i < NUM_THREADS; i++)
00041     {
00042       if (pthread_create (&th, NULL, thread, NULL) != 0)
00043        error (EXIT_FAILURE, 0, "cannot create thread");
00044     }
00045 
00046   (void) thread (NULL);
00047   /* notreached */
00048   return 0;
00049 }
00050 
00051 
00052 static void *
00053 thread (void *arg)
00054 {
00055   int i;
00056   pthread_t self = pthread_self ();
00057   static int linecount; /* protected by flockfile(stdout) */
00058 
00059   for (i = 0; i < NUM_ITERS; i++)
00060     {
00061       struct timespec ts;
00062 
00063       for (;;)
00064        {
00065          int err;
00066 
00067          clock_gettime (CLOCK_REALTIME, &ts);
00068 
00069          ts.tv_nsec += TIMEOUT_NS;
00070 
00071          if (ts.tv_nsec >= 1000000000L) {
00072             ts.tv_sec++;
00073             ts.tv_nsec -= 1000000000L;
00074          }
00075 
00076          switch ((err = pthread_mutex_timedlock (&mutex, &ts)))
00077            {
00078            case 0:
00079              flockfile (stdout);
00080              printf ("%04d: thread %lu got mutex\n", ++linecount,
00081                     (unsigned long) self);
00082              funlockfile (stdout);
00083              break;
00084            case ETIMEDOUT:
00085              flockfile (stdout);
00086              printf ("%04d: thread %lu timed out on mutex\n", ++linecount,
00087                     (unsigned long) self);
00088              funlockfile (stdout);
00089              continue;
00090            default:
00091              error (EXIT_FAILURE, err, "pthread_mutex_timedlock failure");
00092            }
00093          break;
00094        }
00095 
00096       ts.tv_sec = 0;
00097       ts.tv_nsec = TIMEOUT_NS;
00098       nanosleep (&ts, NULL);
00099 
00100       flockfile (stdout);
00101       printf ("%04d: thread %lu releasing mutex\n", ++linecount,
00102              (unsigned long) self);
00103       funlockfile (stdout);
00104       pthread_mutex_unlock (&mutex);
00105     }
00106 
00107   pthread_exit (NULL);
00108 }