Changeset 683 for trunk/kernel/syscalls/sys_thread_sleep.c
- Timestamp:
- Jan 13, 2021, 12:36:17 AM (4 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/kernel/syscalls/sys_thread_sleep.c
r566 r683 1 1 /* 2 * sys_thread_sleep.c - put the calling thread in sleep state2 * sys_thread_sleep.c - block the calling thread on SLEEP, with or without alarm 3 3 * 4 * Author Alain Greiner (2016,2017)4 * Author Alain Greiner (2016,2017,2018,2019,2020) 5 5 * 6 6 * Copyright (c) UPMC Sorbonne Universites … … 28 28 #include <syscalls.h> 29 29 30 ////////////////////// 31 int sys_thread_sleep( void ) 30 /////////////////////////////////////////////////////////////////////////////////////// 31 // This static function implements the alarm handler used to wake-up a thread 32 // when the amarm rings after after a sleep( seconds ) syscall. 33 /////////////////////////////////////////////////////////////////////////////////////// 34 // @ thread_xp : extended pointer on blocked thread. 35 /////////////////////////////////////////////////////////////////////////////////////// 36 static void __attribute__((noinline)) sleep_alarm_handler( xptr_t thread_xp ) 32 37 { 38 // stop the alarm 39 alarm_stop( thread_xp ); 33 40 34 thread_t * this = CURRENT_THREAD; 41 // unblock the thread 42 thread_unblock( thread_xp , THREAD_BLOCKED_SLEEP ); 43 44 } // end sleep_alarm_handler() 45 46 //////////////////////////////////////// 47 int sys_thread_sleep( uint32_t seconds ) 48 { 49 cycle_t ncycles; // number of cycles to sleep 50 51 thread_t * this = CURRENT_THREAD; 52 xptr_t thread_xp = XPTR( local_cxy , this ); 53 54 cycle_t tm_start = hal_get_cycles(); 35 55 36 56 #if DEBUG_SYS_THREAD_SLEEP 37 uint64_t tm_start; 38 uint64_t tm_end; 39 tm_start = hal_get_cycles(); 40 if( DEBUG_SYS_THREAD_SLEEP < tm_start ) 41 printk("\n[DBG] %s : thread %x n process %x blocked / cycle %d\n", 42 __FUNCTION__ , this->trdid, this->process->pid , (uint32_t)tm_start ); 57 if( DEBUG_SYS_THREAD_SLEEP < (uint32_t)tm_start ) 58 printk("\n[%s] thread[%x,%x] enter / cycle %d\n", 59 __FUNCTION__, this->process->pid, this->trdid, (uint32_t)tm_start ); 43 60 #endif 44 61 45 thread_block( XPTR( local_cxy , this ) , THREAD_BLOCKED_GLOBAL ); 46 sched_yield("blocked on sleep"); 62 if( seconds == 0 ) // sleep without alarm 63 { 64 65 #if DEBUG_SYS_THREAD_SLEEP 66 if( DEBUG_SYS_THREAD_SLEEP < tm_start ) 67 printk("\n[%s] thread[%x,%x] blocks on <SLEEP> without alarm / cycle %d\n", 68 __FUNCTION__ , this->process->pid, this->trdid, (uint32_t)tm_start ); 69 #endif 70 // threads blocks and deschedules 71 thread_block( thread_xp , THREAD_BLOCKED_SLEEP ); 72 sched_yield("sleep without alarm"); 73 } 74 else // sleep with alarm 75 { 76 // translate seconds to ncycles 77 ncycles = seconds * LOCAL_CLUSTER->sys_clk; 78 79 // register & start the calling thread alarm 80 alarm_start( thread_xp, 81 tm_start + ncycles, 82 &sleep_alarm_handler, 83 thread_xp ); 84 85 #if DEBUG_SYS_THREAD_SLEEP 86 if( DEBUG_SYS_THREAD_SLEEP < tm_start ) 87 printk("\n[DBG] %s : thread[%x,%x] blocks on <SLEEP> for %d seconds / cycle %d\n", 88 __FUNCTION__ , this->process->pid, this->trdid, seconds, (uint32_t)tm_start ); 89 #endif 90 // thread blocks & deschedules 91 thread_block( thread_xp , THREAD_BLOCKED_SLEEP ); 92 sched_yield("sleep with alarm"); 93 } 47 94 48 95 #if DEBUG_SYS_THREAD_SLEEP 49 tm_end = hal_get_cycles();50 96 if( DEBUG_SYS_THREAD_SLEEP < tm_end ) 51 printk("\n[ DBG] %s : thread %x in process %xresume / cycle %d\n",52 __FUNCTION__ , this-> trdid, this->process->pid, (uint32_t)tm_end );97 printk("\n[%s] thread[%x,%x] resume / cycle %d\n", 98 __FUNCTION__ , this->process->pid, this->trdid, (uint32_t)tm_end ); 53 99 #endif 54 100
Note: See TracChangeset
for help on using the changeset viewer.