source: libcfa/src/concurrency/kernel.hfa @ 2d8f7b0

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

Implemented basic non-blocking io

  • Property mode set to 100644
File size: 5.7 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        // Head and tail of the ring (associated with array)
118        volatile uint32_t * head;
119        volatile uint32_t * tail;
120
121        // The actual kernel ring which uses head/tail
122        // indexes into the sqes arrays
123        uint32_t * array;
124
125        // number of entries and mask to go with it
126        const uint32_t * num;
127        const uint32_t * mask;
128
129        // Submission flags (Not sure what for)
130        uint32_t * flags;
131
132        // number of sqes not submitted (whatever that means)
133        uint32_t * dropped;
134
135        // Like head/tail but not seen by the kernel
136        volatile uint32_t alloc;
137
138        __spinlock_t lock;
139
140        // A buffer of sqes (not the actual ring)
141        struct io_uring_sqe * sqes;
142
143        // The location and size of the mmaped area
144        void * ring_ptr;
145        size_t ring_sz;
146};
147
148struct io_uring_cq {
149        // Head and tail of the ring
150        volatile uint32_t * head;
151        volatile uint32_t * tail;
152
153        // number of entries and mask to go with it
154        const uint32_t * mask;
155        const uint32_t * num;
156
157        // number of cqes not submitted (whatever that means)
158        uint32_t * overflow;
159
160        // the kernel ring
161        struct io_uring_cqe * cqes;
162
163        // The location and size of the mmaped area
164        void * ring_ptr;
165        size_t ring_sz;
166};
167
168struct io_ring {
169        struct io_uring_sq submit_q;
170        struct io_uring_cq completion_q;
171        uint32_t flags;
172        int fd;
173        pthread_t poller;
174        void * stack;
175        volatile bool done;
176        semaphore submit;
177};
178#endif
179
180//-----------------------------------------------------------------------------
181// Cluster
182struct cluster {
183        // Ready queue locks
184        __spinlock_t ready_queue_lock;
185
186        // Ready queue for threads
187        __queue_t($thread) ready_queue;
188
189        // Name of the cluster
190        const char * name;
191
192        // Preemption rate on this cluster
193        Duration preemption_rate;
194
195        // List of processors
196        __spinlock_t idle_lock;
197        __dllist_t(struct processor) procs;
198        __dllist_t(struct processor) idles;
199        unsigned int nprocessors;
200
201        // List of threads
202        __spinlock_t thread_list_lock;
203        __dllist_t(struct $thread) threads;
204        unsigned int nthreads;
205
206        // Link lists fields
207        struct __dbg_node_cltr {
208                cluster * next;
209                cluster * prev;
210        } node;
211
212        #if defined(HAVE_LINUX_IO_URING_H)
213                struct io_ring io;
214        #endif
215};
216extern Duration default_preemption();
217
218void ?{} (cluster & this, const char name[], Duration preemption_rate);
219void ^?{}(cluster & this);
220
221static inline void ?{} (cluster & this)                           { this{"Anonymous Cluster", default_preemption()}; }
222static inline void ?{} (cluster & this, Duration preemption_rate) { this{"Anonymous Cluster", preemption_rate}; }
223static inline void ?{} (cluster & this, const char name[])        { this{name, default_preemption()}; }
224
225static inline [cluster *&, cluster *& ] __get( cluster & this ) __attribute__((const)) { return this.node.[next, prev]; }
226
227static inline struct processor * active_processor() { return TL_GET( this_processor ); } // UNSAFE
228static inline struct cluster   * active_cluster  () { return TL_GET( this_processor )->cltr; }
229
230// Local Variables: //
231// mode: c //
232// tab-width: 4 //
233// End: //
Note: See TracBrowser for help on using the repository browser.