source: trunk/kernel/kern/scheduler.c @ 440

Last change on this file since 440 was 440, checked in by alain, 6 years ago

1/ Fix a bug in the Multithreaded "sort" applicationr:
The pthread_create() arguments must be declared as global variables.
2/ The exit syscall can be called by any thread of a process..

File size: 13.4 KB
Line 
1/*
2 * scheduler.c - Core scheduler implementation.
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 <kernel_config.h>
25#include <hal_types.h>
26#include <hal_switch.h>
27#include <hal_irqmask.h>
28#include <hal_context.h>
29#include <printk.h>
30#include <list.h>
31#include <core.h>
32#include <thread.h>
33#include <chdev.h>
34#include <scheduler.h>
35
36///////////////////////////////////////////////////////////////////////////////////////////
37// Extern global variables
38///////////////////////////////////////////////////////////////////////////////////////////
39
40extern chdev_directory_t    chdev_dir;            // allocated in kernel_init.c file
41extern uint32_t             switch_save_sr[];     // allocated in kernel_init.c file
42
43////////////////////////////////
44void sched_init( core_t * core )
45{
46    scheduler_t * sched = &core->scheduler;
47
48    sched->u_threads_nr   = 0;
49    sched->k_threads_nr   = 0;
50
51    sched->current        = CURRENT_THREAD;
52    sched->idle           = NULL;             // initialized in kernel_init()
53    sched->u_last         = NULL;             // initialized in sched_register_thread()
54    sched->k_last         = NULL;             // initialized in sched_register_thread()
55
56    // initialise threads lists
57    list_root_init( &sched->u_root );
58    list_root_init( &sched->k_root );
59
60    sched->req_ack_pending = false;           // no pending request
61
62}  // end sched_init()
63
64////////////////////////////////////////////
65void sched_register_thread( core_t   * core,
66                            thread_t * thread )
67{
68    scheduler_t * sched = &core->scheduler;
69    thread_type_t type  = thread->type;
70
71    // take lock protecting sheduler lists
72    spinlock_lock( &sched->lock );
73
74    if( type == THREAD_USER )
75    {
76        list_add_last( &sched->u_root , &thread->sched_list );
77        sched->u_threads_nr++;
78        if( sched->u_last == NULL ) sched->u_last = &thread->sched_list;
79    }
80    else // kernel thread
81    {
82        list_add_last( &sched->k_root , &thread->sched_list );
83        sched->k_threads_nr++;
84        if( sched->k_last == NULL ) sched->k_last = &thread->sched_list; 
85    }
86
87    // release lock
88    hal_fence();
89    spinlock_unlock( &sched->lock );
90
91}  // end sched_register_thread()
92
93//////////////////////////////////////////////
94thread_t * sched_select( scheduler_t * sched )
95{
96    thread_t     * thread;
97    list_entry_t * current;
98    list_entry_t * last;
99    list_entry_t * root;
100    bool_t         done;
101
102    // take lock protecting sheduler lists
103    spinlock_lock( &sched->lock );
104
105    // first : scan the kernel threads list if not empty
106    if( list_is_empty( &sched->k_root ) == false )
107    {
108        root    = &sched->k_root;
109        last    = sched->k_last;
110        current = last;
111        done    = false;
112
113        while( done == false )
114        {
115            // get next entry in kernel list
116            current = current->next;
117
118            // check exit condition
119            if( current == last ) done = true;
120
121            // skip the root that does not contain a thread
122            if( current == root ) continue;
123
124            // get thread pointer for this entry
125            thread = LIST_ELEMENT( current , thread_t , sched_list );
126
127            // select kernel thread if non blocked and non IDLE
128            if( (thread->blocked == 0)  && (thread->type != THREAD_IDLE) )
129            {
130                spinlock_unlock( &sched->lock );
131                return thread;
132            }
133        } // end loop on kernel threads
134    } // end if kernel threads
135
136    // second : scan the user threads list if not empty
137    if( list_is_empty( &sched->u_root ) == false )
138    {
139        root    = &sched->u_root;
140        last    = sched->u_last;
141        current = last;
142        done    = false;
143
144        while( done == false )
145        {
146            // get next entry in user list
147            current = current->next;
148
149            // check exit condition
150            if( current == last ) done = true;
151
152            // skip the root that does not contain a thread
153            if( current == root ) continue;
154
155            // get thread pointer for this entry
156            thread = LIST_ELEMENT( current , thread_t , sched_list );
157
158            // return thread if non blocked
159            if( thread->blocked == 0 )
160            {
161                spinlock_unlock( &sched->lock );
162                return thread;
163            }
164        } // end loop on user threads
165    } // end if user threads
166
167    // third : return idle thread if no other runnable thread
168    spinlock_unlock( &sched->lock );
169    return sched->idle;
170
171}  // end sched_select()
172
173///////////////////////////////////////////
174void sched_handle_signals( core_t * core )
175{
176
177    list_entry_t * iter;
178    list_entry_t * root;
179    thread_t     * thread;
180    process_t    * process;
181
182    // get pointer on scheduler
183    scheduler_t  * sched = &core->scheduler;
184
185    // get pointer on user threads root
186    root = &sched->u_root;
187
188    // take lock protecting threads lists
189    spinlock_lock( &sched->lock );
190
191    // We use a while to scan the user threads, to control the iterator increment,
192    // because some threads will be destroyed, and we cannot use a LIST_FOREACH()
193
194    // initialise list iterator
195    iter = root->next;
196
197    // scan all user threads
198    while( iter != root )
199    {
200        // get pointer on thread
201        thread = LIST_ELEMENT( iter , thread_t , sched_list );
202
203        // increment iterator
204        iter = iter->next;
205
206        // handle REQ_ACK
207        if( thread->flags & THREAD_FLAG_REQ_ACK )
208        {
209            // check thread blocked
210            assert( (thread->blocked & THREAD_BLOCKED_GLOBAL) , 
211            __FUNCTION__ , "thread not blocked" );
212 
213            // decrement response counter
214            hal_atomic_add( thread->ack_rsp_count , -1 );
215
216            // reset REQ_ACK in thread descriptor
217            thread_reset_req_ack( thread );
218        }
219
220        // handle REQ_DELETE
221        if( thread->flags & THREAD_FLAG_REQ_DELETE )
222        {
223            // get thread process descriptor
224            process = thread->process;
225
226                // release FPU if required
227                if( thread->core->fpu_owner == thread )  thread->core->fpu_owner = NULL;
228
229            // remove thread from scheduler (scheduler lock already taken)
230            uint32_t threads_nr = sched->u_threads_nr;
231
232            assert( (threads_nr != 0) , __FUNCTION__ , "u_threads_nr cannot be 0\n" );
233
234            sched->u_threads_nr = threads_nr - 1;
235            list_unlink( &thread->sched_list );
236            if( threads_nr == 1 ) sched->u_last = NULL;
237
238            // delete thread
239            thread_destroy( thread );
240
241#if DEBUG_SCHED_HANDLE_SIGNALS
242uint32_t cycle = (uint32_t)hal_get_cycles();
243if( DEBUG_SCHED_HANDLE_SIGNALS < cycle )
244printk("\n[DBG] %s : thread %x in proces %x (%x) deleted / cycle %d\n",
245__FUNCTION__ , thread , process->pid , process , cycle );
246#endif
247            // destroy process descriptor if no more threads
248            if( process->th_nr == 0 ) 
249            {
250                // delete process   
251                process_destroy( process );
252
253#if DEBUG_SCHED_HANDLE_SIGNALS
254cycle = (uint32_t)hal_get_cycles();
255if( DEBUG_SCHED_HANDLE_SIGNALS < cycle )
256printk("\n[DBG] %s : process %x has been deleted / cycle %d\n",
257__FUNCTION__ , process->pid , cycle );
258#endif
259
260            }
261        }
262    }
263
264    // release lock
265    hal_fence();
266    spinlock_unlock( &sched->lock );
267
268} // end sched_handle_signals()
269
270////////////////////////////////
271void sched_yield( char * cause )
272{
273    thread_t    * next;
274    thread_t    * current = CURRENT_THREAD;
275    core_t      * core    = current->core;
276    scheduler_t * sched   = &core->scheduler;
277 
278#if (DEBUG_SCHED_YIELD & 0x1)
279if( DEBUG_SCHED_YIELD < (uint32_t)hal_get_cycles() )
280sched_display( core->lid );
281#endif
282
283    // delay the yield if current thread has locks
284    if( (current->local_locks != 0) || (current->remote_locks != 0) )
285    {
286        current->flags |= THREAD_FLAG_SCHED;
287        return;
288    }
289
290    // enter critical section / save SR in current thread descriptor
291    hal_disable_irq( &CURRENT_THREAD->save_sr );
292
293    // loop on threads to select next thread
294    next = sched_select( sched );
295
296    // check next thread kernel_stack overflow
297    assert( (next->signature == THREAD_SIGNATURE),
298    __FUNCTION__ , "kernel stack overflow for thread %x\n", next );
299
300    // check next thread attached to same core as the calling thread
301    assert( (next->core == current->core),
302    __FUNCTION__ , "next core %x != current core %x\n", next->core, current->core );
303
304    // check next thread not blocked when type != IDLE
305    assert( ((next->blocked == 0) || (next->type == THREAD_IDLE)) , __FUNCTION__ ,
306    "next thread %x (%s) is blocked on core[%x,%d]\n", 
307    next->trdid , thread_type_str(next->type) , local_cxy , core->lid );
308
309    // switch contexts and update scheduler state if next != current
310        if( next != current )
311    {
312
313if( (local_cxy == 0X1) && (core->lid == 1) && ((uint32_t)current == 0xcc000) )
314printk("\n@@@@@ cc000 exit at cycle %d\n", (uint32_t)hal_get_cycles() );
315
316if( (local_cxy == 0X1) && (core->lid == 1) && ((uint32_t)next == 0xcc000) )
317printk("\n@@@@@ cc000 enter at cycle %d\n", (uint32_t)hal_get_cycles() );
318
319#if DEBUG_SCHED_YIELD
320uint32_t cycle = (uint32_t)hal_get_cycles();
321if( DEBUG_SCHED_YIELD < cycle )
322printk("\n[DBG] %s : core[%x,%d] / cause = %s\n"
323"      thread %x (%s) (%x,%x) => thread %x (%s) (%x,%x) / cycle %d\n",
324__FUNCTION__, local_cxy, core->lid, cause, 
325current, thread_type_str(current->type), current->process->pid, current->trdid,
326next , thread_type_str(next->type) , next->process->pid , next->trdid , cycle );
327#endif
328
329        // update scheduler
330        sched->current = next;
331        if( next->type == THREAD_USER ) sched->u_last = &next->sched_list;
332        else                            sched->k_last = &next->sched_list;
333
334        // handle FPU ownership
335            if( next->type == THREAD_USER )
336        {
337                if( next == current->core->fpu_owner )  hal_fpu_enable();
338                else                                    hal_fpu_disable();
339        }
340
341        // switch CPU from current thread context to new thread context
342        hal_do_cpu_switch( current->cpu_context, next->cpu_context );
343    }
344    else
345    {
346
347#if (DEBUG_SCHED_YIELD & 1)
348uint32_t cycle = (uint32_t)hal_get_cycles();
349if( DEBUG_SCHED_YIELD < cycle )
350printk("\n[DBG] %s : core[%x,%d] / cause = %s\n"
351"      thread %x (%s) (%x,%x) continue / cycle %d\n",
352__FUNCTION__, local_cxy, core->lid, cause,
353current, thread_type_str(current->type), current->process->pid, current->trdid, cycle );
354#endif
355
356    }
357
358    // handle pending requests for all threads executing on this core.
359    sched_handle_signals( core );
360
361    // exit critical section / restore SR from current thread descriptor
362    hal_restore_irq( CURRENT_THREAD->save_sr );
363
364}  // end sched_yield()
365
366
367///////////////////////////////
368void sched_display( lid_t lid )
369{
370    list_entry_t * iter;
371    thread_t     * thread;
372    uint32_t       save_sr;
373
374    assert( (lid < LOCAL_CLUSTER->cores_nr), __FUNCTION__, "illegal core index %d\n", lid);
375
376    core_t       * core    = &LOCAL_CLUSTER->core_tbl[lid];
377    scheduler_t  * sched   = &core->scheduler;
378   
379    // get pointers on TXT0 chdev
380    xptr_t    txt0_xp  = chdev_dir.txt_tx[0];
381    cxy_t     txt0_cxy = GET_CXY( txt0_xp );
382    chdev_t * txt0_ptr = GET_PTR( txt0_xp );
383
384    // get extended pointer on remote TXT0 chdev lock
385    xptr_t  lock_xp = XPTR( txt0_cxy , &txt0_ptr->wait_lock );
386
387    // get TXT0 lock in busy waiting mode
388    remote_spinlock_lock_busy( lock_xp , &save_sr );
389
390    nolock_printk("\n***** threads on core[%x,%d] / current %x / cycle %d\n",
391            local_cxy , core->lid, sched->current, (uint32_t)hal_get_cycles() );
392
393    // display kernel threads
394    LIST_FOREACH( &sched->k_root , iter )
395    {
396        thread = LIST_ELEMENT( iter , thread_t , sched_list );
397        if (thread->type == THREAD_DEV) 
398        {
399            nolock_printk(" - %s / pid %X / trdid %X / desc %X / block %X / flags %X / %s\n",
400            thread_type_str( thread->type ), thread->process->pid, thread->trdid,
401            thread, thread->blocked, thread->flags, thread->chdev->name );
402        }
403        else
404        {
405            nolock_printk(" - %s / pid %X / trdid %X / desc %X / block %X / flags %X\n",
406            thread_type_str( thread->type ), thread->process->pid, thread->trdid,
407            thread, thread->blocked, thread->flags );
408        }
409    }
410
411    // display user threads
412    LIST_FOREACH( &sched->u_root , iter )
413    {
414        thread = LIST_ELEMENT( iter , thread_t , sched_list );
415        nolock_printk(" - %s / pid %X / trdid %X / desc %X / block %X / flags %X\n",
416        thread_type_str( thread->type ), thread->process->pid, thread->trdid,
417        thread, thread->blocked, thread->flags );
418    }
419
420    // release TXT0 lock
421    remote_spinlock_unlock_busy( lock_xp , save_sr );
422
423}  // end sched_display()
424
Note: See TracBrowser for help on using the repository browser.