1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
|
.\" $OpenBSD: flockfile.3,v 1.2 1999/03/10 10:04:41 d Exp $
.Dd August 20, 1998
.Os
.Dt FLOCKFILE 3
.Sh NAME
.Nm flockfile ,
.Nm ftrylockfile ,
.Nm funlockfile
.Nd application level locking of stdio files
.Sh SYNOPSIS
.Fd #include <stdio.h>
.Ft void
.Fn flockfile "FILE *file"
.Ft int
.Fn ftrylockfile "FILE *file"
.Ft void
.Fn funlockfile "FILE *file"
.Sh DESCRIPTION
The
.Fn flockfile ,
.Fn ftrylockfile ,
and
.Fn funlockfile
functions provide for explicit application-level locking of stdio
.Ft "FILE *"
objects. These functions can be used by a thread to delineate a sequence
of I/O statements that are to be executed as a unit.
.Pp
The
.Fn flockfile
function is used by a thread to acquire ownership of a
.Ft "FILE *"
object.
.Pp
The
.Fn ftrylockfile
function is used by a thread to acquire ownership of a
.Ft "FILE *"
object if the object is available;
.Fn ftrylockfile
is a non-blocking version of
.Fn flockfile .
.Pp
The
.Fn funlockfile
function is used to relinquish the ownership granted to the thread.
The behaviour is undefined if a thread other than the current owner calls the
.Fn funlockfile
function.
.Pp
Logically, there is a lock count associated with each
.Ft "FILE *"
object. This count is implicitly intialized to zero when the
.Ft "FILE *"
object is created. The
.Ft "FILE *"
object is unlocked when the count is zero.
When the count is positive, a single thread owns the
.Ft "FILE *"
object. When the
.Fn flockfile
function is called, if the count is zero or if the count is positive and
the caller owns the
.Ft "FILE *"
object, the count is incremented.
Otherwise, the calling thread is suspended, waiting for the count to
return to zero.
Each call to
.Fn funlockfile
decrements the count. This allows matching calls to
.Fn flockfile
(or sucessful calls to
.Fn ftrylockfile )
and
.Fn funlockfile
to be nested.
.Pp
Library functions that reference
.Ft "FILE *"
behave as if they use
.Fn flockfile
and
.Fn funlockfile
internally to obtain ownership of these
.Ft "FILE *"
objects.
.Sh RETURN VALUES
None for
.Fn flockfile
and
.Fn funlockfile .
The function
.Fn ftrylock
returns zero for success and non-zero to indicate that the lock cannot
be acquired.
.Sh ERRORS
None.
.Sh SEE ALSO
.Xr getc_unlocked 3 ,
.Xr getchar_unlocked 3 ,
.Xr putc_unlocked 3 ,
.Xr putchar_unlocked 3 ,
.Xr pthreads 3
.Sh STANDARDS
.Fn flockfile ,
.Fn ftrylockfile
and
.Fn funlockfile
conform to ISO/IEC 9945-1 ANSI/IEEE
.Pq Dq Tn POSIX
Std 1003.1c/D10.
.\" Std 1003.1 Second Edition 1996-07-12.
|