thr_rwlock.c 11.5 KB
Newer Older
unknown's avatar
unknown committed
1 2 3 4
/* Copyright (C) 2000 MySQL AB

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
unknown's avatar
unknown committed
5
   the Free Software Foundation; version 2 of the License.
unknown's avatar
unknown committed
6 7

   This program is distributed in the hope that it will be useful,
unknown's avatar
unknown committed
8
   but WITHOUT ANY WARRANTY; without even the implied warranty of
unknown's avatar
unknown committed
9 10 11 12 13 14
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */
unknown's avatar
unknown committed
15 16 17 18

/* Synchronization - readers / writer thread locks */

#include "mysys_priv.h"
19 20
#if defined(THREAD)
#if defined(NEED_MY_RW_LOCK)
21
#include <errno.h>
unknown's avatar
unknown committed
22

23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135
#ifdef _WIN32

static BOOL have_srwlock= FALSE;
/* Prototypes and function pointers for windows  functions */
typedef VOID (WINAPI* srw_func) (PSRWLOCK SRWLock);
typedef BOOL (WINAPI* srw_bool_func) (PSRWLOCK SRWLock);

static srw_func my_InitializeSRWLock;
static srw_func my_AcquireSRWLockExclusive;
static srw_func my_ReleaseSRWLockExclusive;
static srw_func my_AcquireSRWLockShared;
static srw_func my_ReleaseSRWLockShared;

static srw_bool_func my_TryAcquireSRWLockExclusive;
static srw_bool_func my_TryAcquireSRWLockShared;

/**
  Check for presence of Windows slim reader writer lock function.
  Load function pointers.
*/

static void check_srwlock_availability(void)
{
  HMODULE module= GetModuleHandle("kernel32");

  my_InitializeSRWLock= (srw_func) GetProcAddress(module,
    "InitializeSRWLock");
  my_AcquireSRWLockExclusive= (srw_func) GetProcAddress(module,
    "AcquireSRWLockExclusive");
  my_AcquireSRWLockShared= (srw_func) GetProcAddress(module,
    "AcquireSRWLockShared");
  my_ReleaseSRWLockExclusive= (srw_func) GetProcAddress(module,
    "ReleaseSRWLockExclusive");
  my_ReleaseSRWLockShared= (srw_func) GetProcAddress(module,
    "ReleaseSRWLockShared");
  my_TryAcquireSRWLockExclusive=  (srw_bool_func) GetProcAddress(module,
    "TryAcquireSRWLockExclusive");
  my_TryAcquireSRWLockShared=  (srw_bool_func) GetProcAddress(module,
    "TryAcquireSRWLockShared");

  /*
    We currently require TryAcquireSRWLockExclusive. This API is missing on 
    Vista, this means SRWLock are only used starting with Win7.

    If "trylock" usage for rwlocks is eliminated from server codebase (it is used 
    in a single place currently, in query cache), then SRWLock can be enabled on 
    Vista too. In this case  condition below needs to be changed to  e.g check 
    for my_InitializeSRWLock.
  */

  if (my_TryAcquireSRWLockExclusive)
    have_srwlock= TRUE;

}


static int srw_init(my_rw_lock_t *rwp)
{
  my_InitializeSRWLock(&rwp->srwlock);
  rwp->have_exclusive_srwlock = FALSE;
  return 0;
}


static int srw_rdlock(my_rw_lock_t *rwp)
{
  my_AcquireSRWLockShared(&rwp->srwlock);
  return 0;
}


static int srw_tryrdlock(my_rw_lock_t *rwp)
{

  if (!my_TryAcquireSRWLockShared(&rwp->srwlock))
    return EBUSY;
  return 0;
}


static int srw_wrlock(my_rw_lock_t *rwp)
{
  my_AcquireSRWLockExclusive(&rwp->srwlock);
  rwp->have_exclusive_srwlock= TRUE;
  return 0;
}


static int srw_trywrlock(my_rw_lock_t *rwp)
{
  if (!my_TryAcquireSRWLockExclusive(&rwp->srwlock))
    return EBUSY;
  rwp->have_exclusive_srwlock= TRUE;
  return 0;
}


