Ignore:
Timestamp:
Jun 18, 2017, 10:06:41 PM (7 years ago)
Author:
alain
Message:

Introduce syscalls.

File:
1 moved

Legend:

Unmodified
Added
Removed
  • trunk/kernel/libk/remote_sem.c

    r15 r23  
    2323
    2424#include <hal_types.h>
     25#include <hal_remote.h>
    2526#include <thread.h>
    2627#include <kmem.h>
     
    3536{
    3637    // get pointer on local process_descriptor
    37     process_t * process = CURRENT_PROCESS;
     38    process_t * process = CURRENT_THREAD->process;
    3839
    3940    // get extended pointer on reference process
     
    5758    XLIST_FOREACH( root_xp , iter_xp )
    5859    {
    59         sem_xp  = XLIST_ELEMENT( iter_xp , remote_sem_t , sem_list );
     60        sem_xp  = XLIST_ELEMENT( iter_xp , remote_sem_t , list );
    6061        sem_cxy = GET_CXY( sem_xp );
    6162        sem_ptr = (remote_sem_t *)GET_PTR( sem_xp );
    62         ident   = hal_remote_lw( XPTR( sem_cxy , &sem_ptr->ident ) );   
     63        ident   = (intptr_t)hal_remote_lpt( XPTR( sem_cxy , &sem_ptr->ident ) );   
    6364        if( ident == vaddr )
    6465        {
     
    7374}  // end remote_sem_from_vaddr()
    7475
    75 /////////////////////////////////////////
    76 error_t remote_sem_init( intptr_t  vaddr,
    77                          uint32_t  value )
     76///////////////////////////////////////////
     77error_t remote_sem_create( intptr_t  vaddr,
     78                           uint32_t  value )
    7879{
    7980    xptr_t         sem_xp;
     
    8182
    8283    // get pointer on local process descriptor
    83     process_t * process = CURRENT_PROCESS;
     84    process_t * process = CURRENT_THREAD->process;
    8485
    8586    // get extended pointer on reference process
    8687    xptr_t      ref_xp = process->ref_xp;
    8788
     89    // get reference process cluster and local pointer
    8890    cxy_t       ref_cxy = GET_CXY( ref_xp );
    8991    process_t * ref_ptr = (process_t *)GET_PTR( ref_xp );
     
    100102    else                         // reference is remote
    101103    {
    102         rpc_semaphore_alloc_client( ref_cxy , &sem_xp );
     104        rpc_kcm_alloc_client( ref_cxy , KMEM_SEM , &sem_xp );
    103105        sem_ptr = (remote_sem_t *)GET_PTR( sem_xp );
    104106    }
     
    106108    if( sem_xp == XPTR_NULL ) return ENOMEM;
    107109
    108     // initialise semaphore lock
     110    // initialise semaphore
     111    hal_remote_sw ( XPTR( ref_cxy , &sem_ptr->count ) , value );
     112        hal_remote_spt( XPTR( ref_cxy , &sem_ptr->ident ) , (void *)vaddr );
     113
    109114    remote_spinlock_init( XPTR( ref_cxy , &sem_ptr->lock ) );
    110 
    111     // initialise semaphore count
    112     hal_remote_sw( XPTR( ref_cxy , &sem_ptr->count ) , value );
    113 
    114     // initialise vaddr
    115         hal_remote_spt( XPTR( ref_cxy , &sem_ptr->ident ) , (void *)vaddr );
    116 
    117     // initialise waiting threads queue
    118         xlist_root_init( XPTR( ref_cxy , &sem_ptr->wait_queue ) );
    119 
    120     // register new semaphore in reference process xlist
     115        xlist_root_init( XPTR( ref_cxy , &sem_ptr->root ) );
     116        xlist_entry_init( XPTR( ref_cxy , &sem_ptr->list ) );
     117
     118    // register semaphore in reference process xlist
    121119    xptr_t root_xp = XPTR( ref_cxy , &ref_ptr->sem_root );
    122     xptr_t xp_list = XPTR( ref_cxy , &sem_ptr->sem_list );
     120    xptr_t xp_list = XPTR( ref_cxy , &sem_ptr->list );
     121
     122    remote_spinlock_lock( XPTR( ref_cxy , &ref_ptr->sync_lock ) );
    123123    xlist_add_first( root_xp , xp_list );
     124    remote_spinlock_unlock( XPTR( ref_cxy , &ref_ptr->sync_lock ) );
    124125
    125126    return 0;
     
    127128}  // en remote_sem_init()
    128129 
     130////////////////////////////////////////
     131void remote_sem_destroy( xptr_t sem_xp )
     132{
     133    // get pointer on local process descriptor
     134    process_t * process = CURRENT_THREAD->process;
     135
     136    // get extended pointer on reference process
     137    xptr_t      ref_xp = process->ref_xp;
     138
     139    // get reference process cluster and local pointer
     140    cxy_t       ref_cxy = GET_CXY( ref_xp );
     141    process_t * ref_ptr = (process_t *)GET_PTR( ref_xp );
     142
     143    // get semaphore cluster and local pointer
     144    cxy_t          sem_cxy = GET_CXY( sem_xp );
     145    remote_sem_t * sem_ptr = (remote_sem_t *)GET_PTR( sem_xp );
     146
     147    // get lock protecting semaphore
     148    remote_spinlock_lock( XPTR( sem_cxy , &sem_ptr->lock ) );
     149 
     150    // get remote pointer on waiting queue
     151    xptr_t root_xp = (xptr_t)hal_remote_lwd( XPTR( sem_cxy , &sem_ptr->root ) );
     152 
     153    if( !xlist_is_empty( root_xp ) )   // user error
     154    {
     155        printk("WARNING in %s for thread %x in process %x : "
     156               "destroy semaphore, but  waiting threads queue not empty\n",
     157               __FUNCTION__ , CURRENT_THREAD->trdid , CURRENT_THREAD->process->pid );
     158    }
     159
     160    // reset semaphore count
     161    hal_remote_sw( XPTR( sem_cxy , &sem_ptr->count ) , 0 );
     162
     163    // remove semaphore from reference process xlist
     164    remote_spinlock_lock( XPTR( ref_cxy , &ref_ptr->sync_lock ) );
     165    xlist_unlink( XPTR( sem_cxy , &sem_ptr->list ) );
     166    remote_spinlock_unlock( XPTR( ref_cxy , &ref_ptr->sync_lock ) );
     167
     168    // release lock
     169    remote_spinlock_unlock( XPTR( sem_cxy , &sem_ptr->lock ) );
     170
     171    // release memory allocated for semaphore descriptor
     172    if( sem_cxy == local_cxy )                            // reference is local
     173    {
     174        kmem_req_t  req;
     175        req.type = KMEM_SEM;
     176        req.ptr  = sem_ptr;
     177        kmem_free( &req );
     178    }
     179    else                                                  // reference is remote
     180    {
     181        rpc_kcm_free_client( sem_cxy , sem_ptr , KMEM_SEM );
     182    }
     183
     184}  // end remote_sem_destroy()
     185
    129186//////////////////////////////////
    130187void remote_sem_wait( xptr_t sem_xp )
     
    153210
    154211        // register thread in waiting queue
    155         xptr_t root_xp   = (xptr_t)hal_remote_lwd( XPTR( sem_cxy , &sem_ptr->wait_queue ) );
     212        xptr_t root_xp   = (xptr_t)hal_remote_lwd( XPTR( sem_cxy , &sem_ptr->root ) );
    156213        xptr_t thread_xp = XPTR( local_cxy , this );
    157214                xlist_add_last( root_xp , thread_xp );
     
    177234 
    178235    // get remote pointer on waiting queue root
    179     xptr_t queue_xp = (xptr_t)hal_remote_lwd( XPTR( sem_cxy , &sem_ptr->wait_queue ) );
    180  
    181         if( xlist_is_empty( queue_xp ) )   // no waiting thread
     236    xptr_t root_xp = (xptr_t)hal_remote_lwd( XPTR( sem_cxy , &sem_ptr->root ) );
     237 
     238        if( xlist_is_empty( root_xp ) )   // no waiting thread
    182239    {
    183240        // get semaphore current value
     
    190247    {
    191248        // get first waiting thread from queue
    192         xptr_t thread_xp = XLIST_FIRST_ELEMENT( queue_xp , thread_t , wait_list );
     249        xptr_t thread_xp = XLIST_FIRST_ELEMENT( root_xp , thread_t , wait_list );
    193250
    194251        // get thread cluster and local poiner
     
    206263}  // end remote_sem_post()
    207264
    208 ////////////////////////////////////////
    209 void remote_sem_destroy( xptr_t sem_xp )
    210 {
    211     // get semaphore cluster and local pointer
    212     cxy_t          sem_cxy = GET_CXY( sem_xp );
    213     remote_sem_t * sem_ptr = (remote_sem_t *)GET_PTR( sem_xp );
    214 
    215     // get lock protecting semaphore
    216     remote_spinlock_lock( XPTR( sem_cxy , &sem_ptr->lock ) );
    217  
    218     // get remote pointer on waiting queue
    219     xptr_t queue_xp = (xptr_t)hal_remote_lwd( XPTR( sem_cxy , &sem_ptr->wait_queue ) );
    220  
    221     if( !xlist_is_empty( queue_xp ) )   // user error
    222     {
    223         printk("WARNING in %s for thread %x in process %x : "
    224                "destroy semaphore, but  waiting threads queue not empty\n",
    225                __FUNCTION__ , CURRENT_THREAD->trdid , CURRENT_PROCESS->pid );
    226     }
    227 
    228     // reset semaphore count
    229     hal_remote_sw( XPTR( sem_cxy , &sem_ptr->count ) , 0 );
    230 
    231     // remove semaphore from process
    232     xptr_t xp_list = (xptr_t)hal_remote_lwd( XPTR( sem_cxy , &sem_ptr->sem_list ) );
    233     xlist_unlink( xp_list );
    234 
    235     // release lock
    236     remote_spinlock_unlock( XPTR( sem_cxy , &sem_ptr->lock ) );
    237 
    238     // release memory allocated
    239     if( sem_cxy == local_cxy )         // reference is local
    240     {
    241         kmem_req_t  req;
    242         req.type = KMEM_SEM;
    243         req.ptr  = sem_ptr;
    244         kmem_free( &req );
    245     }
    246     else                                // reference is remote
    247     {
    248         rpc_semaphore_free_client( sem_cxy , sem_ptr );
    249     }
    250 
    251 }  // end remote_sem_destroy()
    252265
    253266//////////////////////////////////////////////
Note: See TracChangeset for help on using the changeset viewer.