Home | History | Annotate | Line # | Download | only in stdio
flockfile.c revision 1.6
      1 /*	$NetBSD: flockfile.c,v 1.6 2003/07/19 05:06:16 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.6 2003/07/19 05:06:16 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 	}
    129 
    130 	mutex_unlock(&_LOCK(fp));
    131 }
    132 
    133 void
    134 __funlockfile_internal(FILE *fp, int internal)
    135 {
    136 
    137 	if (__isthreaded == 0)
    138 		return;
    139 
    140 	mutex_lock(&_LOCK(fp));
    141 
    142 	if (internal) {
    143 		_LOCKINTERNAL(fp)--;
    144 		if (_LOCKINTERNAL(fp) == 0)
    145 			thr_setcancelstate(_LOCKCANCELSTATE(fp), NULL);
    146 	}
    147 
    148 	_LOCKCOUNT(fp)--;
    149 	if (_LOCKCOUNT(fp) == 0) {
    150 		_LOCKOWNER(fp) = NULL;
    151 		cond_signal(&_LOCKCOND(fp));
    152 	}
    153 
    154 	mutex_unlock(&_LOCK(fp));
    155 }
    156 
    157 #else /* _REENTRANT */
    158 
    159 void
    160 flockfile(FILE *fp)
    161 {
    162 	/* LINTED deliberate lack of effect */
    163 	(void)fp;
    164 
    165 	return;
    166 }
    167 
    168 int
    169 ftrylockfile(FILE *fp)
    170 {
    171 	/* LINTED deliberate lack of effect */
    172 	(void)fp;
    173 
    174 	return (0);
    175 }
    176 
    177 void
    178 funlockfile(FILE *fp)
    179 {
    180 	/* LINTED deliberate lack of effect */
    181 	(void)fp;
    182 
    183 	return;
    184 }
    185 
    186 #endif /* _REENTRANT */
    187