1 /* Tests for pthread_mutex_timedlock function.
2 Copyright (C) 2000, 2001, 2002 Free Software Foundation, Inc.
3 Contributed by Kaz Kylheku <kaz@ashi.footprints.net>, 2000.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public License as
7 published by the Free Software Foundation; either version 2.1 of the
8 License, or (at your option) any later version.
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; see the file COPYING.LIB. If not,
17 write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA. */
27 #define NUM_THREADS 10
29 #define TIMEOUT_NS 100000000L
31 static void *thread (void *) __attribute__ ((__noreturn__
));
32 static pthread_mutex_t mutex
= PTHREAD_MUTEX_INITIALIZER
;
40 for (i
= 0; i
< NUM_THREADS
; i
++)
42 if (pthread_create (&th
, NULL
, thread
, NULL
) != 0)
43 error (EXIT_FAILURE
, 0, "cannot create thread");
56 pthread_t self
= pthread_self ();
57 static int linecount
; /* protected by flockfile(stdout) */
59 for (i
= 0; i
< NUM_ITERS
; i
++)
67 clock_gettime (CLOCK_REALTIME
, &ts
);
69 ts
.tv_nsec
+= TIMEOUT_NS
;
71 if (ts
.tv_nsec
>= 1000000000L) {
73 ts
.tv_nsec
-= 1000000000L;
76 switch ((err
= pthread_mutex_timedlock (&mutex
, &ts
)))
80 printf ("%04d: thread %lu got mutex\n", ++linecount
,
81 (unsigned long) self
);
86 printf ("%04d: thread %lu timed out on mutex\n", ++linecount
,
87 (unsigned long) self
);
91 error (EXIT_FAILURE
, err
, "pthread_mutex_timedlock failure");
97 ts
.tv_nsec
= TIMEOUT_NS
;
98 nanosleep (&ts
, NULL
);
101 printf ("%04d: thread %lu releasing mutex\n", ++linecount
,
102 (unsigned long) self
);
103 funlockfile (stdout
);
104 pthread_mutex_unlock (&mutex
);