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