1 /* $NetBSD: flockfile.c,v 1.8 2003/07/22 00:56:25 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 #include <LibConfig.h>
39 #include <sys/EfiCdefs.h>
40 #if defined(LIBC_SCCS) && !defined(lint)
41 __RCSID("$NetBSD: flockfile.c,v 1.8 2003/07/22 00:56:25 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
flockfile(FILE * fp)68 flockfile(FILE *fp)
69 {
70
71 __flockfile_internal(fp, 0);
72 }
73
74 int
ftrylockfile(FILE * fp)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
funlockfile(FILE * fp)99 funlockfile(FILE *fp)
100 {
101
102 __funlockfile_internal(fp, 0);
103 }
104
105 void
__flockfile_internal(FILE * fp,int internal)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 _LOCKINTERNAL(fp)++;
118 } else {
119 /* danger! cond_wait() is a cancellation point. */
120 int oldstate;
121 thr_setcancelstate(PTHREAD_CANCEL_DISABLE, &oldstate);
122 while (_LOCKOWNER(fp) != NULL)
123 cond_wait(&_LOCKCOND(fp), &_LOCK(fp));
124 thr_setcancelstate(oldstate, NULL);
125 _LOCKOWNER(fp) = thr_self();
126 _LOCKCOUNT(fp) = 1;
127 if (internal)
128 _LOCKINTERNAL(fp) = 1;
129 }
130
131 if (_LOCKINTERNAL(fp) == 1)
132 /* stash cancellation state and disable */
133 thr_setcancelstate(PTHREAD_CANCEL_DISABLE,
134 &_LOCKCANCELSTATE(fp));
135
136 mutex_unlock(&_LOCK(fp));
137 }
138
139 void
__funlockfile_internal(FILE * fp,int internal)140 __funlockfile_internal(FILE *fp, int internal)
141 {
142
143 if (__isthreaded == 0)
144 return;
145
146 mutex_lock(&_LOCK(fp));
147
148 if (internal) {
149 _LOCKINTERNAL(fp)--;
150 if (_LOCKINTERNAL(fp) == 0)
151 thr_setcancelstate(_LOCKCANCELSTATE(fp), NULL);
152 }
153
154 _LOCKCOUNT(fp)--;
155 if (_LOCKCOUNT(fp) == 0) {
156 _LOCKOWNER(fp) = NULL;
157 cond_signal(&_LOCKCOND(fp));
158 }
159
160 mutex_unlock(&_LOCK(fp));
161 }
162
163 #else /* _REENTRANT */
164
165 void
166 flockfile(FILE *fp)
167 {
168 /* LINTED deliberate lack of effect */
169 (void)fp;
170
171 return;
172 }
173
174 int
175 ftrylockfile(FILE *fp)
176 {
177 /* LINTED deliberate lack of effect */
178 (void)fp;
179
180 return (0);
181 }
182
183 void
184 funlockfile(FILE *fp)
185 {
186 /* LINTED deliberate lack of effect */
187 (void)fp;
188
189 return;
190 }
191
192 #endif /* _REENTRANT */
193