| [1] | 1 | /* |
|---|
| [683] | 2 | * sys_thread_sleep.c - block the calling thread on SLEEP, with or without alarm |
|---|
| [1] | 3 | * |
|---|
| [683] | 4 | * Author Alain Greiner (2016,2017,2018,2019,2020) |
|---|
| [1] | 5 | * |
|---|
| [23] | 6 | * Copyright (c) UPMC Sorbonne Universites |
|---|
| [1] | 7 | * |
|---|
| [23] | 8 | * This file is part of ALMOS-MKH. |
|---|
| 9 | * |
|---|
| 10 | * ALMOS-MKH is free software; you can redistribute it and/or modify it |
|---|
| [1] | 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 | * |
|---|
| [23] | 14 | * ALMOS-MKH is distributed in the hope that it will be useful, but |
|---|
| [1] | 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 |
|---|
| [23] | 20 | * along with ALMOS-MKH; if not, write to the Free Software Foundation, |
|---|
| [1] | 21 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
|---|
| 22 | */ |
|---|
| 23 | |
|---|
| [23] | 24 | #include <hal_special.h> |
|---|
| [1] | 25 | #include <scheduler.h> |
|---|
| 26 | #include <thread.h> |
|---|
| [23] | 27 | #include <printk.h> |
|---|
| [506] | 28 | #include <syscalls.h> |
|---|
| [1] | 29 | |
|---|
| [683] | 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 ) |
|---|
| [1] | 37 | { |
|---|
| [683] | 38 | // stop the alarm |
|---|
| 39 | alarm_stop( thread_xp ); |
|---|
| [436] | 40 | |
|---|
| [683] | 41 | // unblock the thread |
|---|
| 42 | thread_unblock( thread_xp , THREAD_BLOCKED_SLEEP ); |
|---|
| [296] | 43 | |
|---|
| [683] | 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(); |
|---|
| 55 | |
|---|
| [438] | 56 | #if DEBUG_SYS_THREAD_SLEEP |
|---|
| [683] | 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 ); |
|---|
| 60 | #endif |
|---|
| 61 | |
|---|
| 62 | if( seconds == 0 ) // sleep without alarm |
|---|
| 63 | { |
|---|
| 64 | |
|---|
| 65 | #if DEBUG_SYS_THREAD_SLEEP |
|---|
| [438] | 66 | if( DEBUG_SYS_THREAD_SLEEP < tm_start ) |
|---|
| [683] | 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 ); |
|---|
| [436] | 69 | #endif |
|---|
| [683] | 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; |
|---|
| [1] | 78 | |
|---|
| [683] | 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 | } |
|---|
| [1] | 94 | |
|---|
| [438] | 95 | #if DEBUG_SYS_THREAD_SLEEP |
|---|
| 96 | if( DEBUG_SYS_THREAD_SLEEP < tm_end ) |
|---|
| [683] | 97 | printk("\n[%s] thread[%x,%x] resume / cycle %d\n", |
|---|
| 98 | __FUNCTION__ , this->process->pid, this->trdid, (uint32_t)tm_end ); |
|---|
| [436] | 99 | #endif |
|---|
| [1] | 100 | |
|---|
| 101 | return 0; |
|---|
| [436] | 102 | |
|---|
| [23] | 103 | } // end sys_thread_sleep() |
|---|