xref: /openbsd-src/lib/libpthread/man/flockfile.3 (revision 86f9d4cdda0e0267dccd8755ac0035525579c4a7)
1*86f9d4cdStedu.\" $OpenBSD: flockfile.3,v 1.11 2013/06/05 03:44:50 tedu Exp $
2b4059cf6Sd.\" David Leonard <d@openbsd.org>, 1998. Public domain.
3*86f9d4cdStedu.Dd $Mdocdate: June 5 2013 $
4a278733dSd.Dt FLOCKFILE 3
57f3168a2Saaron.Os
6a278733dSd.Sh NAME
7a278733dSd.Nm flockfile ,
8a278733dSd.Nm ftrylockfile ,
9a278733dSd.Nm funlockfile
10a278733dSd.Nd application level locking of stdio files
11a278733dSd.Sh SYNOPSIS
12*86f9d4cdStedu.In stdio.h
13a278733dSd.Ft void
14a278733dSd.Fn flockfile "FILE *file"
15a278733dSd.Ft int
16a278733dSd.Fn ftrylockfile "FILE *file"
17a278733dSd.Ft void
18a278733dSd.Fn funlockfile "FILE *file"
19a278733dSd.Sh DESCRIPTION
20a278733dSdThe
21a278733dSd.Fn flockfile ,
22a278733dSd.Fn ftrylockfile ,
23a278733dSdand
24a278733dSd.Fn funlockfile
25a278733dSdfunctions provide for explicit application-level locking of stdio
26a278733dSd.Ft "FILE *"
27ee997aeaSmpechobjects.
28ee997aeaSmpechThese functions can be used by a thread to delineate a sequence
29a278733dSdof I/O statements that are to be executed as a unit.
30a278733dSd.Pp
31a278733dSdThe
32a278733dSd.Fn flockfile
33a278733dSdfunction is used by a thread to acquire ownership of a
34a278733dSd.Ft "FILE *"
35a278733dSdobject.
36a278733dSd.Pp
37a278733dSdThe
38a278733dSd.Fn ftrylockfile
39a278733dSdfunction is used by a thread to acquire ownership of a
40a278733dSd.Ft "FILE *"
41a278733dSdobject if the object is available;
42a278733dSd.Fn ftrylockfile
43a278733dSdis a non-blocking version of
44a278733dSd.Fn flockfile .
45a278733dSd.Pp
46a278733dSdThe
47a278733dSd.Fn funlockfile
48a278733dSdfunction is used to relinquish the ownership granted to the thread.
49a278733dSdThe behaviour is undefined if a thread other than the current owner calls the
50a278733dSd.Fn funlockfile
51a278733dSdfunction.
52a278733dSd.Pp
53a278733dSdLogically, there is a lock count associated with each
54a278733dSd.Ft "FILE *"
55ee997aeaSmpechobject.
56f9dae475SjmcThis count is implicitly initialized to zero when the
57a278733dSd.Ft "FILE *"
58ee997aeaSmpechobject is created.
59ee997aeaSmpechThe
60a278733dSd.Ft "FILE *"
61a278733dSdobject is unlocked when the count is zero.
62a278733dSdWhen the count is positive, a single thread owns the
63a278733dSd.Ft "FILE *"
64ee997aeaSmpechobject.
65ee997aeaSmpechWhen the
66a278733dSd.Fn flockfile
67a278733dSdfunction is called, if the count is zero or if the count is positive and
68a278733dSdthe caller owns the
69a278733dSd.Ft "FILE *"
70a278733dSdobject, the count is incremented.
71a278733dSdOtherwise, the calling thread is suspended, waiting for the count to
72a278733dSdreturn to zero.
73a278733dSdEach call to
74a278733dSd.Fn funlockfile
75ee997aeaSmpechdecrements the count.
76ee997aeaSmpechThis allows matching calls to
77a278733dSd.Fn flockfile
7851515d46Saaron(or successful calls to
79a278733dSd.Fn ftrylockfile )
80a278733dSdand
81a278733dSd.Fn funlockfile
82a278733dSdto be nested.
83a278733dSd.Pp
84a278733dSdLibrary functions that reference
85a278733dSd.Ft "FILE *"
86a278733dSdbehave as if they use
87a278733dSd.Fn flockfile
88a278733dSdand
89a278733dSd.Fn funlockfile
90a278733dSdinternally to obtain ownership of these
91a278733dSd.Ft "FILE *"
92a278733dSdobjects.
93a278733dSd.Sh RETURN VALUES
94a278733dSdNone for
95a278733dSd.Fn flockfile
96a278733dSdand
97a278733dSd.Fn funlockfile .
98a278733dSdThe function
9943fcef1aSjmc.Fn ftrylockfile
100a278733dSdreturns zero for success and non-zero to indicate that the lock cannot
101a278733dSdbe acquired.
102a278733dSd.Sh ERRORS
103a278733dSdNone.
104a278733dSd.Sh SEE ALSO
105a278733dSd.Xr getc_unlocked 3 ,
106a278733dSd.Xr getchar_unlocked 3 ,
107350bb3b8Salex.Xr pthreads 3 ,
108a278733dSd.Xr putc_unlocked 3 ,
109350bb3b8Salex.Xr putchar_unlocked 3
110a278733dSd.Sh STANDARDS
111a278733dSd.Fn flockfile ,
112a278733dSd.Fn ftrylockfile
113a278733dSdand
114a278733dSd.Fn funlockfile
115a278733dSdconform to ISO/IEC 9945-1 ANSI/IEEE
116a278733dSd.Pq Dq Tn POSIX
117a278733dSdStd 1003.1c/D10.
118a278733dSd.\" Std 1003.1 Second Edition 1996-07-12.
119