Ignore:
Timestamp:
Oct 4, 2018, 11:47:36 PM (3 years ago)
Author:
alain
Message:

Complete restructuration of kernel locks.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/kernel/kern/chdev.c

    r545 r564  
    3737#include <devfs.h>
    3838
    39 
    40 extern chdev_directory_t    chdev_dir;   // allocated in kernel_init.c
     39//////////////////////////////////////////////////////////////////////////////////////
     40// Extern global variables
     41//////////////////////////////////////////////////////////////////////////////////////
     42
     43extern chdev_directory_t    chdev_dir;         // allocated in kernel_init.c
     44
    4145
    4246#if (DEBUG_SYS_READ & 1)
     
    5761char * chdev_func_str( uint32_t func_type )
    5862{
    59   switch ( func_type ) {
     63    switch ( func_type )
     64    {
    6065    case DEV_FUNC_RAM: return "RAM";
    6166    case DEV_FUNC_ROM: return "ROM";
     
    9196    if( chdev == NULL ) return NULL;
    9297
    93     // initialize waiting threads queue and associated lock
    94     remote_spinlock_init( XPTR( local_cxy , &chdev->wait_lock ) );
     98    // initialize lock
     99    remote_busylock_init( XPTR( local_cxy , &chdev->wait_lock ), LOCK_CHDEV_QUEUE );
     100
     101    // initialise waiting queue
    95102    xlist_root_init( XPTR( local_cxy , &chdev->wait_root ) );
    96103
     
    130137    core_t   * core_ptr;      // local pointer on core running the server thread
    131138    uint32_t   server_lid;    // core running the server thread local index
    132     xptr_t     lock_xp;       // extended pointer on lock protecting the chdev queue
     139    xptr_t     lock_xp;       // extended pointer on lock protecting the chdev state
    133140    uint32_t   save_sr;       // for critical section
    134141
     
    147154    chdev_t * chdev_ptr = GET_PTR( chdev_xp );
    148155
     156// check calling thread can yield
     157assert( (this->busylocks == 0),
     158"cannot yield : busylocks = %d\n", this->busylocks );
     159
    149160    // get local and extended pointers on server thread
    150161    server_ptr = (thread_t *)hal_remote_lpt( XPTR( chdev_cxy , &chdev_ptr->server) );
     
    155166
    156167    // get server core local index
    157     server_lid = hal_remote_lw( XPTR( chdev_cxy , &core_ptr->lid ) );
     168    server_lid = hal_remote_l32( XPTR( chdev_cxy , &core_ptr->lid ) );
    158169
    159170#if (DEBUG_CHDEV_CMD_RX || DEBUG_CHDEV_CMD_TX)
    160 bool_t is_rx = hal_remote_lw( XPTR( chdev_cxy , &chdev_ptr->is_rx ) );
     171bool_t is_rx = hal_remote_l32( XPTR( chdev_cxy , &chdev_ptr->is_rx ) );
    161172#endif
    162173   
     
    185196
    186197    // build extended pointer on lock protecting chdev waiting queue
    187     lock_xp = XPTR( chdev_cxy , &chdev_ptr->wait_lock );
     198    lock_xp            = XPTR( chdev_cxy , &chdev_ptr->wait_lock );
    188199
    189200    // critical section for the following sequence:
    190     // (1) take the lock protecting waiting queue
     201    // (1) take the lock protecting the chdev state
    191202    // (2) block the client thread
    192203    // (3) unblock the server thread if required
     
    200211    hal_disable_irq( &save_sr );
    201212
    202     // take the lock protecting chdev waiting queue
    203     remote_spinlock_lock( lock_xp );
     213    // take the lock protecting chdev queue
     214    remote_busylock_acquire( lock_xp );
    204215
    205216    // block current thread
     
    217228
    218229    // unblock server thread if required
    219     if( hal_remote_lw( blocked_xp ) & THREAD_BLOCKED_IDLE )
     230    if( hal_remote_l32( blocked_xp ) & THREAD_BLOCKED_IDLE )
    220231    thread_unblock( server_xp , THREAD_BLOCKED_IDLE );
    221232
     
    243254#endif
    244255 
    245     // send IPI to core running the server thread when server != client
     256    // send IPI to core running the server thread when server core != client core
    246257    if( (server_lid != this->core->lid) || (local_cxy != chdev_cxy) )
    247258    {
     
    262273    }
    263274 
    264     // release lock
    265     remote_spinlock_unlock( lock_xp );
     275    // release lock protecting chdev queue
     276    remote_busylock_release( lock_xp );
    266277
    267278    // deschedule
    268     assert( thread_can_yield() , "illegal sched_yield\n" );
    269279    sched_yield("blocked on I/O");
    270280
     
    308318    server = CURRENT_THREAD;
    309319
    310     // get root and lock on command queue
     320    // build extended pointer on root of client threads queue
    311321    root_xp = XPTR( local_cxy , &chdev->wait_root );
     322
     323    // build extended pointer on lock protecting client threads queue
    312324    lock_xp = XPTR( local_cxy , &chdev->wait_lock );
    313325
     
    316328    while( 1 )
    317329    {
     330
     331#if DEBUG_CHDEV_SERVER_RX
     332uint32_t rx_cycle = (uint32_t)hal_get_cycles();
     333if( (chdev->is_rx) && (DEBUG_CHDEV_SERVER_RX < rx_cycle) )
     334printk("\n[DBG] %s : dev_thread %x start RX / cycle %d\n",
     335__FUNCTION__ , server->trdid , rx_cycle );
     336#endif
     337
     338#if DEBUG_CHDEV_SERVER_TX
     339uint32_t tx_cycle = (uint32_t)hal_get_cycles();
     340if( (chdev->is_rx == 0) && (DEBUG_CHDEV_SERVER_TX < tx_cycle) )
     341printk("\n[DBG] %s : dev_thread %x start TX / cycle %d\n",
     342__FUNCTION__ , server->trdid , tx_cycle );
     343#endif
     344
    318345        // get the lock protecting the waiting queue
    319         remote_spinlock_lock( lock_xp );
     346        remote_busylock_acquire( lock_xp );
    320347
    321348        // check waiting queue state
    322349        if( xlist_is_empty( root_xp ) ) // waiting queue empty
    323350        {
     351
     352#if DEBUG_CHDEV_SERVER_RX
     353rx_cycle = (uint32_t)hal_get_cycles();
     354if( (chdev->is_rx) && (DEBUG_CHDEV_SERVER_RX < rx_cycle) )
     355printk("\n[DBG] %s : dev_thread %x found RX queue empty => blocks / cycle %d\n",
     356__FUNCTION__ , server->trdid , rx_cycle );
     357#endif
     358
     359#if DEBUG_CHDEV_SERVER_TX
     360tx_cycle = (uint32_t)hal_get_cycles();
     361if( (chdev->is_rx == 0) && (DEBUG_CHDEV_SERVER_TX < tx_cycle) )
     362printk("\n[DBG] %s : dev_thread %x found TX queue empty => blocks / cycle %d\n",
     363__FUNCTION__ , server->trdid , tx_cycle );
     364#endif
     365
    324366            // release lock
    325             remote_spinlock_unlock( lock_xp );
     367            remote_busylock_release( lock_xp );
    326368
    327369            // block
    328370            thread_block( XPTR( local_cxy , server ) , THREAD_BLOCKED_IDLE );
    329371
     372// check server thread can yield
     373assert( (server->busylocks == 0),
     374"cannot yield : busylocks = %d\n", server->busylocks );
     375
    330376            // deschedule
    331             assert( thread_can_yield() , "illegal sched_yield\n" );
    332377            sched_yield("I/O queue empty");
    333378        }
     
    335380        {
    336381            // get extended pointer on first client thread
    337             client_xp = XLIST_FIRST_ELEMENT( root_xp , thread_t , wait_list );
     382            client_xp = XLIST_FIRST( root_xp , thread_t , wait_list );
    338383
    339384            // get client thread cluster and local pointer
     
    345390
    346391            // release lock
    347             remote_spinlock_unlock( lock_xp );
     392            remote_busylock_release( lock_xp );
    348393
    349394#if DEBUG_CHDEV_SERVER_RX
    350 uint32_t rx_cycle = (uint32_t)hal_get_cycles();
     395rx_cycle = (uint32_t)hal_get_cycles();
    351396if( (chdev->is_rx) && (DEBUG_CHDEV_SERVER_RX < rx_cycle) )
    352 printk("\n[DBG] %s : server_thread %x start RX / client %x / cycle %d\n",
    353 __FUNCTION__ , server , client_ptr , rx_cycle );
     397printk("\n[DBG] %s : dev_thread %x for RX found client thread %x in process %x / cycle %d\n",
     398__FUNCTION__, server->trdid ,client_ptr->trdid ,client_ptr->process->pid, rx_cycle );
    354399#endif
    355400
    356401#if DEBUG_CHDEV_SERVER_TX
    357 uint32_t tx_cycle = (uint32_t)hal_get_cycles();
     402tx_cycle = (uint32_t)hal_get_cycles();
    358403if( (chdev->is_rx == 0) && (DEBUG_CHDEV_SERVER_TX < tx_cycle) )
    359 printk("\n[DBG] %s : server_thread %x start TX / client %x / cycle %d\n",
    360 __FUNCTION__ , server , client_ptr , tx_cycle );
     404printk("\n[DBG] %s : dev_thread %x for TX found client thread %x in process %x / cycle %d\n",
     405__FUNCTION__, server->trdid ,client_ptr->trdid ,client_ptr->process->pid, tx_cycle );
    361406#endif
    362407
     
    378423rx_cycle = (uint32_t)hal_get_cycles();
    379424if( (chdev->is_rx) && (DEBUG_CHDEV_SERVER_RX < rx_cycle) )
    380 printk("\n[DBG] %s : server_thread %x completes RX / client %x / cycle %d\n",
    381 __FUNCTION__ , server , client_ptr , rx_cycle );
     425printk("\n[DBG] %s : dev_thread %x completes RX for client %x in process %x / cycle %d\n",
     426__FUNCTION__, server->trdid, client_ptr->trdid, client_ptr->process->pid, rx_cycle );
    382427#endif
    383428
     
    385430tx_cycle = (uint32_t)hal_get_cycles();
    386431if( (chdev->is_rx == 0) && (DEBUG_CHDEV_SERVER_TX < tx_cycle) )
    387 printk("\n[DBG] %s : server_thread %x completes TX / client %x / cycle %d\n",
    388 __FUNCTION__ , server , client_ptr , tx_cycle );
     432printk("\n[DBG] %s : dev_thread %x completes TX for client %x in process %x / cycle %d\n",
     433__FUNCTION__, server->trdid, client_ptr->trdid, client_ptr->process->pid, tx_cycle );
    389434#endif
    390435
     
    419464
    420465    // get inode type from file descriptor
    421     inode_type = hal_remote_lw( XPTR( file_cxy , &file_ptr->type ) );
     466    inode_type = hal_remote_l32( XPTR( file_cxy , &file_ptr->type ) );
    422467    inode_ptr  = (vfs_inode_t *)hal_remote_lpt( XPTR( file_cxy , &file_ptr->inode ) );
    423468
     
    432477}  // end chdev_from_file()
    433478
    434 ////////////////////////
     479//////////////////////////////
    435480void chdev_dir_display( void )
    436481{
     
    439484    chdev_t * ptr;
    440485    uint32_t  base;
    441     reg_t     save_sr;
    442486
    443487    // get pointers on TXT0 chdev
     
    446490    chdev_t * txt0_ptr = GET_PTR( txt0_xp );
    447491
    448     // get extended pointer on remote TXT0 chdev lock
     492    // get extended pointer on TXT0 lock
    449493    xptr_t  lock_xp = XPTR( txt0_cxy , &txt0_ptr->wait_lock );
    450494
    451     // get TXT0 lock in busy waiting mode
    452     remote_spinlock_lock_busy( lock_xp , &save_sr );
     495    // get TXT0 lock
     496    remote_busylock_acquire( lock_xp );
    453497
    454498    // header
     
    456500
    457501    // IOB
    458     if (chdev_dir.iob != NULL )
     502    if (chdev_dir.iob != XPTR_NULL )
    459503    {
    460504        cxy  = GET_CXY( chdev_dir.iob );
    461505        ptr  = GET_PTR( chdev_dir.iob );
    462         base = (uint32_t)hal_remote_lwd( XPTR( cxy , &ptr->base ) );
     506        base = (uint32_t)hal_remote_l64( XPTR( cxy , &ptr->base ) );
    463507        nolock_printk("  - iob       : cxy = %X / ptr = %X / base = %X\n", cxy, ptr, base);
    464508    }
     
    467511    cxy  = GET_CXY( chdev_dir.pic );
    468512    ptr  = GET_PTR( chdev_dir.pic );
    469     base = (uint32_t)hal_remote_lwd( XPTR( cxy , &ptr->base ) );
     513    base = (uint32_t)hal_remote_l64( XPTR( cxy , &ptr->base ) );
    470514    nolock_printk("  - pic       : cxy = %X / ptr = %X / base = %X\n", cxy, ptr, base);
    471515
     
    475519        cxy = GET_CXY( chdev_dir.txt_rx[i] );
    476520        ptr = GET_PTR( chdev_dir.txt_rx[i] );
    477         base = (uint32_t)hal_remote_lwd( XPTR( cxy , &ptr->base ) );
     521        base = (uint32_t)hal_remote_l64( XPTR( cxy , &ptr->base ) );
    478522        nolock_printk("  - txt_rx[%d] : cxy = %X / ptr = %X / base = %X\n", i, cxy, ptr, base);
    479523
    480524        cxy = GET_CXY( chdev_dir.txt_tx[i] );
    481525        ptr = GET_PTR( chdev_dir.txt_tx[i] );
    482         base = (uint32_t)hal_remote_lwd( XPTR( cxy , &ptr->base ) );
     526        base = (uint32_t)hal_remote_l64( XPTR( cxy , &ptr->base ) );
    483527        nolock_printk("  - txt_tx[%d] : cxy = %X / ptr = %X / base = %X\n", i, cxy, ptr, base);
    484528    }
     
    489533        cxy = GET_CXY( chdev_dir.ioc[i] );
    490534        ptr = GET_PTR( chdev_dir.ioc[i] );
    491         base = (uint32_t)hal_remote_lwd( XPTR( cxy , &ptr->base ) );
     535        base = (uint32_t)hal_remote_l64( XPTR( cxy , &ptr->base ) );
    492536        nolock_printk("  - ioc[%d]    : cxy = %X / ptr = %X / base = %X\n", i, cxy, ptr, base);
    493537    }
     
    498542        cxy  = GET_CXY( chdev_dir.fbf[i] );
    499543        ptr  = GET_PTR( chdev_dir.fbf[i] );
    500         base = (uint32_t)hal_remote_lwd( XPTR( cxy , &ptr->base ) );
     544        base = (uint32_t)hal_remote_l64( XPTR( cxy , &ptr->base ) );
    501545        nolock_printk("  - fbf[%d]    : cxy = %X / ptr = %X / base = %X\n", i, cxy, ptr, base);
    502546    }
     
    507551        cxy = GET_CXY( chdev_dir.nic_rx[i] );
    508552        ptr = GET_PTR( chdev_dir.nic_rx[i] );
    509         base = (uint32_t)hal_remote_lwd( XPTR( cxy , &ptr->base ) );
     553        base = (uint32_t)hal_remote_l64( XPTR( cxy , &ptr->base ) );
    510554        nolock_printk("  - nic_rx[%d] : cxy = %X / ptr = %X / base = %X\n", i, cxy, ptr, base);
    511555
    512556        cxy = GET_CXY( chdev_dir.nic_tx[i] );
    513557        ptr = GET_PTR( chdev_dir.nic_tx[i] );
    514         base = (uint32_t)hal_remote_lwd( XPTR( cxy , &ptr->base ) );
     558        base = (uint32_t)hal_remote_l64( XPTR( cxy , &ptr->base ) );
    515559        nolock_printk("  - nic_tx[%d] : cxy = %X / ptr = %X / base = %X\n", i, cxy, ptr, base);
    516560    }
    517561
    518562    // release lock
    519     remote_spinlock_unlock_busy( lock_xp , save_sr );
     563    remote_busylock_release( lock_xp );
    520564
    521565}  // end chdev_dir_display()
     
    546590    hal_remote_strcpy( XPTR( local_cxy , name ), XPTR( chdev_cxy , chdev_ptr->name ) );
    547591
     592    // get pointers on TXT0 chdev
     593    xptr_t    txt0_xp  = chdev_dir.txt_tx[0];
     594    cxy_t     txt0_cxy = GET_CXY( txt0_xp );
     595    chdev_t * txt0_ptr = GET_PTR( txt0_xp );
     596
     597    // get extended pointer on TXT0 lock
     598    xptr_t  lock_xp = XPTR( txt0_cxy , &txt0_ptr->wait_lock );
     599
     600    // get TXT0 lock
     601    remote_busylock_acquire( lock_xp );
     602
    548603    // check queue empty
    549604    if( xlist_is_empty( root_xp ) )
    550605    {
    551         printk("\n***** Waiting queue empty for chdev %s\n", name );
     606        nolock_printk("\n***** Waiting queue empty for chdev %s\n", name );
    552607    }
    553608    else
    554609    {
    555         printk("\n***** Waiting queue for chdev %s\n", name );
     610        nolock_printk("\n***** Waiting queue for chdev %s\n", name );
    556611
    557612        // scan the waiting queue
     
    561616            thread_cxy = GET_CXY( thread_xp );
    562617            thread_ptr = GET_PTR( thread_xp );
    563             trdid      = hal_remote_lw ( XPTR( thread_cxy , &thread_ptr->trdid   ) );
     618            trdid      = hal_remote_l32 ( XPTR( thread_cxy , &thread_ptr->trdid   ) );
    564619            process    = hal_remote_lpt( XPTR( thread_cxy , &thread_ptr->process ) );
    565                         pid        = hal_remote_lw ( XPTR( thread_cxy , &process->pid        ) );
    566 
    567             printk("- thread %X / cluster %X / trdid %X / pid %X\n",
     620                        pid        = hal_remote_l32 ( XPTR( thread_cxy , &process->pid        ) );
     621
     622            nolock_printk("- thread %X / cluster %X / trdid %X / pid %X\n",
    568623            thread_ptr, thread_cxy, trdid, pid );
    569624        }
    570625    }
     626
     627    // release TXT0 lock
     628    remote_busylock_release( lock_xp );
     629
    571630}  // end chdev_queue_display()
    572631
Note: See TracChangeset for help on using the changeset viewer.