]>
git.proxmox.com Git - mirror_edk2.git/blob - StdLib/LibC/Stdio/flockfile.c
1 /* $NetBSD: flockfile.c,v 1.8 2003/07/22 00:56:25 nathanw Exp $ */
4 * Copyright (c) 2002 The NetBSD Foundation, Inc.
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Nathan J. Williams.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
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.
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.
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 */
44 #include "namespace.h"
50 #include "reentrant.h"
54 __weak_alias(flockfile
,_flockfile
)
55 __weak_alias(ftrylockfile
,_ftrylockfile
)
56 __weak_alias(funlockfile
,_funlockfile
)
61 * XXX This code makes the assumption that a thr_t (pthread_t) is a
65 extern int __isthreaded
;
71 __flockfile_internal(fp
, 0);
75 ftrylockfile(FILE *fp
)
79 if (__isthreaded
== 0)
83 mutex_lock(&_LOCK(fp
));
85 if (_LOCKOWNER(fp
) == thr_self()) {
87 } else if (_LOCKOWNER(fp
) == NULL
) {
88 _LOCKOWNER(fp
) = thr_self();
93 mutex_unlock(&_LOCK(fp
));
102 __funlockfile_internal(fp
, 0);
106 __flockfile_internal(FILE *fp
, int internal
)
109 if (__isthreaded
== 0)
112 mutex_lock(&_LOCK(fp
));
114 if (_LOCKOWNER(fp
) == thr_self()) {
119 /* danger! cond_wait() is a cancellation point. */
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();
128 _LOCKINTERNAL(fp
) = 1;
131 if (_LOCKINTERNAL(fp
) == 1)
132 /* stash cancellation state and disable */
133 thr_setcancelstate(PTHREAD_CANCEL_DISABLE
,
134 &_LOCKCANCELSTATE(fp
));
136 mutex_unlock(&_LOCK(fp
));
140 __funlockfile_internal(FILE *fp
, int internal
)
143 if (__isthreaded
== 0)
146 mutex_lock(&_LOCK(fp
));
150 if (_LOCKINTERNAL(fp
) == 0)
151 thr_setcancelstate(_LOCKCANCELSTATE(fp
), NULL
);
155 if (_LOCKCOUNT(fp
) == 0) {
156 _LOCKOWNER(fp
) = NULL
;
157 cond_signal(&_LOCKCOND(fp
));
160 mutex_unlock(&_LOCK(fp
));
163 #else /* _REENTRANT */
168 /* LINTED deliberate lack of effect */
175 ftrylockfile(FILE *fp
)
177 /* LINTED deliberate lack of effect */
184 funlockfile(FILE *fp
)
186 /* LINTED deliberate lack of effect */
192 #endif /* _REENTRANT */