[23] | 1 | /* |
---|
[563] | 2 | * remote_condvar.c - remote kernel condition variable implementation. |
---|
[23] | 3 | * |
---|
[563] | 4 | * Authors Alain Greiner (2016,2017,2018) |
---|
| 5 | * |
---|
[23] | 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 <thread.h> |
---|
[563] | 27 | #include <scheduler.h> |
---|
[23] | 28 | #include <xlist.h> |
---|
| 29 | #include <remote_mutex.h> |
---|
[563] | 30 | #include <remote_busylock.h> |
---|
[23] | 31 | #include <remote_condvar.h> |
---|
| 32 | |
---|
[563] | 33 | |
---|
[23] | 34 | /////////////////////////////////////////////////// |
---|
| 35 | xptr_t remote_condvar_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 ); |
---|
[563] | 45 | process_t * ref_ptr = GET_PTR( ref_xp ); |
---|
[23] | 46 | |
---|
[563] | 47 | // get extended pointer on condvars list |
---|
[23] | 48 | xptr_t root_xp = XPTR( ref_cxy , &ref_ptr->condvar_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 | |
---|
| 54 | // scan reference process condvar list |
---|
[23] | 55 | xptr_t iter_xp; |
---|
| 56 | xptr_t condvar_xp; |
---|
| 57 | cxy_t condvar_cxy; |
---|
[563] | 58 | remote_condvar_t * condvar_ptr; |
---|
[23] | 59 | intptr_t current; |
---|
| 60 | bool_t found = false; |
---|
| 61 | |
---|
| 62 | XLIST_FOREACH( root_xp , iter_xp ) |
---|
| 63 | { |
---|
| 64 | condvar_xp = XLIST_ELEMENT( iter_xp , remote_condvar_t , list ); |
---|
| 65 | condvar_cxy = GET_CXY( condvar_xp ); |
---|
[563] | 66 | condvar_ptr = GET_PTR( condvar_xp ); |
---|
| 67 | current = (intptr_t)hal_remote_lpt( XPTR( condvar_cxy , &condvar_ptr->ident ) ); |
---|
| 68 | |
---|
| 69 | if( current == ident ) |
---|
[23] | 70 | { |
---|
| 71 | found = true; |
---|
| 72 | break; |
---|
| 73 | } |
---|
| 74 | } |
---|
| 75 | |
---|
[563] | 76 | // relese lock protecting synchros lists |
---|
| 77 | remote_queuelock_release( lock_xp ); |
---|
| 78 | |
---|
[23] | 79 | if( found == false ) return XPTR_NULL; |
---|
| 80 | else return condvar_xp; |
---|
| 81 | |
---|
| 82 | } // end remote_condvar_from_ident() |
---|
| 83 | |
---|
[563] | 84 | ///////////////////////////////////////////////// |
---|
| 85 | error_t remote_condvar_create( intptr_t ident ) |
---|
[23] | 86 | { |
---|
[563] | 87 | remote_condvar_t * condvar_ptr; |
---|
[23] | 88 | xptr_t condvar_xp; |
---|
| 89 | |
---|
| 90 | // get pointer on local process descriptor |
---|
| 91 | process_t * process = CURRENT_THREAD->process; |
---|
| 92 | |
---|
| 93 | // get extended pointer on reference process |
---|
| 94 | xptr_t ref_xp = process->ref_xp; |
---|
| 95 | |
---|
| 96 | // get reference process cluster and local pointer |
---|
| 97 | cxy_t ref_cxy = GET_CXY( ref_xp ); |
---|
| 98 | process_t * ref_ptr = (process_t *)GET_PTR( ref_xp ); |
---|
| 99 | |
---|
[563] | 100 | // allocate memory for new condvar in reference cluster |
---|
| 101 | if( ref_cxy == local_cxy ) // local cluster is the reference |
---|
[23] | 102 | { |
---|
| 103 | kmem_req_t req; |
---|
[563] | 104 | req.type = KMEM_CONDVAR; |
---|
| 105 | req.flags = AF_ZERO; |
---|
| 106 | condvar_ptr = kmem_alloc( &req ); |
---|
| 107 | condvar_xp = XPTR( local_cxy , condvar_ptr ); |
---|
[23] | 108 | } |
---|
[563] | 109 | else // reference cluster is remote |
---|
[23] | 110 | { |
---|
| 111 | rpc_kcm_alloc_client( ref_cxy , KMEM_CONDVAR , &condvar_xp ); |
---|
[563] | 112 | condvar_ptr = GET_PTR( condvar_xp ); |
---|
[23] | 113 | } |
---|
| 114 | |
---|
[563] | 115 | if( condvar_xp == XPTR_NULL ) return 0xFFFFFFFF; |
---|
[23] | 116 | |
---|
| 117 | // initialise condvar |
---|
[563] | 118 | hal_remote_spt( XPTR( ref_cxy , &condvar_ptr->ident ) , (void *)ident ); |
---|
| 119 | xlist_root_init( XPTR( ref_cxy , &condvar_ptr->root ) ); |
---|
| 120 | xlist_entry_init( XPTR( ref_cxy , &condvar_ptr->list ) ); |
---|
| 121 | remote_busylock_init( XPTR( ref_cxy , &condvar_ptr->lock ), LOCK_CONDVAR_STATE ); |
---|
[23] | 122 | |
---|
[563] | 123 | // register condvar in reference process xlist |
---|
[23] | 124 | xptr_t root_xp = XPTR( ref_cxy , &ref_ptr->condvar_root ); |
---|
[563] | 125 | xptr_t list_xp = XPTR( ref_cxy , &condvar_ptr->list ); |
---|
[23] | 126 | |
---|
[563] | 127 | remote_queuelock_acquire( XPTR( ref_cxy , &ref_ptr->sync_lock ) ); |
---|
| 128 | xlist_add_first( root_xp , list_xp ); |
---|
| 129 | remote_queuelock_release( XPTR( ref_cxy , &ref_ptr->sync_lock ) ); |
---|
[23] | 130 | |
---|
| 131 | return 0; |
---|
| 132 | |
---|
| 133 | } // end remote_condvar_create() |
---|
| 134 | |
---|
| 135 | //////////////////////////////////////////////// |
---|
| 136 | void remote_condvar_destroy( xptr_t condvar_xp ) |
---|
| 137 | { |
---|
| 138 | // get pointer on local process descriptor |
---|
| 139 | process_t * process = CURRENT_THREAD->process; |
---|
| 140 | |
---|
| 141 | // get extended pointer on reference process |
---|
| 142 | xptr_t ref_xp = process->ref_xp; |
---|
| 143 | |
---|
| 144 | // get reference process cluster and local pointer |
---|
| 145 | cxy_t ref_cxy = GET_CXY( ref_xp ); |
---|
[563] | 146 | process_t * ref_ptr = GET_PTR( ref_xp ); |
---|
[23] | 147 | |
---|
| 148 | // get condvar cluster and local pointer |
---|
| 149 | cxy_t condvar_cxy = GET_CXY( condvar_xp ); |
---|
[563] | 150 | remote_condvar_t * condvar_ptr = GET_PTR( condvar_xp ); |
---|
[23] | 151 | |
---|
[563] | 152 | // get remote pointer on waiting queue root |
---|
| 153 | xptr_t root_xp = XPTR( condvar_cxy , &condvar_ptr->root ); |
---|
| 154 | |
---|
| 155 | if( !xlist_is_empty( root_xp ) ) // user error |
---|
| 156 | { |
---|
| 157 | printk("WARNING in %s for thread %x in process %x : " |
---|
| 158 | "destroy condvar, but waiting threads queue not empty\n", |
---|
| 159 | __FUNCTION__ , CURRENT_THREAD->trdid , CURRENT_THREAD->process->pid ); |
---|
| 160 | } |
---|
| 161 | |
---|
[23] | 162 | // remove condvar from reference process xlist |
---|
[563] | 163 | remote_queuelock_acquire( XPTR( ref_cxy , &ref_ptr->sync_lock ) ); |
---|
[23] | 164 | xlist_unlink( XPTR( condvar_cxy , &condvar_ptr->list ) ); |
---|
[563] | 165 | remote_queuelock_release( XPTR( ref_cxy , &ref_ptr->sync_lock ) ); |
---|
[23] | 166 | |
---|
[563] | 167 | // release memory allocated for condvar descriptor |
---|
[23] | 168 | if( condvar_cxy == local_cxy ) // reference is local |
---|
| 169 | { |
---|
| 170 | kmem_req_t req; |
---|
[563] | 171 | req.type = KMEM_SEM; |
---|
[23] | 172 | req.ptr = condvar_ptr; |
---|
| 173 | kmem_free( &req ); |
---|
| 174 | } |
---|
| 175 | else // reference is remote |
---|
| 176 | { |
---|
[563] | 177 | rpc_kcm_free_client( condvar_cxy , condvar_ptr , KMEM_CONDVAR ); |
---|
[23] | 178 | } |
---|
| 179 | |
---|
[563] | 180 | } // end remote_convar_destroy() |
---|
[23] | 181 | |
---|
| 182 | //////////////////////////////////////////// |
---|
| 183 | void remote_condvar_wait( xptr_t condvar_xp, |
---|
| 184 | xptr_t mutex_xp ) |
---|
| 185 | { |
---|
[563] | 186 | thread_t * this = CURRENT_THREAD; |
---|
[23] | 187 | |
---|
[563] | 188 | // check calling thread can yield |
---|
| 189 | assert( (this->busylocks == 0), |
---|
| 190 | "cannot yield : busylocks = %d\n", this->busylocks ); |
---|
[23] | 191 | |
---|
[563] | 192 | // get condvar cluster and local pointer |
---|
| 193 | remote_condvar_t * condvar_ptr = GET_PTR( condvar_xp ); |
---|
[23] | 194 | cxy_t condvar_cxy = GET_CXY( condvar_xp ); |
---|
| 195 | |
---|
[563] | 196 | // register the calling thread in condvar waiting queue |
---|
| 197 | xlist_add_last( XPTR( condvar_cxy , &condvar_ptr->root ), |
---|
| 198 | XPTR( local_cxy , &this->wait_xlist ) ); |
---|
[23] | 199 | |
---|
[563] | 200 | // block the calling thread |
---|
| 201 | thread_block( XPTR( local_cxy , this ) , THREAD_BLOCKED_USERSYNC ); |
---|
[23] | 202 | |
---|
[563] | 203 | // release the mutex |
---|
| 204 | remote_mutex_unlock( mutex_xp ); |
---|
[23] | 205 | |
---|
[563] | 206 | // deschedule |
---|
[408] | 207 | sched_yield("blocked on condvar"); |
---|
[23] | 208 | |
---|
[563] | 209 | // re-acquire the mutex |
---|
| 210 | remote_mutex_lock( mutex_xp ); |
---|
[23] | 211 | |
---|
| 212 | } // end remote_condvar_wait() |
---|
| 213 | |
---|
| 214 | /////////////////////////////////////////////// |
---|
| 215 | void remote_condvar_signal( xptr_t condvar_xp ) |
---|
| 216 | { |
---|
[563] | 217 | // get condvar cluster and local pointer |
---|
| 218 | remote_condvar_t * condvar_ptr = GET_PTR( condvar_xp ); |
---|
[23] | 219 | cxy_t condvar_cxy = GET_CXY( condvar_xp ); |
---|
| 220 | |
---|
[563] | 221 | // does nothing if waiting queue empty |
---|
| 222 | if( xlist_is_empty( XPTR( condvar_cxy, &condvar_ptr->root ) ) == false ) |
---|
| 223 | { |
---|
| 224 | // get first waiting thread |
---|
| 225 | xptr_t thread_xp = XLIST_FIRST( XPTR( condvar_cxy , &condvar_ptr->root ), |
---|
| 226 | thread_t , wait_xlist ); |
---|
[23] | 227 | |
---|
[563] | 228 | // remove this waiting thread from queue |
---|
| 229 | thread_t * thread_ptr = GET_PTR( thread_xp ); |
---|
| 230 | cxy_t thread_cxy = GET_CXY( thread_xp ); |
---|
| 231 | xlist_unlink( XPTR( thread_cxy , &thread_ptr->wait_xlist ) ); |
---|
[23] | 232 | |
---|
[563] | 233 | // unblock this waiting thread |
---|
| 234 | thread_unblock( thread_xp , THREAD_BLOCKED_USERSYNC ); |
---|
| 235 | } |
---|
[23] | 236 | |
---|
| 237 | } // end remote_condvar_signal() |
---|
| 238 | |
---|
| 239 | ////////////////////////////////////////////////// |
---|
| 240 | void remote_condvar_broadcast( xptr_t condvar_xp ) |
---|
| 241 | { |
---|
[563] | 242 | // get condvar cluster and local pointer |
---|
| 243 | remote_condvar_t * condvar_ptr = GET_PTR( condvar_xp ); |
---|
[23] | 244 | cxy_t condvar_cxy = GET_CXY( condvar_xp ); |
---|
| 245 | |
---|
[563] | 246 | // does nothing if waiting queue empty |
---|
| 247 | while( xlist_is_empty( XPTR( condvar_cxy , &condvar_ptr->root ) ) == false ) |
---|
| 248 | { |
---|
| 249 | // get first waiting thread |
---|
| 250 | xptr_t thread_xp = XLIST_FIRST( XPTR( condvar_cxy , &condvar_ptr->root ), |
---|
| 251 | thread_t , wait_xlist ); |
---|
[23] | 252 | |
---|
[563] | 253 | // remove this waiting thread from queue |
---|
| 254 | thread_t * thread_ptr = GET_PTR( thread_xp ); |
---|
| 255 | cxy_t thread_cxy = GET_CXY( thread_xp ); |
---|
| 256 | xlist_unlink( XPTR( thread_cxy , &thread_ptr->wait_xlist ) ); |
---|
[23] | 257 | |
---|
[563] | 258 | // unblock this waiting thread |
---|
| 259 | thread_unblock( thread_xp , THREAD_BLOCKED_USERSYNC ); |
---|
[23] | 260 | } |
---|
[563] | 261 | } // end remote_condvar_broadcast() |
---|
[23] | 262 | |
---|