xref: /freebsd/lib/libc/stdio/flockfile.3 (revision 315ee00f)
1.\" Copyright (c) 2003 Tim J. Robbins
2.\" All rights reserved.
3.\"
4.\" Redistribution and use in source and binary forms, with or without
5.\" modification, are permitted provided that the following conditions
6.\" are met:
7.\" 1. Redistributions of source code must retain the above copyright
8.\"    notice, this list of conditions and the following disclaimer.
9.\" 2. Redistributions in binary form must reproduce the above copyright
10.\"    notice, this list of conditions and the following disclaimer in the
11.\"    documentation and/or other materials provided with the distribution.
12.\"
13.\" THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
14.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16.\" ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
17.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
18.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
19.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
20.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
21.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
22.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
23.\" SUCH DAMAGE.
24.\"
25.Dd January 10, 2003
26.Dt FLOCKFILE 3
27.Os
28.Sh NAME
29.Nm flockfile ,
30.Nm ftrylockfile ,
31.Nm funlockfile
32.Nd "stdio locking functions"
33.Sh LIBRARY
34.Lb libc
35.Sh SYNOPSIS
36.In stdio.h
37.Ft void
38.Fn flockfile "FILE *stream"
39.Ft int
40.Fn ftrylockfile "FILE *stream"
41.Ft void
42.Fn funlockfile "FILE *stream"
43.Sh DESCRIPTION
44These functions provide explicit application-level locking of stdio streams.
45They can be used to avoid output from multiple threads being interspersed,
46input being dispersed among multiple readers, and to avoid the overhead
47of locking the stream for each operation.
48.Pp
49The
50.Fn flockfile
51function acquires an exclusive lock on the specified stream.
52If another thread has already locked the stream,
53.Fn flockfile
54will block until the lock is released.
55.Pp
56The
57.Fn ftrylockfile
58function is a non-blocking version of
59.Fn flockfile ;
60if the lock cannot be acquired immediately,
61.Fn ftrylockfile
62returns non-zero instead of blocking.
63.Pp
64The
65.Fn funlockfile
66function releases the lock on a stream acquired by an earlier call to
67.Fn flockfile
68or
69.Fn ftrylockfile .
70.Pp
71These functions behave as if there is a lock count associated
72with each stream.
73Each time
74.Fn flockfile
75is called on the stream, the count is incremented,
76and each time
77.Fn funlockfile
78is called on the stream, the count is decremented.
79The lock is only actually released when the count reaches zero.
80.Sh RETURN VALUES
81The
82.Fn flockfile
83and
84.Fn funlockfile
85functions return no value.
86.Pp
87The
88.Fn ftrylockfile
89function
90returns zero if the stream was successfully locked,
91non-zero otherwise.
92.Sh SEE ALSO
93.Xr getc_unlocked 3 ,
94.Xr putc_unlocked 3
95.Sh STANDARDS
96The
97.Fn flockfile ,
98.Fn ftrylockfile
99and
100.Fn funlockfile
101functions conform to
102.St -p1003.1-2001 .
103