source: trunk/kernel/syscalls/sys_thread_sleep.c

Last change on this file was 683, checked in by alain, 13 months ago

All modifications required to support the <tcp_chat> application
including error recovery in case of packet loss.A

File size: 3.5 KB
Line 
1/*
2 * sys_thread_sleep.c - block the calling thread on SLEEP, with or without alarm
3 *
4 * Author    Alain Greiner    (2016,2017,2018,2019,2020)
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 <hal_special.h>
25#include <scheduler.h>
26#include <thread.h>
27#include <printk.h>
28#include <syscalls.h>
29
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///////////////////////////////////////////////////////////////////////////////////////
36static void __attribute__((noinline)) sleep_alarm_handler( xptr_t thread_xp )
37{
38    // stop the alarm
39    alarm_stop( thread_xp );
40
41    // unblock the thread
42    thread_unblock( thread_xp , THREAD_BLOCKED_SLEEP );
43
44}  // end sleep_alarm_handler()
45
46////////////////////////////////////////
47int 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
56#if DEBUG_SYS_THREAD_SLEEP
57if( DEBUG_SYS_THREAD_SLEEP < (uint32_t)tm_start )
58printk("\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
66if( DEBUG_SYS_THREAD_SLEEP < tm_start )
67printk("\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
86if( DEBUG_SYS_THREAD_SLEEP < tm_start )
87printk("\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    }
94
95#if DEBUG_SYS_THREAD_SLEEP
96if( DEBUG_SYS_THREAD_SLEEP < tm_end )
97printk("\n[%s] thread[%x,%x] resume / cycle %d\n",
98__FUNCTION__ , this->process->pid, this->trdid, (uint32_t)tm_end );
99#endif
100
101        return 0;
102
103}  // end sys_thread_sleep()
Note: See TracBrowser for help on using the repository browser.