[1] | 1 | /* |
---|
| 2 | * scheduler.c - Core scheduler implementation. |
---|
| 3 | * |
---|
[564] | 4 | * Author Alain Greiner (2016,2017,2018) |
---|
[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 | |
---|
[14] | 24 | #include <kernel_config.h> |
---|
[457] | 25 | #include <hal_kernel_types.h> |
---|
[407] | 26 | #include <hal_switch.h> |
---|
[1] | 27 | #include <hal_irqmask.h> |
---|
| 28 | #include <hal_context.h> |
---|
| 29 | #include <printk.h> |
---|
| 30 | #include <list.h> |
---|
[619] | 31 | #include <rpc.h> |
---|
[1] | 32 | #include <core.h> |
---|
| 33 | #include <thread.h> |
---|
[296] | 34 | #include <chdev.h> |
---|
[1] | 35 | #include <scheduler.h> |
---|
| 36 | |
---|
[443] | 37 | |
---|
[296] | 38 | /////////////////////////////////////////////////////////////////////////////////////////// |
---|
[564] | 39 | // global variables |
---|
[296] | 40 | /////////////////////////////////////////////////////////////////////////////////////////// |
---|
[1] | 41 | |
---|
[564] | 42 | extern chdev_directory_t chdev_dir; // allocated in kernel_init.c |
---|
[583] | 43 | extern process_t process_zero; // allocated in kernel_init.c |
---|
[296] | 44 | |
---|
[564] | 45 | /////////////////////////////////////////////////////////////////////////////////////////// |
---|
| 46 | // private functions |
---|
| 47 | /////////////////////////////////////////////////////////////////////////////////////////// |
---|
[443] | 48 | |
---|
[1] | 49 | |
---|
[564] | 50 | //////////////////////////////////////////////////////////////////////////////////////////// |
---|
| 51 | // This static function does NOT modify the scheduler state. |
---|
| 52 | // It just select a thread in the list of attached threads, implementing the following |
---|
| 53 | // three steps policy: |
---|
| 54 | // 1) It scan the list of kernel threads, from the next thread after the last executed one, |
---|
| 55 | // and returns the first runnable found : not IDLE, not blocked, client queue not empty. |
---|
| 56 | // It can be the current thread. |
---|
| 57 | // 2) If no kernel thread found, it scan the list of user thread, from the next thread after |
---|
| 58 | // the last executed one, and returns the first runable found : not blocked. |
---|
| 59 | // It can be the current thread. |
---|
| 60 | // 3) If no runable thread found, it returns the idle thread. |
---|
| 61 | //////////////////////////////////////////////////////////////////////////////////////////// |
---|
| 62 | // @ sched : local pointer on scheduler. |
---|
| 63 | // @ returns pointer on selected thread descriptor |
---|
| 64 | //////////////////////////////////////////////////////////////////////////////////////////// |
---|
[629] | 65 | static thread_t * sched_select( scheduler_t * sched ) |
---|
[1] | 66 | { |
---|
[408] | 67 | thread_t * thread; |
---|
| 68 | list_entry_t * current; |
---|
| 69 | list_entry_t * last; |
---|
[437] | 70 | list_entry_t * root; |
---|
| 71 | bool_t done; |
---|
[450] | 72 | uint32_t count; |
---|
[1] | 73 | |
---|
[437] | 74 | // first : scan the kernel threads list if not empty |
---|
[279] | 75 | if( list_is_empty( &sched->k_root ) == false ) |
---|
[1] | 76 | { |
---|
[437] | 77 | root = &sched->k_root; |
---|
[279] | 78 | last = sched->k_last; |
---|
[450] | 79 | done = false; |
---|
| 80 | count = 0; |
---|
[437] | 81 | current = last; |
---|
| 82 | |
---|
| 83 | while( done == false ) |
---|
[279] | 84 | { |
---|
[450] | 85 | |
---|
[564] | 86 | // check kernel threads list |
---|
[583] | 87 | assert( (count < sched->k_threads_nr), "bad kernel threads list" ); |
---|
[564] | 88 | |
---|
[279] | 89 | // get next entry in kernel list |
---|
[437] | 90 | current = current->next; |
---|
[1] | 91 | |
---|
[437] | 92 | // check exit condition |
---|
| 93 | if( current == last ) done = true; |
---|
| 94 | |
---|
[279] | 95 | // skip the root that does not contain a thread |
---|
[437] | 96 | if( current == root ) continue; |
---|
[450] | 97 | else count++; |
---|
[1] | 98 | |
---|
[279] | 99 | // get thread pointer for this entry |
---|
| 100 | thread = LIST_ELEMENT( current , thread_t , sched_list ); |
---|
[1] | 101 | |
---|
[450] | 102 | // select kernel thread if non blocked and non THREAD_IDLE |
---|
[564] | 103 | if( (thread->blocked == 0) && (thread->type != THREAD_IDLE) ) return thread; |
---|
| 104 | |
---|
[437] | 105 | } // end loop on kernel threads |
---|
[450] | 106 | } // end kernel threads |
---|
[437] | 107 | |
---|
| 108 | // second : scan the user threads list if not empty |
---|
[279] | 109 | if( list_is_empty( &sched->u_root ) == false ) |
---|
[1] | 110 | { |
---|
[437] | 111 | root = &sched->u_root; |
---|
[279] | 112 | last = sched->u_last; |
---|
[450] | 113 | done = false; |
---|
| 114 | count = 0; |
---|
[437] | 115 | current = last; |
---|
| 116 | |
---|
| 117 | while( done == false ) |
---|
[279] | 118 | { |
---|
[450] | 119 | |
---|
[564] | 120 | // check user threads list |
---|
[583] | 121 | assert( (count < sched->u_threads_nr), "bad user threads list" ); |
---|
[564] | 122 | |
---|
[279] | 123 | // get next entry in user list |
---|
[437] | 124 | current = current->next; |
---|
[1] | 125 | |
---|
[437] | 126 | // check exit condition |
---|
| 127 | if( current == last ) done = true; |
---|
| 128 | |
---|
[279] | 129 | // skip the root that does not contain a thread |
---|
[437] | 130 | if( current == root ) continue; |
---|
[450] | 131 | else count++; |
---|
[1] | 132 | |
---|
[279] | 133 | // get thread pointer for this entry |
---|
| 134 | thread = LIST_ELEMENT( current , thread_t , sched_list ); |
---|
[1] | 135 | |
---|
[450] | 136 | // select thread if non blocked |
---|
[564] | 137 | if( thread->blocked == 0 ) return thread; |
---|
| 138 | |
---|
[437] | 139 | } // end loop on user threads |
---|
[450] | 140 | } // end user threads |
---|
[1] | 141 | |
---|
[437] | 142 | // third : return idle thread if no other runnable thread |
---|
[1] | 143 | return sched->idle; |
---|
| 144 | |
---|
[296] | 145 | } // end sched_select() |
---|
[1] | 146 | |
---|
[564] | 147 | //////////////////////////////////////////////////////////////////////////////////////////// |
---|
[592] | 148 | // This static function is the only function that can actually delete a thread, |
---|
[619] | 149 | // (and the associated process descriptor if required). |
---|
| 150 | // It is private, because it is only called by the sched_yield() public function. |
---|
[564] | 151 | // It scan all threads attached to a given scheduler, and executes the relevant |
---|
[583] | 152 | // actions for two types of pending requests: |
---|
[592] | 153 | // |
---|
[564] | 154 | // - REQ_ACK : it checks that target thread is blocked, decrements the response counter |
---|
| 155 | // to acknowledge the client thread, and reset the pending request. |
---|
[583] | 156 | // - REQ_DELETE : it removes the target thread from the process th_tbl[], remove it |
---|
| 157 | // from the scheduler list, and release the memory allocated to thread descriptor. |
---|
| 158 | // For an user thread, it destroys the process descriptor it the target thread is |
---|
| 159 | // the last thread in the local process descriptor. |
---|
| 160 | // |
---|
| 161 | // Implementation note: |
---|
| 162 | // We use a while to scan the threads in scheduler lists, because some threads can |
---|
| 163 | // be destroyed, and we want not use a LIST_FOREACH() |
---|
[564] | 164 | //////////////////////////////////////////////////////////////////////////////////////////// |
---|
| 165 | // @ core : local pointer on the core descriptor. |
---|
| 166 | //////////////////////////////////////////////////////////////////////////////////////////// |
---|
| 167 | static void sched_handle_signals( core_t * core ) |
---|
[1] | 168 | { |
---|
[437] | 169 | |
---|
[1] | 170 | list_entry_t * iter; |
---|
[440] | 171 | list_entry_t * root; |
---|
[1] | 172 | thread_t * thread; |
---|
[428] | 173 | process_t * process; |
---|
[564] | 174 | scheduler_t * sched; |
---|
[583] | 175 | uint32_t threads_nr; // number of threads in scheduler list |
---|
| 176 | ltid_t ltid; // thread local index |
---|
| 177 | uint32_t count; // number of threads in local process |
---|
[409] | 178 | |
---|
[440] | 179 | // get pointer on scheduler |
---|
[564] | 180 | sched = &core->scheduler; |
---|
[1] | 181 | |
---|
[635] | 182 | ////////////////// scan user threads to handle ACK and DELETE requests |
---|
[440] | 183 | root = &sched->u_root; |
---|
| 184 | iter = root->next; |
---|
| 185 | while( iter != root ) |
---|
[1] | 186 | { |
---|
[440] | 187 | // get pointer on thread |
---|
[1] | 188 | thread = LIST_ELEMENT( iter , thread_t , sched_list ); |
---|
| 189 | |
---|
[440] | 190 | // increment iterator |
---|
| 191 | iter = iter->next; |
---|
| 192 | |
---|
[416] | 193 | // handle REQ_ACK |
---|
| 194 | if( thread->flags & THREAD_FLAG_REQ_ACK ) |
---|
[408] | 195 | { |
---|
[564] | 196 | |
---|
[635] | 197 | // check target thread blocked |
---|
[592] | 198 | assert( (thread->blocked & THREAD_BLOCKED_GLOBAL) , "thread not blocked" ); |
---|
[416] | 199 | |
---|
| 200 | // decrement response counter |
---|
| 201 | hal_atomic_add( thread->ack_rsp_count , -1 ); |
---|
[408] | 202 | |
---|
[416] | 203 | // reset REQ_ACK in thread descriptor |
---|
| 204 | thread_reset_req_ack( thread ); |
---|
[408] | 205 | } |
---|
[416] | 206 | |
---|
[564] | 207 | // handle REQ_DELETE only if target thread != calling thread |
---|
[635] | 208 | if( thread->flags & THREAD_FLAG_REQ_DELETE ) |
---|
[416] | 209 | { |
---|
[635] | 210 | |
---|
| 211 | // check calling thread != target thread |
---|
| 212 | assert( (thread != CURRENT_THREAD) , "calling thread cannot delete itself" ); |
---|
| 213 | |
---|
[428] | 214 | // get thread process descriptor |
---|
| 215 | process = thread->process; |
---|
[416] | 216 | |
---|
[583] | 217 | // get thread ltid |
---|
| 218 | ltid = LTID_FROM_TRDID( thread->trdid); |
---|
[416] | 219 | |
---|
[593] | 220 | // take the lock protecting sheduler state |
---|
| 221 | busylock_acquire( &sched->lock ); |
---|
| 222 | |
---|
[564] | 223 | // update scheduler state |
---|
[583] | 224 | threads_nr = sched->u_threads_nr; |
---|
[428] | 225 | sched->u_threads_nr = threads_nr - 1; |
---|
[416] | 226 | list_unlink( &thread->sched_list ); |
---|
[450] | 227 | if( sched->u_last == &thread->sched_list ) |
---|
| 228 | { |
---|
| 229 | if( threads_nr == 1 ) |
---|
| 230 | { |
---|
| 231 | sched->u_last = NULL; |
---|
| 232 | } |
---|
| 233 | else if( sched->u_root.next == &thread->sched_list ) |
---|
| 234 | { |
---|
| 235 | sched->u_last = sched->u_root.pred; |
---|
| 236 | } |
---|
| 237 | else |
---|
| 238 | { |
---|
| 239 | sched->u_last = sched->u_root.next; |
---|
| 240 | } |
---|
| 241 | } |
---|
[416] | 242 | |
---|
[593] | 243 | // release the lock protecting sheduler state |
---|
| 244 | busylock_release( &sched->lock ); |
---|
| 245 | |
---|
[625] | 246 | // release memory allocated for thread |
---|
| 247 | count = thread_destroy( thread ); |
---|
[583] | 248 | |
---|
[593] | 249 | hal_fence(); |
---|
| 250 | |
---|
[438] | 251 | #if DEBUG_SCHED_HANDLE_SIGNALS |
---|
[440] | 252 | uint32_t cycle = (uint32_t)hal_get_cycles(); |
---|
[438] | 253 | if( DEBUG_SCHED_HANDLE_SIGNALS < cycle ) |
---|
[630] | 254 | printk("\n[%s] thread[%x,%x] on core[%x,%d] deleted / cycle %d\n", |
---|
| 255 | __FUNCTION__, process->pid, thread->trdid, local_cxy, thread->core->lid, cycle ); |
---|
[433] | 256 | #endif |
---|
[583] | 257 | // destroy process descriptor if last thread |
---|
| 258 | if( count == 1 ) |
---|
[428] | 259 | { |
---|
| 260 | // delete process |
---|
| 261 | process_destroy( process ); |
---|
| 262 | |
---|
[438] | 263 | #if DEBUG_SCHED_HANDLE_SIGNALS |
---|
[433] | 264 | cycle = (uint32_t)hal_get_cycles(); |
---|
[438] | 265 | if( DEBUG_SCHED_HANDLE_SIGNALS < cycle ) |
---|
[610] | 266 | printk("\n[%s] process %x in cluster %x deleted / cycle %d\n", |
---|
[443] | 267 | __FUNCTION__ , process->pid , local_cxy , cycle ); |
---|
[433] | 268 | #endif |
---|
[428] | 269 | } |
---|
[416] | 270 | } |
---|
[583] | 271 | } // end user threads |
---|
| 272 | |
---|
[625] | 273 | ///////////// scan kernel threads for DELETE only |
---|
[583] | 274 | root = &sched->k_root; |
---|
| 275 | iter = root->next; |
---|
| 276 | while( iter != root ) |
---|
| 277 | { |
---|
| 278 | // get pointer on thread |
---|
| 279 | thread = LIST_ELEMENT( iter , thread_t , sched_list ); |
---|
| 280 | |
---|
| 281 | // increment iterator |
---|
| 282 | iter = iter->next; |
---|
| 283 | |
---|
| 284 | // handle REQ_DELETE only if target thread != calling thread |
---|
| 285 | if( (thread->flags & THREAD_FLAG_REQ_DELETE) && (thread != CURRENT_THREAD) ) |
---|
| 286 | { |
---|
| 287 | |
---|
| 288 | // check process descriptor is local kernel process |
---|
[625] | 289 | assert( ( thread->process == &process_zero ) , "illegal process descriptor"); |
---|
[583] | 290 | |
---|
| 291 | // get thread ltid |
---|
| 292 | ltid = LTID_FROM_TRDID( thread->trdid); |
---|
| 293 | |
---|
[593] | 294 | // take the lock protecting sheduler state |
---|
| 295 | busylock_acquire( &sched->lock ); |
---|
| 296 | |
---|
[583] | 297 | // update scheduler state |
---|
| 298 | threads_nr = sched->k_threads_nr; |
---|
| 299 | sched->k_threads_nr = threads_nr - 1; |
---|
| 300 | list_unlink( &thread->sched_list ); |
---|
| 301 | if( sched->k_last == &thread->sched_list ) |
---|
| 302 | { |
---|
| 303 | if( threads_nr == 1 ) |
---|
| 304 | { |
---|
| 305 | sched->k_last = NULL; |
---|
| 306 | } |
---|
| 307 | else if( sched->k_root.next == &thread->sched_list ) |
---|
| 308 | { |
---|
| 309 | sched->k_last = sched->k_root.pred; |
---|
| 310 | } |
---|
| 311 | else |
---|
| 312 | { |
---|
| 313 | sched->k_last = sched->k_root.next; |
---|
| 314 | } |
---|
| 315 | } |
---|
| 316 | |
---|
[593] | 317 | // release the lock protecting sheduler state |
---|
| 318 | busylock_release( &sched->lock ); |
---|
| 319 | |
---|
[583] | 320 | // get number of threads in local kernel process |
---|
| 321 | count = process_zero.th_nr; |
---|
| 322 | |
---|
| 323 | // check th_nr value |
---|
[625] | 324 | assert( (process_zero.th_nr > 0) , "kernel process th_nr cannot be 0" ); |
---|
[583] | 325 | |
---|
| 326 | // remove thread from process th_tbl[] |
---|
| 327 | process_zero.th_tbl[ltid] = NULL; |
---|
[592] | 328 | hal_atomic_add( &process_zero.th_nr , - 1 ); |
---|
[583] | 329 | |
---|
| 330 | // delete thread descriptor |
---|
| 331 | thread_destroy( thread ); |
---|
| 332 | |
---|
| 333 | #if DEBUG_SCHED_HANDLE_SIGNALS |
---|
| 334 | uint32_t cycle = (uint32_t)hal_get_cycles(); |
---|
| 335 | if( DEBUG_SCHED_HANDLE_SIGNALS < cycle ) |
---|
[610] | 336 | printk("\n[%s] thread[%x,%x] on core[%x,%d] deleted / cycle %d\n", |
---|
[583] | 337 | __FUNCTION__ , process_zero.pid , thread->trdid , local_cxy , thread->core->lid , cycle ); |
---|
| 338 | #endif |
---|
| 339 | } |
---|
[1] | 340 | } |
---|
[564] | 341 | } // end sched_handle_signals() |
---|
[1] | 342 | |
---|
[564] | 343 | //////////////////////////////////////////////////////////////////////////////////////////// |
---|
| 344 | // This static function is called by the sched_yield function when the RFC_FIFO |
---|
| 345 | // associated to the core is not empty. |
---|
[583] | 346 | // It search an idle RPC thread for this core, and unblock it if found. |
---|
| 347 | // It creates a new RPC thread if no idle RPC thread is found. |
---|
[564] | 348 | //////////////////////////////////////////////////////////////////////////////////////////// |
---|
| 349 | // @ sched : local pointer on scheduler. |
---|
| 350 | //////////////////////////////////////////////////////////////////////////////////////////// |
---|
[582] | 351 | static void sched_rpc_activate( scheduler_t * sched ) |
---|
[564] | 352 | { |
---|
| 353 | error_t error; |
---|
| 354 | thread_t * thread; |
---|
| 355 | list_entry_t * iter; |
---|
| 356 | lid_t lid = CURRENT_THREAD->core->lid; |
---|
| 357 | bool_t found = false; |
---|
| 358 | |
---|
| 359 | // search one IDLE RPC thread associated to the selected core |
---|
| 360 | LIST_FOREACH( &sched->k_root , iter ) |
---|
| 361 | { |
---|
| 362 | thread = LIST_ELEMENT( iter , thread_t , sched_list ); |
---|
[583] | 363 | |
---|
| 364 | if( (thread->type == THREAD_RPC) && |
---|
| 365 | (thread->blocked == THREAD_BLOCKED_IDLE ) ) |
---|
[564] | 366 | { |
---|
| 367 | found = true; |
---|
| 368 | break; |
---|
| 369 | } |
---|
| 370 | } |
---|
| 371 | |
---|
| 372 | if( found == false ) // create new RPC thread |
---|
| 373 | { |
---|
| 374 | error = thread_kernel_create( &thread, |
---|
| 375 | THREAD_RPC, |
---|
[619] | 376 | &rpc_server_func, |
---|
[564] | 377 | NULL, |
---|
| 378 | lid ); |
---|
| 379 | // check memory |
---|
| 380 | if ( error ) |
---|
| 381 | { |
---|
[583] | 382 | printk("\n[ERROR] in %s : no memory to create a RPC thread in cluster %x\n", |
---|
[564] | 383 | __FUNCTION__, local_cxy ); |
---|
| 384 | } |
---|
| 385 | else |
---|
| 386 | { |
---|
| 387 | // unblock created RPC thread |
---|
| 388 | thread->blocked = 0; |
---|
| 389 | |
---|
| 390 | // update RPC threads counter |
---|
| 391 | hal_atomic_add( &LOCAL_CLUSTER->rpc_threads[lid] , 1 ); |
---|
| 392 | |
---|
| 393 | #if DEBUG_SCHED_RPC_ACTIVATE |
---|
| 394 | uint32_t cycle = (uint32_t)hal_get_cycles(); |
---|
| 395 | if( DEBUG_SCHED_RPC_ACTIVATE < cycle ) |
---|
[610] | 396 | printk("\n[%s] new RPC thread %x created for core[%x,%d] / total %d / cycle %d\n", |
---|
[583] | 397 | __FUNCTION__, thread->trdid, local_cxy, lid, LOCAL_CLUSTER->rpc_threads[lid], cycle ); |
---|
[564] | 398 | #endif |
---|
| 399 | } |
---|
| 400 | } |
---|
| 401 | else // RPC thread found => unblock it |
---|
| 402 | { |
---|
| 403 | // unblock found RPC thread |
---|
| 404 | thread_unblock( XPTR( local_cxy , thread ) , THREAD_BLOCKED_IDLE ); |
---|
| 405 | |
---|
| 406 | #if DEBUG_SCHED_RPC_ACTIVATE |
---|
| 407 | uint32_t cycle = (uint32_t)hal_get_cycles(); |
---|
| 408 | if( DEBUG_SCHED_RPC_ACTIVATE < cycle ) |
---|
[610] | 409 | printk("\n[%s] idle RPC thread %x unblocked for core[%x,%d] / cycle %d\n", |
---|
[564] | 410 | __FUNCTION__, thread->trdid, local_cxy, lid, cycle ); |
---|
| 411 | #endif |
---|
| 412 | |
---|
| 413 | } |
---|
| 414 | |
---|
| 415 | } // end sched_rpc_activate() |
---|
| 416 | |
---|
| 417 | |
---|
| 418 | |
---|
| 419 | /////////////////////////////////////////////////////////////////////////////////////////// |
---|
| 420 | // public functions |
---|
| 421 | /////////////////////////////////////////////////////////////////////////////////////////// |
---|
| 422 | |
---|
| 423 | //////////////////////////////// |
---|
| 424 | void sched_init( core_t * core ) |
---|
| 425 | { |
---|
| 426 | scheduler_t * sched = &core->scheduler; |
---|
| 427 | |
---|
| 428 | sched->u_threads_nr = 0; |
---|
| 429 | sched->k_threads_nr = 0; |
---|
| 430 | |
---|
| 431 | sched->current = CURRENT_THREAD; |
---|
| 432 | sched->idle = NULL; // initialized in kernel_init() |
---|
| 433 | sched->u_last = NULL; // initialized in sched_register_thread() |
---|
| 434 | sched->k_last = NULL; // initialized in sched_register_thread() |
---|
| 435 | |
---|
| 436 | // initialise threads lists |
---|
| 437 | list_root_init( &sched->u_root ); |
---|
| 438 | list_root_init( &sched->k_root ); |
---|
| 439 | |
---|
| 440 | // init lock |
---|
| 441 | busylock_init( &sched->lock , LOCK_SCHED_STATE ); |
---|
| 442 | |
---|
| 443 | sched->req_ack_pending = false; // no pending request |
---|
| 444 | sched->trace = false; // context switches trace desactivated |
---|
| 445 | |
---|
| 446 | } // end sched_init() |
---|
| 447 | |
---|
| 448 | //////////////////////////////////////////// |
---|
| 449 | void sched_register_thread( core_t * core, |
---|
| 450 | thread_t * thread ) |
---|
| 451 | { |
---|
| 452 | scheduler_t * sched = &core->scheduler; |
---|
| 453 | thread_type_t type = thread->type; |
---|
| 454 | |
---|
| 455 | // take lock protecting sheduler state |
---|
| 456 | busylock_acquire( &sched->lock ); |
---|
| 457 | |
---|
| 458 | if( type == THREAD_USER ) |
---|
| 459 | { |
---|
| 460 | list_add_last( &sched->u_root , &thread->sched_list ); |
---|
| 461 | sched->u_threads_nr++; |
---|
| 462 | if( sched->u_last == NULL ) sched->u_last = &thread->sched_list; |
---|
| 463 | } |
---|
| 464 | else // kernel thread |
---|
| 465 | { |
---|
| 466 | list_add_last( &sched->k_root , &thread->sched_list ); |
---|
| 467 | sched->k_threads_nr++; |
---|
| 468 | if( sched->k_last == NULL ) sched->k_last = &thread->sched_list; |
---|
| 469 | } |
---|
| 470 | |
---|
[1] | 471 | // release lock |
---|
[564] | 472 | busylock_release( &sched->lock ); |
---|
[1] | 473 | |
---|
[564] | 474 | } // end sched_register_thread() |
---|
[416] | 475 | |
---|
[625] | 476 | ////////////////////////////////////////////////////////////////// |
---|
| 477 | void sched_yield( const char * cause __attribute__((__unused__)) ) |
---|
[1] | 478 | { |
---|
[564] | 479 | thread_t * next; |
---|
| 480 | thread_t * current = CURRENT_THREAD; |
---|
| 481 | core_t * core = current->core; |
---|
| 482 | lid_t lid = core->lid; |
---|
| 483 | scheduler_t * sched = &core->scheduler; |
---|
| 484 | remote_fifo_t * fifo = &LOCAL_CLUSTER->rpc_fifo[lid]; |
---|
[407] | 485 | |
---|
[635] | 486 | #if DEBUG_SCHED_YIELD |
---|
| 487 | uint32_t cycle = (uint32_t)hal_get_cycles(); |
---|
| 488 | #endif |
---|
| 489 | |
---|
[438] | 490 | #if (DEBUG_SCHED_YIELD & 0x1) |
---|
[635] | 491 | if( sched->trace || (cycle > DEBUG_SCHED_YIELD) ) |
---|
[640] | 492 | sched_remote_display( local_cxy , lid ); |
---|
[407] | 493 | #endif |
---|
[1] | 494 | |
---|
[614] | 495 | // This assert should never be false, as this check has been |
---|
| 496 | // done before, by any function that can possibly deschedule... |
---|
[564] | 497 | assert( (current->busylocks == 0), |
---|
[581] | 498 | "unexpected descheduling of thread holding %d busylocks = %d\n", current->busylocks ); |
---|
[1] | 499 | |
---|
[564] | 500 | // activate or create an RPC thread if RPC_FIFO non empty |
---|
| 501 | if( remote_fifo_is_empty( fifo ) == false ) sched_rpc_activate( sched ); |
---|
[408] | 502 | |
---|
[564] | 503 | // disable IRQs / save SR in current thread descriptor |
---|
| 504 | hal_disable_irq( ¤t->save_sr ); |
---|
| 505 | |
---|
| 506 | // take lock protecting sheduler state |
---|
| 507 | busylock_acquire( &sched->lock ); |
---|
| 508 | |
---|
| 509 | // select next thread |
---|
[408] | 510 | next = sched_select( sched ); |
---|
[1] | 511 | |
---|
[564] | 512 | // check next thread kernel_stack overflow |
---|
| 513 | assert( (next->signature == THREAD_SIGNATURE), |
---|
[625] | 514 | "kernel stack overflow for thread %x on core[%x,%d]", next, local_cxy, lid ); |
---|
[436] | 515 | |
---|
[564] | 516 | // check next thread attached to same core as the calling thread |
---|
| 517 | assert( (next->core == current->core), |
---|
[625] | 518 | "next core %x != current core %x", next->core, current->core ); |
---|
[296] | 519 | |
---|
[564] | 520 | // check next thread not blocked when type != IDLE |
---|
| 521 | assert( ((next->blocked == 0) || (next->type == THREAD_IDLE)) , |
---|
[625] | 522 | "next thread %x (%s) is blocked on core[%x,%d]", |
---|
[564] | 523 | next->trdid , thread_type_str(next->type) , local_cxy , lid ); |
---|
[296] | 524 | |
---|
| 525 | // switch contexts and update scheduler state if next != current |
---|
| 526 | if( next != current ) |
---|
[1] | 527 | { |
---|
[296] | 528 | // update scheduler |
---|
[408] | 529 | sched->current = next; |
---|
| 530 | if( next->type == THREAD_USER ) sched->u_last = &next->sched_list; |
---|
| 531 | else sched->k_last = &next->sched_list; |
---|
[1] | 532 | |
---|
[407] | 533 | // handle FPU ownership |
---|
[306] | 534 | if( next->type == THREAD_USER ) |
---|
[296] | 535 | { |
---|
[407] | 536 | if( next == current->core->fpu_owner ) hal_fpu_enable(); |
---|
| 537 | else hal_fpu_disable(); |
---|
[296] | 538 | } |
---|
[1] | 539 | |
---|
[564] | 540 | // release lock protecting scheduler state |
---|
| 541 | busylock_release( &sched->lock ); |
---|
| 542 | |
---|
| 543 | #if DEBUG_SCHED_YIELD |
---|
[635] | 544 | if( sched->trace || (cycle > DEBUG_SCHED_YIELD) ) |
---|
[610] | 545 | printk("\n[%s] core[%x,%d] / cause = %s\n" |
---|
[564] | 546 | " thread %x (%s) (%x,%x) => thread %x (%s) (%x,%x) / cycle %d\n", |
---|
| 547 | __FUNCTION__, local_cxy, lid, cause, |
---|
| 548 | current, thread_type_str(current->type), current->process->pid, current->trdid,next , |
---|
[635] | 549 | thread_type_str(next->type) , next->process->pid , next->trdid , cycle ); |
---|
[564] | 550 | #endif |
---|
| 551 | |
---|
[435] | 552 | // switch CPU from current thread context to new thread context |
---|
[407] | 553 | hal_do_cpu_switch( current->cpu_context, next->cpu_context ); |
---|
[296] | 554 | } |
---|
| 555 | else |
---|
| 556 | { |
---|
[564] | 557 | // release lock protecting scheduler state |
---|
| 558 | busylock_release( &sched->lock ); |
---|
[407] | 559 | |
---|
[635] | 560 | #if DEBUG_SCHED_YIELD |
---|
| 561 | if( sched->trace || (cycle > DEBUG_SCHED_YIELD) ) |
---|
[610] | 562 | printk("\n[%s] core[%x,%d] / cause = %s\n" |
---|
[435] | 563 | " thread %x (%s) (%x,%x) continue / cycle %d\n", |
---|
[564] | 564 | __FUNCTION__, local_cxy, lid, cause, current, thread_type_str(current->type), |
---|
[443] | 565 | current->process->pid, current->trdid, (uint32_t)hal_get_cycles() ); |
---|
[428] | 566 | #endif |
---|
[407] | 567 | |
---|
[296] | 568 | } |
---|
[408] | 569 | |
---|
[416] | 570 | // handle pending requests for all threads executing on this core. |
---|
[433] | 571 | sched_handle_signals( core ); |
---|
[409] | 572 | |
---|
[435] | 573 | // exit critical section / restore SR from current thread descriptor |
---|
| 574 | hal_restore_irq( CURRENT_THREAD->save_sr ); |
---|
[408] | 575 | |
---|
[1] | 576 | } // end sched_yield() |
---|
| 577 | |
---|
[407] | 578 | |
---|
[450] | 579 | ///////////////////////////////////// |
---|
| 580 | void sched_remote_display( cxy_t cxy, |
---|
| 581 | lid_t lid ) |
---|
| 582 | { |
---|
| 583 | thread_t * thread; |
---|
| 584 | |
---|
| 585 | // get local pointer on target scheduler |
---|
| 586 | core_t * core = &LOCAL_CLUSTER->core_tbl[lid]; |
---|
| 587 | scheduler_t * sched = &core->scheduler; |
---|
| 588 | |
---|
| 589 | // get local pointer on current thread in target scheduler |
---|
| 590 | thread_t * current = hal_remote_lpt( XPTR( cxy, &sched->current ) ); |
---|
| 591 | |
---|
| 592 | // get local pointer on the first kernel and user threads list_entry |
---|
| 593 | list_entry_t * k_entry = hal_remote_lpt( XPTR( cxy , &sched->k_root.next ) ); |
---|
| 594 | list_entry_t * u_entry = hal_remote_lpt( XPTR( cxy , &sched->u_root.next ) ); |
---|
| 595 | |
---|
| 596 | // get pointers on TXT0 chdev |
---|
| 597 | xptr_t txt0_xp = chdev_dir.txt_tx[0]; |
---|
| 598 | cxy_t txt0_cxy = GET_CXY( txt0_xp ); |
---|
| 599 | chdev_t * txt0_ptr = GET_PTR( txt0_xp ); |
---|
| 600 | |
---|
| 601 | // get extended pointer on remote TXT0 chdev lock |
---|
| 602 | xptr_t lock_xp = XPTR( txt0_cxy , &txt0_ptr->wait_lock ); |
---|
| 603 | |
---|
[564] | 604 | // get TXT0 lock |
---|
| 605 | remote_busylock_acquire( lock_xp ); |
---|
[450] | 606 | |
---|
[583] | 607 | // get rpc_threads |
---|
| 608 | uint32_t rpcs = hal_remote_l32( XPTR( cxy , &LOCAL_CLUSTER->rpc_threads[lid] ) ); |
---|
| 609 | |
---|
[450] | 610 | // display header |
---|
[583] | 611 | nolock_printk("\n***** threads on core[%x,%d] / current %x / rpc_threads %d / cycle %d\n", |
---|
| 612 | cxy , lid, current, rpcs, (uint32_t)hal_get_cycles() ); |
---|
[640] | 613 | nolock_printk(" type | pid | trdid | desc | block | flags | func\n"); |
---|
[450] | 614 | |
---|
| 615 | // display kernel threads |
---|
| 616 | while( k_entry != &sched->k_root ) |
---|
| 617 | { |
---|
| 618 | // get local pointer on kernel_thread |
---|
| 619 | thread = LIST_ELEMENT( k_entry , thread_t , sched_list ); |
---|
| 620 | |
---|
| 621 | // get relevant thead info |
---|
[564] | 622 | thread_type_t type = hal_remote_l32 ( XPTR( cxy , &thread->type ) ); |
---|
| 623 | trdid_t trdid = hal_remote_l32 ( XPTR( cxy , &thread->trdid ) ); |
---|
| 624 | uint32_t blocked = hal_remote_l32 ( XPTR( cxy , &thread->blocked ) ); |
---|
| 625 | uint32_t flags = hal_remote_l32 ( XPTR( cxy , &thread->flags ) ); |
---|
[610] | 626 | process_t * process = hal_remote_lpt ( XPTR( cxy , &thread->process ) ); |
---|
[564] | 627 | pid_t pid = hal_remote_l32 ( XPTR( cxy , &process->pid ) ); |
---|
[450] | 628 | |
---|
| 629 | // display thread info |
---|
| 630 | if (type == THREAD_DEV) |
---|
| 631 | { |
---|
| 632 | char name[16]; |
---|
| 633 | chdev_t * chdev = hal_remote_lpt( XPTR( cxy , &thread->chdev ) ); |
---|
[610] | 634 | hal_remote_strcpy( XPTR( local_cxy , name ), XPTR( cxy , chdev->name ) ); |
---|
[450] | 635 | |
---|
[640] | 636 | nolock_printk(" - %s | %X | %X | %X | %X | %X | %s\n", |
---|
[450] | 637 | thread_type_str( type ), pid, trdid, thread, blocked, flags, name ); |
---|
| 638 | } |
---|
| 639 | else |
---|
| 640 | { |
---|
[640] | 641 | nolock_printk(" - %s | %X | %X | %X | %X | %X |\n", |
---|
[450] | 642 | thread_type_str( type ), pid, trdid, thread, blocked, flags ); |
---|
| 643 | } |
---|
| 644 | |
---|
| 645 | // get next remote kernel thread list_entry |
---|
| 646 | k_entry = hal_remote_lpt( XPTR( cxy , &k_entry->next ) ); |
---|
| 647 | } |
---|
| 648 | |
---|
| 649 | // display user threads |
---|
| 650 | while( u_entry != &sched->u_root ) |
---|
| 651 | { |
---|
| 652 | // get local pointer on user_thread |
---|
| 653 | thread = LIST_ELEMENT( u_entry , thread_t , sched_list ); |
---|
| 654 | |
---|
| 655 | // get relevant thead info |
---|
[564] | 656 | thread_type_t type = hal_remote_l32 ( XPTR( cxy , &thread->type ) ); |
---|
| 657 | trdid_t trdid = hal_remote_l32 ( XPTR( cxy , &thread->trdid ) ); |
---|
| 658 | uint32_t blocked = hal_remote_l32 ( XPTR( cxy , &thread->blocked ) ); |
---|
| 659 | uint32_t flags = hal_remote_l32 ( XPTR( cxy , &thread->flags ) ); |
---|
[610] | 660 | process_t * process = hal_remote_lpt ( XPTR( cxy , &thread->process ) ); |
---|
[564] | 661 | pid_t pid = hal_remote_l32 ( XPTR( cxy , &process->pid ) ); |
---|
[640] | 662 | void * func = hal_remote_lpt ( XPTR( cxy , &thread->entry_func ) ); |
---|
[450] | 663 | |
---|
[640] | 664 | nolock_printk(" - %s | %X | %X | %X | %X | %X | %x\n", |
---|
| 665 | thread_type_str( type ), pid, trdid, thread, blocked, flags, (uint32_t)func ); |
---|
[450] | 666 | |
---|
| 667 | // get next user thread list_entry |
---|
| 668 | u_entry = hal_remote_lpt( XPTR( cxy , &u_entry->next ) ); |
---|
| 669 | } |
---|
| 670 | |
---|
| 671 | // release TXT0 lock |
---|
[564] | 672 | remote_busylock_release( lock_xp ); |
---|
[450] | 673 | |
---|
| 674 | } // end sched_remote_display() |
---|
| 675 | |
---|
[564] | 676 | |
---|