xref: /netbsd-src/lib/libc/stdio/flockfile.3 (revision d3d055e20b3c0d5f2c2d91268d8a219c86785dc5)
1.\"	$NetBSD: flockfile.3,v 1.6 2011/10/15 21:43:19 wiz Exp $
2.\"
3.\" Copyright (c) 2003 The NetBSD Foundation, Inc.
4.\" All rights reserved.
5.\"
6.\" This code is derived from software contributed to The NetBSD Foundation
7.\" by Klaus Klein.
8.\"
9.\" Redistribution and use in source and binary forms, with or without
10.\" modification, are permitted provided that the following conditions
11.\" are met:
12.\" 1. Redistributions of source code must retain the above copyright
13.\"    notice, this list of conditions and the following disclaimer.
14.\" 2. Redistributions in binary form must reproduce the above copyright
15.\"    notice, this list of conditions and the following disclaimer in the
16.\"    documentation and/or other materials provided with the distribution.
17.\"
18.\" THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
19.\" ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
20.\" TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
21.\" PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
22.\" BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23.\" CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24.\" SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25.\" INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26.\" CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27.\" ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28.\" POSSIBILITY OF SUCH DAMAGE.
29.\"
30.Dd October 15, 2011
31.Dt FLOCKFILE 3
32.Os
33.Sh NAME
34.Nm flockfile ,
35.Nm ftrylockfile ,
36.Nm funlockfile
37.Nd stdio stream locking functions
38.Sh LIBRARY
39.Lb libc
40.Sh SYNOPSIS
41.In stdio.h
42.Ft void
43.Fn flockfile "FILE *file"
44.Ft int
45.Fn ftrylockfile "FILE *file"
46.Ft void
47.Fn funlockfile "FILE *file"
48.Sh DESCRIPTION
49The
50.Fn flockfile ,
51.Fn ftrylockfile ,
52and
53.Fn funlockfile
54functions provide applications with explicit control of locking of
55stdio stream objects.
56They can be used by a thread to execute a sequence of I/O operations
57as a unit, without interference from another thread.
58.Pp
59Locks on stdio streams are recursive, and a lock count is maintained.
60stdio streams are created unlocked, with a lock count of zero.
61After successful acquisition of the lock, its count is incremented
62to one, indicating locked state of the stdio stream.
63Each subsequent relock operation performed by the owner thread
64increments the lock count by one, and each subsequent unlock
65operation performed by the owner thread decrements the lock count by one,
66allowing matching lock and unlock operations to be nested.
67After its lock count is decremented to zero, the stdio stream returns
68to unlocked state, and ownership of the stdio stream is relinquished.
69.Pp
70The
71.Fn flockfile
72function acquires the ownership of
73.Fa file
74for the calling thread.
75If
76.Fa file
77is already owned by another thread, the calling thread is suspended
78until the acquisition is possible (i.e.,
79.Fa file
80is relinquished again and the calling thread is scheduled to acquire it).
81.Pp
82The
83.Fn ftrylockfile
84function acquires the ownership of
85.Fa file
86for the calling thread only if
87.Fa file
88is available.
89.Pp
90The
91.Fn funlockfile
92function relinquishes the ownership of
93.Fa file
94previously granted to the calling thread.
95Only the current owner of
96.Fa file
97may
98.Fn funlockfile
99it.
100.Sh RETURN VALUES
101If successful, the
102.Fn ftrylockfile
103function returns 0.
104Otherwise, it returns non-zero to indicate that the lock cannot be acquired.
105.Sh SEE ALSO
106.Xr flock 2 ,
107.Xr getc_unlocked 3 ,
108.Xr getchar_unlocked 3 ,
109.Xr lockf 3 ,
110.Xr putc_unlocked 3 ,
111.Xr putchar_unlocked 3
112.Sh STANDARDS
113The
114.Fn flockfile ,
115.Fn ftrylockfile
116and
117.Fn funlockfile
118functions conform to
119.St -p1003.1-2001 .
120.Sh HISTORY
121The
122.Fn flockfile
123function first appeared in
124.Fx 2.0 .
125.Sh BUGS
126The design of these interfaces does not allow for addressing the
127problem of priority inversion.
128