Subversion Repositories shark

Rev

Rev 3 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
2 pj 1
 
1063 tullio 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; either version 2 of the License, or
6
 * (at your option) any later version.
7
 *
8
 * This program is distributed in the hope that it will be useful,
9
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11
 * GNU General Public License for more details.
12
 *
13
 * You should have received a copy of the GNU General Public License
14
 * along with this program; if not, write to the Free Software
15
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
16
 *
17
 */
18
 
2 pj 19
#include <fs/mutex.h>
20
#include <fs/semaph.h>
21
#include "mutex.h"
22
#include "rwlock.h"
23
 
24
void __rwlock_init(__rwlock_t *ptr)
25
{
26
  __mutex_init(&ptr->mutex,fsmutexattr);
27
  __sem_init(&ptr->readers_semaph,0);
28
  __sem_init(&ptr->writers_semaph,0);
29
  ptr->active_readers=0;
30
  ptr->active_writers=0;
31
  ptr->blocked_readers=0;
32
  ptr->blocked_writers=0;  
33
}
34
 
35
void __rwlock_rdlock(__rwlock_t *ptr)
36
{
37
  __mutex_lock(&ptr->mutex);
38
 
39
  if (ptr->active_writers==0&&ptr->blocked_writers==0) {
40
    ptr->active_readers++;
41
    __sem_signal(&ptr->readers_semaph);
42
  } else
43
    ptr->blocked_readers++;
44
 
45
  __mutex_unlock(&ptr->mutex);
46
 
47
  __sem_wait(&ptr->readers_semaph);
48
}
49
 
50
int __rwlock_tryrdlock(__rwlock_t *ptr)
51
{
52
  int ret;
53
 
54
  __mutex_lock(&ptr->mutex);
55
 
56
  ret=0;
57
  if (ptr->active_writers==0&&ptr->blocked_writers==0) {
58
    ptr->active_readers++;
59
    __sem_signal(&ptr->readers_semaph);
60
  } else
61
    ret=-1;
62
 
63
  __mutex_unlock(&ptr->mutex);
64
 
65
  if (ret==0) __sem_wait(&ptr->readers_semaph);
66
  return ret;
67
}
68
 
69
void __rwlock_wrlock(__rwlock_t *ptr)
70
{
71
  __mutex_lock(&ptr->mutex);
72
 
73
  if (ptr->active_readers==0&&ptr->active_writers==0) {
74
    ptr->active_writers=1;
75
    __sem_signal(&ptr->writers_semaph);
76
  }
77
  else
78
    ptr->blocked_writers++;
79
 
80
  __mutex_unlock(&ptr->mutex);
81
 
82
  __sem_wait(&ptr->writers_semaph);
83
}
84
 
85
int __rwlock_trywrlock(__rwlock_t *ptr)
86
{
87
  int ret;
88
 
89
  __mutex_lock(&ptr->mutex);
90
 
91
  ret=0;
92
  if (ptr->active_readers==0&&ptr->active_writers==0) {
93
    ptr->active_writers=1;
94
    __sem_signal(&ptr->writers_semaph);
95
  }
96
  else
97
    ret=-1;
98
 
99
  __mutex_unlock(&ptr->mutex);
100
 
101
  if (ret==0) __sem_wait(&ptr->writers_semaph);
102
  return ret;
103
}
104
 
105
void __rwlock_rdunlock(__rwlock_t *ptr)
106
{
107
  __mutex_lock(&ptr->mutex);
108
 
109
  ptr->active_readers--;
110
  if (ptr->active_readers==0&&ptr->blocked_writers>0) {
111
    ptr->active_writers=1;
112
    ptr->blocked_writers--;
113
    __sem_signal(&ptr->writers_semaph);
114
  }
115
 
116
  __mutex_unlock(&ptr->mutex);
117
}
118
 
119
void __rwlock_wrunlock(__rwlock_t *ptr)
120
{
121
  __mutex_lock(&ptr->mutex);
122
 
123
  ptr->active_writers=0;
124
  if (ptr->blocked_readers>0) {
125
    do {
126
      ptr->blocked_readers--;
127
      ptr->active_readers++;
128
      __sem_signal(&ptr->readers_semaph);
129
    } while(ptr->blocked_readers!=0);
130
  } else if (ptr->blocked_writers>0) {
131
    ptr->active_writers=1;
132
    ptr->blocked_writers--;
133
    __sem_signal(&ptr->writers_semaph);
134
  }
135
 
136
  __mutex_unlock(&ptr->mutex);
137
}