[23] | 1 | /* |
---|
[563] | 2 | * remote_mutex.c - POSIX mutex implementation. |
---|
[23] | 3 | * |
---|
[563] | 4 | * Authors Alain Greiner (2016,2017,2018) |
---|
[23] | 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 | |
---|
[563] | 24 | #include <kernel_config.h> |
---|
[457] | 25 | #include <hal_kernel_types.h> |
---|
[23] | 26 | #include <hal_remote.h> |
---|
| 27 | #include <thread.h> |
---|
[563] | 28 | #include <xlist.h> |
---|
[23] | 29 | #include <scheduler.h> |
---|
[563] | 30 | #include <remote_busylock.h> |
---|
[23] | 31 | #include <remote_mutex.h> |
---|
| 32 | |
---|
[563] | 33 | |
---|
[23] | 34 | ///////////////////////////////////////////////// |
---|
| 35 | xptr_t remote_mutex_from_ident( intptr_t ident ) |
---|
| 36 | { |
---|
| 37 | // get pointer on local process_descriptor |
---|
| 38 | process_t * process = CURRENT_THREAD->process; |
---|
| 39 | |
---|
| 40 | // get extended pointer on reference process |
---|
| 41 | xptr_t ref_xp = process->ref_xp; |
---|
| 42 | |
---|
| 43 | // get cluster and local pointer on reference process |
---|
| 44 | cxy_t ref_cxy = GET_CXY( ref_xp ); |
---|
| 45 | process_t * ref_ptr = (process_t *)GET_PTR( ref_xp ); |
---|
| 46 | |
---|
[563] | 47 | // get extended pointers on mutexes list |
---|
[23] | 48 | xptr_t root_xp = XPTR( ref_cxy , &ref_ptr->mutex_root ); |
---|
[563] | 49 | xptr_t lock_xp = XPTR( ref_cxy , &ref_ptr->sync_lock ); |
---|
| 50 | |
---|
| 51 | // get lock protecting synchro lists |
---|
| 52 | remote_queuelock_acquire( lock_xp ); |
---|
| 53 | |
---|
[23] | 54 | // scan reference process mutex list |
---|
| 55 | xptr_t iter_xp; |
---|
| 56 | xptr_t mutex_xp; |
---|
| 57 | cxy_t mutex_cxy; |
---|
| 58 | remote_mutex_t * mutex_ptr; |
---|
| 59 | intptr_t current; |
---|
| 60 | bool_t found = false; |
---|
| 61 | |
---|
| 62 | XLIST_FOREACH( root_xp , iter_xp ) |
---|
| 63 | { |
---|
| 64 | mutex_xp = XLIST_ELEMENT( iter_xp , remote_mutex_t , list ); |
---|
| 65 | mutex_cxy = GET_CXY( mutex_xp ); |
---|
| 66 | mutex_ptr = (remote_mutex_t *)GET_PTR( mutex_xp ); |
---|
| 67 | current = (intptr_t)hal_remote_lpt( XPTR( mutex_cxy , &mutex_ptr->ident ) ); |
---|
| 68 | if( ident == current ) |
---|
| 69 | { |
---|
| 70 | found = true; |
---|
| 71 | break; |
---|
| 72 | } |
---|
| 73 | } |
---|
| 74 | |
---|
[563] | 75 | // relese lock protecting synchros lists |
---|
| 76 | remote_queuelock_release( lock_xp ); |
---|
| 77 | |
---|
[23] | 78 | if( found == false ) return XPTR_NULL; |
---|
| 79 | else return mutex_xp; |
---|
| 80 | |
---|
| 81 | } // end remote_mutex_from_ident() |
---|
| 82 | |
---|
| 83 | ///////////////////////////////////////////// |
---|
| 84 | error_t remote_mutex_create( intptr_t ident ) |
---|
| 85 | { |
---|
| 86 | xptr_t mutex_xp; |
---|
| 87 | remote_mutex_t * mutex_ptr; |
---|
| 88 | |
---|
| 89 | // get pointer on local process descriptor |
---|
| 90 | process_t * process = CURRENT_THREAD->process; |
---|
| 91 | |
---|
| 92 | // get extended pointer on reference process |
---|
| 93 | xptr_t ref_xp = process->ref_xp; |
---|
| 94 | |
---|
| 95 | // get reference process cluster and local pointer |
---|
| 96 | cxy_t ref_cxy = GET_CXY( ref_xp ); |
---|
| 97 | process_t * ref_ptr = (process_t *)GET_PTR( ref_xp ); |
---|
| 98 | |
---|
[563] | 99 | // allocate memory for mutex descriptor |
---|
[23] | 100 | if( ref_cxy == local_cxy ) // local cluster is the reference |
---|
| 101 | { |
---|
| 102 | kmem_req_t req; |
---|
| 103 | req.type = KMEM_MUTEX; |
---|
| 104 | req.flags = AF_ZERO; |
---|
| 105 | mutex_ptr = kmem_alloc( &req ); |
---|
| 106 | mutex_xp = XPTR( local_cxy , mutex_ptr ); |
---|
| 107 | } |
---|
| 108 | else // reference is remote |
---|
| 109 | { |
---|
| 110 | rpc_kcm_alloc_client( ref_cxy , KMEM_MUTEX , &mutex_xp ); |
---|
[563] | 111 | mutex_ptr = GET_PTR( mutex_xp ); |
---|
[23] | 112 | } |
---|
| 113 | |
---|
[563] | 114 | if( mutex_ptr == NULL ) return 0xFFFFFFFF; |
---|
[23] | 115 | |
---|
| 116 | // initialise mutex |
---|
[563] | 117 | hal_remote_s32 ( XPTR( ref_cxy , &mutex_ptr->taken ) , 0 ); |
---|
[23] | 118 | hal_remote_spt( XPTR( ref_cxy , &mutex_ptr->ident ) , (void *)ident ); |
---|
| 119 | xlist_entry_init( XPTR( ref_cxy , &mutex_ptr->list ) ); |
---|
| 120 | xlist_root_init( XPTR( ref_cxy , &mutex_ptr->root ) ); |
---|
[563] | 121 | hal_remote_s64( XPTR( ref_cxy , &mutex_ptr->owner ) , XPTR_NULL ); |
---|
| 122 | remote_busylock_init( XPTR( ref_cxy , &mutex_ptr->lock ), LOCK_MUTEX_STATE ); |
---|
[23] | 123 | |
---|
[563] | 124 | // get root of mutexes list in process, and list_entry in mutex |
---|
[23] | 125 | xptr_t root_xp = XPTR( ref_cxy , &ref_ptr->mutex_root ); |
---|
| 126 | xptr_t xp_list = XPTR( ref_cxy , &mutex_ptr->list ); |
---|
| 127 | |
---|
[563] | 128 | // get lock protecting user synchros lists |
---|
| 129 | remote_queuelock_acquire( XPTR( ref_cxy , &ref_ptr->sync_lock ) ); |
---|
| 130 | |
---|
| 131 | // register mutex in process descriptor |
---|
[23] | 132 | xlist_add_first( root_xp , xp_list ); |
---|
| 133 | |
---|
[563] | 134 | // release lock protecting user synchros lists |
---|
| 135 | remote_queuelock_release( XPTR( ref_cxy , &ref_ptr->sync_lock ) ); |
---|
| 136 | |
---|
| 137 | #if DEBUG_MUTEX |
---|
| 138 | thread_t * this = CURRENT_THREAD; |
---|
| 139 | if( (uint32_t)hal_get_cycles() > DEBUG_QUEUELOCK ) |
---|
| 140 | printk("\n[DBG] %s : thread %x in %x process / mutex(%x,%x)\n", |
---|
| 141 | __FUNCTION__, this->trdid, this->process->pid, local_cxy, mutex_ptr ); |
---|
| 142 | #endif |
---|
| 143 | |
---|
| 144 | |
---|
[23] | 145 | return 0; |
---|
| 146 | |
---|
| 147 | } // end remote_mutex_create() |
---|
| 148 | |
---|
| 149 | //////////////////////////////////////////// |
---|
| 150 | void remote_mutex_destroy( xptr_t mutex_xp ) |
---|
| 151 | { |
---|
| 152 | // get pointer on local process descriptor |
---|
| 153 | process_t * process = CURRENT_THREAD->process; |
---|
| 154 | |
---|
| 155 | // get extended pointer on reference process |
---|
| 156 | xptr_t ref_xp = process->ref_xp; |
---|
| 157 | |
---|
| 158 | // get reference process cluster and local pointer |
---|
| 159 | cxy_t ref_cxy = GET_CXY( ref_xp ); |
---|
| 160 | process_t * ref_ptr = (process_t *)GET_PTR( ref_xp ); |
---|
| 161 | |
---|
| 162 | // get mutex cluster and local pointer |
---|
| 163 | cxy_t mutex_cxy = GET_CXY( mutex_xp ); |
---|
| 164 | remote_mutex_t * mutex_ptr = (remote_mutex_t *)GET_PTR( mutex_xp ); |
---|
| 165 | |
---|
[563] | 166 | // get lock protecting user synchros lists |
---|
| 167 | remote_queuelock_acquire( XPTR( ref_cxy , &ref_ptr->sync_lock ) ); |
---|
| 168 | |
---|
[23] | 169 | // remove mutex from reference process xlist |
---|
| 170 | xlist_unlink( XPTR( mutex_cxy , &mutex_ptr->list ) ); |
---|
| 171 | |
---|
[563] | 172 | // release lock protecting user synchros lists |
---|
| 173 | remote_queuelock_release( XPTR( ref_cxy , &ref_ptr->sync_lock ) ); |
---|
| 174 | |
---|
[23] | 175 | // release memory allocated for mutexaphore descriptor |
---|
| 176 | if( mutex_cxy == local_cxy ) // reference is local |
---|
| 177 | { |
---|
| 178 | kmem_req_t req; |
---|
| 179 | req.type = KMEM_MUTEX; |
---|
| 180 | req.ptr = mutex_ptr; |
---|
| 181 | kmem_free( &req ); |
---|
| 182 | } |
---|
| 183 | else // reference is remote |
---|
| 184 | { |
---|
| 185 | rpc_kcm_free_client( mutex_cxy , mutex_ptr , KMEM_BARRIER ); |
---|
| 186 | } |
---|
| 187 | |
---|
| 188 | } // end remote_mutex_destroy() |
---|
| 189 | |
---|
| 190 | ///////////////////////////////////////// |
---|
| 191 | void remote_mutex_lock( xptr_t mutex_xp ) |
---|
| 192 | { |
---|
[563] | 193 | // get cluster and local pointer on mutex |
---|
| 194 | remote_mutex_t * mutex_ptr = GET_PTR( mutex_xp ); |
---|
[23] | 195 | cxy_t mutex_cxy = GET_CXY( mutex_xp ); |
---|
| 196 | |
---|
[563] | 197 | // get extended pointers on mutex fields |
---|
| 198 | xptr_t taken_xp = XPTR( mutex_cxy , &mutex_ptr->taken ); |
---|
| 199 | xptr_t owner_xp = XPTR( mutex_cxy , &mutex_ptr->owner ); |
---|
| 200 | xptr_t root_xp = XPTR( mutex_cxy , &mutex_ptr->root ); |
---|
| 201 | xptr_t lock_xp = XPTR( mutex_cxy , &mutex_ptr->lock ); |
---|
[23] | 202 | |
---|
[563] | 203 | // get cluster and pointers on calling thread |
---|
| 204 | cxy_t caller_cxy = local_cxy; |
---|
| 205 | thread_t * caller_ptr = CURRENT_THREAD; |
---|
| 206 | xptr_t caller_xp = XPTR( caller_cxy , caller_ptr ); |
---|
[23] | 207 | |
---|
[563] | 208 | // check calling thread can yield |
---|
| 209 | assert( (caller_ptr->busylocks == 0), |
---|
| 210 | "cannot yield : busylocks = %d\n", caller_ptr->busylocks ); |
---|
[23] | 211 | |
---|
[563] | 212 | while( 1 ) |
---|
[23] | 213 | { |
---|
[563] | 214 | // get busylock protecting mutex state |
---|
| 215 | remote_busylock_acquire( lock_xp ); |
---|
[23] | 216 | |
---|
[563] | 217 | // test mutex state |
---|
| 218 | if( hal_remote_l32( taken_xp ) == 0 ) // success |
---|
| 219 | { |
---|
| 220 | // register calling thread as mutex owner |
---|
| 221 | hal_remote_s64( owner_xp , caller_xp ); |
---|
[23] | 222 | |
---|
[563] | 223 | // update mutex state |
---|
| 224 | hal_remote_s32( taken_xp , 1 ); |
---|
[23] | 225 | |
---|
[563] | 226 | #if DEBUG_MUTEX |
---|
| 227 | thread_t * this = CURRENT_THREAD; |
---|
| 228 | if( (uint32_t)hal_get_cycles() > DEBUG_MUTEX ) |
---|
| 229 | printk("\n[DBG] %s : thread %x in process %x SUCCESS on mutex(%x,%x)\n", |
---|
| 230 | __FUNCTION__, this->trdid, this->process->pid, mutex_cxy, mutex_ptr ); |
---|
| 231 | #endif |
---|
[23] | 232 | |
---|
[563] | 233 | // release busylock protecting mutex state |
---|
| 234 | remote_busylock_release( lock_xp ); |
---|
[23] | 235 | |
---|
[563] | 236 | return; |
---|
| 237 | } |
---|
| 238 | else // already taken |
---|
| 239 | { |
---|
| 240 | // block the calling thread |
---|
| 241 | thread_block( caller_xp , THREAD_BLOCKED_USERSYNC ); |
---|
[23] | 242 | |
---|
[563] | 243 | // register calling thread in mutex waiting queue |
---|
| 244 | xptr_t entry_xp = XPTR( caller_cxy , &caller_ptr->wait_xlist ); |
---|
| 245 | xlist_add_last( root_xp , entry_xp ); |
---|
| 246 | |
---|
| 247 | #if DEBUG_MUTEX |
---|
| 248 | thread_t * this = CURRENT_THREAD; |
---|
| 249 | if( (uint32_t)hal_get_cycles() > DEBUG_MUTEX ) |
---|
| 250 | printk("\n[DBG] %s : thread %x in process %x BLOCKED on mutex(%x,%x)\n", |
---|
| 251 | __FUNCTION__, this->trdid, this->process->pid, mutex_cxy, mutex_ptr ); |
---|
| 252 | #endif |
---|
| 253 | |
---|
| 254 | // release busylock protecting mutex state |
---|
| 255 | remote_busylock_release( lock_xp ); |
---|
| 256 | |
---|
| 257 | // deschedule calling thread |
---|
| 258 | sched_yield("blocked on mutex"); |
---|
| 259 | } |
---|
| 260 | } |
---|
[23] | 261 | } // end remote_mutex_lock() |
---|
| 262 | |
---|
[563] | 263 | ////////////////////////////////////////////// |
---|
| 264 | error_t remote_mutex_unlock( xptr_t mutex_xp ) |
---|
[23] | 265 | { |
---|
[563] | 266 | // memory barrier before mutex release |
---|
| 267 | hal_fence(); |
---|
[23] | 268 | |
---|
[563] | 269 | // get cluster and local pointer on mutex |
---|
| 270 | remote_mutex_t * mutex_ptr = GET_PTR( mutex_xp ); |
---|
[23] | 271 | cxy_t mutex_cxy = GET_CXY( mutex_xp ); |
---|
| 272 | |
---|
[563] | 273 | // get cluster and pointers on calling thread |
---|
| 274 | cxy_t caller_cxy = local_cxy; |
---|
| 275 | thread_t * caller_ptr = CURRENT_THREAD; |
---|
| 276 | xptr_t caller_xp = XPTR( caller_cxy , caller_ptr ); |
---|
[23] | 277 | |
---|
[563] | 278 | // get extended pointers on mutex fields |
---|
| 279 | xptr_t taken_xp = XPTR( mutex_cxy , &mutex_ptr->taken ); |
---|
[23] | 280 | xptr_t owner_xp = XPTR( mutex_cxy , &mutex_ptr->owner ); |
---|
| 281 | xptr_t root_xp = XPTR( mutex_cxy , &mutex_ptr->root ); |
---|
[563] | 282 | xptr_t lock_xp = XPTR( mutex_cxy , &mutex_ptr->lock ); |
---|
| 283 | |
---|
| 284 | // get busylock protecting mutex state |
---|
| 285 | remote_busylock_acquire( lock_xp ); |
---|
[23] | 286 | |
---|
[563] | 287 | // check calling thread is mutex owner |
---|
| 288 | if( hal_remote_l64( owner_xp ) != caller_xp ) |
---|
| 289 | { |
---|
| 290 | // release busylock protecting mutex state |
---|
| 291 | remote_busylock_release( lock_xp ); |
---|
[23] | 292 | |
---|
[563] | 293 | return 0xFFFFFFFF; |
---|
| 294 | } |
---|
[23] | 295 | |
---|
[563] | 296 | #if DEBUG_MUTEX |
---|
| 297 | thread_t * this = CURRENT_THREAD; |
---|
| 298 | if( (uint32_t)hal_get_cycles() > DEBUG_MUTEX ) |
---|
| 299 | printk("\n[DBG] %s : thread %x in %x process EXIT / mutex(%x,%x)\n", |
---|
| 300 | __FUNCTION__, this->trdid, this->process->pid, mutex_cxy, mutex_ptr ); |
---|
| 301 | #endif |
---|
| 302 | |
---|
| 303 | // update owner field, |
---|
| 304 | hal_remote_s64( owner_xp , XPTR_NULL ); |
---|
| 305 | |
---|
| 306 | // update taken field |
---|
| 307 | hal_remote_s32( taken_xp , 0 ); |
---|
| 308 | |
---|
| 309 | // unblock first waiting thread if waiting list non empty |
---|
| 310 | if( xlist_is_empty( root_xp ) == false ) |
---|
[23] | 311 | { |
---|
| 312 | // get extended pointer on first waiting thread |
---|
[563] | 313 | xptr_t thread_xp = XLIST_FIRST( root_xp , thread_t , wait_xlist ); |
---|
| 314 | thread_t * thread_ptr = GET_PTR( thread_xp ); |
---|
| 315 | cxy_t thread_cxy = GET_CXY( thread_xp ); |
---|
[23] | 316 | |
---|
[563] | 317 | #if DEBUG_MUTEX |
---|
| 318 | if( (uint32_t)hal_get_cycles() > DEBUG_MUTEX ) |
---|
| 319 | { |
---|
| 320 | trdid_t trdid = hal_remote_l32( XPTR( thread_cxy , &thread_ptr->trdid ) ); |
---|
| 321 | process_t * process = hal_remote_lpt( XPTR( thread_cxy , &thread_ptr->process ) ); |
---|
| 322 | pid_t pid = hal_remote_l32( XPTR( thread_cxy , &process->pid ) ); |
---|
| 323 | printk("\n[DBG] %s : thread %x in process %x UNBLOCK thread %x in process %d / mutex(%x,%x)\n", |
---|
| 324 | __FUNCTION__, this->trdid, this->process->pid, trdid, pid, mutex_cxy, mutex_ptr ); |
---|
| 325 | } |
---|
| 326 | #endif |
---|
[23] | 327 | |
---|
[563] | 328 | // remove this thread from waiting queue |
---|
| 329 | xlist_unlink( XPTR( thread_cxy , &thread_ptr->wait_xlist ) ); |
---|
| 330 | |
---|
[23] | 331 | // unblock first waiting thread |
---|
| 332 | thread_unblock( thread_xp , THREAD_BLOCKED_USERSYNC ); |
---|
| 333 | } |
---|
[563] | 334 | |
---|
| 335 | // release busylock protecting mutex state |
---|
| 336 | remote_busylock_release( lock_xp ); |
---|
| 337 | |
---|
| 338 | return 0; |
---|
| 339 | |
---|
| 340 | } // end remote_mutex_unlock() |
---|
| 341 | |
---|
| 342 | /////////////////////////////////////////////// |
---|
| 343 | error_t remote_mutex_trylock( xptr_t mutex_xp ) |
---|
| 344 | { |
---|
| 345 | // get cluster and local pointer on mutex |
---|
| 346 | remote_mutex_t * mutex_ptr = GET_PTR( mutex_xp ); |
---|
| 347 | cxy_t mutex_cxy = GET_CXY( mutex_xp ); |
---|
| 348 | |
---|
| 349 | // get cluster and pointers on calling thread |
---|
| 350 | cxy_t caller_cxy = local_cxy; |
---|
| 351 | thread_t * caller_ptr = CURRENT_THREAD; |
---|
| 352 | xptr_t caller_xp = XPTR( caller_cxy , caller_ptr ); |
---|
| 353 | |
---|
| 354 | // get extended pointers on mutex fields |
---|
| 355 | xptr_t taken_xp = XPTR( mutex_cxy , &mutex_ptr->taken ); |
---|
| 356 | xptr_t owner_xp = XPTR( mutex_cxy , &mutex_ptr->owner ); |
---|
| 357 | xptr_t lock_xp = XPTR( mutex_cxy , &mutex_ptr->lock ); |
---|
| 358 | |
---|
| 359 | // get busylock protecting mutex state |
---|
| 360 | remote_busylock_acquire( lock_xp ); |
---|
| 361 | |
---|
| 362 | // test mutex state |
---|
| 363 | if( hal_remote_l32( taken_xp ) == 0 ) // success |
---|
[23] | 364 | { |
---|
[563] | 365 | // register calling thread as mutex owner |
---|
| 366 | hal_remote_s64( owner_xp , caller_xp ); |
---|
| 367 | |
---|
| 368 | // update mutex state |
---|
| 369 | hal_remote_s32( taken_xp , 1 ); |
---|
| 370 | |
---|
| 371 | #if DEBUG_MUTEX |
---|
| 372 | thread_t * this = CURRENT_THREAD; |
---|
| 373 | if( (uint32_t)hal_get_cycles() > DEBUG_QUEUELOCK ) |
---|
| 374 | printk("\n[DBG] %s : SUCCESS for thread %x in process %x / mutex(%x,%x)\n", |
---|
| 375 | __FUNCTION__, this->trdid, this->process->pid, mutex_cxy, mutex_ptr ); |
---|
| 376 | #endif |
---|
| 377 | // release busylock protecting mutex state |
---|
| 378 | remote_busylock_release( lock_xp ); |
---|
| 379 | |
---|
| 380 | return 0; |
---|
[23] | 381 | } |
---|
[563] | 382 | else // already taken |
---|
| 383 | { |
---|
[23] | 384 | |
---|
[563] | 385 | #if DEBUG_MUTEX |
---|
| 386 | thread_t * this = CURRENT_THREAD; |
---|
| 387 | if( (uint32_t)hal_get_cycles() > DEBUG_QUEUELOCK ) |
---|
| 388 | printk("\n[DBG] %s : FAILURE for thread %x in process %x / mutex(%x,%x)\n", |
---|
| 389 | __FUNCTION__, this->trdid, this->process->pid, mutex_cxy, mutex_ptr ); |
---|
| 390 | #endif |
---|
| 391 | // release busylock protecting mutex state |
---|
| 392 | remote_busylock_release( lock_xp ); |
---|
[23] | 393 | |
---|
[563] | 394 | return 0xFFFFFFFF; |
---|
| 395 | } |
---|
| 396 | } // end remote_mutex_trylock() |
---|