[23] | 1 | /* |
---|
| 2 | * remote_condvar.c - distributed kernel condvar implementaion |
---|
| 3 | * |
---|
| 4 | * Author Alain Greiner (2016) |
---|
| 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 | |
---|
| 24 | #include <hal_types.h> |
---|
| 25 | #include <hal_remote.h> |
---|
| 26 | #include <hal_irqmask.h> |
---|
| 27 | #include <thread.h> |
---|
| 28 | #include <kmem.h> |
---|
| 29 | #include <printk.h> |
---|
| 30 | #include <process.h> |
---|
| 31 | #include <vmm.h> |
---|
| 32 | #include <xlist.h> |
---|
| 33 | #include <remote_mutex.h> |
---|
| 34 | #include <remote_condvar.h> |
---|
| 35 | |
---|
| 36 | /////////////////////////////////////////////////// |
---|
| 37 | xptr_t remote_condvar_from_ident( intptr_t ident ) |
---|
| 38 | { |
---|
| 39 | // get pointer on local process_descriptor |
---|
| 40 | process_t * process = CURRENT_THREAD->process; |
---|
| 41 | |
---|
| 42 | // get extended pointer on reference process |
---|
| 43 | xptr_t ref_xp = process->ref_xp; |
---|
| 44 | |
---|
| 45 | // get cluster and local pointer on reference process |
---|
| 46 | cxy_t ref_cxy = GET_CXY( ref_xp ); |
---|
| 47 | process_t * ref_ptr = (process_t *)GET_PTR( ref_xp ); |
---|
| 48 | |
---|
| 49 | // get extended pointer on root of condvars list |
---|
| 50 | xptr_t root_xp = XPTR( ref_cxy , &ref_ptr->condvar_root ); |
---|
| 51 | |
---|
| 52 | // scan reference process condvars list |
---|
| 53 | xptr_t iter_xp; |
---|
| 54 | xptr_t condvar_xp; |
---|
| 55 | cxy_t condvar_cxy; |
---|
| 56 | struct remote_condvar_s * condvar_ptr; |
---|
| 57 | intptr_t current; |
---|
| 58 | bool_t found = false; |
---|
| 59 | |
---|
| 60 | XLIST_FOREACH( root_xp , iter_xp ) |
---|
| 61 | { |
---|
| 62 | condvar_xp = XLIST_ELEMENT( iter_xp , remote_condvar_t , list ); |
---|
| 63 | condvar_cxy = GET_CXY( condvar_xp ); |
---|
| 64 | condvar_ptr = (remote_condvar_t *)GET_PTR( condvar_xp ); |
---|
| 65 | current = (intptr_t)hal_remote_lpt( XPTR( condvar_cxy , &condvar_ptr->ident ) ); |
---|
| 66 | if( ident == current ) |
---|
| 67 | { |
---|
| 68 | found = true; |
---|
| 69 | break; |
---|
| 70 | } |
---|
| 71 | } |
---|
| 72 | |
---|
| 73 | if( found == false ) return XPTR_NULL; |
---|
| 74 | else return condvar_xp; |
---|
| 75 | |
---|
| 76 | } // end remote_condvar_from_ident() |
---|
| 77 | |
---|
| 78 | /////////////////////////////////////////////// |
---|
| 79 | error_t remote_condvar_create( intptr_t ident ) |
---|
| 80 | { |
---|
| 81 | xptr_t condvar_xp; |
---|
| 82 | remote_condvar_t * condvar_ptr; |
---|
| 83 | |
---|
| 84 | // get pointer on local process descriptor |
---|
| 85 | process_t * process = CURRENT_THREAD->process; |
---|
| 86 | |
---|
| 87 | // get extended pointer on reference process |
---|
| 88 | xptr_t ref_xp = process->ref_xp; |
---|
| 89 | |
---|
| 90 | // get reference process cluster and local pointer |
---|
| 91 | cxy_t ref_cxy = GET_CXY( ref_xp ); |
---|
| 92 | process_t * ref_ptr = (process_t *)GET_PTR( ref_xp ); |
---|
| 93 | |
---|
| 94 | // allocate memory for condvar descriptor |
---|
| 95 | if( ref_cxy == local_cxy ) // local cluster is the reference |
---|
| 96 | { |
---|
| 97 | kmem_req_t req; |
---|
| 98 | req.type = KMEM_CONDVAR; |
---|
| 99 | req.flags = AF_ZERO; |
---|
| 100 | condvar_ptr = kmem_alloc( &req ); |
---|
| 101 | condvar_xp = XPTR( local_cxy , condvar_ptr ); |
---|
| 102 | } |
---|
| 103 | else // reference is remote |
---|
| 104 | { |
---|
| 105 | rpc_kcm_alloc_client( ref_cxy , KMEM_CONDVAR , &condvar_xp ); |
---|
| 106 | condvar_ptr = (remote_condvar_t *)GET_PTR( condvar_xp ); |
---|
| 107 | } |
---|
| 108 | |
---|
| 109 | if( condvar_ptr == NULL ) return ENOMEM; |
---|
| 110 | |
---|
| 111 | // initialise condvar |
---|
| 112 | hal_remote_spt( XPTR( ref_cxy , &condvar_ptr->ident ) , (void*)ident ); |
---|
| 113 | xlist_entry_init( XPTR( ref_cxy , &condvar_ptr->list ) ); |
---|
| 114 | xlist_root_init( XPTR( ref_cxy , &condvar_ptr->root ) ); |
---|
| 115 | |
---|
| 116 | // register condvar in reference process xlist |
---|
| 117 | xptr_t root_xp = XPTR( ref_cxy , &ref_ptr->condvar_root ); |
---|
| 118 | xptr_t xp_list = XPTR( ref_cxy , &condvar_ptr->list ); |
---|
| 119 | |
---|
| 120 | remote_spinlock_lock( XPTR( ref_cxy , &ref_ptr->sync_lock ) ); |
---|
| 121 | xlist_add_first( root_xp , xp_list ); |
---|
| 122 | remote_spinlock_unlock( XPTR( ref_cxy , &ref_ptr->sync_lock ) ); |
---|
| 123 | |
---|
| 124 | return 0; |
---|
| 125 | |
---|
| 126 | } // end remote_condvar_create() |
---|
| 127 | |
---|
| 128 | //////////////////////////////////////////////// |
---|
| 129 | void remote_condvar_destroy( xptr_t condvar_xp ) |
---|
| 130 | { |
---|
| 131 | // get pointer on local process descriptor |
---|
| 132 | process_t * process = CURRENT_THREAD->process; |
---|
| 133 | |
---|
| 134 | // get extended pointer on reference process |
---|
| 135 | xptr_t ref_xp = process->ref_xp; |
---|
| 136 | |
---|
| 137 | // get reference process cluster and local pointer |
---|
| 138 | cxy_t ref_cxy = GET_CXY( ref_xp ); |
---|
| 139 | process_t * ref_ptr = (process_t *)GET_PTR( ref_xp ); |
---|
| 140 | |
---|
| 141 | // get condvar cluster and local pointer |
---|
| 142 | cxy_t condvar_cxy = GET_CXY( condvar_xp ); |
---|
| 143 | remote_condvar_t * condvar_ptr = (remote_condvar_t *)GET_PTR( condvar_xp ); |
---|
| 144 | |
---|
| 145 | // remove condvar from reference process xlist |
---|
| 146 | remote_spinlock_lock( XPTR( ref_cxy , &ref_ptr->sync_lock ) ); |
---|
| 147 | xlist_unlink( XPTR( condvar_cxy , &condvar_ptr->list ) ); |
---|
| 148 | remote_spinlock_unlock( XPTR( ref_cxy , &ref_ptr->sync_lock ) ); |
---|
| 149 | |
---|
| 150 | // release memory allocated for condvaraphore descriptor |
---|
| 151 | if( condvar_cxy == local_cxy ) // reference is local |
---|
| 152 | { |
---|
| 153 | kmem_req_t req; |
---|
| 154 | req.type = KMEM_BARRIER; |
---|
| 155 | req.ptr = condvar_ptr; |
---|
| 156 | kmem_free( &req ); |
---|
| 157 | } |
---|
| 158 | else // reference is remote |
---|
| 159 | { |
---|
| 160 | rpc_kcm_free_client( condvar_cxy , condvar_ptr , KMEM_BARRIER ); |
---|
| 161 | } |
---|
| 162 | |
---|
| 163 | } // end remote_condvar_destroy() |
---|
| 164 | |
---|
| 165 | //////////////////////////////////////////// |
---|
| 166 | void remote_condvar_wait( xptr_t condvar_xp, |
---|
| 167 | xptr_t mutex_xp ) |
---|
| 168 | { |
---|
| 169 | // unlock the mutex |
---|
| 170 | remote_mutex_unlock( mutex_xp ); |
---|
| 171 | |
---|
| 172 | thread_t * this = CURRENT_THREAD; |
---|
| 173 | |
---|
| 174 | // get condvar cluster an local pointer |
---|
| 175 | remote_condvar_t * condvar_ptr = (remote_condvar_t *)GET_PTR( condvar_xp ); |
---|
| 176 | cxy_t condvar_cxy = GET_CXY( condvar_xp ); |
---|
| 177 | |
---|
| 178 | // get extended pointer on condvar waiting queue |
---|
| 179 | xptr_t root_xp = XPTR( condvar_cxy , &condvar_ptr->root ); |
---|
| 180 | |
---|
| 181 | // get extended pointer on calling thread xlist_entry |
---|
| 182 | xptr_t entry_xp = XPTR( local_cxy , &this->wait_list ); |
---|
| 183 | |
---|
| 184 | // register the calling thread in the condvar waiting queue |
---|
| 185 | remote_spinlock_lock( XPTR( condvar_cxy , &condvar_ptr->lock ) ); |
---|
| 186 | xlist_add_last( root_xp , entry_xp ); |
---|
| 187 | remote_spinlock_unlock( XPTR( condvar_cxy , &condvar_ptr->lock ) ); |
---|
| 188 | |
---|
| 189 | // block the calling thread |
---|
[436] | 190 | thread_block( XPTR( local_cxy , CURRENT_THREAD ) , THREAD_BLOCKED_USERSYNC ); |
---|
[408] | 191 | sched_yield("blocked on condvar"); |
---|
[23] | 192 | |
---|
| 193 | // lock the mutex before return |
---|
| 194 | remote_mutex_unlock( mutex_xp ); |
---|
| 195 | |
---|
| 196 | } // end remote_condvar_wait() |
---|
| 197 | |
---|
| 198 | /////////////////////////////////////////////// |
---|
| 199 | void remote_condvar_signal( xptr_t condvar_xp ) |
---|
| 200 | { |
---|
[60] | 201 | reg_t irq_state; |
---|
[23] | 202 | |
---|
| 203 | // get condvar cluster an local pointer |
---|
| 204 | remote_condvar_t * condvar_ptr = (remote_condvar_t *)GET_PTR( condvar_xp ); |
---|
| 205 | cxy_t condvar_cxy = GET_CXY( condvar_xp ); |
---|
| 206 | |
---|
| 207 | // get extended pointer on condvar waiting queue |
---|
| 208 | xptr_t root_xp = XPTR( condvar_cxy , &condvar_ptr->root ); |
---|
| 209 | |
---|
| 210 | if( xlist_is_empty( root_xp ) ) return; |
---|
| 211 | |
---|
| 212 | // disable interrupts |
---|
| 213 | hal_disable_irq( &irq_state ); |
---|
| 214 | |
---|
| 215 | // get extended pointer on the first waiting thread |
---|
| 216 | xptr_t thread_xp = XLIST_FIRST_ELEMENT( root_xp , thread_t , wait_list ); |
---|
| 217 | |
---|
| 218 | // remove the first waiting thread from queue |
---|
| 219 | remote_spinlock_lock( XPTR( condvar_cxy , &condvar_ptr->lock ) ); |
---|
| 220 | xlist_unlink( XPTR( condvar_cxy , &condvar_ptr->list ) ); |
---|
| 221 | remote_spinlock_unlock( XPTR( condvar_cxy , &condvar_ptr->lock ) ); |
---|
| 222 | |
---|
| 223 | // unblock first waiting thread |
---|
| 224 | thread_unblock( thread_xp , THREAD_BLOCKED_USERSYNC ); |
---|
| 225 | |
---|
| 226 | // restore interrupts |
---|
| 227 | hal_restore_irq( irq_state ); |
---|
| 228 | |
---|
| 229 | } // end remote_condvar_signal() |
---|
| 230 | |
---|
| 231 | ////////////////////////////////////////////////// |
---|
| 232 | void remote_condvar_broadcast( xptr_t condvar_xp ) |
---|
| 233 | { |
---|
[60] | 234 | reg_t irq_state; |
---|
[23] | 235 | |
---|
| 236 | // get condvar cluster an local pointer |
---|
| 237 | remote_condvar_t * condvar_ptr = (remote_condvar_t *)GET_PTR( condvar_xp ); |
---|
| 238 | cxy_t condvar_cxy = GET_CXY( condvar_xp ); |
---|
| 239 | |
---|
| 240 | // get extended pointer on condvar waiting queue |
---|
| 241 | xptr_t root_xp = XPTR( condvar_cxy , &condvar_ptr->root ); |
---|
| 242 | |
---|
| 243 | if( xlist_is_empty( root_xp ) ) return; |
---|
| 244 | |
---|
| 245 | // disable interrupts |
---|
| 246 | hal_disable_irq( &irq_state ); |
---|
| 247 | |
---|
| 248 | // loop on waiting threads |
---|
| 249 | xptr_t iter_xp; |
---|
| 250 | xptr_t thread_xp; |
---|
| 251 | XLIST_FOREACH( root_xp , iter_xp ) |
---|
| 252 | { |
---|
| 253 | // get extended pointer on waiting thread |
---|
| 254 | thread_xp = XLIST_ELEMENT( iter_xp , thread_t , wait_list ); |
---|
| 255 | |
---|
| 256 | // remove waiting thread from queue |
---|
| 257 | remote_spinlock_lock( XPTR( condvar_cxy , &condvar_ptr->lock ) ); |
---|
| 258 | xlist_unlink( XPTR( condvar_cxy , &condvar_ptr->list ) ); |
---|
| 259 | remote_spinlock_unlock( XPTR( condvar_cxy , &condvar_ptr->lock ) ); |
---|
| 260 | |
---|
| 261 | // unblock waiting thread |
---|
| 262 | thread_unblock( thread_xp , THREAD_BLOCKED_USERSYNC ); |
---|
| 263 | } |
---|
| 264 | |
---|
| 265 | // restore interrupts |
---|
| 266 | hal_restore_irq( irq_state ); |
---|
| 267 | |
---|
| 268 | } // end remote_condvar_broadcast() |
---|