~drizzle-trunk/drizzle/development

1 by brian
clean slate
1
/* Copyright (C) 2000 MySQL AB
2
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.
6
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.
11
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 */
15
16
/* Synchronization - readers / writer thread locks */
17
18
#include "mysys_priv.h"
28.1.35 by Monty Taylor
Removed all references to THREAD.
19
#if !defined(HAVE_PTHREAD_RWLOCK_RDLOCK) && !defined(HAVE_RWLOCK_INIT)
1 by brian
clean slate
20
#include <errno.h>
21
22
/*
23
  Source base from Sun Microsystems SPILT, simplified for MySQL use
24
  -- Joshua Chamas
25
  Some cleanup and additional code by Monty
26
*/
27
28
/*
29
*  Multithreaded Demo Source
30
*
31
*  Copyright (C) 1995 by Sun Microsystems, Inc.
32
*  All rights reserved.
33
*
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.
38
*
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.
42
*
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
45
*  enhancement.
46
*
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.
50
*
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
54
*  DAMAGES.
55
*
56
*  SunSoft, Inc.
57
*  2550 Garcia Avenue
58
*  Mountain View, California  94043
59
*/
60
61
int my_rwlock_init(rw_lock_t *rwp, void *arg __attribute__((unused)))
62
{
63
  pthread_condattr_t	cond_attr;
64
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);
70
71
  rwp->state	= 0;
72
  rwp->waiters	= 0;
73
74
  return(0);
75
}
76
77
78
int my_rwlock_destroy(rw_lock_t *rwp)
79
{
80
  pthread_mutex_destroy( &rwp->lock );
81
  pthread_cond_destroy( &rwp->readers );
82
  pthread_cond_destroy( &rwp->writers );
83
  return(0);
84
}
85
86
87
int my_rw_rdlock(rw_lock_t *rwp)
88
{
89
  pthread_mutex_lock(&rwp->lock);
90
91
  /* active or queued writers */
92
  while (( rwp->state < 0 ) || rwp->waiters)
93
    pthread_cond_wait( &rwp->readers, &rwp->lock);
94
95
  rwp->state++;
96
  pthread_mutex_unlock(&rwp->lock);
97
  return(0);
98
}
99
100
int my_rw_tryrdlock(rw_lock_t *rwp)
101
{
102
  int res;
103
  pthread_mutex_lock(&rwp->lock);
104
  if ((rwp->state < 0 ) || rwp->waiters)
105
    res= EBUSY;					/* Can't get lock */
106
  else
107
  {
108
    res=0;
109
    rwp->state++;
110
  }
111
  pthread_mutex_unlock(&rwp->lock);
112
  return(res);
113
}
114
115
116
int my_rw_wrlock(rw_lock_t *rwp)
117
{
118
  pthread_mutex_lock(&rwp->lock);
119
  rwp->waiters++;				/* another writer queued */
120
121
  while (rwp->state)
122
    pthread_cond_wait(&rwp->writers, &rwp->lock);
123
  rwp->state	= -1;
124
  rwp->waiters--;
125
  pthread_mutex_unlock(&rwp->lock);
126
  return(0);
127
}
128
129
130
int my_rw_trywrlock(rw_lock_t *rwp)
131
{
132
  int res;
133
  pthread_mutex_lock(&rwp->lock);
134
  if (rwp->state)
135
    res= EBUSY;					/* Can't get lock */    
136
  else
137
  {
138
    res=0;
139
    rwp->state	= -1;
140
  }
141
  pthread_mutex_unlock(&rwp->lock);
142
  return(res);
143
}
144
145
146
int my_rw_unlock(rw_lock_t *rwp)
147
{
148
  pthread_mutex_lock(&rwp->lock);
149
150
  if (rwp->state == -1)		/* writer releasing */
151
  {
152
    rwp->state= 0;		/* mark as available */
153
154
    if ( rwp->waiters )		/* writers queued */
155
      pthread_cond_signal( &rwp->writers );
156
    else
157
      pthread_cond_broadcast( &rwp->readers );
158
  }
159
  else
160
  {
161
    if ( --rwp->state == 0 )	/* no more readers */
162
      pthread_cond_signal( &rwp->writers );
163
  }
164
165
  pthread_mutex_unlock( &rwp->lock );
166
  return(0);
167
}
168
169
#endif