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