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

Last change on this file since 560 was 457, checked in by alain, 6 years ago

This version modifies the exec syscall and fixes a large number of small bugs.
The version number has been updated (0.1)

File size: 3.8 KB
RevLine 
[1]1/*
2 * rwlock.c - kernel read/write lock synchronization.
3 *
[436]4 * Author  Alain Greiner     (2016,2017,2018)
[1]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>
[457]25#include <hal_kernel_types.h>
[1]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;
[409]39
[438]40#if DEBUG_RWLOCKS
[436]41lock->owner   = NULL;
42list_entry_init( &lock->list ); 
[409]43#endif
44
[1]45}
46
47//////////////////////////////////////
48void rwlock_rd_lock( rwlock_t * lock )
49{
[60]50        reg_t               mode;
[1]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
[438]72#if DEBUG_RWLOCKS
[436]73list_add_first( &this->locks_root , &lock->list );
[409]74#endif
75
[1]76    // consistency
[124]77    hal_fence();
[1]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{
[60]90    reg_t      mode;
[1]91        thread_t * this = CURRENT_THREAD;
92
93    // disable IRQs
94        hal_disable_irq( &mode );
95 
96    // decrement number of readers
[23]97    hal_atomic_add( &lock->count , -1 );
[1]98    this->local_locks--;
99
[438]100#if DEBUG_RWLOCKS
[436]101list_unlink( &lock->list );
[409]102#endif
103
[1]104    // enable IRQs
105        hal_restore_irq( mode );
[337]106
107    // deschedule if pending request
108    thread_check_sched();
[1]109}
110
111//////////////////////////////////////
112void rwlock_wr_lock( rwlock_t * lock )
113{
[60]114        reg_t              mode;
[1]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
[438]140#if DEBUG_RWLOCKS
[436]141lock->owner = this;
142list_add_first( &this->locks_root , &lock->list );
[409]143#endif
144
[1]145    // enable IRQs
146        hal_restore_irq( mode );
147
148}  // end rwlock_wr_lock()
149
150////////////////////////////////////////////
151void rwlock_wr_unlock( rwlock_t * lock )
152{
[60]153    reg_t      mode;
[1]154        thread_t * this = CURRENT_THREAD;
155
156    // disable IRQs
157        hal_disable_irq( &mode );
158 
[438]159#if DEBUG_RWLOCKS
[436]160lock->owner = NULL;
161list_unlink( &lock->list );
[409]162#endif
163
[1]164    // release lock
165    lock->current++;
166    this->local_locks--;
[337]167
[1]168    // enable IRQs
169        hal_restore_irq( mode );
[337]170   
171    // deschedule if pending request
172    thread_check_sched();
[1]173}
174
Note: See TracBrowser for help on using the repository browser.