// // Cforall Version 1.0.0 Copyright (C) 2016 University of Waterloo // // The contents of this file are covered under the licence agreement in the // file "LICENCE" distributed with Cforall. // // kernel -- // // Author : Thierry Delisle // Created On : Tue Jan 17 12:27:26 2017 // Last Modified By : Peter A. Buhr // Last Modified On : Wed Dec 4 07:54:51 2019 // Update Count : 18 // #pragma once #include #include "invoke.h" #include "time_t.hfa" #include "coroutine.hfa" extern "C" { #include #include } //----------------------------------------------------------------------------- // Locks struct semaphore { __spinlock_t lock; int count; __queue_t(thread_desc) waiting; }; void ?{}(semaphore & this, int count = 1); void ^?{}(semaphore & this); void P (semaphore & this); void V (semaphore & this); //----------------------------------------------------------------------------- // Processor extern struct cluster * mainCluster; enum FinishOpCode { No_Action, Release, Schedule, Release_Schedule, Release_Multi, Release_Multi_Schedule, Callback }; typedef void (*__finish_callback_fptr_t)(void); //TODO use union, many of these fields are mutually exclusive (i.e. MULTI vs NOMULTI) struct FinishAction { FinishOpCode action_code; /* // Union of possible actions union { // Option 1 : locks and threads struct { // 1 thread or N thread union { thread_desc * thrd; struct { thread_desc ** thrds; unsigned short thrd_count; }; }; // 1 lock or N lock union { __spinlock_t * lock; struct { __spinlock_t ** locks; unsigned short lock_count; }; }; }; // Option 2 : action pointer __finish_callback_fptr_t callback; }; /*/ thread_desc * thrd; thread_desc ** thrds; unsigned short thrd_count; __spinlock_t * lock; __spinlock_t ** locks; unsigned short lock_count; __finish_callback_fptr_t callback; //*/ }; static inline void ?{}(FinishAction & this) { this.action_code = No_Action; this.thrd = 0p; this.lock = 0p; } static inline void ^?{}(FinishAction &) {} // Processor coroutine processorCtx_t { struct processor * proc; }; // Wrapper around kernel threads struct processor { // Main state // Coroutine ctx who does keeps the state of the processor struct processorCtx_t runner; // Cluster from which to get threads struct cluster * cltr; unsigned int id; // Name of the processor const char * name; // Handle to pthreads pthread_t kernel_thread; // RunThread data // Action to do after a thread is ran struct FinishAction finish; // Preemption data // Node which is added in the discrete event simulaiton struct alarm_node_t * preemption_alarm; // If true, a preemption was triggered in an unsafe region, the processor must preempt as soon as possible bool pending_preemption; // Idle lock __bin_sem_t idleLock; // Termination // Set to true to notify the processor should terminate volatile bool do_terminate; // Termination synchronisation semaphore terminated; // pthread Stack void * stack; // Link lists fields struct __dbg_node_proc { struct processor * next; struct processor * prev; } node; #ifdef __CFA_DEBUG__ // Last function to enable preemption on this processor const char * last_enable; #endif }; void ?{}(processor & this, const char * name, struct cluster & cltr); void ^?{}(processor & this); static inline void ?{}(processor & this) { this{ "Anonymous Processor", *mainCluster}; } static inline void ?{}(processor & this, struct cluster & cltr) { this{ "Anonymous Processor", cltr}; } static inline void ?{}(processor & this, const char * name) { this{name, *mainCluster }; } static inline [processor *&, processor *& ] __get( processor & this ) { return this.node.[next, prev]; } //----------------------------------------------------------------------------- // Cluster Tools struct __processor_id; // Reader-Writer lock protecting the ready-queue struct __clusterRWLock_t { // total cachelines allocated unsigned int max; // cachelines currently in use volatile unsigned int alloc; // cachelines ready to itereate over // (!= to alloc when thread is in second half of doregister) volatile unsigned int ready; // writer lock volatile bool lock; // data pointer __processor_id * data; }; void ?{}(__clusterRWLock_t & this); void ^?{}(__clusterRWLock_t & this); // Underlying sub quues of the ready queue struct __attribute__((aligned(128))) __intrusive_ready_queue_t { // spin lock protecting the queue volatile bool lock; unsigned int last_id; unsigned int count; // anchor for the head and the tail of the queue struct __sentinel_t { // Link lists fields // instrusive link field for threads // must be exactly as in thread_desc __thread_desc_link link; } before, after; // Optional statistic counters #if !defined(__CFA_NO_SCHED_STATS__) struct __attribute__((aligned(64))) { // difference between number of push and pops ssize_t diff; // total number of pushes and pops size_t push; size_t pop ; } stat; #endif }; void ?{}(__intrusive_ready_queue_t & this); void ^?{}(__intrusive_ready_queue_t & this); typedef unsigned long long __cfa_readyQ_mask_t; // enum { // __cfa_ready_queue_mask_size = (64 - sizeof(size_t)) / sizeof(size_t), // __cfa_max_ready_queues = __cfa_ready_queue_mask_size * 8 * sizeof(size_t) // }; #define __cfa_readyQ_mask_size ((64 - sizeof(size_t)) / sizeof(__cfa_readyQ_mask_t)) #define __cfa_max_readyQs (__cfa_readyQ_mask_size * 8 * sizeof(__cfa_readyQ_mask_t)) //TODO adjust cache size to ARCHITECTURE struct __attribute__((aligned(128))) __ready_queue_t { struct { volatile size_t count; volatile __cfa_readyQ_mask_t mask[ __cfa_readyQ_mask_size ]; } empty; struct __attribute__((aligned(64))) { __intrusive_ready_queue_t * volatile data; volatile size_t count; } list; #if !defined(__CFA_NO_STATISTICS__) __attribute__((aligned(64))) struct { struct { struct { volatile size_t attempt; volatile size_t success; } push; struct { volatile size_t maskrds; volatile size_t attempt; volatile size_t success; } pop; } pick; struct { volatile size_t value; volatile size_t count; } full; } global_stats; #endif }; void ?{}(__ready_queue_t & this); void ^?{}(__ready_queue_t & this); //----------------------------------------------------------------------------- // Cluster struct cluster { // Ready queue locks __clusterRWLock_t ready_lock; // Ready queue for threads __ready_queue_t ready_queue; // Name of the cluster const char * name; // Preemption rate on this cluster Duration preemption_rate; // List of processors __spinlock_t proc_list_lock; __dllist_t(struct processor) idles; // List of threads __spinlock_t thread_list_lock; __dllist_t(struct thread_desc) threads; unsigned int nthreads; // Link lists fields struct __dbg_node_cltr { cluster * next; cluster * prev; } node; }; extern Duration default_preemption(); void ?{} (cluster & this, const char * name, Duration preemption_rate); void ^?{}(cluster & this); static inline void ?{} (cluster & this) { this{"Anonymous Cluster", default_preemption()}; } static inline void ?{} (cluster & this, Duration preemption_rate) { this{"Anonymous Cluster", preemption_rate}; } static inline void ?{} (cluster & this, const char * name) { this{name, default_preemption()}; } static inline [cluster *&, cluster *& ] __get( cluster & this ) { return this.node.[next, prev]; } static inline struct processor * active_processor() { return TL_GET( this_processor ); } // UNSAFE static inline struct cluster * active_cluster () { return TL_GET( this_processor )->cltr; } // Local Variables: // // mode: c // // tab-width: 4 // // End: //