source: libcfa/src/concurrency/kernel.hfa @ 7df014f

ADTarm-ehast-experimentalenumforall-pointer-decayjacob/cs343-translationnew-astnew-ast-unique-exprpthread-emulationqualifiedEnum
Last change on this file since 7df014f was 92976d9, checked in by Thierry Delisle <tdelisle@…>, 4 years ago

Implemented basic io_uring setup and poller

  • Property mode set to 100644
File size: 5.1 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
[8def349]25extern "C" {
26#include <pthread.h>
[6b4cdd3]27#include <semaphore.h>
[8def349]28}
29
[db6f06a]30//-----------------------------------------------------------------------------
31// Locks
[bdeba0b]32struct semaphore {
[ea7d2b0]33        __spinlock_t lock;
[bdeba0b]34        int count;
[ac2b598]35        __queue_t($thread) waiting;
[9c31349]36};
37
[242a902]38void  ?{}(semaphore & this, int count = 1);
39void ^?{}(semaphore & this);
[4cedd9f]40void   P (semaphore & this);
[f0ce5f4]41bool   V (semaphore & this);
[9c31349]42
[db6f06a]43
[bd98b58]44//-----------------------------------------------------------------------------
[de94a60]45// Processor
[de6319f]46extern struct cluster * mainCluster;
[bd98b58]47
[e60e0dc]48// Processor
[094476d]49coroutine processorCtx_t {
50        struct processor * proc;
51};
52
[e60e0dc]53// Wrapper around kernel threads
[c84e80a]54struct processor {
[e60e0dc]55        // Main state
[025278e]56        // Coroutine ctx who does keeps the state of the processor
[094476d]57        struct processorCtx_t runner;
[025278e]58
59        // Cluster from which to get threads
[de94a60]60        struct cluster * cltr;
[025278e]61
[de6319f]62        // Name of the processor
63        const char * name;
64
[025278e]65        // Handle to pthreads
66        pthread_t kernel_thread;
[2ac095d]67
[e60e0dc]68        // RunThread data
[025278e]69        // Action to do after a thread is ran
[ac2b598]70        $thread * destroyer;
[c81ebf9]71
[e60e0dc]72        // Preemption data
[025278e]73        // Node which is added in the discrete event simulaiton
74        struct alarm_node_t * preemption_alarm;
75
76        // If true, a preemption was triggered in an unsafe region, the processor must preempt as soon as possible
77        bool pending_preemption;
[c81ebf9]78
[92e7631]79        // Idle lock (kernel semaphore)
80        __bin_sem_t idle;
[85b1deb]81
82        // Termination
83        // Set to true to notify the processor should terminate
84        volatile bool do_terminate;
85
[92e7631]86        // Termination synchronisation (user semaphore)
[85b1deb]87        semaphore terminated;
[de94a60]88
[27f5f71]89        // pthread Stack
90        void * stack;
91
[de94a60]92        // Link lists fields
[ea8b2f7]93        struct __dbg_node_proc {
[de94a60]94                struct processor * next;
95                struct processor * prev;
96        } node;
[14a61b5]97
[e60e0dc]98#ifdef __CFA_DEBUG__
[025278e]99        // Last function to enable preemption on this processor
[cdbfab0]100        const char * last_enable;
[e60e0dc]101#endif
[c84e80a]102};
103
[e3fea42]104void  ?{}(processor & this, const char name[], struct cluster & cltr);
[242a902]105void ^?{}(processor & this);
[c84e80a]106
[de6319f]107static inline void  ?{}(processor & this)                    { this{ "Anonymous Processor", *mainCluster}; }
[de94a60]108static inline void  ?{}(processor & this, struct cluster & cltr)    { this{ "Anonymous Processor", cltr}; }
[e3fea42]109static inline void  ?{}(processor & this, const char name[]) { this{name, *mainCluster }; }
[de6319f]110
[c7a900a]111static inline [processor *&, processor *& ] __get( processor & this ) __attribute__((const)) { return this.node.[next, prev]; }
[de94a60]112
[92976d9]113//-----------------------------------------------------------------------------
114// I/O
115#if defined(HAVE_LINUX_IO_URING_H)
116struct io_uring_sq {
117        uint32_t * head;
118        uint32_t * tail;
119        uint32_t * mask;
120        uint32_t * entries;
121        uint32_t * flags;
122        uint32_t * dropped;
123        uint32_t * array;
124        struct io_uring_sqe * sqes;
125
126        uint32_t sqe_head;
127        uint32_t sqe_tail;
128
129        size_t ring_sz;
130        void * ring_ptr;
131};
132
133struct io_uring_cq {
134        volatile uint32_t * head;
135        volatile uint32_t * tail;
136        uint32_t * mask;
137        struct io_uring_cqe * entries;
138        uint32_t * overflow;
139        struct io_uring_cqe * cqes;
140
141        size_t ring_sz;
142        void * ring_ptr;
143};
144
145struct io_ring {
146        struct io_uring_sq submit_q;
147        struct io_uring_cq completion_q;
148        uint32_t flags;
149        int fd;
150        pthread_t poller;
151        void * stack;
152        volatile bool done;
153};
154#endif
155
[de94a60]156//-----------------------------------------------------------------------------
157// Cluster
158struct cluster {
159        // Ready queue locks
160        __spinlock_t ready_queue_lock;
161
162        // Ready queue for threads
[ac2b598]163        __queue_t($thread) ready_queue;
[de94a60]164
165        // Name of the cluster
166        const char * name;
167
168        // Preemption rate on this cluster
169        Duration preemption_rate;
170
171        // List of processors
[92e7631]172        __spinlock_t idle_lock;
[de94a60]173        __dllist_t(struct processor) procs;
174        __dllist_t(struct processor) idles;
[d4e68a6]175        unsigned int nprocessors;
[de94a60]176
[d4e68a6]177        // List of threads
[a1a17a74]178        __spinlock_t thread_list_lock;
[ac2b598]179        __dllist_t(struct $thread) threads;
[d4e68a6]180        unsigned int nthreads;
[a1a17a74]181
[de94a60]182        // Link lists fields
[ea8b2f7]183        struct __dbg_node_cltr {
[de94a60]184                cluster * next;
185                cluster * prev;
186        } node;
[92976d9]187
188        #if defined(HAVE_LINUX_IO_URING_H)
189                struct io_ring io;
190        #endif
[de94a60]191};
192extern Duration default_preemption();
193
[e3fea42]194void ?{} (cluster & this, const char name[], Duration preemption_rate);
[de94a60]195void ^?{}(cluster & this);
196
197static inline void ?{} (cluster & this)                           { this{"Anonymous Cluster", default_preemption()}; }
198static inline void ?{} (cluster & this, Duration preemption_rate) { this{"Anonymous Cluster", preemption_rate}; }
[e3fea42]199static inline void ?{} (cluster & this, const char name[])        { this{name, default_preemption()}; }
[de94a60]200
[c7a900a]201static inline [cluster *&, cluster *& ] __get( cluster & this ) __attribute__((const)) { return this.node.[next, prev]; }
[de94a60]202
[0f2c555]203static inline struct processor * active_processor() { return TL_GET( this_processor ); } // UNSAFE
204static inline struct cluster   * active_cluster  () { return TL_GET( this_processor )->cltr; }
[d4e68a6]205
[8118303]206// Local Variables: //
[6b0b624]207// mode: c //
208// tab-width: 4 //
[8118303]209// End: //
Note: See TracBrowser for help on using the repository browser.