Ignore:
Timestamp:
Jan 13, 2021, 12:36:17 AM (4 years ago)
Author:
alain
Message:

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

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/kernel/syscalls/sys_thread_sleep.c

    r566 r683  
    11/*
    2  * sys_thread_sleep.c - put the calling thread in sleep state
     2 * sys_thread_sleep.c - block the calling thread on SLEEP, with or without alarm
    33 *
    4  * Author    Alain Greiner (2016,2017)
     4 * Author    Alain Greiner    (2016,2017,2018,2019,2020)
    55 *
    66 * Copyright (c) UPMC Sorbonne Universites
     
    2828#include <syscalls.h>
    2929
    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///////////////////////////////////////////////////////////////////////////////////////
     36static void __attribute__((noinline)) sleep_alarm_handler( xptr_t thread_xp )
    3237{
     38    // stop the alarm
     39    alarm_stop( thread_xp );
    3340
    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////////////////////////////////////////
     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();
    3555
    3656#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 );
     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 );
    4360#endif
    4461
    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
     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    }
    4794
    4895#if DEBUG_SYS_THREAD_SLEEP
    49 tm_end = hal_get_cycles();
    5096if( DEBUG_SYS_THREAD_SLEEP < tm_end )
    51 printk("\n[DBG] %s : thread %x in process %x resume / cycle %d\n",
    52 __FUNCTION__ , this->trdid, this->process->pid , (uint32_t)tm_end );
     97printk("\n[%s] thread[%x,%x] resume / cycle %d\n",
     98__FUNCTION__ , this->process->pid, this->trdid, (uint32_t)tm_end );
    5399#endif
    54100
Note: See TracChangeset for help on using the changeset viewer.