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