1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 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 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156
|
/*
* thread_rwlock.c
*
* $Id$
*
* Read-write locks implementation
*
* This file is part of the OpenLink Software Virtuoso Open-Source (VOS)
* project.
*
* Copyright (C) 1998-2012 OpenLink Software
*
* This project is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the
* Free Software Foundation; only version 2 of the License, dated June 1991.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* 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.,
* 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*
*
*/
#include "thread_int.h"
struct rwlock_s {
dk_mutex_t *mtx;
semaphore_t *read_sem;
semaphore_t *write_sem;
int state; /* 0 = idle >0 = # of readers -1 = writer */
int blocked_writers;
int blocked_readers;
};
rwlock_t *
rwlock_allocate (void)
{
NEW_VARZ (rwlock_t, l);
l->mtx = mutex_allocate ();
l->read_sem = semaphore_allocate (0);
l->write_sem = semaphore_allocate (0);
l->state = 0;
l->blocked_writers = 0;
l->blocked_readers = 0;
return l;
}
void
rwlock_free (rwlock_t *l)
{
mutex_free (l->mtx);
semaphore_free (l->read_sem);
semaphore_free (l->write_sem);
dk_free (l, sizeof (*l));
}
void
rwlock_rdlock (rwlock_t *l)
{
mutex_enter (l->mtx);
while (l->blocked_writers || l->state < 0)
{
++l->blocked_readers;
mutex_leave (l->mtx);
semaphore_enter (l->read_sem);
mutex_enter (l->mtx);
--l->blocked_readers;
}
++l->state;
mutex_leave (l->mtx);
}
int
rwlock_tryrdlock (rwlock_t *l)
{
mutex_enter (l->mtx);
if (l->blocked_writers || l->state < 0)
{
mutex_leave (l->mtx);
return 0;
}
++l->state;
mutex_leave (l->mtx);
return 1;
}
void
rwlock_wrlock (rwlock_t *l)
{
mutex_enter (l->mtx);
while (l->state)
{
++l->blocked_writers;
mutex_leave (l->mtx);
semaphore_enter (l->write_sem);
mutex_enter (l->mtx);
--l->blocked_writers;
}
l->state = -1;
mutex_leave (l->mtx);
}
int
rwlock_trywrlock (rwlock_t *l)
{
mutex_enter (l->mtx);
if (l->state)
{
mutex_leave (l->mtx);
return 0;
}
l->state = -1;
mutex_leave (l->mtx);
return 1;
}
void
rwlock_unlock (rwlock_t *l)
{
mutex_enter (l->mtx);
if (l->state > 0)
{
if (--l->state == 0 && l->blocked_writers)
semaphore_leave (l->write_sem);
}
else if (l->state < 0)
{
l->state = 0;
if (l->blocked_writers)
{
/*
* wake up a waiting writer
*/
semaphore_leave (l->write_sem);
}
else
{
/*
* wake up all the waiting readers
*/
int i;
for (i = 0; i < l->blocked_readers; i++)
semaphore_leave (l->read_sem);
}
}
mutex_leave (l->mtx);
}
|