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
Line 
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//
7// kernel --
8//
9// Author           : Thierry Delisle
10// Created On       : Tue Jan 17 12:27:26 2017
11// Last Modified By : Peter A. Buhr
12// Last Modified On : Tue Feb  4 12:29:26 2020
13// Update Count     : 22
14//
15
16#pragma once
17
18#include <stdbool.h>
19#include <stdint.h>
20
21#include "invoke.h"
22#include "time_t.hfa"
23#include "coroutine.hfa"
24
25extern "C" {
26#include <pthread.h>
27#include <semaphore.h>
28}
29
30//-----------------------------------------------------------------------------
31// Locks
32struct semaphore {
33        __spinlock_t lock;
34        int count;
35        __queue_t($thread) waiting;
36};
37
38void  ?{}(semaphore & this, int count = 1);
39void ^?{}(semaphore & this);
40void   P (semaphore & this);
41bool   V (semaphore & this);
42
43
44//-----------------------------------------------------------------------------
45// Processor
46extern struct cluster * mainCluster;
47
48// Processor
49coroutine processorCtx_t {
50        struct processor * proc;
51};
52
53// Wrapper around kernel threads
54struct processor {
55        // Main state
56        // Coroutine ctx who does keeps the state of the processor
57        struct processorCtx_t runner;
58
59        // Cluster from which to get threads
60        struct cluster * cltr;
61
62        // Name of the processor
63        const char * name;
64
65        // Handle to pthreads
66        pthread_t kernel_thread;
67
68        // RunThread data
69        // Action to do after a thread is ran
70        $thread * destroyer;
71
72        // Preemption data
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;
78
79        // Idle lock (kernel semaphore)
80        __bin_sem_t idle;
81
82        // Termination
83        // Set to true to notify the processor should terminate
84        volatile bool do_terminate;
85
86        // Termination synchronisation (user semaphore)
87        semaphore terminated;
88
89        // pthread Stack
90        void * stack;
91
92        // Link lists fields
93        struct __dbg_node_proc {
94                struct processor * next;
95                struct processor * prev;
96        } node;
97
98#ifdef __CFA_DEBUG__
99        // Last function to enable preemption on this processor
100        const char * last_enable;
101#endif
102};
103
104void  ?{}(processor & this, const char name[], struct cluster & cltr);
105void ^?{}(processor & this);
106
107static inline void  ?{}(processor & this)                    { this{ "Anonymous Processor", *mainCluster}; }
108static inline void  ?{}(processor & this, struct cluster & cltr)    { this{ "Anonymous Processor", cltr}; }
109static inline void  ?{}(processor & this, const char name[]) { this{name, *mainCluster }; }
110
111static inline [processor *&, processor *& ] __get( processor & this ) __attribute__((const)) { return this.node.[next, prev]; }
112
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
156//-----------------------------------------------------------------------------
157// Cluster
158struct cluster {
159        // Ready queue locks
160        __spinlock_t ready_queue_lock;
161
162        // Ready queue for threads
163        __queue_t($thread) ready_queue;
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
172        __spinlock_t idle_lock;
173        __dllist_t(struct processor) procs;
174        __dllist_t(struct processor) idles;
175        unsigned int nprocessors;
176
177        // List of threads
178        __spinlock_t thread_list_lock;
179        __dllist_t(struct $thread) threads;
180        unsigned int nthreads;
181
182        // Link lists fields
183        struct __dbg_node_cltr {
184                cluster * next;
185                cluster * prev;
186        } node;
187
188        #if defined(HAVE_LINUX_IO_URING_H)
189                struct io_ring io;
190        #endif
191};
192extern Duration default_preemption();
193
194void ?{} (cluster & this, const char name[], Duration preemption_rate);
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}; }
199static inline void ?{} (cluster & this, const char name[])        { this{name, default_preemption()}; }
200
201static inline [cluster *&, cluster *& ] __get( cluster & this ) __attribute__((const)) { return this.node.[next, prev]; }
202
203static inline struct processor * active_processor() { return TL_GET( this_processor ); } // UNSAFE
204static inline struct cluster   * active_cluster  () { return TL_GET( this_processor )->cltr; }
205
206// Local Variables: //
207// mode: c //
208// tab-width: 4 //
209// End: //
Note: See TracBrowser for help on using the repository browser.