1 // Copyright (c) 2011-2012 Ryan Prichard
3 // Permission is hereby granted, free of charge, to any person obtaining a copy
4 // of this software and associated documentation files (the "Software"), to
5 // deal in the Software without restriction, including without limitation the
6 // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
7 // sell copies of the Software, and to permit persons to whom the Software is
8 // furnished to do so, subject to the following conditions:
10 // The above copyright notice and this permission notice shall be included in
11 // all copies or substantial portions of the Software.
13 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
18 // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21 #include "EventLoop.h"
25 #include "NamedPipe.h"
26 #include "../shared/DebugClient.h"
27 #include "../shared/WinptyAssert.h"
29 EventLoop::~EventLoop() {
30 for (NamedPipe *pipe : m_pipes) {
36 // Enter the event loop. Runs until the I/O or timeout handler calls exit().
39 std::vector<HANDLE> waitHandles;
40 DWORD lastTime = GetTickCount();
42 bool didSomething = false;
44 // Attempt to make progress with the pipes.
46 for (size_t i = 0; i < m_pipes.size(); ++i) {
47 if (m_pipes[i]->serviceIo(&waitHandles)) {
48 onPipeIo(*m_pipes[i]);
53 // Call the timeout if enough time has elapsed.
54 if (m_pollInterval > 0) {
55 int elapsed = GetTickCount() - lastTime;
56 if (elapsed >= m_pollInterval) {
58 lastTime = GetTickCount();
66 // If there's nothing to do, wait.
67 DWORD timeout = INFINITE;
68 if (m_pollInterval > 0)
69 timeout = std::max(0, (int)(lastTime + m_pollInterval - GetTickCount()));
70 if (waitHandles.size() == 0) {
71 ASSERT(timeout != INFINITE);
75 DWORD result = WaitForMultipleObjects(waitHandles.size(),
79 ASSERT(result != WAIT_FAILED);
84 NamedPipe &EventLoop::createNamedPipe()
86 NamedPipe *ret = new NamedPipe();
87 m_pipes.push_back(ret);
91 void EventLoop::setPollInterval(int ms)
96 void EventLoop::shutdown()