static int srw_unlock(my_rw_lock_t *rwp)
{
  if (rwp->have_exclusive_srwlock)
  {
    rwp->have_exclusive_srwlock= FALSE;
    my_ReleaseSRWLockExclusive(&rwp->srwlock);
  }
  else
  {
    my_ReleaseSRWLockShared(&rwp->srwlock);
  }
  return 0;
}

#endif /*_WIN32 */

unknown's avatar
unknown committed
136
/*
137 138 139 140
  Source base from Sun Microsystems SPILT, simplified for MySQL use
  -- Joshua Chamas
  Some cleanup and additional code by Monty
*/
unknown's avatar
unknown committed
141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174

/*
*  Multithreaded Demo Source
*
*  Copyright (C) 1995 by Sun Microsystems, Inc.
*  All rights reserved.
*
*  This file is a product of SunSoft, Inc. and is provided for
*  unrestricted use provided that this legend is included on all
*  media and as a part of the software program in whole or part.
*  Users may copy, modify or distribute this file at will.
*
*  THIS FILE IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING
*  THE WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
*  PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
*
*  This file is provided with no support and without any obligation on the
*  part of SunSoft, Inc. to assist in its use, correction, modification or
*  enhancement.
*
*  SUNSOFT AND SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT
*  TO THE INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY THIS
*  FILE OR ANY PART THEREOF.
*
*  IN NO EVENT WILL SUNSOFT OR SUN MICROSYSTEMS, INC. BE LIABLE FOR ANY
*  LOST REVENUE OR PROFITS OR OTHER SPECIAL, INDIRECT AND CONSEQUENTIAL
*  DAMAGES, EVEN IF THEY HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH
*  DAMAGES.
*
*  SunSoft, Inc.
*  2550 Garcia Avenue
*  Mountain View, California  94043
*/

175
int my_rw_init(my_rw_lock_t *rwp)
unknown's avatar
unknown committed
176 177 178
{
  pthread_condattr_t	cond_attr;

179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194
#ifdef _WIN32
  /*
    Once initialization is used here rather than in my_init(), in order to
    - avoid  my_init() pitfalls- (undefined order in which initialization should
    run)
    - be potentially useful C++ (static constructors) 
    - just to simplify  the API. 
    Also, the overhead is of my_pthread_once is very small.
  */
  static my_pthread_once_t once_control= MY_PTHREAD_ONCE_INIT;
  my_pthread_once(&once_control, check_srwlock_availability);

  if (have_srwlock)
    return srw_init(rwp);
#endif

195
  pthread_mutex_init( &rwp->lock, MY_MUTEX_INIT_FAST);
unknown's avatar
unknown committed
196 197 198 199 200 201 202
  pthread_condattr_init( &cond_attr );
  pthread_cond_init( &rwp->readers, &cond_attr );
  pthread_cond_init( &rwp->writers, &cond_attr );
  pthread_condattr_destroy(&cond_attr);

  rwp->state	= 0;
  rwp->waiters	= 0;
203 204 205
#ifdef SAFE_MUTEX
  rwp->write_thread   = 0;
#endif
unknown's avatar
unknown committed
206

207
  return(0);
unknown's avatar
unknown committed
208 209
}

210

211
int my_rw_destroy(my_rw_lock_t *rwp)
212
{
213 214 215 216
#ifdef _WIN32
  if (have_srwlock)
    return 0; /* no destroy function */
#endif
217
  DBUG_ASSERT(rwp->state == 0);
unknown's avatar
unknown committed
218 219 220
  pthread_mutex_destroy( &rwp->lock );
  pthread_cond_destroy( &rwp->readers );
  pthread_cond_destroy( &rwp->writers );
221
  return(0);
unknown's avatar
unknown committed
222 223
}

224

