DCL 4.0
Loading...
Searching...
No Matches
PollThread Class Reference

#include <PollThread.h>

Inheritance diagram for PollThread:
Thread Object

Classes

struct  Update

Protected Types

typedef List< UpdateUpdateList

Protected Member Functions

virtual int run ()
virtual void onRemoved (PollAble *_pPollAble)
Protected Member Functions inherited from Thread
virtual bool init ()
Protected Member Functions inherited from Object
virtual ~Object ()
 Object ()

Protected Attributes

UpdateList __updateList
Thread::Mutex __updateLock
Thread::Event __pollInterrupt
volatile bool __terminate

Additional Inherited Members

Public Types inherited from Thread
typedef void(* initRoutine) ()
typedef SingleLock< MutexSingleLockMutex
typedef SingleLock< SpinLock > SingleLockSpin
typedef SingleTryLock< MutexSingleTryLockMutex
typedef SingleTryLock< SpinLock > SingleTryLockSpin
Public Member Functions inherited from Object
virtual String toString () const
virtual void destroy ()
String className () const
bool isInstanceOf (const std::type_info &typeinfo) const
virtual const std::type_info & typeInfo () const
Static Public Member Functions inherited from Thread
static void sleep (unsigned int _mills)
static void yield ()
static unsigned long getCurrentThreadId ()
static ThreadgetCurrentThread ()
static void crtLock (const void *_p)
static void crtUnlock (const void *_p)
static long incrementAndGet (volatile long &_n)
static long long incrementAndGet (volatile long long &_n)
static long decrementAndGet (volatile long &_n)
static long long decrementAndGet (volatile long long &_n)

Detailed Description

Non-Blocking 입출력 장치에대한 비동기 감시기능을 제공한다.

UNIX에서는 poll을 사용하여 구현되어 있으며, 모든 입출력 fd에 대하여 사용하능하다.

Windows에서는 WSAPoll를 사용한다. 이 때문에, Windows은 Windows Vista, Windows 2008 이상의 버전(_WIN32_WINNT >= 0x0600) 의 WinSock2의 SOCKET을 감시에서만 사용할 수 있다. Windows에서 다른 입출력에 대한 감시는 관련 클래스를 참고한다.

See also
SerialPollThread
SocketPollThread
Author
Daejung Kim
Since
DCL Version 3.0

Definition at line 34 of file PollThread.h.

Member Typedef Documentation

◆ UpdateList

typedef List<Update> PollThread::UpdateList
protected

Definition at line 95 of file PollThread.h.

Member Function Documentation

◆ onRemoved()

void PollThread::onRemoved ( PollAble * _pPollAble)
protectedvirtual

입출력 감시대상에서 제거된 후 해당 객체를 처분한다.

기본구현은 _pPollAble의 destroy()를 호출하여 파괴한다

Definition at line 221 of file PollThread.cpp.

222{
223 __DCL_TRACE1(__T("PollThread::onRemoved, destroy [%ls]\n"), _pPollAble->toString().data());
224 _pPollAble->destroy();
225}
#define __DCL_TRACE1(fmt, arg1)
Definition Object.h:376
#define __T(str)
Definition Object.h:44
virtual String toString() const
virtual void destroy()
Definition Object.cpp:192

◆ run()

int PollThread::run ( )
protectedvirtual

스레드의 주 실행 루프를 구현한다.

잊지말자!! 다음의 코드를 ~PollThread() 에 포함시키면 파생클래스 영역은 이미 파괴된 후 이기 때문에 파생클래스의 onRemoved는 호출되지 않는다.

Implements Thread.

Definition at line 97 of file PollThread.cpp.

