source: trunk/kernel/libk/rwlock.c @ 454

Last change on this file since 454 was 438, checked in by alain, 7 years ago

Fix a bug in scheduler related to RPC blocking.

File size: 3.8 KB
Line 
1/*
2 * rwlock.c - kernel read/write lock synchronization.
3 *
4 * Author  Alain Greiner     (2016,2017,2018)
5 *
6 * Copyright (c) UPMC Sorbonne Universites
7 *
8 * This file is part of ALMOS-MKH.
9 *
10 * ALMOS-MKH is free software; you can redistribute it and/or modify it
11 * under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; version 2.0 of the License.
13 *
14 * ALMOS-MKH is distributed in the hope that it will be useful, but
15 * WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17 * General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with ALMOS-MKH; if not, write to the Free Software Foundation,
21 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
22 */
23
24#include <kernel_config.h>
25#include <hal_types.h>
26#include <hal_atomic.h>
27#include <hal_special.h>
28#include <hal_irqmask.h>
29#include <thread.h>
30#include <printk.h>
31#include <rwlock.h>
32
33///////////////////////////////////////
34void rwlock_init( rwlock_t * lock )
35{ 
36        lock->ticket  = 0;
37    lock->current = 0;
38    lock->count   = 0;
39
40#if DEBUG_RWLOCKS
41lock->owner   = NULL;
42list_entry_init( &lock->list ); 
43#endif
44
45}
46
47//////////////////////////////////////
48void rwlock_rd_lock( rwlock_t * lock )
49{
50        reg_t               mode;
51        uint32_t            ticket;
52        thread_t          * this = CURRENT_THREAD;
53
54    // disable IRQs
55        hal_disable_irq( &mode );
56
57    // get next free ticket
58    ticket = hal_atomic_add( &lock->ticket , 1 );
59 
60    // poll the current ticket value
61        while( lock->current != ticket )
62    {
63        hal_fixed_delay( CONFIG_RWLOCK_DELAY );
64    }
65
66    ////////// From here we have the lock  ////////////
67
68    // increment number of readers
69    lock->count++;
70    this->local_locks++;
71
72#if DEBUG_RWLOCKS
73list_add_first( &this->locks_root , &lock->list );
74#endif
75
76    // consistency
77    hal_fence();
78
79    // release the lock to allow several simultaneous readers
80    lock->current++;
81
82    // enable IRQs
83        hal_restore_irq( mode );
84
85}  // end  rwlock_rd_lock()
86
87////////////////////////////////////////
88void rwlock_rd_unlock( rwlock_t * lock )
89{
90    reg_t      mode;
91        thread_t * this = CURRENT_THREAD;
92
93    // disable IRQs
94        hal_disable_irq( &mode );
95 
96    // decrement number of readers
97    hal_atomic_add( &lock->count , -1 );
98    this->local_locks--;
99
100#if DEBUG_RWLOCKS
101list_unlink( &lock->list );
102#endif
103
104    // enable IRQs
105        hal_restore_irq( mode );
106
107    // deschedule if pending request
108    thread_check_sched();
109}
110
111//////////////////////////////////////
112void rwlock_wr_lock( rwlock_t * lock )
113{
114        reg_t              mode;
115    uint32_t           ticket;
116        thread_t         * this = CURRENT_THREAD;
117
118    // disable IRQs
119        hal_disable_irq( &mode );
120 
121    // get next free ticket
122    ticket = hal_atomic_add( &lock->ticket , 1 );
123 
124    // poll the current ticket value
125        while( lock->current != ticket )
126    {
127        hal_fixed_delay( CONFIG_RWLOCK_DELAY );
128    } 
129
130    ////////// From here we have the lock  ////////////
131
132    // wait completion of existing read access
133    while( lock->count != 0 )
134    {
135        hal_fixed_delay( CONFIG_RWLOCK_DELAY );
136    }
137
138    this->local_locks++;
139
140#if DEBUG_RWLOCKS
141lock->owner = this;
142list_add_first( &this->locks_root , &lock->list );
143#endif
144
145    // enable IRQs
146        hal_restore_irq( mode );
147
148}  // end rwlock_wr_lock()
149
150////////////////////////////////////////////
151void rwlock_wr_unlock( rwlock_t * lock )
152{
153    reg_t      mode;
154        thread_t * this = CURRENT_THREAD;
155
156    // disable IRQs
157        hal_disable_irq( &mode );
158 
159#if DEBUG_RWLOCKS
160lock->owner = NULL;
161list_unlink( &lock->list );
162#endif
163
164    // release lock
165    lock->current++;
166    this->local_locks--;
167
168    // enable IRQs
169        hal_restore_irq( mode );
170   
171    // deschedule if pending request
172    thread_check_sched();
173}
174
Note: See TracBrowser for help on using the repository browser.