1 /* Copyright (C) 2002, 2003, 2004 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3 Contributed by Ulrich Drepper <drepper@redhat.com>, 2002.
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
7 License as published by the Free Software Foundation; either
8 version 2.1 of the 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; if not, see
17 <http://www.gnu.org/licenses/>. */
29 static pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;
30 static pthread_mutex_t lock2 = PTHREAD_MUTEX_INITIALIZER;
44 if (TEMP_FAILURE_RETRY (fcntl (fd, F_SETLKW, &fl)) != 0)
46 puts ("fourth fcntl failed");
50 pthread_mutex_unlock (&lock);
52 pthread_mutex_lock (&lock2);
61 char tmp[] = "/tmp/tst-flock2-XXXXXX";
66 puts ("mkstemp failed");
73 for (i = 0; i < 20; ++i)
74 write (fd, "foobar xyzzy", 12);
77 b = mmap (NULL, sizeof (pthread_barrier_t), PROT_READ | PROT_WRITE,
85 pthread_barrierattr_t ba;
86 if (pthread_barrierattr_init (&ba) != 0)
88 puts ("barrierattr_init failed");
92 if (pthread_barrierattr_setpshared (&ba, PTHREAD_PROCESS_SHARED) != 0)
94 puts ("barrierattr_setpshared failed");
98 if (pthread_barrier_init (b, &ba, 2) != 0)
100 puts ("barrier_init failed");
104 if (pthread_barrierattr_destroy (&ba) != 0)
106 puts ("barrierattr_destroy failed");
114 .l_whence = SEEK_SET,
117 if (TEMP_FAILURE_RETRY (fcntl (fd, F_SETLKW, &fl)) != 0)
119 puts ("first fcntl failed");
126 puts ("fork failed");
132 /* Make sure the child does not stay around indefinitely. */
135 /* Try to get the lock. */
136 if (TEMP_FAILURE_RETRY (fcntl (fd, F_SETLK, &fl)) == 0)
138 puts ("child: second flock succeeded");
143 pthread_barrier_wait (b);
148 if (TEMP_FAILURE_RETRY (fcntl (fd, F_SETLKW, &fl)) != 0)
150 puts ("third fcntl failed");
155 pthread_barrier_wait (b);
160 if (pthread_mutex_lock (&lock) != 0)
162 puts ("1st locking of lock failed");
166 if (pthread_mutex_lock (&lock2) != 0)
168 puts ("1st locking of lock2 failed");
172 if (pthread_create (&th, NULL, tf, NULL) != 0)
174 puts ("pthread_create failed");
178 if (pthread_mutex_lock (&lock) != 0)
180 puts ("2nd locking of lock failed");
184 puts ("child locked file");
187 pthread_barrier_wait (b);
192 if (TEMP_FAILURE_RETRY (fcntl (fd, F_SETLK, &fl)) == 0)
194 puts ("fifth fcntl succeeded");
198 puts ("file locked by child");
201 pthread_barrier_wait (b);
205 if (pthread_mutex_unlock (&lock2) != 0)
207 puts ("unlock of lock2 failed");
211 if (pthread_join (th, NULL) != 0)
213 puts ("join failed");
217 puts ("child's thread terminated");
220 pthread_barrier_wait (b);
225 if (TEMP_FAILURE_RETRY (fcntl (fd, F_SETLK, &fl)) == 0)
227 puts ("fifth fcntl succeeded");
231 puts ("file still locked");
234 pthread_barrier_wait (b);
242 if (TEMP_FAILURE_RETRY (waitpid (pid, &status, 0)) != pid)
244 puts ("waitpid failed");
247 puts ("child terminated");
249 if (TEMP_FAILURE_RETRY (fcntl (fd, F_SETLKW, &fl)) != 0)
251 puts ("sixth fcntl failed");
258 #define TEST_FUNCTION do_test ()
259 #include "../test-skeleton.c"