source: libcfa/src/concurrency/kernel.hfa @ 71c8b7e

arm-ehjacob/cs343-translationnew-astnew-ast-unique-expr
Last change on this file since 71c8b7e was 71c8b7e, checked in by Thierry Delisle <tdelisle@…>, 21 months ago

Semaphore P() now returned whether or not it block

  • 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);
40bool   P (semaphore & this);
41bool   V (semaphore & this);
42bool   V (semaphore & this, unsigned count);
43
44
45//-----------------------------------------------------------------------------
46// Processor
47extern struct cluster * mainCluster;
48
49// Processor
50coroutine processorCtx_t {
51        struct processor * proc;
52};
53
54// Wrapper around kernel threads
55struct processor {
56        // Main state
57        // Coroutine ctx who does keeps the state of the processor
58        struct processorCtx_t runner;
59
60        // Cluster from which to get threads
61        struct cluster * cltr;
62
63        // Name of the processor
64        const char * name;
65
66        // Handle to pthreads
67        pthread_t kernel_thread;
68
69        // RunThread data
70        // Action to do after a thread is ran
71        $thread * destroyer;
72
73        // Preemption data
74        // Node which is added in the discrete event simulaiton
75        struct alarm_node_t * preemption_alarm;
76
77        // If true, a preemption was triggered in an unsafe region, the processor must preempt as soon as possible
78        bool pending_preemption;
79
80        // Idle lock (kernel semaphore)
81        __bin_sem_t idle;
82
83        // Termination
84        // Set to true to notify the processor should terminate
85        volatile bool do_terminate;
86
87        // Termination synchronisation (user semaphore)
88        semaphore terminated;
89
90        // pthread Stack
91        void * stack;
92
93        // Link lists fields
94        struct __dbg_node_proc {
95                struct processor * next;
96                struct processor * prev;
97        } node;
98
99#ifdef __CFA_DEBUG__
100        // Last function to enable preemption on this processor
101        const char * last_enable;
102#endif
103};
104
105void  ?{}(processor & this, const char name[], struct cluster & cltr);
106void ^?{}(processor & this);
107
108static inline void  ?{}(processor & this)                    { this{ "Anonymous Processor", *mainCluster}; }
109static inline void  ?{}(processor & this, struct cluster & cltr)    { this{ "Anonymous Processor", cltr}; }
110static inline void  ?{}(processor & this, const char name[]) { this{name, *mainCluster }; }
111
112static inline [processor *&, processor *& ] __get( processor & this ) __attribute__((const)) { return this.node.[next, prev]; }
113
114//-----------------------------------------------------------------------------
115// I/O
116struct __io_data;
117
118#define CFA_CLUSTER_IO_POLLER_USER_THREAD 1 << 0
119// #define CFA_CLUSTER_IO_POLLER_KERNEL_SIDE 1 << 1
120
121//-----------------------------------------------------------------------------
122// Cluster
123struct cluster {
124        // Ready queue locks
125        __spinlock_t ready_queue_lock;
126
127        // Ready queue for threads
128        __queue_t($thread) ready_queue;
129
130        // Name of the cluster
131        const char * name;
132
133        // Preemption rate on this cluster
134        Duration preemption_rate;
135
136        // List of processors
137        __spinlock_t idle_lock;
138        __dllist_t(struct processor) procs;
139        __dllist_t(struct processor) idles;
140        unsigned int nprocessors;
141
142        // List of threads
143        __spinlock_t thread_list_lock;
144        __dllist_t(struct $thread) threads;
145        unsigned int nthreads;
146
147        // Link lists fields
148        struct __dbg_node_cltr {
149                cluster * next;
150                cluster * prev;
151        } node;
152
153        struct __io_data * io;
154
155        #if !defined(__CFA_NO_STATISTICS__)
156                bool print_stats;
157        #endif
158};
159extern Duration default_preemption();
160
161void ?{} (cluster & this, const char name[], Duration preemption_rate, int flags);
162void ^?{}(cluster & this);
163
164static inline void ?{} (cluster & this)                                      { this{"Anonymous Cluster", default_preemption(), 0}; }
165static inline void ?{} (cluster & this, Duration preemption_rate)            { this{"Anonymous Cluster", preemption_rate, 0}; }
166static inline void ?{} (cluster & this, const char name[])                   { this{name, default_preemption(), 0}; }
167static inline void ?{} (cluster & this, int flags)                           { this{"Anonymous Cluster", default_preemption(), flags}; }
168static inline void ?{} (cluster & this, Duration preemption_rate, int flags) { this{"Anonymous Cluster", preemption_rate, flags}; }
169static inline void ?{} (cluster & this, const char name[], int flags)        { this{name, default_preemption(), flags}; }
170
171static inline [cluster *&, cluster *& ] __get( cluster & this ) __attribute__((const)) { return this.node.[next, prev]; }
172
173static inline struct processor * active_processor() { return TL_GET( this_processor ); } // UNSAFE
174static inline struct cluster   * active_cluster  () { return TL_GET( this_processor )->cltr; }
175
176#if !defined(__CFA_NO_STATISTICS__)
177        static inline void print_stats_at_exit( cluster & this ) {
178                this.print_stats = true;
179        }
180#endif
181
182// Local Variables: //
183// mode: c //
184// tab-width: 4 //
185// End: //
Note: See TracBrowser for help on using the repository browser.