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 : Sat Jul 22 09:58:39 2017 |
---|
13 | // Update Count : 2 |
---|
14 | // |
---|
15 | |
---|
16 | #pragma once |
---|
17 | |
---|
18 | #include <stdbool.h> |
---|
19 | |
---|
20 | #include "invoke.h" |
---|
21 | |
---|
22 | extern "C" { |
---|
23 | #include <pthread.h> |
---|
24 | } |
---|
25 | |
---|
26 | //----------------------------------------------------------------------------- |
---|
27 | // Locks |
---|
28 | // // Lock the spinlock, spin if already acquired |
---|
29 | // void lock ( spinlock * DEBUG_CTX_PARAM2 ); |
---|
30 | |
---|
31 | // // Lock the spinlock, yield repeatedly if already acquired |
---|
32 | // void lock_yield( spinlock * DEBUG_CTX_PARAM2 ); |
---|
33 | |
---|
34 | // // Lock the spinlock, return false if already acquired |
---|
35 | // bool try_lock ( spinlock * DEBUG_CTX_PARAM2 ); |
---|
36 | |
---|
37 | // // Unlock the spinlock |
---|
38 | // void unlock ( spinlock * ); |
---|
39 | |
---|
40 | struct semaphore { |
---|
41 | __spinlock_t lock; |
---|
42 | int count; |
---|
43 | __thread_queue_t waiting; |
---|
44 | }; |
---|
45 | |
---|
46 | void ?{}(semaphore & this, int count = 1); |
---|
47 | void ^?{}(semaphore & this); |
---|
48 | void P (semaphore & this); |
---|
49 | void V (semaphore & this); |
---|
50 | |
---|
51 | |
---|
52 | //----------------------------------------------------------------------------- |
---|
53 | // Cluster |
---|
54 | struct cluster { |
---|
55 | // Ready queue locks |
---|
56 | __spinlock_t ready_queue_lock; |
---|
57 | |
---|
58 | // Ready queue for threads |
---|
59 | __thread_queue_t ready_queue; |
---|
60 | |
---|
61 | // Preemption rate on this cluster |
---|
62 | unsigned long long int preemption; |
---|
63 | }; |
---|
64 | |
---|
65 | void ?{} (cluster & this); |
---|
66 | void ^?{}(cluster & this); |
---|
67 | |
---|
68 | //----------------------------------------------------------------------------- |
---|
69 | // Processor |
---|
70 | enum FinishOpCode { No_Action, Release, Schedule, Release_Schedule, Release_Multi, Release_Multi_Schedule }; |
---|
71 | |
---|
72 | //TODO use union, many of these fields are mutually exclusive (i.e. MULTI vs NOMULTI) |
---|
73 | struct FinishAction { |
---|
74 | FinishOpCode action_code; |
---|
75 | thread_desc * thrd; |
---|
76 | __spinlock_t * lock; |
---|
77 | __spinlock_t ** locks; |
---|
78 | unsigned short lock_count; |
---|
79 | thread_desc ** thrds; |
---|
80 | unsigned short thrd_count; |
---|
81 | }; |
---|
82 | static inline void ?{}(FinishAction & this) { |
---|
83 | this.action_code = No_Action; |
---|
84 | this.thrd = NULL; |
---|
85 | this.lock = NULL; |
---|
86 | } |
---|
87 | static inline void ^?{}(FinishAction & this) {} |
---|
88 | |
---|
89 | // Processor |
---|
90 | // Wrapper around kernel threads |
---|
91 | struct processor { |
---|
92 | // Main state |
---|
93 | // Coroutine ctx who does keeps the state of the processor |
---|
94 | struct processorCtx_t * runner; |
---|
95 | |
---|
96 | // Cluster from which to get threads |
---|
97 | cluster * cltr; |
---|
98 | |
---|
99 | // Handle to pthreads |
---|
100 | pthread_t kernel_thread; |
---|
101 | |
---|
102 | // Termination |
---|
103 | // Set to true to notify the processor should terminate |
---|
104 | volatile bool do_terminate; |
---|
105 | |
---|
106 | // Termination synchronisation |
---|
107 | semaphore terminated; |
---|
108 | |
---|
109 | // RunThread data |
---|
110 | // Action to do after a thread is ran |
---|
111 | struct FinishAction finish; |
---|
112 | |
---|
113 | // Preemption data |
---|
114 | // Node which is added in the discrete event simulaiton |
---|
115 | struct alarm_node_t * preemption_alarm; |
---|
116 | |
---|
117 | // If true, a preemption was triggered in an unsafe region, the processor must preempt as soon as possible |
---|
118 | bool pending_preemption; |
---|
119 | |
---|
120 | #ifdef __CFA_DEBUG__ |
---|
121 | // Last function to enable preemption on this processor |
---|
122 | char * last_enable; |
---|
123 | #endif |
---|
124 | }; |
---|
125 | |
---|
126 | void ?{}(processor & this); |
---|
127 | void ?{}(processor & this, cluster * cltr); |
---|
128 | void ^?{}(processor & this); |
---|
129 | |
---|
130 | // Local Variables: // |
---|
131 | // mode: c // |
---|
132 | // tab-width: 4 // |
---|
133 | // End: // |
---|