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