225
int my_rw_rdlock(my_rw_lock_t *rwp)
226
{
227 228 229 230 231
#ifdef _WIN32
  if (have_srwlock)
    return srw_rdlock(rwp);
#endif

unknown's avatar
unknown committed
232 233
  pthread_mutex_lock(&rwp->lock);

234
  /* active or queued writers */
235
  while (( rwp->state < 0 ) || rwp->waiters)
unknown's avatar
unknown committed
236 237 238 239
    pthread_cond_wait( &rwp->readers, &rwp->lock);

  rwp->state++;
  pthread_mutex_unlock(&rwp->lock);
240 241
  return(0);
}
unknown's avatar
unknown committed
242

243
int my_rw_tryrdlock(my_rw_lock_t *rwp)
244 245
{
  int res;
246 247 248 249 250 251

#ifdef _WIN32
  if (have_srwlock)
    return srw_tryrdlock(rwp);
#endif

252
  pthread_mutex_lock(&rwp->lock);
253
  if ((rwp->state < 0 ) || rwp->waiters)
254 255 256 257 258 259 260 261
    res= EBUSY;					/* Can't get lock */
  else
  {
    res=0;
    rwp->state++;
  }
  pthread_mutex_unlock(&rwp->lock);
  return(res);
unknown's avatar
unknown committed
262 263
}

264

265
int my_rw_wrlock(my_rw_lock_t *rwp)
266
{
267 268 269 270 271
#ifdef _WIN32
  if (have_srwlock)
    return srw_wrlock(rwp);
#endif

unknown's avatar
unknown committed
272
  pthread_mutex_lock(&rwp->lock);
273
  rwp->waiters++;				/* another writer queued */
unknown's avatar
unknown committed
274

275
  my_rw_lock_assert_not_write_owner(rwp);
276

277 278
  while (rwp->state)
    pthread_cond_wait(&rwp->writers, &rwp->lock);
unknown's avatar
unknown committed
279
  rwp->state	= -1;
280
  rwp->waiters--;
281 282 283
#ifdef SAFE_MUTEX
  rwp->write_thread= pthread_self();
#endif
284
  pthread_mutex_unlock(&rwp->lock);
285
  return(0);
unknown's avatar
unknown committed
286 287
}

288

289
int my_rw_trywrlock(my_rw_lock_t *rwp)
290 291
{
  int res;
292 293 294 295 296 297

#ifdef _WIN32
  if (have_srwlock)
    return srw_trywrlock(rwp);
#endif

298 299 300 301 302 303 304
  pthread_mutex_lock(&rwp->lock);
  if (rwp->state)
    res= EBUSY;					/* Can't get lock */    
  else
  {
    res=0;
    rwp->state	= -1;
305 306 307
#ifdef SAFE_MUTEX
  rwp->write_thread= pthread_self();
#endif
308 309 310 311 312 313
  }
  pthread_mutex_unlock(&rwp->lock);
  return(res);
}


314
int my_rw_unlock(my_rw_lock_t *rwp)
315
{
316 317 318 319 320
#ifdef _WIN32
  if (have_srwlock)
    return srw_unlock(rwp);
#endif

unknown's avatar
unknown committed
321 322 323 324
  DBUG_PRINT("rw_unlock",
	     ("state: %d waiters: %d", rwp->state, rwp->waiters));
  pthread_mutex_lock(&rwp->lock);

325 326
  DBUG_ASSERT(rwp->state != 0);

327 328
  if (rwp->state == -1)		/* writer releasing */
  {
329
    my_rw_lock_assert_write_owner(rwp);
330
    rwp->state= 0;		/* mark as available */
331 332 333
#ifdef SAFE_MUTEX
    rwp->write_thread= 0;
#endif
unknown's avatar
unknown committed
334

335
    if ( rwp->waiters )		/* writers queued */
unknown's avatar
unknown committed
336 337 338
      pthread_cond_signal( &rwp->writers );
    else
      pthread_cond_broadcast( &rwp->readers );
339 340 341
  }
  else
  {
342 343
    if ( --rwp->state == 0 &&   /* no more readers */
        rwp->waiters)
unknown's avatar
unknown committed
344 345 346 347
      pthread_cond_signal( &rwp->writers );
  }

  pthread_mutex_unlock( &rwp->lock );
348
  return(0);
unknown's avatar
unknown committed
349 350
}

