source: libcfa/src/concurrency/kernel/startup.cfa @ cd5b58f

ADTast-experimental
Last change on this file since cd5b58f was cd5b58f, checked in by Thierry Delisle <tdelisle@…>, 18 months ago

Changed node link in thread to use dlink called cltr_link

  • Property mode set to 100644
File size: 27.8 KB
Line 
1//
2// Cforall Version 1.0.0 Copyright (C) 2020 University of Waterloo
3//
4// The contents of this file are covered under the licence agreement in the
5// file "LICENCE" distributed with Cforall.
6//
7// kernel/startup.cfa --
8//
9// Author           : Thierry Delisle
10// Created On       : Thu Jul 30 15:12:54 2020
11// Last Modified By :
12// Last Modified On :
13// Update Count     :
14//
15
16#define __cforall_thread__
17#define _GNU_SOURCE
18
19// #define __CFA_DEBUG_PRINT_RUNTIME_CORE__
20
21// C Includes
22#include <errno.h>                                      // errno
23#include <signal.h>
24#include <string.h>                                     // strerror
25#include <unistd.h>                                     // sysconf
26
27extern "C" {
28        #include <limits.h>                             // PTHREAD_STACK_MIN
29        #include <unistd.h>                             // syscall
30        #include <sys/eventfd.h>                        // eventfd
31        #include <sys/mman.h>                           // mprotect
32        #include <sys/resource.h>                       // getrlimit
33}
34
35// CFA Includes
36#include "kernel/private.hfa"
37#include "iofwd.hfa"
38#include "startup.hfa"                                  // STARTUP_PRIORITY_XXX
39#include "limits.hfa"
40#include "math.hfa"
41
42#define CFA_PROCESSOR_USE_MMAP 0
43
44//-----------------------------------------------------------------------------
45// Some assembly required
46#if defined( __i386 )
47        #define CtxGet( ctx ) __asm__ volatile ( \
48                "movl %%esp,%0\n" \
49                "movl %%ebp,%1\n" \
50                : "=rm" (ctx.SP), \
51                  "=rm" (ctx.FP) \
52        )
53#elif defined( __x86_64 )
54        #define CtxGet( ctx ) __asm__ volatile ( \
55                "movq %%rsp,%0\n" \
56                "movq %%rbp,%1\n" \
57                : "=rm" (ctx.SP), \
58                  "=rm" (ctx.FP) \
59        )
60#elif defined( __aarch64__ )
61        #define CtxGet( ctx ) __asm__ volatile ( \
62                "mov %0, sp\n" \
63                "mov %1, fp\n" \
64                : "=rm" (ctx.SP), \
65                  "=rm" (ctx.FP) \
66        )
67#else
68        #error unknown hardware architecture
69#endif
70
71//-----------------------------------------------------------------------------
72// Start and stop routine for the kernel, declared first to make sure they run first
73static void __kernel_startup (void) __attribute__(( constructor( STARTUP_PRIORITY_KERNEL ) ));
74static void __kernel_shutdown(void) __attribute__(( destructor ( STARTUP_PRIORITY_KERNEL ) ));
75
76//-----------------------------------------------------------------------------
77// Static Forward Declarations
78struct current_stack_info_t;
79
80static void * __invoke_processor(void * arg);
81static void __kernel_first_resume( processor * this );
82static void __kernel_last_resume ( processor * this );
83static void init(processor & this, const char name[], cluster & _cltr, thread$ * initT);
84static void deinit(processor & this);
85static void doregister( struct cluster & cltr );
86static void unregister( struct cluster & cltr );
87static void register_tls( processor * this );
88static void unregister_tls( processor * this );
89static void ?{}( coroutine$ & this, current_stack_info_t * info);
90static void ?{}( thread$ & this, current_stack_info_t * info);
91static void ?{}(processorCtx_t & this) {}
92static void ?{}(processorCtx_t & this, processor * proc, current_stack_info_t * info);
93
94#if defined(__CFA_WITH_VERIFY__)
95        static bool verify_fwd_bck_rng(void);
96#endif
97
98//-----------------------------------------------------------------------------
99// Forward Declarations for other modules
100extern void __kernel_alarm_startup(void);
101extern void __kernel_alarm_shutdown(void);
102extern void __cfa_io_start( processor * );
103extern void __cfa_io_stop ( processor * );
104
105//-----------------------------------------------------------------------------
106// Other Forward Declarations
107extern void __wake_proc(processor *);
108extern int cfa_main_returned;                                                   // from interpose.cfa
109uint32_t __global_random_prime = 4_294_967_291u, __global_random_mask = false;
110
111//-----------------------------------------------------------------------------
112// Kernel storage
113KERNEL_STORAGE(cluster,              mainCluster);
114KERNEL_STORAGE(processor,            mainProcessor);
115KERNEL_STORAGE(thread$,              mainThread);
116KERNEL_STORAGE(__stack_t,            mainThreadCtx);
117// KERNEL_STORAGE(__scheduler_RWLock_t, __scheduler_lock);
118KERNEL_STORAGE(eventfd_t,            mainIdleEventFd);
119KERNEL_STORAGE(io_future_t,          mainIdleFuture);
120#if !defined(__CFA_NO_STATISTICS__)
121KERNEL_STORAGE(__stats_t, mainProcStats);
122#endif
123
124cluster              * mainCluster libcfa_public;
125processor            * mainProcessor;
126thread$              * mainThread;
127
128extern "C" {
129        struct { __dllist_t(cluster) list; __spinlock_t lock; } __cfa_dbg_global_clusters;
130}
131
132extern size_t __page_size;
133extern int __map_prot;
134
135//-----------------------------------------------------------------------------
136// Global state
137__thread struct KernelThreadData __cfaabi_tls __attribute__ ((tls_model ( "initial-exec" ))) @= {
138        NULL,                                                                                           // cannot use 0p
139        NULL,
140        false,
141        { 1, false, false },
142        0,
143        { 0, 0 },
144        NULL,
145        #ifdef __CFA_WITH_VERIFY__
146                false,
147                0,
148        #endif
149};
150
151__scheduler_RWLock_t __scheduler_lock @= { 0 };
152
153#if   defined(CFA_HAVE_LINUX_LIBRSEQ)
154        // No data needed
155#elif defined(CFA_HAVE_LINUX_RSEQ_H)
156        extern "Cforall" {
157                __attribute__((aligned(64))) __thread volatile struct rseq __cfaabi_rseq @= {
158                        .cpu_id : RSEQ_CPU_ID_UNINITIALIZED,
159                };
160        }
161#else
162        // No data needed
163#endif
164
165//-----------------------------------------------------------------------------
166// Struct to steal stack
167struct current_stack_info_t {
168        __stack_t * storage;  // pointer to stack object
169        void * base;          // base of stack
170        void * limit;         // stack grows towards stack limit
171        void * context;       // address of cfa_context_t
172};
173
174static void ?{}( current_stack_info_t & this ) {
175        __stack_context_t ctx;
176        CtxGet( ctx );
177        this.base = ctx.FP;
178
179        rlimit r;
180        getrlimit( RLIMIT_STACK, &r);
181        size_t size = r.rlim_cur;
182
183        this.limit = (void *)(((intptr_t)this.base) - size);
184        this.context = &storage_mainThreadCtx;
185}
186
187
188extern void heapManagerCtor();
189extern void heapManagerDtor();
190
191//=============================================================================================
192// Kernel Setup logic
193//=============================================================================================
194//-----------------------------------------------------------------------------
195// Kernel boot procedures
196static void __kernel_startup(void) {
197        /* paranoid */ verify( ! __preemption_enabled() );
198        __cfadbg_print_safe(runtime_core, "Kernel : Starting\n");
199
200        __cfa_dbg_global_clusters.list{ __get };
201        __cfa_dbg_global_clusters.lock{};
202
203        /* paranoid */ verify( verify_fwd_bck_rng() );
204
205        // Initialize the global scheduler lock
206        // __scheduler_lock = (__scheduler_RWLock_t*)&storage___scheduler_lock;
207        (__scheduler_lock){};
208
209        // Initialize the main cluster
210        mainCluster = (cluster *)&storage_mainCluster;
211        (*mainCluster){"Main Cluster", 0};
212
213        __cfadbg_print_safe(runtime_core, "Kernel : Main cluster ready\n");
214
215        // Construct the processor context of the main processor
216        void ?{}(processorCtx_t & this, processor * proc) {
217                (this.self){ "Processor" };
218                this.self.starter = 0p;
219                this.proc = proc;
220        }
221
222        void ?{}(processor & this) with( this ) {
223                ( this.terminated ){};
224                ( this.runner ){};
225                init( this, "Main Processor", *mainCluster, 0p );
226                kernel_thread = __cfaabi_pthread_self();
227
228                runner{ &this };
229                __cfadbg_print_safe(runtime_core, "Kernel : constructed main processor context %p\n", &runner);
230        }
231
232        // Initialize the main processor and the main processor ctx
233        // (the coroutine that contains the processing control flow)
234        mainProcessor = (processor *)&storage_mainProcessor;
235        (*mainProcessor){};
236
237        mainProcessor->idle_wctx.rdbuf = &storage_mainIdleEventFd;
238        mainProcessor->idle_wctx.ftr   = (io_future_t*)&storage_mainIdleFuture;
239        /* paranoid */ verify( sizeof(storage_mainIdleEventFd) == sizeof(eventfd_t) );
240
241        __cfa_io_start( mainProcessor );
242        register_tls( mainProcessor );
243
244        // Start by initializing the main thread
245        // SKULLDUGGERY: the mainThread steals the process main thread
246        // which will then be scheduled by the mainProcessor normally
247        mainThread = (thread$ *)&storage_mainThread;
248        current_stack_info_t info;
249        info.storage = (__stack_t*)&storage_mainThreadCtx;
250        (*mainThread){ &info };
251
252        __cfadbg_print_safe(runtime_core, "Kernel : Main thread ready\n");
253
254        //initialize the global state variables
255        __cfaabi_tls.this_processor = mainProcessor;
256        __cfaabi_tls.this_thread    = mainThread;
257
258        #if !defined( __CFA_NO_STATISTICS__ )
259                __cfaabi_tls.this_stats = (__stats_t *)& storage_mainProcStats;
260                __init_stats( __cfaabi_tls.this_stats );
261        #endif
262        mainProcessor->local_data = &__cfaabi_tls;
263
264        // Enable preemption
265        __kernel_alarm_startup();
266
267        // Add the main thread to the ready queue
268        // once resume is called on mainProcessor->runner the mainThread needs to be scheduled like any normal thread
269        schedule_thread$(mainThread, UNPARK_LOCAL);
270
271        // SKULLDUGGERY: Force a context switch to the main processor to set the main thread's context to the current UNIX
272        // context. Hence, the main thread does not begin through __cfactx_invoke_thread, like all other threads. The trick here is that
273        // mainThread is on the ready queue when this call is made.
274        __kernel_first_resume( __cfaabi_tls.this_processor );
275
276
277        // THE SYSTEM IS NOW COMPLETELY RUNNING
278
279        __cfadbg_print_safe(runtime_core, "Kernel : Started\n--------------------------------------------------\n\n");
280
281        /* paranoid */ verify( ! __preemption_enabled() );
282        enable_interrupts();
283        /* paranoid */ verify( __preemption_enabled() );
284
285}
286
287extern "C"{
288        void pthread_delete_kernel_threads_();
289}
290
291
292static void __kernel_shutdown(void) {
293        if(!cfa_main_returned) return;
294
295        //delete kernel threads for pthread_concurrency
296        pthread_delete_kernel_threads_();
297
298        /* paranoid */ verify( __preemption_enabled() );
299        disable_interrupts();
300        /* paranoid */ verify( ! __preemption_enabled() );
301
302        __cfadbg_print_safe(runtime_core, "\n--------------------------------------------------\nKernel : Shutting down\n");
303
304        // SKULLDUGGERY: Notify the mainProcessor it needs to terminates.
305        // When its coroutine terminates, it return control to the mainThread
306        // which is currently here
307        /* paranoid */ verify( !__atomic_load_n(&mainProcessor->do_terminate, __ATOMIC_ACQUIRE) );
308        __atomic_store_n(&mainProcessor->do_terminate, true, __ATOMIC_RELEASE);
309        __wake_proc( mainProcessor );
310        __kernel_last_resume( __cfaabi_tls.this_processor );
311        mainThread->self_cor.state = Halted;
312
313        // THE SYSTEM IS NOW COMPLETELY STOPPED
314
315        // Disable preemption
316        __kernel_alarm_shutdown();
317
318        #if !defined( __CFA_NO_STATISTICS__ )
319                __stats_t * st = (__stats_t *)& storage_mainProcStats;
320                __tally_stats(mainCluster->stats, st);
321                if( 0 != mainProcessor->print_stats ) {
322                        __print_stats( st, mainProcessor->print_stats, "Processor ", mainProcessor->name, (void*)mainProcessor );
323                }
324                #if defined(CFA_STATS_ARRAY)
325                        __flush_stat( st, "Processor", mainProcessor );
326                #endif
327        #endif
328
329        mainProcessor->local_data = 0p;
330
331        unregister_tls( mainProcessor );
332        __cfa_io_stop( mainProcessor );
333
334        // Destroy the main processor and its context in reverse order of construction
335        // These were manually constructed so we need manually destroy them
336        void ^?{}(processor & this) with( this ){
337                deinit( this );
338
339                /* paranoid */ verify( this.do_terminate == true );
340                __cfadbg_print_safe(runtime_core, "Kernel : destroyed main processor context %p\n", &runner);
341        }
342
343        ^(*mainProcessor){};
344
345        // Final step, destroy the main thread since it is no longer needed
346
347        // Since we provided a stack to this taxk it will not destroy anything
348        /* paranoid */ verify(mainThread->self_cor.stack.storage == (__stack_t*)(((uintptr_t)&storage_mainThreadCtx)| 0x1));
349        ^(*mainThread){};
350
351        ^(*mainCluster){};
352
353        ^(__scheduler_lock){};
354
355        ^(__cfa_dbg_global_clusters.list){};
356        ^(__cfa_dbg_global_clusters.lock){};
357
358        __cfadbg_print_safe(runtime_core, "Kernel : Shutdown complete\n");
359}
360
361//=============================================================================================
362// Kernel Initial Scheduling logic
363//=============================================================================================
364
365// Context invoker for processors
366// This is the entry point for processors (kernel threads) *except* for the main processor
367// It effectively constructs a coroutine by stealing the pthread stack
368static void * __invoke_processor(void * arg) {
369        #if !defined( __CFA_NO_STATISTICS__ )
370                __stats_t local_stats;
371                __init_stats( &local_stats );
372                __cfaabi_tls.this_stats = &local_stats;
373        #endif
374
375        processor * proc = (processor *) arg;
376        __cfaabi_tls.this_processor = proc;
377        __cfaabi_tls.this_thread    = 0p;
378        __cfaabi_tls.preemption_state.[enabled, disable_count] = [false, 1];
379        proc->local_data = &__cfaabi_tls;
380
381        heapManagerCtor();                                                                      // initialize heap
382
383        __cfa_io_start( proc );
384        register_tls( proc );
385
386        // used for idle sleep when io_uring is present
387        io_future_t future;
388        eventfd_t idle_buf;
389        proc->idle_wctx.ftr = &future;
390        proc->idle_wctx.rdbuf = &idle_buf;
391
392
393        // SKULLDUGGERY: We want to create a context for the processor coroutine
394        // which is needed for the 2-step context switch. However, there is no reason
395        // to waste the perfectly valid stack create by pthread.
396        current_stack_info_t info;
397        __stack_t ctx;
398        info.storage = &ctx;
399        (proc->runner){ proc, &info };
400
401        __cfadbg_print_safe(runtime_core, "Coroutine : created stack %p\n", get_coroutine(proc->runner)->stack.storage);
402
403        //Set global state
404        __cfaabi_tls.this_thread = 0p;
405
406        //We now have a proper context from which to schedule threads
407        __cfadbg_print_safe(runtime_core, "Kernel : core %p created (%p, %p)\n", proc, &proc->runner, &ctx);
408
409        // SKULLDUGGERY: Since the coroutine doesn't have its own stack, we can't
410        // resume it to start it like it normally would, it will just context switch
411        // back to here. Instead directly call the main since we already are on the
412        // appropriate stack.
413        get_coroutine(proc->runner)->state = Active;
414        main( proc->runner );
415        get_coroutine(proc->runner)->state = Halted;
416
417        // Main routine of the core returned, the core is now fully terminated
418        __cfadbg_print_safe(runtime_core, "Kernel : core %p main ended (%p)\n", proc, &proc->runner);
419
420        #if !defined(__CFA_NO_STATISTICS__)
421                __tally_stats(proc->cltr->stats, &local_stats);
422                if( 0 != proc->print_stats ) {
423                        __print_stats( &local_stats, proc->print_stats, "Processor ", proc->name, (void*)proc );
424                }
425                #if defined(CFA_STATS_ARRAY)
426                        __flush_stat( &local_stats, "Processor", proc );
427                #endif
428        #endif
429
430        proc->local_data = 0p;
431
432        unregister_tls( proc );
433        __cfa_io_stop( proc );
434
435        heapManagerDtor();                                                                      // de-initialize heap
436
437        return 0p;
438}
439
440static void __kernel_first_resume( processor * this ) {
441        thread$ * src = mainThread;
442        coroutine$ * dst = get_coroutine(this->runner);
443
444        /* paranoid */ verify( ! __preemption_enabled() );
445
446        __cfaabi_tls.this_thread->curr_cor = dst;
447        __stack_prepare( &dst->stack, DEFAULT_STACK_SIZE );
448        __cfactx_start(main, dst, this->runner, __cfactx_invoke_coroutine);
449
450        /* paranoid */ verify( ! __preemption_enabled() );
451
452        dst->last = &src->self_cor;
453        dst->starter = dst->starter ? dst->starter : &src->self_cor;
454
455        // make sure the current state is still correct
456        /* paranoid */ verify(src->state == Ready);
457        src->corctx_flag = true;
458
459        // context switch to specified coroutine
460        verify( dst->context.SP );
461        __cfactx_switch( &src->context, &dst->context );
462        // when __cfactx_switch returns we are back in the src coroutine
463
464        mainThread->curr_cor = &mainThread->self_cor;
465
466        // make sure the current state has been update
467        /* paranoid */ verify(src->state == Active);
468
469        /* paranoid */ verify( ! __preemption_enabled() );
470}
471
472// KERNEL_ONLY
473static void __kernel_last_resume( processor * this ) {
474        coroutine$ * src = &mainThread->self_cor;
475        coroutine$ * dst = get_coroutine(this->runner);
476
477        /* paranoid */ verify( ! __preemption_enabled() );
478        /* paranoid */ verify( dst->starter == src );
479        /* paranoid */ verify( dst->context.SP );
480
481        // SKULLDUGGERY in debug the processors check that the
482        // stack is still within the limit of the stack limits after running a thread.
483        // that check doesn't make sense if we context switch to the processor using the
484        // coroutine semantics. Since this is a special case, use the current context
485        // info to populate these fields.
486        __cfaabi_dbg_debug_do(
487                __stack_context_t ctx;
488                CtxGet( ctx );
489                mainThread->context.SP = ctx.SP;
490                mainThread->context.FP = ctx.FP;
491        )
492
493        // context switch to the processor
494        __cfactx_switch( &src->context, &dst->context );
495}
496
497
498//=============================================================================================
499// Kernel Object Constructors logic
500//=============================================================================================
501//-----------------------------------------------------------------------------
502// Main thread construction
503static void ?{}( coroutine$ & this, current_stack_info_t * info) with( this ) {
504        stack.storage = info->storage;
505        with(*stack.storage) {
506                limit     = info->limit;
507                base      = info->base;
508        }
509        __attribute__((may_alias)) intptr_t * istorage = (intptr_t*) &stack.storage;
510        *istorage |= 0x1;
511        name = "Main Thread";
512        state = Start;
513        starter = 0p;
514        last = 0p;
515        cancellation = 0p;
516}
517
518static void ?{}( thread$ & this, current_stack_info_t * info) with( this ) {
519        ticket = TICKET_RUNNING;
520        state = Start;
521        self_cor{ info };
522        curr_cor = &self_cor;
523        curr_cluster = mainCluster;
524        self_mon.owner = &this;
525        self_mon.recursion = 1;
526        self_mon_p = &self_mon;
527        rdy_link.next = 0p;
528        rdy_link.ts   = MAX;
529        preferred = ready_queue_new_preferred();
530        last_proc = 0p;
531        random_state = __global_random_mask ? __global_random_prime : __global_random_prime ^ rdtscl();
532        #if defined( __CFA_WITH_VERIFY__ )
533                executing = 0p;
534                canary = 0x0D15EA5E0D15EA5Ep;
535        #endif
536
537        doregister(curr_cluster, this);
538
539        monitors{ &self_mon_p, 1, (fptr_t)0 };
540}
541
542//-----------------------------------------------------------------------------
543// Processor
544// Construct the processor context of non-main processors
545static void ?{}(processorCtx_t & this, processor * proc, current_stack_info_t * info) {
546        (this.self){ info };
547        this.proc = proc;
548}
549
550static void init(processor & this, const char name[], cluster & _cltr, thread$ * initT) with( this ) {
551        this.name = name;
552        this.cltr = &_cltr;
553        this.rdq.its = 0;
554        this.rdq.itr = 0;
555        this.rdq.id  = 0;
556        this.rdq.target = MAX;
557        this.rdq.last = MAX;
558        this.rdq.cpu = 0;
559        // this.rdq.cutoff = 0ull;
560        do_terminate = false;
561        preemption_alarm = 0p;
562        pending_preemption = false;
563
564        this.io.ctx = 0p;
565        this.io.pending = false;
566        this.io.dirty   = false;
567
568        this.init.thrd = initT;
569
570        this.local_data = 0p;
571
572        idle_wctx.evfd = eventfd(0, 0);
573        if (idle_wctx.evfd < 0) {
574                abort("KERNEL ERROR: PROCESSOR EVENTFD - %s\n", strerror(errno));
575        }
576
577        idle_wctx.sem = 0;
578        idle_wctx.wake__time = 0;
579
580        // I'm assuming these two are reserved for standard input and output
581        // so I'm using them as sentinels with idle_wctx.
582        /* paranoid */ verify( idle_wctx.evfd != 0 );
583        /* paranoid */ verify( idle_wctx.evfd != 1 );
584
585        #if !defined(__CFA_NO_STATISTICS__)
586                print_stats = 0;
587                print_halts = false;
588        #endif
589
590        __cfadbg_print_safe(runtime_core, "Kernel : core %p created\n", &this);
591}
592
593// Not a ctor, it just preps the destruction but should not destroy members
594static void deinit(processor & this) {
595        close(this.idle_wctx.evfd);
596}
597
598void ?{}(processor & this, const char name[], cluster & _cltr, thread$ * initT) libcfa_public {
599        ( this.terminated ){};
600        ( this.runner ){};
601
602        disable_interrupts();
603                init( this, name, _cltr, initT );
604        enable_interrupts();
605
606        __cfadbg_print_safe(runtime_core, "Kernel : Starting core %p\n", &this);
607
608        this.stack = __create_pthread( &this.kernel_thread, __invoke_processor, (void *)&this );
609}
610
611void ?{}(processor & this, const char name[], cluster & _cltr) libcfa_public {
612        (this){name, _cltr, 0p};
613}
614
615extern size_t __page_size;
616void ^?{}(processor & this) libcfa_public with( this ) {
617        /* paranoid */ verify( !__atomic_load_n(&do_terminate, __ATOMIC_ACQUIRE) );
618        __cfadbg_print_safe(runtime_core, "Kernel : core %p signaling termination\n", &this);
619
620        __atomic_store_n(&do_terminate, true, __ATOMIC_RELAXED);
621        __disable_interrupts_checked();
622                __wake_proc( &this );
623        __enable_interrupts_checked();
624
625        wait( terminated );
626        /* paranoid */ verify( active_processor() != &this);
627
628        __destroy_pthread( kernel_thread, this.stack, 0p );
629
630        disable_interrupts();
631                deinit( this );
632        enable_interrupts();
633}
634
635//-----------------------------------------------------------------------------
636// Cluster
637static void ?{}(__cluster_proc_list & this) {
638        this.fdw   = 0p;
639        this.idle  = 0;
640        this.total = 0;
641}
642
643void ?{}(cluster & this, const char name[], Duration preemption_rate, unsigned num_io, const io_context_params & io_params) libcfa_public with( this ) {
644        this.name = name;
645        this.preemption_rate = preemption_rate;
646        this.sched.readyQ.data = 0p;
647        this.sched.readyQ.tscs = 0p;
648        this.sched.readyQ.count = 0;
649        this.sched.io.tscs = 0p;
650        this.sched.io.data = 0p;
651        this.sched.caches = 0p;
652
653        #if !defined(__CFA_NO_STATISTICS__)
654                print_stats = 0;
655                stats = alloc();
656                __init_stats( stats );
657        #endif
658
659        threads{};
660
661        io.arbiter = create();
662        io.params = io_params;
663
664        managed.procs = 0p;
665        managed.cnt = 0;
666
667        doregister(this);
668
669        // Lock the RWlock so no-one pushes/pops while we are changing the queue
670        disable_interrupts();
671        uint_fast32_t last_size = ready_mutate_lock();
672
673                // Adjust the ready queue size
674                ready_queue_grow( &this );
675
676        // Unlock the RWlock
677        ready_mutate_unlock( last_size );
678        enable_interrupts( false ); // Don't poll, could be in main cluster
679}
680
681void ^?{}(cluster & this) libcfa_public {
682        set_concurrency( this, 0 );
683
684        destroy(this.io.arbiter);
685
686        // Lock the RWlock so no-one pushes/pops while we are changing the queue
687        disable_interrupts();
688        uint_fast32_t last_size = ready_mutate_lock();
689
690                // Adjust the ready queue size
691                ready_queue_shrink( &this );
692
693        // Unlock the RWlock
694        ready_mutate_unlock( last_size );
695
696        ready_queue_close( &this );
697        /* paranoid */ verify( this.sched.readyQ.data == 0p );
698        /* paranoid */ verify( this.sched.readyQ.tscs == 0p );
699        /* paranoid */ verify( this.sched.readyQ.count == 0 );
700        /* paranoid */ verify( this.sched.io.tscs == 0p );
701        /* paranoid */ verify( this.sched.caches == 0p );
702
703        enable_interrupts( false ); // Don't poll, could be in main cluster
704
705
706        #if !defined(__CFA_NO_STATISTICS__)
707                if( 0 != this.print_stats ) {
708                        __print_stats( this.stats, this.print_stats, "Cluster", this.name, (void*)&this );
709                }
710                #if defined(CFA_STATS_ARRAY)
711                        __flush_stat( this.stats, "Cluster", &this );
712                #endif
713                free( this.stats );
714        #endif
715
716        unregister(this);
717}
718
719//=============================================================================================
720// Miscellaneous Initialization
721//=============================================================================================
722//-----------------------------------------------------------------------------
723// Global Queues
724static void doregister( cluster     & cltr ) {
725        lock      ( __cfa_dbg_global_clusters.lock __cfaabi_dbg_ctx2);
726        push_front( __cfa_dbg_global_clusters.list, cltr );
727        unlock    ( __cfa_dbg_global_clusters.lock );
728}
729
730static void unregister( cluster     & cltr ) {
731        lock  ( __cfa_dbg_global_clusters.lock __cfaabi_dbg_ctx2);
732        remove( __cfa_dbg_global_clusters.list, cltr );
733        unlock( __cfa_dbg_global_clusters.lock );
734}
735
736void doregister( cluster * cltr, thread$ & thrd ) {
737        lock      (cltr->thread_list_lock __cfaabi_dbg_ctx2);
738        cltr->nthreads += 1;
739        insert_first(cltr->threads, thrd);
740        unlock    (cltr->thread_list_lock);
741}
742
743void unregister( cluster * cltr, thread$ & thrd ) {
744        lock  (cltr->thread_list_lock __cfaabi_dbg_ctx2);
745        {
746                tytagref( dlink(thread$), dlink(thread$) ) ?`inner( thread$ & this ) = void;
747                with( DLINK_VIA( thread$, thread$.cltr_link ) )
748                        remove( thrd );
749                cltr->nthreads -= 1;
750        }
751        unlock(cltr->thread_list_lock);
752}
753
754static void register_tls( processor * this ) {
755        // Register and Lock the RWlock so no-one pushes/pops while we are changing the queue
756        uint_fast32_t last_size;
757        [this->unique_id, last_size] = ready_mutate_register();
758
759                this->rdq.cpu = __kernel_getcpu();
760
761                this->cltr->procs.total += 1u;
762                insert_last(this->cltr->procs.actives, *this);
763
764                // Adjust the ready queue size
765                ready_queue_grow( this->cltr );
766
767        // Unlock the RWlock
768        ready_mutate_unlock( last_size );
769}
770
771
772static void unregister_tls( processor * this ) {
773        // Lock the RWlock so no-one pushes/pops while we are changing the queue
774        uint_fast32_t last_size = ready_mutate_lock();
775                this->cltr->procs.total -= 1u;
776                remove(*this);
777
778                // clear the cluster so nothing gets pushed to local queues
779                cluster * cltr = this->cltr;
780                this->cltr = 0p;
781
782                // Adjust the ready queue size
783                ready_queue_shrink( cltr );
784
785        // Unlock the RWlock and unregister: we don't need the read_lock any more
786        ready_mutate_unregister( this->unique_id, last_size );
787}
788
789static void check( int ret, const char func[] ) {
790        if ( ret ) {                                                                            // pthread routines return errno values
791                abort( "%s : internal error, error(%d) %s.", func, ret, strerror( ret ) );
792        } // if
793} // Abort
794
795void * __create_pthread( pthread_t * pthread, void * (*start)(void *), void * arg ) {
796        pthread_attr_t attr;
797
798        check( __cfaabi_pthread_attr_init( &attr ), "pthread_attr_init" ); // initialize attribute
799
800        size_t stacksize = max( PTHREAD_STACK_MIN, DEFAULT_STACK_SIZE );
801
802        void * stack;
803        #if CFA_PROCESSOR_USE_MMAP
804                stacksize = ceiling( stacksize, __page_size ) + __page_size;
805                stack = mmap(0p, stacksize, __map_prot, MAP_PRIVATE | MAP_ANONYMOUS, 0, 0);
806                if(stack == ((void*)-1)) {
807                        abort( "pthread stack creation : internal error, mmap failure, error(%d) %s.", errno, strerror( errno ) );
808                }
809                if ( mprotect( stack, __page_size, PROT_NONE ) == -1 ) {
810                        abort( "pthread stack creation : internal error, mprotect failure, error(%d) %s.", errno, strerror( errno ) );
811                } // if
812        #else
813                __cfaabi_dbg_debug_do(
814                        stack = memalign( __page_size, stacksize + __page_size );
815                        // pthread has no mechanism to create the guard page in user supplied stack.
816                        if ( mprotect( stack, __page_size, PROT_NONE ) == -1 ) {
817                                abort( "mprotect : internal error, mprotect failure, error(%d) %s.", errno, strerror( errno ) );
818                        } // if
819                );
820                __cfaabi_dbg_no_debug_do(
821                        stack = malloc( stacksize );
822                );
823        #endif
824
825        check( __cfaabi_pthread_attr_setstack( &attr, stack, stacksize ), "pthread_attr_setstack" );
826        check( __cfaabi_pthread_create( pthread, &attr, start, arg ), "pthread_create" );
827        return stack;
828}
829
830void __destroy_pthread( pthread_t pthread, void * stack, void ** retval ) {
831        int err = __cfaabi_pthread_join( pthread, retval );
832        if( err != 0 ) abort("KERNEL ERROR: joining pthread %p caused error %s\n", (void*)pthread, strerror(err));
833
834        #if CFA_PROCESSOR_USE_MMAP
835                pthread_attr_t attr;
836
837                check( __cfaabi_pthread_attr_init( &attr ), "pthread_attr_init" ); // initialize attribute
838
839                size_t stacksize;
840                // default stack size, normally defined by shell limit
841                check( __cfaabi_pthread_attr_getstacksize( &attr, &stacksize ), "pthread_attr_getstacksize" );
842                assert( stacksize >= PTHREAD_STACK_MIN );
843                stacksize += __page_size;
844
845                if(munmap(stack, stacksize) == -1) {
846                        abort( "pthread stack destruction : internal error, munmap failure, error(%d) %s.", errno, strerror( errno ) );
847                }
848        #else
849                __cfaabi_dbg_debug_do(
850                        // pthread has no mechanism to create the guard page in user supplied stack.
851                        if ( mprotect( stack, __page_size, __map_prot ) == -1 ) {
852                                abort( "mprotect : internal error, mprotect failure, error(%d) %s.", errno, strerror( errno ) );
853                        } // if
854                );
855                free( stack );
856        #endif
857}
858
859unsigned set_concurrency( cluster & this, unsigned new ) libcfa_public {
860        unsigned old = this.managed.cnt;
861
862        __cfadbg_print_safe(runtime_core, "Kernel : resizing cluster from %u to %u\n", old, (unsigned)new);
863
864        // Delete all the old unneeded procs
865        if(old > new) for(i; (unsigned)new ~ old) {
866                __cfadbg_print_safe(runtime_core, "Kernel : destroying %u\n", i);
867                delete( this.managed.procs[i] );
868        }
869
870        // Allocate new array (uses realloc and memcpies the data)
871        this.managed.procs = alloc( new, this.managed.procs`realloc );
872        this.managed.cnt = new;
873
874        // Create the desired new procs
875        if(old < new) for(i; old ~ new) {
876                __cfadbg_print_safe(runtime_core, "Kernel : constructing %u\n", i);
877                (*(this.managed.procs[i] = alloc())){ this };
878        }
879
880        // return the old count
881        return old;
882}
883
884#if defined(__CFA_WITH_VERIFY__)
885static bool verify_fwd_bck_rng(void) {
886        __cfaabi_tls.ready_rng.fwd_seed = 25214903917_l64u * (rdtscl() ^ (uintptr_t)&verify_fwd_bck_rng);
887
888        unsigned values[10];
889        for(i; 10) {
890                values[i] = __tls_rand_fwd();
891        }
892
893        __tls_rand_advance_bck();
894
895        for ( i; 9 -~= 0 ) {
896                if(values[i] != __tls_rand_bck()) {
897                        return false;
898                }
899        }
900
901        return true;
902}
903#endif
Note: See TracBrowser for help on using the repository browser.