The Pedigree Project  0.1
PollEvent.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/PollEvent.h"
21 #include "modules/system/vfs/File.h"
22 #include "pedigree/kernel/Log.h"
23 
24 static void pollEventHandler(uint8_t *pBuffer);
25 
27  : Event(0, false), m_pSemaphore(0), m_pFd(0), m_nREvent(0), m_pFile(0)
28 {
29 }
30 
32  Semaphore *pSemaphore, struct pollfd *fd, int revent, File *pFile)
33  : Event(reinterpret_cast<uintptr_t>(&pollEventHandler), false),
34  m_pSemaphore(pSemaphore), m_pFd(fd), m_nREvent(revent), m_pFile(pFile)
35 {
36  assert(pSemaphore);
37 }
38 
39 PollEvent::~PollEvent()
40 {
41 }
42 
43 void PollEvent::fire()
44 {
45  m_pFd->revents |= m_nREvent;
46 
47  m_pSemaphore->release();
48 }
49 
50 size_t PollEvent::serialize(uint8_t *pBuffer)
51 {
52  void *alignedBuffer = ASSUME_ALIGNMENT(pBuffer, sizeof(size_t));
53  size_t *pBuf = reinterpret_cast<size_t *>(alignedBuffer);
54  pBuf[0] = EventNumbers::PollEvent;
55  pBuf[1] = reinterpret_cast<size_t>(m_pSemaphore);
56  pBuf[2] = reinterpret_cast<size_t>(m_pFd);
57  pBuf[3] = static_cast<size_t>(m_nREvent);
58  pBuf[4] = reinterpret_cast<size_t>(m_pFile);
59 
60  return 5 * sizeof(size_t);
61 }
62 
63 bool PollEvent::unserialize(uint8_t *pBuffer, PollEvent &event)
64 {
65  void *alignedBuffer = ASSUME_ALIGNMENT(pBuffer, sizeof(size_t));
66  size_t *pBuf = reinterpret_cast<size_t *>(alignedBuffer);
67  if (pBuf[0] != EventNumbers::PollEvent)
68  return false;
69 
70  event.m_pSemaphore = reinterpret_cast<Semaphore *>(pBuf[1]);
71  event.m_pFd = reinterpret_cast<struct pollfd *>(pBuf[2]);
72  event.m_nREvent = static_cast<int>(pBuf[3]);
73  event.m_pFile = reinterpret_cast<File *>(pBuf[4]);
74 
75  return true;
76 }
77 
78 void pollEventHandler(uint8_t *pBuffer)
79 {
80  PollEvent e;
81  if (!PollEvent::unserialize(pBuffer, e))
82  {
83  FATAL("PollEventHandler: unable to unserialize event!");
84  }
85  e.fire();
86 }
void release(size_t n=1)
Definition: Semaphore.cc:239
#define assert(x)
Definition: assert.h:37
Definition: Event.h:48
#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
virtual size_t serialize(uint8_t *pBuffer)
Definition: PollEvent.cc:50