1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
|
/* $OpenBSD: flockfile_test.c,v 1.1 2026/01/19 23:01:00 guenther Exp $ */
/*
* Copyright (c) 2026 Philip Guenther <guenther@openbsd.org>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include <stdio.h>
#include <pthread.h>
#include "local.h"
pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
int did_test1 = 0;
static void *
test1(void *v)
{
int r;
if (ftrylockfile(stdin) == 0)
errx(1, "Lock on stdin never held");
if ((r = pthread_mutex_lock(&lock)))
errc(1, r, "could not lock lock");
did_test1 = 1;
if ((r = pthread_cond_signal(&cond)))
errc(1, r, "could not signal cond");
if ((r = pthread_mutex_unlock(&lock)))
errc(1, r, "could not unlock lock");
flockfile(stdin);
funlockfile(stdin);
return NULL;
}
int
main(void)
{
int fd, r;
pthread_t thr;
/*
* Make sure flockfile() is handled correctly before the first
* thread is created.
*/
flockfile(stdin);
flockfile(stdin);
if ((r = pthread_create(&thr, NULL, test1, NULL)))
warnc(r, "could not create thread");
if ((r = pthread_mutex_lock(&lock)))
errc(1, r, "could not lock lock");
while (did_test1 == 0)
if ((r = pthread_cond_wait(&cond, &lock)))
errc(1, r, "could not wait on cond");
if ((r = pthread_mutex_unlock(&lock)))
errc(1, r, "could not unlock lock");
funlockfile(stdin);
funlockfile(stdin);
if ((r = pthread_join(thr, NULL)))
warnc(r, "could not join thread");
exit(0);
}
|