source: trunk/kernel/libk/busylock.c @ 563

Last change on this file since 563 was 563, checked in by alain, 5 years ago

Complete restructuration of kernel spinlocks.

File size: 3.6 KB
Line 
1/*
2 * busylock.c - local kernel-busy waiting lock implementation.
3 *
4 * Authors     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_kernel_types.h>
26#include <hal_irqmask.h>
27#include <hal_special.h>
28#include <hal_atomic.h>
29#include <thread.h>
30#include <busylock.h>
31
32//////////////////////////////////////////////////////////////////////////////
33//                Extern global variables
34//////////////////////////////////////////////////////////////////////////////
35
36extern char               * lock_type_str[];    // allocated in kernel_init.c
37extern chdev_directory_t    chdev_dir;          // allocated in kernel_init.c
38
39//////////////////////////////////////
40void busylock_init( busylock_t * lock,
41                    uint32_t     type )
42{
43    lock->ticket  = 0;
44    lock->current = 0;
45    lock->type    = type;
46
47#if DEBUG_BUSYLOCK
48    xlist_entry_init( XPTR( local_cxy , &lock->xlist ) ); 
49#endif
50
51}
52
53//////////////////////////////////////////
54void busylock_acquire( busylock_t * lock )
55{
56    reg_t      save_sr;
57    thread_t * this = CURRENT_THREAD;
58
59    // enter critical section
60    hal_disable_irq( &save_sr );
61 
62    // get one free ticket and update ticket allocator
63    uint32_t ticket = hal_atomic_add( &lock->ticket , 1 );
64
65    // poll current until success
66    while( lock->current != ticket )  asm volatile ("nop");
67
68    // increment thread locks counter
69    this->busylocks++;
70
71    // save SR in lock descriptor
72    lock->save_sr = save_sr;
73
74    // memory barrier to update lock and thread state
75    hal_fence();
76
77#if DEBUG_BUSYLOCK
78if( (lock->type != LOCK_CHDEV_TXT0) && 
79    (XPTR( local_cxy , this ) == DEBUG_BUSYLOCK_THREAD_XP) &&
80    ((uint32_t)hal_get_cycles() > DEBUG_BUSYLOCK) )
81{
82    xptr_t root_xp = XPTR( local_cxy , &this->busylocks_root );
83
84    // update thread list of busylocks
85    xlist_add_last( root_xp , XPTR( local_cxy , &lock->xlist ) );
86
87    // display list of taken locks for the selected thread
88    thread_display_busylocks( lock->type , true );
89}
90#endif
91
92} // end busylock_acquire()
93
94//////////////////////////////////////////
95void busylock_release( busylock_t * lock )
96{
97    thread_t * this = CURRENT_THREAD;
98
99    // memory barrier to update the protected object
100    hal_fence();
101
102    // update lock state
103    lock->current++;
104
105    // decrement thread busylocks counter
106    this->busylocks--;
107
108    // memory barrier to update busylock and thread state
109    hal_fence();
110
111#if DEBUG_BUSYLOCK
112if( (lock->type != LOCK_CHDEV_TXT0) && 
113    (XPTR( local_cxy , this ) == DEBUG_BUSYLOCK_THREAD_XP) &&
114    ((uint32_t)hal_get_cycles() > DEBUG_BUSYLOCK) )
115{
116    // remove lock from thread list of busylocks
117    xlist_unlink( XPTR( local_cxy , &lock->xlist ) );
118
119    // display list of taken locks for the selected thread
120    thread_display_busylocks( lock->type , false );
121}
122#endif
123
124    // exit critical section
125    hal_restore_irq( lock->save_sr );
126 
127}  // end busylock_release()
128
129       
Note: See TracBrowser for help on using the repository browser.