Changeset 23 for trunk/kernel/libk/remote_sem.c
- Timestamp:
- Jun 18, 2017, 10:06:41 PM (7 years ago)
- File:
-
- 1 moved
Legend:
- Unmodified
- Added
- Removed
-
trunk/kernel/libk/remote_sem.c
r15 r23 23 23 24 24 #include <hal_types.h> 25 #include <hal_remote.h> 25 26 #include <thread.h> 26 27 #include <kmem.h> … … 35 36 { 36 37 // get pointer on local process_descriptor 37 process_t * process = CURRENT_ PROCESS;38 process_t * process = CURRENT_THREAD->process; 38 39 39 40 // get extended pointer on reference process … … 57 58 XLIST_FOREACH( root_xp , iter_xp ) 58 59 { 59 sem_xp = XLIST_ELEMENT( iter_xp , remote_sem_t , sem_list );60 sem_xp = XLIST_ELEMENT( iter_xp , remote_sem_t , list ); 60 61 sem_cxy = GET_CXY( sem_xp ); 61 62 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 ) ); 63 64 if( ident == vaddr ) 64 65 { … … 73 74 } // end remote_sem_from_vaddr() 74 75 75 ///////////////////////////////////////// 76 error_t remote_sem_ init( intptr_t vaddr,77 uint32_t value )76 /////////////////////////////////////////// 77 error_t remote_sem_create( intptr_t vaddr, 78 uint32_t value ) 78 79 { 79 80 xptr_t sem_xp; … … 81 82 82 83 // get pointer on local process descriptor 83 process_t * process = CURRENT_ PROCESS;84 process_t * process = CURRENT_THREAD->process; 84 85 85 86 // get extended pointer on reference process 86 87 xptr_t ref_xp = process->ref_xp; 87 88 89 // get reference process cluster and local pointer 88 90 cxy_t ref_cxy = GET_CXY( ref_xp ); 89 91 process_t * ref_ptr = (process_t *)GET_PTR( ref_xp ); … … 100 102 else // reference is remote 101 103 { 102 rpc_ semaphore_alloc_client( ref_cxy, &sem_xp );104 rpc_kcm_alloc_client( ref_cxy , KMEM_SEM , &sem_xp ); 103 105 sem_ptr = (remote_sem_t *)GET_PTR( sem_xp ); 104 106 } … … 106 108 if( sem_xp == XPTR_NULL ) return ENOMEM; 107 109 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 109 114 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 121 119 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 ) ); 123 123 xlist_add_first( root_xp , xp_list ); 124 remote_spinlock_unlock( XPTR( ref_cxy , &ref_ptr->sync_lock ) ); 124 125 125 126 return 0; … … 127 128 } // en remote_sem_init() 128 129 130 //////////////////////////////////////// 131 void 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 129 186 ////////////////////////////////// 130 187 void remote_sem_wait( xptr_t sem_xp ) … … 153 210 154 211 // 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 ) ); 156 213 xptr_t thread_xp = XPTR( local_cxy , this ); 157 214 xlist_add_last( root_xp , thread_xp ); … … 177 234 178 235 // 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 thread236 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 182 239 { 183 240 // get semaphore current value … … 190 247 { 191 248 // 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 ); 193 250 194 251 // get thread cluster and local poiner … … 206 263 } // end remote_sem_post() 207 264 208 ////////////////////////////////////////209 void remote_sem_destroy( xptr_t sem_xp )210 {211 // get semaphore cluster and local pointer212 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 semaphore216 remote_spinlock_lock( XPTR( sem_cxy , &sem_ptr->lock ) );217 218 // get remote pointer on waiting queue219 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 error222 {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 count229 hal_remote_sw( XPTR( sem_cxy , &sem_ptr->count ) , 0 );230 231 // remove semaphore from process232 xptr_t xp_list = (xptr_t)hal_remote_lwd( XPTR( sem_cxy , &sem_ptr->sem_list ) );233 xlist_unlink( xp_list );234 235 // release lock236 remote_spinlock_unlock( XPTR( sem_cxy , &sem_ptr->lock ) );237 238 // release memory allocated239 if( sem_cxy == local_cxy ) // reference is local240 {241 kmem_req_t req;242 req.type = KMEM_SEM;243 req.ptr = sem_ptr;244 kmem_free( &req );245 }246 else // reference is remote247 {248 rpc_semaphore_free_client( sem_cxy , sem_ptr );249 }250 251 } // end remote_sem_destroy()252 265 253 266 //////////////////////////////////////////////
Note: See TracChangeset
for help on using the changeset viewer.