8322 nl: misleading-indentation
[unleashed/tickless.git] / usr / src / man / man3c / flockfile.3c
bloba447d503a364df15c5cc9dcc7662bb4fe0989eed
1 '\" te
2 .\" Copyright (c) 2003 Sun Microsystems, Inc.  All Rights Reserved.
3 .\" Copyright 1989 AT&T
4 .\" Sun Microsystems, Inc. gratefully acknowledges The Open Group for permission to reproduce portions of its copyrighted documentation. Original documentation from The Open Group can be obtained online at
5 .\" http://www.opengroup.org/bookstore/.
6 .\" The Institute of Electrical and Electronics Engineers and The Open Group, have given us permission to reprint portions of their documentation. In the following statement, the phrase "this text" refers to portions of the system documentation. Portions of this text are reprinted and reproduced in electronic form in the Sun OS Reference Manual, from IEEE Std 1003.1, 2004 Edition, Standard for Information Technology -- Portable Operating System Interface (POSIX), The Open Group Base Specifications Issue 6, Copyright (C) 2001-2004 by the Institute of Electrical and Electronics Engineers, Inc and The Open Group. In the event of any discrepancy between these versions and the original IEEE and The Open Group Standard, the original IEEE and The Open Group Standard is the referee document. The original Standard can be obtained online at http://www.opengroup.org/unix/online.html.
7 .\"  This notice shall appear on any product containing this material.
8 .\" The contents of this file are subject to the terms of the Common Development and Distribution License (the "License").  You may not use this file except in compliance with the License.
9 .\" You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE or http://www.opensolaris.org/os/licensing.  See the License for the specific language governing permissions and limitations under the License.
10 .\" When distributing Covered Code, include this CDDL HEADER in each file and include the License file at usr/src/OPENSOLARIS.LICENSE.  If applicable, add the following below this CDDL HEADER, with the fields enclosed by brackets "[]" replaced with your own identifying information: Portions Copyright [yyyy] [name of copyright owner]
11 .TH FLOCKFILE 3C "Sep 10, 2003"
12 .SH NAME
13 flockfile, funlockfile, ftrylockfile \- acquire and release stream lock
14 .SH SYNOPSIS
15 .LP
16 .nf
17 #include <stdio.h>
19 \fBvoid\fR \fBflockfile\fR(\fBFILE *\fR\fIstream\fR);
20 .fi
22 .LP
23 .nf
24 \fBvoid\fR \fBfunlockfile\fR(\fBFILE *\fR\fIstream\fR);
25 .fi
27 .LP
28 .nf
29 \fBint\fR \fBftrylockfile\fR(\fBFILE *\fR\fIstream\fR);
30 .fi
32 .SH DESCRIPTION
33 .sp
34 .LP
35 The \fBflockfile()\fR function acquires an internal lock of a stream
36 \fIstream\fR. If the lock is already acquired by another thread, the thread
37 calling \fBflockfile()\fR is suspended until it can acquire the lock.  In the
38 case that the stream lock is available, \fBflockfile()\fR not only acquires the
39 lock, but keeps track of the number of times it is being called by the current
40 thread.  This implies that the stream lock can be acquired more than once by
41 the same thread.
42 .sp
43 .LP
44 The \fBfunlockfile()\fR function releases the lock being held by the current
45 thread.  In the case of recursive locking, this function must be called the
46 same number of times \fBflockfile()\fR was called.  After the number of
47 \fBfunlockfile()\fR calls is equal to the number of \fBflockfile()\fR calls,
48 the stream lock is available for other threads to acquire.
49 .sp
50 .LP
51 The \fBftrylockfile()\fR function acquires an internal lock of a stream
52 \fIstream\fR, only if that object is available.  In essence
53 \fBftrylockfile()\fR is a non-blocking version of \fBflockfile()\fR.
54 .SH RETURN VALUES
55 .sp
56 .LP
57 The \fBftrylockfile()\fR function returns \fB0\fR on success and non-zero to
58 indicate a lock cannot be acquired.
59 .SH EXAMPLES
60 .LP
61 \fBExample 1 \fRA sample program of \fBflockfile()\fR.
62 .sp
63 .LP
64 The following example prints everything out together, blocking other threads
65 that might want to write to the same file between calls to \fBfprintf\fR(3C):
67 .sp
68 .in +2
69 .nf
70 FILE iop;
71 flockfile(iop);
72 fprintf(iop, "hello ");
73 fprintf(iop, "world);
74 fputc(iop, 'a');
75 funlockfile(iop);
76 .fi
77 .in -2
79 .sp
80 .LP
81 An unlocked interface is available in case performance is an issue.  For
82 example:
84 .sp
85 .in +2
86 .nf
87 flockfile(iop);
88 while (!feof(iop)) {
89         *c++ = getc_unlocked(iop);
91 funlockfile(iop);
92 .fi
93 .in -2
95 .SH ATTRIBUTES
96 .sp
97 .LP
98 See \fBattributes\fR(5) for descriptions of the following attributes:
99 .sp
103 box;
104 c | c
105 l | l .
106 ATTRIBUTE TYPE  ATTRIBUTE VALUE
108 Interface Stability     Standard
110 MT-Level        MT-Safe
113 .SH SEE ALSO
116 \fBIntro\fR(3), \fB__fsetlocking\fR(3C), \fBferror\fR(3C), \fBfprintf\fR(3C),
117 \fBgetc\fR(3C), \fBputc\fR(3C), \fBstdio\fR(3C), \fBungetc\fR(3C),
118 \fBattributes\fR(5), \fBstandards\fR(5)
119 .SH NOTES
122 The interfaces on this page  are as specified in IEEE Std 1003.1:2001.  See
123 \fBstandards\fR(5).