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

Last change on this file since 408 was 337, checked in by alain, 7 years ago

Introduce the delayed context switch if current thread has a lock.

File size: 3.5 KB
Line 
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
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        lock->owner   = NULL;
40}
41
42//////////////////////////////////////
43void rwlock_rd_lock( rwlock_t * lock )
44{
45        reg_t               mode;
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
68    hal_fence();
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{
81    reg_t      mode;
82        thread_t * this = CURRENT_THREAD;
83
84    // disable IRQs
85        hal_disable_irq( &mode );
86 
87    // decrement number of readers
88    hal_atomic_add( &lock->count , -1 );
89    this->local_locks--;
90
91    // enable IRQs
92        hal_restore_irq( mode );
93
94    // deschedule if pending request
95    thread_check_sched();
96}
97
98//////////////////////////////////////
99void rwlock_wr_lock( rwlock_t * lock )
100{
101        reg_t              mode;
102    uint32_t           ticket;
103        thread_t         * this = CURRENT_THREAD;
104
105    // disable IRQs
106        hal_disable_irq( &mode );
107 
108    // get next free ticket
109    ticket = hal_atomic_add( &lock->ticket , 1 );
110 
111    // poll the current ticket value
112        while( lock->current != ticket )
113    {
114        hal_fixed_delay( CONFIG_RWLOCK_DELAY );
115    } 
116
117    ////////// From here we have the lock  ////////////
118
119    // wait completion of existing read access
120    while( lock->count != 0 )
121    {
122        hal_fixed_delay( CONFIG_RWLOCK_DELAY );
123    }
124
125    lock->owner = this;
126    this->local_locks++;
127
128    // enable IRQs
129        hal_restore_irq( mode );
130
131}  // end rwlock_wr_lock()
132
133////////////////////////////////////////////
134void rwlock_wr_unlock( rwlock_t * lock )
135{
136    reg_t      mode;
137        thread_t * this = CURRENT_THREAD;
138
139    // disable IRQs
140        hal_disable_irq( &mode );
141 
142    // release lock
143    lock->current++;
144    lock->owner = NULL;
145    this->local_locks--;
146
147    // enable IRQs
148        hal_restore_irq( mode );
149   
150    // deschedule if pending request
151    thread_check_sched();
152}
153
Note: See TracBrowser for help on using the repository browser.