[1] | 1 | /* |
---|
[23] | 2 | * remote_barrier.c - Access a POSIX barrier. |
---|
[104] | 3 | * |
---|
[23] | 4 | * Author Alain Greiner (2016,2017) |
---|
[1] | 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> |
---|
[23] | 26 | #include <hal_irqmask.h> |
---|
| 27 | #include <remote_spinlock.h> |
---|
| 28 | #include <thread.h> |
---|
| 29 | #include <kmem.h> |
---|
| 30 | #include <printk.h> |
---|
| 31 | #include <process.h> |
---|
| 32 | #include <vmm.h> |
---|
[1] | 33 | #include <remote_barrier.h> |
---|
| 34 | |
---|
[23] | 35 | ///////////////////////////////////////////////// |
---|
| 36 | inline void remote_barrier( xptr_t barrier_xp, |
---|
[104] | 37 | uint32_t count ) |
---|
[1] | 38 | { |
---|
| 39 | uint32_t expected; |
---|
| 40 | |
---|
[23] | 41 | remote_barrier_t * ptr = (remote_barrier_t *)GET_PTR( barrier_xp ); |
---|
| 42 | cxy_t cxy = GET_CXY( barrier_xp ); |
---|
[1] | 43 | |
---|
| 44 | // get barrier sense value |
---|
| 45 | uint32_t sense = hal_remote_lw( XPTR( cxy , &ptr->sense ) ); |
---|
| 46 | |
---|
[104] | 47 | // compute expected value |
---|
[1] | 48 | if ( sense == 0 ) expected = 1; |
---|
| 49 | else expected = 0; |
---|
| 50 | |
---|
[104] | 51 | // atomically increment current |
---|
[1] | 52 | uint32_t current = hal_remote_atomic_add( XPTR( cxy , &ptr->current ) , 1 ); |
---|
| 53 | |
---|
| 54 | // last task reset current and toggle sense |
---|
[104] | 55 | if( current == (count-1) ) |
---|
[1] | 56 | { |
---|
[104] | 57 | hal_remote_sw( XPTR( cxy , &ptr->current) , 0 ); |
---|
| 58 | hal_remote_sw( XPTR( cxy , &ptr->sense ) , expected ); |
---|
[1] | 59 | } |
---|
[104] | 60 | else // other tasks poll the sense |
---|
[1] | 61 | { |
---|
| 62 | while( hal_remote_lw( XPTR( cxy , &ptr->sense ) ) != expected ) asm volatile ("nop"); |
---|
| 63 | } |
---|
| 64 | } |
---|
| 65 | |
---|
[23] | 66 | /////////////////////////////////////////////////// |
---|
| 67 | xptr_t remote_barrier_from_ident( intptr_t ident ) |
---|
| 68 | { |
---|
| 69 | // get pointer on local process_descriptor |
---|
| 70 | process_t * process = CURRENT_THREAD->process; |
---|
[1] | 71 | |
---|
[23] | 72 | // get extended pointer on reference process |
---|
| 73 | xptr_t ref_xp = process->ref_xp; |
---|
| 74 | |
---|
[104] | 75 | // get cluster and local pointer on reference process |
---|
[23] | 76 | cxy_t ref_cxy = GET_CXY( ref_xp ); |
---|
| 77 | process_t * ref_ptr = (process_t *)GET_PTR( ref_xp ); |
---|
| 78 | |
---|
[104] | 79 | // get extended pointer on root of barriers list |
---|
[23] | 80 | xptr_t root_xp = XPTR( ref_cxy , &ref_ptr->barrier_root ); |
---|
[104] | 81 | |
---|
[23] | 82 | // scan reference process barriers list |
---|
| 83 | xptr_t iter_xp; |
---|
| 84 | xptr_t barrier_xp; |
---|
| 85 | cxy_t barrier_cxy; |
---|
| 86 | remote_barrier_t * barrier_ptr; |
---|
| 87 | intptr_t current; |
---|
| 88 | bool_t found = false; |
---|
[104] | 89 | |
---|
[23] | 90 | XLIST_FOREACH( root_xp , iter_xp ) |
---|
| 91 | { |
---|
| 92 | barrier_xp = XLIST_ELEMENT( iter_xp , remote_barrier_t , list ); |
---|
| 93 | barrier_cxy = GET_CXY( barrier_xp ); |
---|
| 94 | barrier_ptr = (remote_barrier_t *)GET_PTR( barrier_xp ); |
---|
[104] | 95 | current = (intptr_t)hal_remote_lpt( XPTR( barrier_cxy , &barrier_ptr->ident ) ); |
---|
[23] | 96 | if( ident == current ) |
---|
| 97 | { |
---|
| 98 | found = true; |
---|
| 99 | break; |
---|
| 100 | } |
---|
| 101 | } |
---|
| 102 | |
---|
| 103 | if( found == false ) return XPTR_NULL; |
---|
| 104 | else return barrier_xp; |
---|
[104] | 105 | } |
---|
[23] | 106 | |
---|
| 107 | ////////////////////////////////////////////// |
---|
| 108 | error_t remote_barrier_create( intptr_t ident, |
---|
| 109 | uint32_t count ) |
---|
| 110 | { |
---|
| 111 | xptr_t barrier_xp; |
---|
| 112 | remote_barrier_t * barrier_ptr; |
---|
| 113 | |
---|
| 114 | // get pointer on local process descriptor |
---|
| 115 | process_t * process = CURRENT_THREAD->process; |
---|
| 116 | |
---|
| 117 | // get extended pointer on reference process |
---|
| 118 | xptr_t ref_xp = process->ref_xp; |
---|
| 119 | |
---|
| 120 | // get reference process cluster and local pointer |
---|
| 121 | cxy_t ref_cxy = GET_CXY( ref_xp ); |
---|
| 122 | process_t * ref_ptr = (process_t *)GET_PTR( ref_xp ); |
---|
| 123 | |
---|
| 124 | // allocate memory for barrier descriptor |
---|
[104] | 125 | if( ref_cxy == local_cxy ) // local cluster is the reference |
---|
[23] | 126 | { |
---|
[104] | 127 | kmem_req_t req; |
---|
[23] | 128 | req.type = KMEM_BARRIER; |
---|
| 129 | req.flags = AF_ZERO; |
---|
| 130 | barrier_ptr = kmem_alloc( &req ); |
---|
| 131 | barrier_xp = XPTR( local_cxy , barrier_ptr ); |
---|
| 132 | } |
---|
| 133 | else // reference is remote |
---|
| 134 | { |
---|
| 135 | rpc_kcm_alloc_client( ref_cxy , KMEM_BARRIER , &barrier_xp ); |
---|
| 136 | barrier_ptr = (remote_barrier_t *)GET_PTR( barrier_xp ); |
---|
| 137 | } |
---|
| 138 | |
---|
| 139 | if( barrier_ptr == NULL ) return ENOMEM; |
---|
| 140 | |
---|
[104] | 141 | // initialise barrier |
---|
[23] | 142 | hal_remote_sw ( XPTR( ref_cxy , &barrier_ptr->nb_threads ) , count ); |
---|
| 143 | hal_remote_sw ( XPTR( ref_cxy , &barrier_ptr->current ) , 0 ); |
---|
| 144 | hal_remote_sw ( XPTR( ref_cxy , &barrier_ptr->sense ) , 0 ); |
---|
| 145 | hal_remote_spt( XPTR( ref_cxy , &barrier_ptr->ident ) , (void*)ident ); |
---|
| 146 | |
---|
| 147 | xlist_entry_init( XPTR( ref_cxy , &barrier_ptr->list ) ); |
---|
| 148 | |
---|
| 149 | // register barrier in reference process xlist |
---|
| 150 | xptr_t root_xp = XPTR( ref_cxy , &ref_ptr->barrier_root ); |
---|
| 151 | xptr_t entry_xp = XPTR( ref_cxy , &barrier_ptr->list ); |
---|
| 152 | |
---|
| 153 | remote_spinlock_lock( XPTR( ref_cxy , &ref_ptr->sync_lock ) ); |
---|
| 154 | xlist_add_first( root_xp , entry_xp ); |
---|
| 155 | remote_spinlock_unlock( XPTR( ref_cxy , &ref_ptr->sync_lock ) ); |
---|
| 156 | |
---|
| 157 | return 0; |
---|
[104] | 158 | } |
---|
[23] | 159 | |
---|
| 160 | //////////////////////////////////////////////// |
---|
| 161 | void remote_barrier_destroy( xptr_t barrier_xp ) |
---|
| 162 | { |
---|
| 163 | // get pointer on local process descriptor |
---|
| 164 | process_t * process = CURRENT_THREAD->process; |
---|
| 165 | |
---|
| 166 | // get extended pointer on reference process |
---|
| 167 | xptr_t ref_xp = process->ref_xp; |
---|
| 168 | |
---|
| 169 | // get reference process cluster and local pointer |
---|
| 170 | cxy_t ref_cxy = GET_CXY( ref_xp ); |
---|
| 171 | process_t * ref_ptr = (process_t *)GET_PTR( ref_xp ); |
---|
| 172 | |
---|
| 173 | // get barrier cluster and local pointer |
---|
| 174 | cxy_t barrier_cxy = GET_CXY( barrier_xp ); |
---|
| 175 | remote_barrier_t * barrier_ptr = (remote_barrier_t *)GET_PTR( barrier_xp ); |
---|
| 176 | |
---|
| 177 | // remove barrier from reference process xlist |
---|
| 178 | remote_spinlock_lock( XPTR( ref_cxy , &ref_ptr->sync_lock ) ); |
---|
| 179 | xlist_unlink( XPTR( barrier_cxy , &barrier_ptr->list ) ); |
---|
| 180 | remote_spinlock_unlock( XPTR( ref_cxy , &ref_ptr->sync_lock ) ); |
---|
| 181 | |
---|
| 182 | // release memory allocated for barrier descriptor |
---|
| 183 | if( barrier_cxy == local_cxy ) // reference is local |
---|
| 184 | { |
---|
| 185 | kmem_req_t req; |
---|
| 186 | req.type = KMEM_BARRIER; |
---|
| 187 | req.ptr = barrier_ptr; |
---|
| 188 | kmem_free( &req ); |
---|
| 189 | } |
---|
| 190 | else // reference is remote |
---|
| 191 | { |
---|
| 192 | rpc_kcm_free_client( barrier_cxy , barrier_ptr , KMEM_BARRIER ); |
---|
| 193 | } |
---|
[104] | 194 | } |
---|
[23] | 195 | |
---|
| 196 | ///////////////////////////////////////////// |
---|
| 197 | void remote_barrier_wait( xptr_t barrier_xp ) |
---|
| 198 | { |
---|
| 199 | uint32_t expected; |
---|
| 200 | uint32_t current; |
---|
| 201 | uint32_t count; |
---|
| 202 | uint32_t sense; |
---|
[60] | 203 | reg_t irq_state; |
---|
[23] | 204 | xptr_t root_xp; |
---|
| 205 | |
---|
| 206 | // get cluster and local pointer on calling thread |
---|
| 207 | cxy_t thread_cxy = local_cxy; |
---|
| 208 | thread_t * thread_ptr = CURRENT_THREAD; |
---|
| 209 | |
---|
| 210 | // get cluster and local pointer on remote barrier |
---|
| 211 | remote_barrier_t * barrier_ptr = (remote_barrier_t *)GET_PTR( barrier_xp ); |
---|
| 212 | cxy_t barrier_cxy = GET_CXY( barrier_xp ); |
---|
| 213 | |
---|
| 214 | // get count and root fields from barrier descriptor |
---|
| 215 | count = hal_remote_lw ( XPTR( barrier_cxy , &barrier_ptr->nb_threads ) ); |
---|
| 216 | root_xp = hal_remote_lwd( XPTR( barrier_cxy , &barrier_ptr->root ) ); |
---|
| 217 | |
---|
| 218 | // get barrier sense value |
---|
| 219 | sense = hal_remote_lw( XPTR( barrier_cxy , &barrier_ptr->sense ) ); |
---|
| 220 | |
---|
[104] | 221 | // compute expected value |
---|
[23] | 222 | if ( sense == 0 ) expected = 1; |
---|
| 223 | else expected = 0; |
---|
| 224 | |
---|
[104] | 225 | // atomically increment current |
---|
[23] | 226 | current = hal_remote_atomic_add( XPTR( barrier_cxy , &barrier_ptr->current ) , 1 ); |
---|
| 227 | |
---|
| 228 | // last thread reset current, toggle sense, and activate all waiting threads |
---|
[104] | 229 | // other threads block, register in queue, and deschedule |
---|
[23] | 230 | |
---|
| 231 | if( current == (count-1) ) // last thread |
---|
| 232 | { |
---|
[104] | 233 | hal_remote_sw( XPTR( barrier_cxy , &barrier_ptr->current) , 0 ); |
---|
| 234 | hal_remote_sw( XPTR( barrier_cxy , &barrier_ptr->sense ) , expected ); |
---|
[23] | 235 | |
---|
| 236 | // activate waiting threads if required |
---|
[104] | 237 | if( xlist_is_empty( root_xp ) == false ) |
---|
[23] | 238 | { |
---|
| 239 | // disable interrupts |
---|
[104] | 240 | hal_disable_irq( &irq_state ); |
---|
| 241 | |
---|
[23] | 242 | xptr_t iter_xp; |
---|
| 243 | xptr_t thread_xp; |
---|
| 244 | XLIST_FOREACH( root_xp , iter_xp ) |
---|
| 245 | { |
---|
| 246 | // get extended pointer on waiting thread |
---|
| 247 | thread_xp = XLIST_ELEMENT( iter_xp , thread_t , wait_list ); |
---|
[104] | 248 | |
---|
[23] | 249 | // remove waiting thread from queue |
---|
| 250 | remote_spinlock_lock( XPTR( barrier_cxy , &barrier_ptr->lock ) ); |
---|
| 251 | xlist_unlink( XPTR( barrier_cxy , &barrier_ptr->list ) ); |
---|
| 252 | remote_spinlock_unlock( XPTR( barrier_cxy , &barrier_ptr->lock ) ); |
---|
| 253 | |
---|
| 254 | // unblock waiting thread |
---|
[104] | 255 | thread_unblock( thread_xp , THREAD_BLOCKED_USERSYNC ); |
---|
[23] | 256 | } |
---|
| 257 | |
---|
| 258 | // restore interrupts |
---|
| 259 | hal_restore_irq( irq_state ); |
---|
| 260 | } |
---|
| 261 | } |
---|
[104] | 262 | else // not the last thread |
---|
[23] | 263 | { |
---|
| 264 | // disable interrupts |
---|
[104] | 265 | hal_disable_irq( &irq_state ); |
---|
| 266 | |
---|
[23] | 267 | // register calling thread in barrier waiting queue |
---|
| 268 | xptr_t entry_xp = XPTR( thread_cxy , &thread_ptr->wait_list ); |
---|
| 269 | |
---|
[104] | 270 | remote_spinlock_lock( XPTR( barrier_cxy , &barrier_ptr->lock ) ); |
---|
[23] | 271 | xlist_add_last( root_xp , entry_xp ); |
---|
[104] | 272 | remote_spinlock_unlock( XPTR( barrier_cxy , &barrier_ptr->lock ) ); |
---|
[23] | 273 | |
---|
[104] | 274 | // block & deschedule the calling thread |
---|
[436] | 275 | thread_block( XPTR( local_cxy , thread_ptr ) , THREAD_BLOCKED_USERSYNC ); |
---|
[408] | 276 | sched_yield("blocked on barrier"); |
---|
[23] | 277 | |
---|
| 278 | // restore interrupts |
---|
| 279 | hal_restore_irq( irq_state ); |
---|
| 280 | } |
---|
[104] | 281 | } |
---|