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

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

Cosmetic: improve debug.

File size: 4.3 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    ((uint32_t)hal_get_cycles() > DEBUG_BUSYLOCK) )
80{
81    xptr_t root_xp = XPTR( local_cxy , &this->busylocks_root );
82
83    // update thread list of busylocks
84    xlist_add_last( root_xp , XPTR( local_cxy , &lock->xlist ) );
85}
86#endif
87
88#if( DEBUG_BUSYLOCK && DEBUG_BUSYLOCK_THREAD_XP )
89if( (lock->type != LOCK_CHDEV_TXT0) && 
90    (XPTR( local_cxy , this ) == DEBUG_BUSYLOCK_THREAD_XP) )
91{
92    // get cluster and local pointer of target thread
93    cxy_t      thread_cxy = GET_CXY( DEBUG_BUSYLOCK_THREAD_XP );
94    thread_t * thread_ptr = GET_PTR( DEBUG_BUSYLOCK_THREAD_XP );
95
96    // display message on kernel TXT0
97    printk("\n[%s] thread[%x,%x] ACQUIRE lock %s\n",
98    __FUNCTION__, this->process->pid, this->trdid, lock_type_str[lock->type] );
99}
100#endif
101
102} // end busylock_acquire()
103
104//////////////////////////////////////////
105void busylock_release( busylock_t * lock )
106{
107    thread_t * this = CURRENT_THREAD;
108
109    // memory barrier to update the protected object
110    hal_fence();
111
112    // update lock state
113    lock->current++;
114
115    // decrement thread busylocks counter
116    this->busylocks--;
117
118    // memory barrier to update busylock and thread state
119    hal_fence();
120
121#if DEBUG_BUSYLOCK
122if( (lock->type != LOCK_CHDEV_TXT0) && 
123    ((uint32_t)hal_get_cycles() > DEBUG_BUSYLOCK) )
124{
125    // remove lock from thread list of busylocks
126    xlist_unlink( XPTR( local_cxy , &lock->xlist ) );
127}
128#endif
129
130#if( DEBUG_BUSYLOCK && DEBUG_BUSYLOCK_THREAD_XP )
131if( (lock->type != LOCK_CHDEV_TXT0) && 
132    (XPTR( local_cxy , this ) == DEBUG_BUSYLOCK_THREAD_XP) )
133{
134    // get cluster and local pointer of target thread
135    cxy_t      thread_cxy = GET_CXY( DEBUG_BUSYLOCK_THREAD_XP );
136    thread_t * thread_ptr = GET_PTR( DEBUG_BUSYLOCK_THREAD_XP );
137
138    // display message on kernel TXT0
139    printk("\n[%s] thread[%x,%x] RELEASE lock %s\n",
140    __FUNCTION__, this->process->pid, this->trdid, lock_type_str[lock->type] );
141}
142#endif
143
144    // exit critical section
145    hal_restore_irq( lock->save_sr );
146 
147}  // end busylock_release()
148
149       
Note: See TracBrowser for help on using the repository browser.