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