98{
99 Array<PollAble*> pollAbles;
100 Array<POLLFD> fds;
101
102#if __DCL_WINDOWS
103 // Windows에서 __pollInterrupt를 사용하지 않는다.
104 pollAbles.add(NULL);
105 fds.add(POLLFD((File::HandleType) INVALID_SOCKET, POLLIN));
106#else
107 pollAbles.add(NULL);
108 fds.add(POLLFD(__pollInterrupt.handle(), POLLIN));
109#endif
110
111 while (!__terminate) {
112 {
114 if (!__updateList.isEmpty()) {
115#if !__DCL_WINDOWS
116 __pollInterrupt.reset();
117#endif
118 for (UpdateList::Iterator itUpdate = __updateList.begin();
119 itUpdate != __updateList.end(); itUpdate++) {
120 Update& update = *itUpdate;
121 // __DCL_TRACE2(__T("add [%ls][%d]\n"), update.pPollAble->path(), update.bAdd);
122 if (update.bAdd) {
123 __DCL_TRACE1(__T("added [%ls]\n"), update.pPollAble->toString().data());
124 pollAbles.add(update.pPollAble);
125 fds.add(POLLFD(update.pPollAble->handle(), update.pPollAble->__events));
126 }
127 else {
128 Array<PollAble*>::Iterator itPollAble =
129 pollAbles.find(update.pPollAble);
130 if (itPollAble != pollAbles.end()) {
131 __DCL_TRACE1(__T("remove [%ls]\n"), update.pPollAble->toString().data());
132 onRemoved(*itPollAble);
133 pollAbles.erase(itPollAble);
134 fds.erase(pollAbles.index(itPollAble));
135 }
136 }
137 } // end of for
138 __updateList.clear();
139 }
140 }
141
142 // Wait poll
143#if __DCL_WINDOWS
144 int r = WSAPoll(fds.data() + 1, (ULONG)(fds.size() - 1), WSA_INFINITE);
145#else
146 int r = poll(fds.data(), fds.size(), INFINITE);
147#endif
148 if (r > 0) {
149#if !__DCL_WINDOWS
150 if (fds[0].revents)
151 __pollInterrupt.reset();
152 else
153#endif
154 {
155 for (size_t i = 1; i < fds.size(); i++) {
156 if (fds[i].revents)
157 {
158 PollAble* pPollAble = pollAbles[i];
159 try {
160 if (!(pPollAble->onEvent(fds[i].revents, this)))
161 remove(pPollAble);
162 }
163 catch (IOException* cause) {
164 remove(pPollAble);
165 /*
166 * 복구 불가능한 치명적 포트오류가 발생했다.
167 * USB to COM 변환기를 사용한 경우 포트가 제거된 경우일 수 있다.
168 */
169 __DCL_TRACE1(__T("onEvent Fail! %ls\n"), cause->toString().data());
170 cause->destroy();
171 }
172 } // end of fds[i].revents
173 } // end of for
174 }
175 }
176 else if (r < 0) {
177#if __DCL_DEBUG
178 #if __DCL_WINDOWS
179 // SOCKET_ERROR
180 __DCL_TRACE1(__T("WSAPoll error [%d]\n"), WSAGetLastError());
181 #else
182 // fd error
183 __DCL_TRACE1(__T("poll error [%d]\n"), errno);
184 #endif
185 Thread::sleep(1000);
186#endif
187 }
188 else {
189 // r == 0, time out
190 __DCL_ASSERT(false);
191 }
192 } // end of while
193
199 for (Array<PollAble*>::Iterator itPollAble = pollAbles.begin();
200 itPollAble != pollAbles.end(); itPollAble++) {
201 // NULL 인 것은 __pollInterrupt를 위한 것이다.
202 if (*itPollAble != NULL) {
203 __DCL_TRACE1(__T("terminate remove [%ls]\n"), (*itPollAble)->toString().data());
204 onRemoved(*itPollAble);
205 }
206 }
207
208 // __updateList에 추가된 것이 있을 수 있다.
209 for (UpdateList::Iterator itUpdate = __updateList.begin();
210 itUpdate != __updateList.end(); itUpdate++) {
211 Update& update = *itUpdate;
212 if (update.bAdd) {
213 __DCL_TRACE1(__T("terminate remove [%ls]\n"), update.pPollAble->toString().data());
214 onRemoved(update.pPollAble);
215 }
216 }
217
218 return 0;
219}
#define NULL
Definition Config.h:340
#define __DCL_ASSERT(expr)
Definition Object.h:371
ByteString r
#define INVALID_SOCKET
Definition Socket.cpp:38
#define INFINITE
Definition Thread.h:17
Iterator erase(Iterator _pos)
Definition ArrayT.h:152
ConstIterator end() const
Definition ArrayT.h:120
Iterator find(const ELEMENT &_element)
Definition ArrayT.h:491
ELEMENT * data() const
Definition ArrayT.h:231
size_t size() const
Definition ArrayT.h:197
Array< ELEMENT > & add(const ELEMENT &_element)
Definition ArrayT.h:144
ConstIterator begin() const
Definition ArrayT.h:112
size_t index(Iterator _pos) const
Definition ArrayT.h:188
virtual void destroy()
Definition Exception.cpp:74
virtual String toString() const
UpdateList __updateList
Definition PollThread.h:96
virtual void onRemoved(PollAble *_pPollAble)
volatile bool __terminate
Definition PollThread.h:105
Thread::Event __pollInterrupt
Definition PollThread.h:103
Thread::Mutex __updateLock
Definition PollThread.h:97
static void sleep(unsigned int _mills)
Definition Thread.cpp:150
SingleLock< Mutex > SingleLockMutex
Definition Thread.h:403

Member Data Documentation

◆ __pollInterrupt

Thread::Event PollThread::__pollInterrupt
protected

Definition at line 103 of file PollThread.h.

◆ __terminate

volatile bool PollThread::__terminate
protected

Definition at line 105 of file PollThread.h.

◆ __updateList

UpdateList PollThread::__updateList
protected

Definition at line 96 of file PollThread.h.

◆ __updateLock

Thread::Mutex PollThread::__updateLock
protected

Definition at line 97 of file PollThread.h.


The documentation for this class was generated from the following files: