source: libcfa/src/concurrency/kernel.hfa @ 52769ba

ADTarm-ehast-experimentalenumforall-pointer-decayjacob/cs343-translationnew-astnew-ast-unique-exprpthread-emulationqualifiedEnum
Last change on this file since 52769ba was 13c5e19, checked in by Thierry Delisle <tdelisle@…>, 4 years ago
  • Moved snzi and subqueues outside of ready_queue.cfa.
  • Added random.hfa with multiple prng.
  • Minor optimizations to ready-queue
  • Stats now track number of local pops( bias pops )
  • Fixed stats for io
  • Fixed calculaton of nprocessors
  • Fixed IO to work with new ready-queue
  • Property mode set to 100644
File size: 6.5 KB
RevLine 
[8118303]1//
2// Cforall Version 1.0.0 Copyright (C) 2016 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//
[75a17f1]7// kernel --
[8118303]8//
9// Author           : Thierry Delisle
[75f3522]10// Created On       : Tue Jan 17 12:27:26 2017
[6b0b624]11// Last Modified By : Peter A. Buhr
[e3fea42]12// Last Modified On : Tue Feb  4 12:29:26 2020
13// Update Count     : 22
[8118303]14//
15
[6b0b624]16#pragma once
[8118303]17
[c84e80a]18#include <stdbool.h>
[92976d9]19#include <stdint.h>
[8118303]20
[bd98b58]21#include "invoke.h"
[73abe95]22#include "time_t.hfa"
[d76bd79]23#include "coroutine.hfa"
[bd98b58]24
[64a7146]25#include "containers/stackLockFree.hfa"
26
[8def349]27extern "C" {
28#include <pthread.h>
[6b4cdd3]29#include <semaphore.h>
[8def349]30}
31
[db6f06a]32//-----------------------------------------------------------------------------
33// Locks
[bdeba0b]34struct semaphore {
[ea7d2b0]35        __spinlock_t lock;
[bdeba0b]36        int count;
[ac2b598]37        __queue_t($thread) waiting;
[9c31349]38};
39
[242a902]40void  ?{}(semaphore & this, int count = 1);
41void ^?{}(semaphore & this);
[71c8b7e]42bool   P (semaphore & this);
[f0ce5f4]43bool   V (semaphore & this);
[d384787]44bool   V (semaphore & this, unsigned count);
[9c31349]45
[db6f06a]46
[bd98b58]47//-----------------------------------------------------------------------------
[de94a60]48// Processor
[de6319f]49extern struct cluster * mainCluster;
[bd98b58]50
[9b1dcc2]51// Processor id, required for scheduling threads
52struct __processor_id_t {
53        unsigned id;
[8834751]54
55        #if !defined(__CFA_NO_STATISTICS__)
56                struct __stats_t * stats;
57        #endif
[9b1dcc2]58};
59
[094476d]60coroutine processorCtx_t {
61        struct processor * proc;
62};
63
[e60e0dc]64// Wrapper around kernel threads
[37ba662]65struct __attribute__((aligned(128))) processor {
[e60e0dc]66        // Main state
[37ba662]67        inline __processor_id_t;
[025278e]68
69        // Cluster from which to get threads
[de94a60]70        struct cluster * cltr;
[025278e]71
[37ba662]72        // Set to true to notify the processor should terminate
73        volatile bool do_terminate;
74
75        // Coroutine ctx who does keeps the state of the processor
76        struct processorCtx_t runner;
77
[de6319f]78        // Name of the processor
79        const char * name;
80
[025278e]81        // Handle to pthreads
82        pthread_t kernel_thread;
[2ac095d]83
[e60e0dc]84        // RunThread data
[025278e]85        // Action to do after a thread is ran
[ac2b598]86        $thread * destroyer;
[c81ebf9]87
[e60e0dc]88        // Preemption data
[025278e]89        // Node which is added in the discrete event simulaiton
90        struct alarm_node_t * preemption_alarm;
91
92        // If true, a preemption was triggered in an unsafe region, the processor must preempt as soon as possible
93        bool pending_preemption;
[c81ebf9]94
[92e7631]95        // Idle lock (kernel semaphore)
96        __bin_sem_t idle;
[85b1deb]97
[92e7631]98        // Termination synchronisation (user semaphore)
[85b1deb]99        semaphore terminated;
[de94a60]100
[27f5f71]101        // pthread Stack
102        void * stack;
103
[de94a60]104        // Link lists fields
[64a7146]105        Link(processor) link;
[14a61b5]106
[e60e0dc]107#ifdef __CFA_DEBUG__
[025278e]108        // Last function to enable preemption on this processor
[cdbfab0]109        const char * last_enable;
[e60e0dc]110#endif
[c84e80a]111};
112
[e3fea42]113void  ?{}(processor & this, const char name[], struct cluster & cltr);
[242a902]114void ^?{}(processor & this);
[c84e80a]115
[de6319f]116static inline void  ?{}(processor & this)                    { this{ "Anonymous Processor", *mainCluster}; }
[de94a60]117static inline void  ?{}(processor & this, struct cluster & cltr)    { this{ "Anonymous Processor", cltr}; }
[e3fea42]118static inline void  ?{}(processor & this, const char name[]) { this{name, *mainCluster }; }
[de6319f]119
[0f89d4f]120static inline Link(processor) * ?`next( processor * this ) { return &this->link; }
[de94a60]121
[92976d9]122//-----------------------------------------------------------------------------
123// I/O
[61dd73d]124struct __io_data;
[92976d9]125
[5dadc9b]126#define CFA_CLUSTER_IO_POLLER_USER_THREAD    1 << 0 // 0x1
127#define CFA_CLUSTER_IO_POLLER_THREAD_SUBMITS 1 << 1 // 0x2
128// #define CFA_CLUSTER_IO_POLLER_KERNEL_SIDE 1 << 2 // 0x4
[dd4e2d7]129#define CFA_CLUSTER_IO_BUFFLEN_OFFSET        16
[de94a60]130
[7768b8d]131
132//-----------------------------------------------------------------------------
133// Cluster Tools
[dca5802]134
135// Intrusives lanes which are used by the relaxed ready queue
[61d7bec]136struct __attribute__((aligned(128))) __intrusive_lane_t;
[dca5802]137void  ?{}(__intrusive_lane_t & this);
138void ^?{}(__intrusive_lane_t & this);
[7768b8d]139
[61d7bec]140// Counter used for wether or not the lanes are all empty
141struct __attribute__((aligned(128))) __snzi_node_t;
142struct __snzi_t {
143        unsigned mask;
144        int root;
145        __snzi_node_t * nodes;
146};
[b798713]147
[61d7bec]148void  ?{}( __snzi_t & this, unsigned depth );
149void ^?{}( __snzi_t & this );
[b798713]150
151//TODO adjust cache size to ARCHITECTURE
[dca5802]152// Structure holding the relaxed ready queue
[37ba662]153struct __ready_queue_t {
[dca5802]154        // Data tracking how many/which lanes are used
155        // Aligned to 128 for cache locality
[61d7bec]156        __snzi_t snzi;
[dca5802]157
158        // Data tracking the actual lanes
159        // On a seperate cacheline from the used struct since
160        // used can change on each push/pop but this data
161        // only changes on shrink/grow
[37ba662]162        struct {
[dca5802]163                // Arary of lanes
164                __intrusive_lane_t * volatile data;
165
166                // Number of lanes (empty or not)
[b798713]167                volatile size_t count;
[dca5802]168        } lanes;
[b798713]169};
170
171void  ?{}(__ready_queue_t & this);
172void ^?{}(__ready_queue_t & this);
173
[de94a60]174//-----------------------------------------------------------------------------
175// Cluster
[37ba662]176struct __attribute__((aligned(128))) cluster {
[de94a60]177        // Ready queue for threads
[b798713]178        __ready_queue_t ready_queue;
[de94a60]179
180        // Name of the cluster
181        const char * name;
182
183        // Preemption rate on this cluster
184        Duration preemption_rate;
185
[64a7146]186        // List of idle processors
187        StackLF(processor) idles;
[13c5e19]188        volatile unsigned int nprocessors;
[de94a60]189
[d4e68a6]190        // List of threads
[a1a17a7]191        __spinlock_t thread_list_lock;
[ac2b598]192        __dllist_t(struct $thread) threads;
[d4e68a6]193        unsigned int nthreads;
[a1a17a7]194
[de94a60]195        // Link lists fields
[ea8b2f7]196        struct __dbg_node_cltr {
[de94a60]197                cluster * next;
198                cluster * prev;
199        } node;
[92976d9]200
[61dd73d]201        struct __io_data * io;
[038be32]202
203        #if !defined(__CFA_NO_STATISTICS__)
204                bool print_stats;
[8834751]205                struct __stats_t * stats;
[038be32]206        #endif
[de94a60]207};
208extern Duration default_preemption();
209
[dd4e2d7]210void ?{} (cluster & this, const char name[], Duration preemption_rate, unsigned flags);
[de94a60]211void ^?{}(cluster & this);
212
[dd4e2d7]213static inline void ?{} (cluster & this)                                           { this{"Anonymous Cluster", default_preemption(), 0}; }
214static inline void ?{} (cluster & this, Duration preemption_rate)                 { this{"Anonymous Cluster", preemption_rate, 0}; }
215static inline void ?{} (cluster & this, const char name[])                        { this{name, default_preemption(), 0}; }
216static inline void ?{} (cluster & this, unsigned flags)                           { this{"Anonymous Cluster", default_preemption(), flags}; }
217static inline void ?{} (cluster & this, Duration preemption_rate, unsigned flags) { this{"Anonymous Cluster", preemption_rate, flags}; }
218static inline void ?{} (cluster & this, const char name[], unsigned flags)        { this{name, default_preemption(), flags}; }
[de94a60]219
[c7a900a]220static inline [cluster *&, cluster *& ] __get( cluster & this ) __attribute__((const)) { return this.node.[next, prev]; }
[de94a60]221
[d4e68a6]222static inline struct processor * active_processor() { return TL_GET( this_processor ); } // UNSAFE
223static inline struct cluster   * active_cluster  () { return TL_GET( this_processor )->cltr; }
224
[038be32]225#if !defined(__CFA_NO_STATISTICS__)
226        static inline void print_stats_at_exit( cluster & this ) {
227                this.print_stats = true;
228        }
229#endif
230
[8118303]231// Local Variables: //
[6b0b624]232// mode: c //
233// tab-width: 4 //
[8118303]234// End: //
Note: See TracBrowser for help on using the repository browser.