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

Last change on this file since 303 was 124, checked in by max@…, 7 years ago

rename hal_wbflush->hal_fence

File size: 3.3 KB
RevLine 
[1]1/*
2 * rwlock.c - kernel read/write lock synchronization.
3 *
4 * Author  Alain Greiner     (2016}
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
[14]24#include <kernel_config.h>
[1]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        lock->owner   = NULL;
40}
41
42//////////////////////////////////////
43void rwlock_rd_lock( rwlock_t * lock )
44{
[60]45        reg_t               mode;
[1]46        uint32_t            ticket;
47        thread_t          * this = CURRENT_THREAD;
48
49    // disable IRQs
50        hal_disable_irq( &mode );
51
52    // get next free ticket
53    ticket = hal_atomic_add( &lock->ticket , 1 );
54 
55    // poll the current ticket value
56        while( lock->current != ticket )
57    {
58        hal_fixed_delay( CONFIG_RWLOCK_DELAY );
59    }
60
61    ////////// From here we have the lock  ////////////
62
63    // increment number of readers
64    lock->count++;
65    this->local_locks++;
66
67    // consistency
[124]68    hal_fence();
[1]69
70    // release the lock to allow several simultaneous readers
71    lock->current++;
72
73    // enable IRQs
74        hal_restore_irq( mode );
75
76}  // end  rwlock_rd_lock()
77
78////////////////////////////////////////
79void rwlock_rd_unlock( rwlock_t * lock )
80{
[60]81    reg_t      mode;
[1]82        thread_t * this = CURRENT_THREAD;
83
84    // disable IRQs
85        hal_disable_irq( &mode );
86 
87    // decrement number of readers
[23]88    hal_atomic_add( &lock->count , -1 );
[1]89    this->local_locks--;
90
91    // enable IRQs
92        hal_restore_irq( mode );
93}
94
95//////////////////////////////////////
96void rwlock_wr_lock( rwlock_t * lock )
97{
[60]98        reg_t              mode;
[1]99    uint32_t           ticket;
100        thread_t         * this = CURRENT_THREAD;
101
102    // disable IRQs
103        hal_disable_irq( &mode );
104 
105    // get next free ticket
106    ticket = hal_atomic_add( &lock->ticket , 1 );
107 
108    // poll the current ticket value
109        while( lock->current != ticket )
110    {
111        hal_fixed_delay( CONFIG_RWLOCK_DELAY );
112    } 
113
114    ////////// From here we have the lock  ////////////
115
116    // wait completion of existing read access
117    while( lock->count != 0 )
118    {
119        hal_fixed_delay( CONFIG_RWLOCK_DELAY );
120    }
121
122    lock->owner = this;
123    this->local_locks++;
124
125    // enable IRQs
126        hal_restore_irq( mode );
127
128}  // end rwlock_wr_lock()
129
130////////////////////////////////////////////
131void rwlock_wr_unlock( rwlock_t * lock )
132{
[60]133    reg_t      mode;
[1]134        thread_t * this = CURRENT_THREAD;
135
136    // disable IRQs
137        hal_disable_irq( &mode );
138 
139    // release lock
140    lock->current++;
141    lock->owner = NULL;
142    this->local_locks--;
143   
144    // enable IRQs
145        hal_restore_irq( mode );
146}
147
Note: See TracBrowser for help on using the repository browser.