1
/* Copyright (C) 2000 MySQL AB
3
This program is free software; you can redistribute it and/or modify
4
it under the terms of the GNU General Public License as published by
5
the Free Software Foundation; version 2 of the License.
7
This program is distributed in the hope that it will be useful,
8
but WITHOUT ANY WARRANTY; without even the implied warranty of
9
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10
GNU General Public License for more details.
12
You should have received a copy of the GNU General Public License
13
along with this program; if not, write to the Free Software
14
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
16
/* Synchronization - readers / writer thread locks */
18
#include "mysys_priv.h"
19
#if defined(THREAD) && !defined(HAVE_PTHREAD_RWLOCK_RDLOCK) && !defined(HAVE_RWLOCK_INIT)
23
Source base from Sun Microsystems SPILT, simplified for MySQL use
25
Some cleanup and additional code by Monty
29
* Multithreaded Demo Source
31
* Copyright (C) 1995 by Sun Microsystems, Inc.
32
* All rights reserved.
34
* This file is a product of SunSoft, Inc. and is provided for
35
* unrestricted use provided that this legend is included on all
36
* media and as a part of the software program in whole or part.
37
* Users may copy, modify or distribute this file at will.
39
* THIS FILE IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING
40
* THE WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
41
* PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
43
* This file is provided with no support and without any obligation on the
44
* part of SunSoft, Inc. to assist in its use, correction, modification or
47
* SUNSOFT AND SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT
48
* TO THE INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY THIS
49
* FILE OR ANY PART THEREOF.
51
* IN NO EVENT WILL SUNSOFT OR SUN MICROSYSTEMS, INC. BE LIABLE FOR ANY
52
* LOST REVENUE OR PROFITS OR OTHER SPECIAL, INDIRECT AND CONSEQUENTIAL
53
* DAMAGES, EVEN IF THEY HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH
58
* Mountain View, California 94043
61
int my_rwlock_init(rw_lock_t *rwp, void *arg __attribute__((unused)))
63
pthread_condattr_t cond_attr;
65
pthread_mutex_init( &rwp->lock, MY_MUTEX_INIT_FAST);
66
pthread_condattr_init( &cond_attr );
67
pthread_cond_init( &rwp->readers, &cond_attr );
68
pthread_cond_init( &rwp->writers, &cond_attr );
69
pthread_condattr_destroy(&cond_attr);
78
int my_rwlock_destroy(rw_lock_t *rwp)
80
pthread_mutex_destroy( &rwp->lock );
81
pthread_cond_destroy( &rwp->readers );
82
pthread_cond_destroy( &rwp->writers );
87
int my_rw_rdlock(rw_lock_t *rwp)
89
pthread_mutex_lock(&rwp->lock);
91
/* active or queued writers */
92
while (( rwp->state < 0 ) || rwp->waiters)
93
pthread_cond_wait( &rwp->readers, &rwp->lock);
96
pthread_mutex_unlock(&rwp->lock);
100
int my_rw_tryrdlock(rw_lock_t *rwp)
103
pthread_mutex_lock(&rwp->lock);
104
if ((rwp->state < 0 ) || rwp->waiters)
105
res= EBUSY; /* Can't get lock */
111
pthread_mutex_unlock(&rwp->lock);
116
int my_rw_wrlock(rw_lock_t *rwp)
118
pthread_mutex_lock(&rwp->lock);
119
rwp->waiters++; /* another writer queued */
122
pthread_cond_wait(&rwp->writers, &rwp->lock);
125
pthread_mutex_unlock(&rwp->lock);
130
int my_rw_trywrlock(rw_lock_t *rwp)
133
pthread_mutex_lock(&rwp->lock);
135
res= EBUSY; /* Can't get lock */
141
pthread_mutex_unlock(&rwp->lock);
146
int my_rw_unlock(rw_lock_t *rwp)
148
DBUG_PRINT("rw_unlock",
149
("state: %d waiters: %d", rwp->state, rwp->waiters));
150
pthread_mutex_lock(&rwp->lock);
152
if (rwp->state == -1) /* writer releasing */
154
rwp->state= 0; /* mark as available */
156
if ( rwp->waiters ) /* writers queued */
157
pthread_cond_signal( &rwp->writers );
159
pthread_cond_broadcast( &rwp->readers );
163
if ( --rwp->state == 0 ) /* no more readers */
164
pthread_cond_signal( &rwp->writers );
167
pthread_mutex_unlock( &rwp->lock );