flockfile.c revision 1.7 1 /* $NetBSD: flockfile.c,v 1.7 2003/07/21 22:24:47 nathanw Exp $ */
2
3 /*-
4 * Copyright (c) 2002 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Nathan J. Williams.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by the NetBSD
21 * Foundation, Inc. and its contributors.
22 * 4. Neither the name of The NetBSD Foundation nor the names of its
23 * contributors may be used to endorse or promote products derived
24 * from this software without specific prior written permission.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 * POSSIBILITY OF SUCH DAMAGE.
37 */
38
39 #include <sys/cdefs.h>
40 #if defined(LIBC_SCCS) && !defined(lint)
41 __RCSID("$NetBSD: flockfile.c,v 1.7 2003/07/21 22:24:47 nathanw Exp $");
42 #endif /* LIBC_SCCS and not lint */
43
44 #include "namespace.h"
45
46 #include <assert.h>
47 #include <errno.h>
48 #include <stdio.h>
49 #include <string.h>
50 #include "reentrant.h"
51 #include "local.h"
52
53 #ifdef __weak_alias
54 __weak_alias(flockfile,_flockfile)
55 __weak_alias(ftrylockfile,_ftrylockfile)
56 __weak_alias(funlockfile,_funlockfile)
57 #endif
58
59 #ifdef _REENTRANT
60 /*
61 * XXX This code makes the assumption that a thr_t (pthread_t) is a
62 * XXX pointer.
63 */
64
65 extern int __isthreaded;
66
67 void
68 flockfile(FILE *fp)
69 {
70
71 __flockfile_internal(fp, 0);
72 }
73
74 int
75 ftrylockfile(FILE *fp)
76 {
77 int retval;
78
79 if (__isthreaded == 0)
80 return 0;
81
82 retval = 0;
83 mutex_lock(&_LOCK(fp));
84
85 if (_LOCKOWNER(fp) == thr_self()) {
86 _LOCKCOUNT(fp)++;
87 } else if (_LOCKOWNER(fp) == NULL) {
88 _LOCKOWNER(fp) = thr_self();
89 _LOCKCOUNT(fp) = 1;
90 } else
91 retval = -1;
92
93 mutex_unlock(&_LOCK(fp));
94
95 return retval;
96 }
97
98 void
99 funlockfile(FILE *fp)
100 {
101
102 __funlockfile_internal(fp, 0);
103 }
104
105 void
106 __flockfile_internal(FILE *fp, int internal)
107 {
108
109 if (__isthreaded == 0)
110 return;
111
112 mutex_lock(&_LOCK(fp));
113
114 if (_LOCKOWNER(fp) == thr_self()) {
115 _LOCKCOUNT(fp)++;
116 if (internal) {
117 if (_LOCKINTERNAL(fp) == 0)
118 /* stash cancellation state and disable */
119 thr_setcancelstate(PTHREAD_CANCEL_DISABLE,
120 &_LOCKCANCELSTATE(fp));
121 _LOCKINTERNAL(fp)++;
122 }
123 } else {
124 while (_LOCKOWNER(fp) != NULL)
125 cond_wait(&_LOCKCOND(fp), &_LOCK(fp));
126 _LOCKOWNER(fp) = thr_self();
127 _LOCKCOUNT(fp) = 1;
128 if (internal)
129 _LOCKINTERNAL(fp) = 1;
130 }
131
132 mutex_unlock(&_LOCK(fp));
133 }
134
135 void
136 __funlockfile_internal(FILE *fp, int internal)
137 {
138
139 if (__isthreaded == 0)
140 return;
141
142 mutex_lock(&_LOCK(fp));
143
144 if (internal) {
145 _LOCKINTERNAL(fp)--;
146 if (_LOCKINTERNAL(fp) == 0)
147 thr_setcancelstate(_LOCKCANCELSTATE(fp), NULL);
148 }
149
150 _LOCKCOUNT(fp)--;
151 if (_LOCKCOUNT(fp) == 0) {
152 _LOCKOWNER(fp) = NULL;
153 cond_signal(&_LOCKCOND(fp));
154 }
155
156 mutex_unlock(&_LOCK(fp));
157 }
158
159 #else /* _REENTRANT */
160
161 void
162 flockfile(FILE *fp)
163 {
164 /* LINTED deliberate lack of effect */
165 (void)fp;
166
167 return;
168 }
169
170 int
171 ftrylockfile(FILE *fp)
172 {
173 /* LINTED deliberate lack of effect */
174 (void)fp;
175
176 return (0);
177 }
178
179 void
180 funlockfile(FILE *fp)
181 {
182 /* LINTED deliberate lack of effect */
183 (void)fp;
184
185 return;
186 }
187
188 #endif /* _REENTRANT */
189