_flock_stub.c (71579) | _flock_stub.c (72373) |
---|---|
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 --- 15 unchanged lines hidden (view full) --- 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 * | 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 --- 15 unchanged lines hidden (view full) --- 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 71579 2001-01-24 13:01:12Z deischen $ | 32 * $FreeBSD: head/lib/libc/stdio/_flock_stub.c 72373 2001-02-11 22:06:43Z deischen $ |
33 * 34 */ 35 | 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" |
|
36#include <stdio.h> | 44#include <stdio.h> |
45#include <stdlib.h> 46#include <string.h> 47#include <pthread.h> 48#include "un-namespace.h" |
|
37 38/* | 49 50/* |
39 * Declare weak references in case the application is not linked 40 * with libpthread. | 51 * Weak symbols for externally visible functions in this file: |
41 */ | 52 */ |
42#pragma weak flockfile=_flockfile_stub 43#pragma weak _flockfile=_flockfile_stub 44#pragma weak _flockfile_debug=_flockfile_debug_stub 45#pragma weak ftrylockfile=_ftrylockfile_stub 46#pragma weak _ftrylockfile=_ftrylockfile_stub 47#pragma weak funlockfile=_funlockfile_stub 48#pragma weak _funlockfile=_funlockfile_stub | 53#pragma weak flockfile=_flockfile 54#pragma weak _flockfile_debug=_flockfile_debug_stub 55#pragma weak ftrylockfile=_ftrylockfile 56#pragma weak funlockfile=_funlockfile |
49 | 57 |
58static int init_lock(FILE *); 59 |
|
50/* | 60/* |
51 * This function is a stub for the _flockfile function in libpthread. | 61 * The FILE lock structure. The FILE *fp is locked when the mutex 62 * is locked. |
52 */ | 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 * Allocate and initialize a file lock. 72 */ 73static int 74init_lock(FILE *fp) 75{ 76 struct __file_lock *p; 77 int ret; 78 79 if ((p = malloc(sizeof(struct __file_lock))) == NULL) 80 ret = -1; 81 else { 82 p->fl_mutex = PTHREAD_MUTEX_INITIALIZER; 83 p->fl_owner = NULL; 84 p->fl_count = 0; 85 fp->_lock = p; 86 ret = 0; 87 } 88 return (ret); 89} 90 |
|
53void | 91void |
54_flockfile_stub(FILE *fp) | 92_flockfile(FILE *fp) |
55{ | 93{ |
94 pthread_t curthread = _pthread_self(); 95 96 /* 97 * Check if this is a real file with a valid lock, creating 98 * the lock if needed: 99 */ 100 if ((fp->_file >= 0) && 101 ((fp->_lock != NULL) || (init_lock(fp) == 0))) { 102 if (fp->_lock->fl_owner == curthread) 103 fp->_lock->fl_count++; 104 else { 105 /* 106 * Make sure this mutex is treated as a private 107 * internal mutex: 108 */ 109 _pthread_mutex_lock(&fp->_lock->fl_mutex); 110 fp->_lock->fl_owner = curthread; 111 fp->_lock->fl_count = 1; 112 } 113 } |
|
56} 57 58/* | 114} 115 116/* |
59 * This function is a stub for the _flockfile_debug function in libpthread. | 117 * This can be overriden by the threads library if it is linked in. |
60 */ 61void 62_flockfile_debug_stub(FILE *fp, char *fname, int lineno) 63{ | 118 */ 119void 120_flockfile_debug_stub(FILE *fp, char *fname, int lineno) 121{ |
122 _flockfile(fp); |
|
64} 65 | 123} 124 |
66/* 67 * This function is a stub for the _ftrylockfile function in libpthread. 68 */ | |
69int | 125int |
70_ftrylockfile_stub(FILE *fp) | 126_ftrylockfile(FILE *fp) |
71{ | 127{ |
72 return(0); | 128 pthread_t curthread = _pthread_self(); 129 int ret = 0; 130 131 /* 132 * Check if this is a real file with a valid lock, creating 133 * the lock if needed: 134 */ 135 if ((fp->_file >= 0) && 136 ((fp->_lock != NULL) || (init_lock(fp) == 0))) { 137 if (fp->_lock->fl_owner == curthread) 138 fp->_lock->fl_count++; 139 /* 140 * Make sure this mutex is treated as a private 141 * internal mutex: 142 */ 143 else if (_pthread_mutex_trylock(&fp->_lock->fl_mutex) == 0) { 144 fp->_lock->fl_owner = curthread; 145 fp->_lock->fl_count = 1; 146 } 147 else 148 ret = -1; 149 } 150 else 151 ret = -1; 152 return (ret); |
73} 74 | 153} 154 |
75/* 76 * This function is a stub for the _funlockfile function in libpthread. 77 */ 78void 79_funlockfile_stub(FILE *fp) | 155void 156_funlockfile(FILE *fp) |
80{ | 157{ |
158 pthread_t curthread = _pthread_self(); 159 160 /* 161 * Check if this is a real file with a valid lock owned 162 * by the current thread: 163 */ 164 if ((fp->_file >= 0) && (fp->_lock != NULL) && 165 (fp->_lock->fl_owner == curthread)) { 166 /* 167 * Check if this thread has locked the FILE 168 * more than once: 169 */ 170 if (fp->_lock->fl_count > 1) 171 /* 172 * Decrement the count of the number of 173 * times the running thread has locked this 174 * file: 175 */ 176 fp->_lock->fl_count--; 177 else { 178 /* 179 * The running thread will release the 180 * lock now: 181 */ 182 fp->_lock->fl_count = 0; 183 fp->_lock->fl_owner = NULL; 184 _pthread_mutex_unlock(&fp->_lock->fl_mutex); 185 } 186 } |
|
81} | 187} |