The Pedigree Project 0.1
Event.h
1/*
2 * Copyright (c) 2008-2014, Pedigree Developers
3 *
4 * Please see the CONTRIB file in the root of the source tree for a full
5 * list of contributors.
6 *
7 * Permission to use, copy, modify, and distribute this software for any
8 * purpose with or without fee is hereby granted, provided that the above
9 * copyright notice and this permission notice appear in all copies.
10 *
11 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18 */
19
20#ifndef EVENT_H
21#define EVENT_H
22#include "pedigree/kernel/Spinlock.h"
23#include "pedigree/kernel/compiler.h"
24#include "pedigree/kernel/process/WaitQueue.h"
25#include "pedigree/kernel/processor/state_forward.h"
26#include "pedigree/kernel/processor/types.h"
27#include "pedigree/kernel/utilities/List.h"
28#include "pedigree/kernel/utilities/new"
29
30#include <config.h>
31
32class Thread;
33
35#define EVENT_LIMIT 4096
37#define EVENT_TID_MAX 255
40#define MAX_NESTED_EVENTS 16
41
42#define EVENT_MAGIC 0x8899AABBCCDDEEFFULL
43
49class EXPORTED_PUBLIC Event {
50 public:
52 enum class HandlerPrivilege {
53 Kernel,
54 User,
55 };
56
58 enum class UserReturnDelivery {
59 NotApplicable,
60 Delivered,
61 Failed,
62 };
63
65 class SendLease {
66 public:
67 SendLease();
68 SendLease(SendLease&& other) noexcept;
69 ~SendLease();
70
71 SendLease& operator=(SendLease&& other) noexcept;
72
73 explicit operator bool() const {
74 return m_pEvent != nullptr;
75 }
76
77 private:
78 friend class Event;
79 friend class Thread;
80
81 explicit SendLease(Event* event);
82 void reset();
83
84 SendLease(const SendLease&) = delete;
85 SendLease& operator=(const SendLease&) = delete;
86
87 Event* m_pEvent;
88 };
89
97 class Retirement {
98 public:
99 Retirement();
100 Retirement(Retirement&& other) noexcept;
101 ~Retirement();
102
103 Retirement& operator=(Retirement&& other) noexcept;
104
105 private:
106 friend class Event;
107
108 explicit Retirement(Event* event);
109 void reset();
110
111 Retirement(const Retirement&) = delete;
112 Retirement& operator=(const Retirement&) = delete;
113
114 Event* m_pEvent;
115 };
116
123 class Delivery {
124 public:
125 Delivery();
126 Delivery(Delivery&& other) noexcept;
127 ~Delivery();
128
129 Delivery& operator=(Delivery&& other) noexcept;
130
131 Event* get() const {
132 return m_pEvent;
133 }
134
135 Event* operator->() const {
136 return m_pEvent;
137 }
138
139 explicit operator bool() const {
140 return m_pEvent != nullptr;
141 }
142
144 void reset();
145
146 private:
147 friend class Event;
148 friend class Thread;
149 friend class PerProcessorScheduler;
150
151 Delivery(Event* event, Thread* thread);
152 void beginDispatch();
153
154 Delivery(const Delivery&) = delete;
155 Delivery& operator=(const Delivery&) = delete;
156
157 Event* m_pEvent;
158 Thread* m_pThread;
159 Delivery* m_pPreviousActive;
160 Delivery* m_pNextActive;
161 bool m_bActive;
162 };
163
174 Event(uintptr_t handlerAddress, bool isDeletable, size_t specificNestingLevel = ~0UL,
175 HandlerPrivilege handlerPrivilege = HandlerPrivilege::Kernel);
176 virtual ~Event();
177
179 static uintptr_t getTrampoline();
180
182 static uintptr_t getSecondaryTrampoline();
183
185 static uintptr_t getHandlerBuffer();
186
188 static size_t getHandlerBufferSize();
189
191 static uintptr_t getLastHandlerBuffer();
192
193#if HOSTED && PEDIGREE_HOSTED_SMOKE_TESTS
195 static constexpr size_t getHostedHandlerBufferSize(size_t pageSize) {
196 return pageSize ? handlerBufferSize(pageSize) : 0;
197 }
198#endif
199
203 virtual bool isDeletable();
204
216 virtual size_t serialize(uint8_t* pBuffer) = 0;
217
224 static bool unserialize(uint8_t* pBuffer, Event& event);
225
228 static size_t getEventType(uint8_t* pBuffer);
229
231 uintptr_t getHandlerAddress() {
232 return m_HandlerAddress;
233 }
234
237 return m_HandlerPrivilege;
238 }
239
241 bool isValidHandlerMapping(size_t mappingFlags) const;
242
244 virtual bool isSignalEvent() const {
245 return false;
246 }
247
249 virtual bool prefersAlternateUserStack() const {
250 return false;
251 }
252
254 virtual bool requiresExactUserReturnState() const {
255 return false;
256 }
257
259 virtual UserReturnDelivery deliverAtUserReturn(InterruptState&) {
260 return UserReturnDelivery::NotApplicable;
261 }
262
265 return UserReturnDelivery::NotApplicable;
266 }
267
270 return true;
271 }
272
278 return this;
279 }
280
283 return m_NestingLevel;
284 }
285
287 virtual size_t getNumber() = 0;
288
289 Event(const Event& other);
290 Event& operator=(const Event& other);
291
293 size_t pendingCount();
294
299 bool tryAcquireRegistration(SendLease& registration);
300
307 virtual void waitForDeliveries();
308
314 void retire();
315
320 void beginRetirement(Retirement& retirement);
321
322 protected:
325
328
331
334
336 uint64_t m_Magic;
337
340
343
344 private:
345 friend class Thread;
346
347 static constexpr size_t handlerBufferSize(size_t pageSize) {
348 const size_t pageCount = (EVENT_LIMIT / pageSize) + ((EVENT_LIMIT % pageSize) ? 1 : 0);
349 return pageCount * pageSize;
350 }
351
353 SendLease beginSend();
354 void endSend();
355 void finishRetirement();
356
358 bool registerThread(Thread* thread);
359
361 void deregisterThread(Thread* thread);
362
367 void completeDelivery(Thread* thread);
368
370 void beginDispatch(Delivery* delivery);
371 void endDispatch(Delivery* delivery);
372
375
378
381
384
387
390};
391
392#endif
Definition Event.h:49
virtual bool prefersAlternateUserStack() const
Definition Event.h:249
WaitQueue m_DeliveryWaiters
Definition Event.h:389
Spinlock m_Lock
Definition Event.h:342
virtual UserReturnDelivery deliverAtUserReturn(SyscallState &)
Definition Event.h:264
HandlerPrivilege m_HandlerPrivilege
Definition Event.h:327
HandlerPrivilege
Definition Event.h:52
bool m_DrainClaimed
Definition Event.h:380
uintptr_t getHandlerAddress()
Definition Event.h:231
HandlerPrivilege getHandlerPrivilege() const
Definition Event.h:236
virtual bool isSignalEvent() const
Definition Event.h:244
bool m_DeleteWhenUnused
Definition Event.h:374
virtual UserReturnDelivery deliverAtUserReturn(InterruptState &)
Definition Event.h:259
virtual bool requiresExactUserReturnState() const
Definition Event.h:254
UserReturnDelivery
Definition Event.h:58
size_t getSpecificNestingLevel()
Definition Event.h:282
size_t m_SendersInFlight
Definition Event.h:383
uint64_t m_Magic
Definition Event.h:336
virtual size_t getNumber()=0
bool m_DeliveriesClosed
Definition Event.h:377
List< Thread * > m_Threads
Definition Event.h:339
size_t m_NestingLevel
Definition Event.h:333
virtual bool isDeliverableWhileProcessSuspended() const
Definition Event.h:269
virtual size_t serialize(uint8_t *pBuffer)=0
Delivery * m_pFirstActiveDelivery
Definition Event.h:386
virtual Event * cloneForDelivery()
Definition Event.h:277
uintptr_t m_HandlerAddress
Definition Event.h:324
bool m_bIsDeletable
Definition Event.h:330
Definition List.h:61
Definition User.h:32