The Pedigree Project 0.1
IoEvent.cc
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#include "modules/subsys/posix/IoEvent.h"
21
22#include <signal.h>
23
24#include "modules/subsys/posix/PosixSubsystem.h"
25#include "modules/system/vfs/File.h"
26
27static void ioEventHandler(uint8_t* pBuffer);
28
29IoEvent::IoEvent() : Event(0, false), m_pSubsystem(0), m_pFile(0), m_pRetriggerInstance(0) {}
30
31IoEvent::IoEvent(PosixSubsystem* subsystem, File* file)
32 : Event(reinterpret_cast<uintptr_t>(&ioEventHandler), false),
33 m_pSubsystem(subsystem),
34 m_pFile(file),
35 m_pRetriggerInstance(0) {
36 assert(subsystem);
37}
38
39void IoEvent::fire() {
40 EMIT_IF(THREADS) {
41 Thread* pThread = Processor::information().getCurrentThread();
42 m_pSubsystem->sendSignal(pThread, SIGIO, true, true);
43
44 // Re-monitor now that we've gotten the event.
45 if (m_pRetriggerInstance) {
46 // NOTE: using retrigger instance which is the original serialized
47 // event - not the temporary one we deserialize later.
48 m_pFile->monitor(pThread, m_pRetriggerInstance);
49 }
50 }
51}
52
53size_t IoEvent::serialize(uint8_t* pBuffer) {
54 void* alignedBuffer = ASSUME_ALIGNMENT(pBuffer, sizeof(size_t));
55 size_t* pBuf = reinterpret_cast<size_t*>(alignedBuffer);
56 pBuf[0] = EventNumbers::IoEvent;
57 pBuf[1] = reinterpret_cast<size_t>(m_pSubsystem);
58 pBuf[2] = reinterpret_cast<size_t>(m_pFile);
59 pBuf[3] = reinterpret_cast<size_t>(this);
60
61 return 4 * sizeof(size_t);
62}
63
64bool IoEvent::unserialize(uint8_t* pBuffer, IoEvent& event) {
65 void* alignedBuffer = ASSUME_ALIGNMENT(pBuffer, sizeof(size_t));
66 size_t* pBuf = reinterpret_cast<size_t*>(alignedBuffer);
67 if (pBuf[0] != EventNumbers::IoEvent)
68 return false;
69
70 event.m_pSubsystem = reinterpret_cast<PosixSubsystem*>(pBuf[1]);
71 event.m_pFile = reinterpret_cast<File*>(pBuf[2]);
72 event.m_pRetriggerInstance = reinterpret_cast<IoEvent*>(pBuf[3]);
73
74 return true;
75}
76
77void ioEventHandler(uint8_t* pBuffer) {
78 IoEvent e;
79 if (!IoEvent::unserialize(pBuffer, e)) {
80 FATAL("PollEventHandler: unable to unserialize event!");
81 }
82 e.fire();
83}
Definition Event.h:49
Definition File.h:75
void monitor(Thread *pThread, Event *pEvent)
Definition File.cc:1482
IoEvent()
Definition IoEvent.cc:29
virtual size_t serialize(uint8_t *pBuffer)
Definition IoEvent.cc:53
virtual void sendSignal(Thread *pThread, int signal, bool yield=true, bool processDirected=false)
static ProcessorInformation & information()