351 352
#endif /* defined(NEED_MY_RW_LOCK) */

353

354
int rw_pr_init(rw_pr_lock_t *rwlock)
355
{
356 357 358 359 360 361 362 363 364
  pthread_mutex_init(&rwlock->lock, NULL);
  pthread_cond_init(&rwlock->no_active_readers, NULL);
  rwlock->active_readers= 0;
  rwlock->writers_waiting_readers= 0;
  rwlock->active_writer= FALSE;
#ifdef SAFE_MUTEX
  rwlock->writer_thread= 0;
#endif
  return 0;
365 366 367
}


368 369 370 371 372 373
int rw_pr_destroy(rw_pr_lock_t *rwlock)
{
  pthread_cond_destroy(&rwlock->no_active_readers);
  pthread_mutex_destroy(&rwlock->lock);
  return 0;
}
374

375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391

int rw_pr_rdlock(rw_pr_lock_t *rwlock)
{
  pthread_mutex_lock(&rwlock->lock);
  /*
    The fact that we were able to acquire 'lock' mutex means
    that there are no active writers and we can acquire rd-lock.
    Increment active readers counter to prevent requests for
    wr-lock from succeeding and unlock mutex.
  */
  rwlock->active_readers++;
  pthread_mutex_unlock(&rwlock->lock);
  return 0;
}


int rw_pr_wrlock(rw_pr_lock_t *rwlock)
392
{
393 394 395 396 397 398
  pthread_mutex_lock(&rwlock->lock);

  if (rwlock->active_readers != 0)
  {
    /* There are active readers. We have to wait until they are gone. */
    rwlock->writers_waiting_readers++;
399

400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422
    while (rwlock->active_readers != 0)
      pthread_cond_wait(&rwlock->no_active_readers, &rwlock->lock);

    rwlock->writers_waiting_readers--;
  }

  /*
    We own 'lock' mutex so there is no active writers.
    Also there are no active readers.
    This means that we can grant wr-lock.
    Not releasing 'lock' mutex until unlock will block
    both requests for rd and wr-locks.
    Set 'active_writer' flag to simplify unlock.

    Thanks to the fact wr-lock/unlock in the absence of
    contention from readers is essentially mutex lock/unlock
    with a few simple checks make this rwlock implementation
    wr-lock optimized.
  */
  rwlock->active_writer= TRUE;
#ifdef SAFE_MUTEX
  rwlock->writer_thread= pthread_self();
#endif
423 424 425
  return 0;
}

426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473

int rw_pr_unlock(rw_pr_lock_t *rwlock)
{
  if (rwlock->active_writer)
  {
    /* We are unlocking wr-lock. */
#ifdef SAFE_MUTEX
    rwlock->writer_thread= 0;
#endif
    rwlock->active_writer= FALSE;
    if (rwlock->writers_waiting_readers)
    {
      /*
        Avoid expensive cond signal in case when there is no contention
        or it is wr-only.

        Note that from view point of performance it would be better to
        signal on the condition variable after unlocking mutex (as it
        reduces number of contex switches).

        Unfortunately this would mean that such rwlock can't be safely
        used by MDL subsystem, which relies on the fact that it is OK
        to destroy rwlock once it is in unlocked state.
      */
      pthread_cond_signal(&rwlock->no_active_readers);
    }
    pthread_mutex_unlock(&rwlock->lock);
  }
  else
  {
    /* We are unlocking rd-lock. */
    pthread_mutex_lock(&rwlock->lock);
    rwlock->active_readers--;
    if (rwlock->active_readers == 0 &&
        rwlock->writers_waiting_readers)
    {
      /*
        If we are last reader and there are waiting
        writers wake them up.
      */
      pthread_cond_signal(&rwlock->no_active_readers);
    }
    pthread_mutex_unlock(&rwlock->lock);
  }
  return 0;
}


474
#endif /* defined(THREAD) */