~stewart/drizzle/embedded-innodb-create-select-transaction-arrgh

« back to all changes in this revision

Viewing changes to mysys/thr_rwlock.c

  • Committer: brian
  • Date: 2008-06-25 05:29:13 UTC
  • Revision ID: brian@localhost.localdomain-20080625052913-6upwo0jsrl4lnapl
clean slate

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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"
 
19
#if defined(THREAD) && !defined(HAVE_PTHREAD_RWLOCK_RDLOCK) && !defined(HAVE_RWLOCK_INIT)
 
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
  DBUG_PRINT("rw_unlock",
 
149
             ("state: %d waiters: %d", rwp->state, rwp->waiters));
 
150
  pthread_mutex_lock(&rwp->lock);
 
151
 
 
152
  if (rwp->state == -1)         /* writer releasing */
 
153
  {
 
154
    rwp->state= 0;              /* mark as available */
 
155
 
 
156
    if ( rwp->waiters )         /* writers queued */
 
157
      pthread_cond_signal( &rwp->writers );
 
158
    else
 
159
      pthread_cond_broadcast( &rwp->readers );
 
160
  }
 
161
  else
 
162
  {
 
163
    if ( --rwp->state == 0 )    /* no more readers */
 
164
      pthread_cond_signal( &rwp->writers );
 
165
  }
 
166
 
 
167
  pthread_mutex_unlock( &rwp->lock );
 
168
  return(0);
 
169
}
 
170
 
 
171
#endif