Index | Thread | Search

From:
"Ted Unangst" <tedu@tedunangst.com>
Subject:
flockfile.3
To:
tech@openbsd.org
Date:
Tue, 08 Jul 2025 23:30:44 -0400

Download raw body.

Thread
  • Ted Unangst:

    flockfile.3

Here's a new man page for flockfile.3 to be placed in libc/stdio.

Also, a diff for the getc.3 and putc.3 for the unlocked functions.

.\" $OpenBSD$
.\"
.\" Copyright (c) 2025 Ted Unangst <tedu@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.
.\"
.Dd $Mdocdate$
.Dt FLOCKFILE 3
.Os
.Sh NAME
.Nm flockfile ,
.Nm ftrylockfile ,
.Nm funlockfile
.Nd locking for stdio streams
.Sh SYNOPSIS
.In stdio.h
.Ft void
.Fn flockfile "FILE *file"
.Ft int
.Fn ftrylockfile "FILE *file"
.Ft void
.Fn funlockfile "FILE *file"
.Sh DESCRIPTION
These functions provide application control over the locking of stdio streams.
.Fn flockfile
increments the lock count associated with
.Fa file ,
blocking until the lock is available.
.Fn ftrylockfile
does the same but will not block if another thread owns the lock.
.Pp
.Fn funlockfile
decrements the lock count by one.
.Pp
Functions such as
.Xr fread 3
and
.Xr fprintf 3
are internally thread safe by default, but additional locking may be used
to coordinate sequences of multiple calls.
.Sh RETURN VALUES
.Fn ftrylockfile
returns zero for success and non-zero for failure.
.Sh ERRORS
None.
.Sh SEE ALSO
.Xr getc_unlocked ,
.Xr putc_unlocked ,
.Xr pthreads 3
.Sh STANDARDS
These functions conform to
.St -p1003.1-2024 .
.Sh HISTORY
These functions have been available since
.Ox 2.5 .
.Sh CAVEATS
Reading from one stream can flush a different buffered output stream,
leading to deadlocks.


Index: Makefile.inc
===================================================================
RCS file: /home/cvs/src/lib/libc/stdio/Makefile.inc,v
diff -u -p -r1.31 Makefile.inc
--- Makefile.inc	12 Aug 2024 20:56:55 -0000	1.31
+++ Makefile.inc	9 Jul 2025 03:14:10 -0000
@@ -22,7 +22,8 @@ SRCS+=	asprintf.c clrerr.c fclose.c fdop
 	getdelim.c getline.c dprintf.c vdprintf.c \
 	fpending.c freadahead.c freading.c freadptr.c fseterr.c fwriting.c
 
-MAN+=	fclose.3 ferror.3 fflush.3 fgetln.3 fgets.3 fgetwln.3 fmemopen.3 \
+MAN+=	fclose.3 ferror.3 fflush.3 fgetln.3 fgets.3 fgetwln.3 \
+	flockfile.3 fmemopen.3 \
 	fopen.3 __fpending.3 fputs.3 fread.3 fseek.3 funopen.3 getc.3 \
 	open_memstream.3 perror.3 printf.3 putc.3 remove.3 scanf.3 setbuf.3 \
 	setvbuf.3 stdio.3 tmpnam.3 ungetc.3 fgetws.3 fputws.3 fwide.3 getwc.3 \
Index: getc.3
===================================================================
RCS file: /home/cvs/src/lib/libc/stdio/getc.3,v
diff -u -p -r1.17 getc.3
--- getc.3	28 Sep 2023 01:51:00 -0000	1.17
+++ getc.3	9 Jul 2025 03:25:14 -0000
@@ -49,6 +49,10 @@
 .Ft int
 .Fn getchar "void"
 .Ft int
+.Fn getc_unlocked "FILE *stream"
+.Ft int
+.Fn getchar_unlocked "void"
+.Ft int
 .Fn getw "FILE *stream"
 .Sh DESCRIPTION
 The
@@ -73,6 +77,14 @@ with the argument
 .Em stdin .
 .Pp
 The
+.Fn getc_unlocked
+and
+.Fn getchar_unlocked
+functions perform the same action, but do not obtain the stream lock.
+They require that the stream first be locked with
+.Xr flockfile 3 .
+.Pp
+The
 .Fn getw
 function obtains the next
 .Vt int
@@ -99,6 +111,7 @@ until the condition is cleared with
 .Xr clearerr 3 .
 .Sh SEE ALSO
 .Xr ferror 3 ,
+.Xr flockfile ,
 .Xr fopen 3 ,
 .Xr fread 3 ,
 .Xr getwc 3 ,
@@ -112,6 +125,12 @@ and
 .Fn getchar
 functions conform to
 .St -ansiC .
+The
+.Fn getc_unlocked
+and
+.Fn getchar_unlocked
+conform to
+.St -p1003.1-2024 .
 .Sh HISTORY
 The
 .Fn getc
Index: putc.3
===================================================================
RCS file: /home/cvs/src/lib/libc/stdio/putc.3,v
diff -u -p -r1.14 putc.3
--- putc.3	11 Sep 2022 06:38:11 -0000	1.14
+++ putc.3	9 Jul 2025 03:24:34 -0000
@@ -49,6 +49,10 @@
 .Ft int
 .Fn putchar "int c"
 .Ft int
+.Fn putc_unlocked "int c" "FILE *stream"
+.Ft int
+.Fn putchar_unlocked "int c"
+.Ft int
 .Fn putw "int w" "FILE *stream"
 .Sh DESCRIPTION
 The
@@ -77,6 +81,14 @@ with an output stream of
 .Em stdout .
 .Pp
 The
+.Fn putc_unlocked
+and
+.Fn putchar_unlocked
+functions perform the same action, but do not obtain the stream lock.
+They require that the stream first be locked with
+.Xr flockfile 3 .
+.Pp
+The
 .Fn putw
 function writes the specified
 .Vt int
@@ -115,6 +127,7 @@ or
 .Xr realloc 3 .
 .Sh SEE ALSO
 .Xr ferror 3 ,
+.Xr flockfile ,
 .Xr fopen 3 ,
 .Xr getc 3 ,
 .Xr putwc 3 ,
@@ -127,6 +140,12 @@ and
 .Fn putchar ,
 conform to
 .St -ansiC .
+The
+.Fn putc_unlocked
+and
+.Fn putchar_unlocked
+conform to
+.St -p1003.1-2024 .
 .Sh HISTORY
 The
 .Fn putc