_flock_stub.c revision 72937
1/*
2 * Copyright (c) 1998 John Birrell <jb@cimlogic.com.au>.
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 *    notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 *    notice, this list of conditions and the following disclaimer in the
12 *    documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 *    must display the following acknowledgement:
15 *	This product includes software developed by John Birrell.
16 * 4. Neither the name of the author nor the names of any co-contributors
17 *    may be used to endorse or promote products derived from this software
18 *    without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY JOHN BIRRELL AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * SUCH DAMAGE.
31 *
32 * $FreeBSD: head/lib/libc/stdio/_flock_stub.c 72937 2001-02-23 04:59:12Z green $
33 *
34 */
35
36/*
37 * POSIX stdio FILE locking functions. These assume that the locking
38 * is only required at FILE structure level, not at file descriptor
39 * level too.
40 *
41 */
42
43#include "namespace.h"
44#include <stdio.h>
45#include <stdlib.h>
46#include <string.h>
47#include <pthread.h>
48#include "un-namespace.h"
49
50/*
51 * Weak symbols for externally visible functions in this file:
52 */
53#pragma weak	flockfile=_flockfile
54#pragma weak	_flockfile_debug=_flockfile_debug_stub
55#pragma weak	ftrylockfile=_ftrylockfile
56#pragma weak	funlockfile=_funlockfile
57
58static int	init_lock(FILE *);
59
60/*
61 * The FILE lock structure. The FILE *fp is locked when the mutex
62 * is locked.
63 */
64struct	__file_lock {
65	pthread_mutex_t	fl_mutex;
66	pthread_t	fl_owner;	/* current owner */
67	int		fl_count;	/* recursive lock count */
68};
69
70/*
71 * We need to retain binary compatibility for a while.  So pretend
72 * that _lock is part of FILE * even though it is dereferenced off
73 * _extra now.  When we stop encoding the size of FILE into binaries
74 * this can be changed in stdio.h.  This will reduce the amount of
75 * code that has to change in the future (just remove this comment
76 * and #define).
77 */
78#define _lock _extra->_mtlock
79
80/*
81 * Allocate and initialize a file lock.
82 */
83static int
84init_lock(FILE *fp)
85{
86	static pthread_mutex_t init_lock_mutex = PTHREAD_MUTEX_INITIALIZER;
87	struct __file_lock *p;
88	int	ret;
89
90	if ((p = malloc(sizeof(struct __file_lock))) == NULL)
91		ret = -1;
92	else {
93		p->fl_mutex = PTHREAD_MUTEX_INITIALIZER;
94		p->fl_owner = NULL;
95		p->fl_count = 0;
96		if (pthread_mutex_lock(&init_lock_mutex) != 0) {
97			free(p);
98			return (-1);
99		}
100		if (fp->_lock != NULL) {	/* lost the race */
101			free(p);
102			return (0);
103		}
104		fp->_lock = p;
105		pthread_mutex_unlock(&init_lock_mutex);
106		ret = 0;
107	}
108	return (ret);
109}
110
111void
112_flockfile(FILE *fp)
113{
114	pthread_t curthread = _pthread_self();
115
116	/*
117	 * Check if this is a real file with a valid lock, creating
118	 * the lock if needed:
119	 */
120	if ((fp->_file >= 0) &&
121	    ((fp->_lock != NULL) || (init_lock(fp) == 0))) {
122		if (fp->_lock->fl_owner == curthread)
123			fp->_lock->fl_count++;
124		else {
125			/*
126			 * Make sure this mutex is treated as a private
127			 * internal mutex:
128			 */
129			_pthread_mutex_lock(&fp->_lock->fl_mutex);
130			fp->_lock->fl_owner = curthread;
131			fp->_lock->fl_count = 1;
132		}
133	}
134}
135
136/*
137 * This can be overriden by the threads library if it is linked in.
138 */
139void
140_flockfile_debug_stub(FILE *fp, char *fname, int lineno)
141{
142	_flockfile(fp);
143}
144
145int
146_ftrylockfile(FILE *fp)
147{
148	pthread_t curthread = _pthread_self();
149	int	ret = 0;
150
151	/*
152	 * Check if this is a real file with a valid lock, creating
153	 * the lock if needed:
154	 */
155	if (((fp->_lock != NULL) || (init_lock(fp) == 0))) {
156		if (fp->_lock->fl_owner == curthread)
157			fp->_lock->fl_count++;
158		/*
159		 * Make sure this mutex is treated as a private
160		 * internal mutex:
161		 */
162		else if (_pthread_mutex_trylock(&fp->_lock->fl_mutex) == 0) {
163			fp->_lock->fl_owner = curthread;
164			fp->_lock->fl_count = 1;
165		}
166		else
167			ret = -1;
168	}
169	else
170		ret = -1;
171	return (ret);
172}
173
174void
175_funlockfile(FILE *fp)
176{
177	pthread_t	curthread = _pthread_self();
178
179	/*
180	 * Check if this is a real file with a valid lock owned
181	 * by the current thread:
182	 */
183	if ((fp->_lock != NULL) &&
184	    (fp->_lock->fl_owner == curthread)) {
185		/*
186		 * Check if this thread has locked the FILE
187		 * more than once:
188		 */
189		if (fp->_lock->fl_count > 1)
190			/*
191			 * Decrement the count of the number of
192			 * times the running thread has locked this
193			 * file:
194			 */
195			fp->_lock->fl_count--;
196		else {
197			/*
198			 * The running thread will release the
199			 * lock now:
200			 */
201			fp->_lock->fl_count = 0;
202			fp->_lock->fl_owner = NULL;
203			_pthread_mutex_unlock(&fp->_lock->fl_mutex);
204		}
205	}
206}
207