1 /* 2 * COPYRIGHT: See COPYING in the top level directory 3 * PROJECT: ReactOS HTTP Daemon 4 * FILE: thread.cpp 5 * PURPOSE: Generic thread class 6 * PROGRAMMERS: Casper S. Hornstrup (chorns@users.sourceforge.net) 7 * REVISIONS: 8 * CSH 01/09/2000 Created 9 */ 10 #include <debug.h> 11 #include <assert.h> 12 #include <windows.h> 13 #include <thread.h> 14 15 // This is the thread entry code 16 DWORD WINAPI ThreadEntry(LPVOID parameter) 17 { 18 ThreadData *p = (ThreadData*) parameter; 19 20 p->ClassPtr->Execute(); 21 22 SetEvent(p->hFinished); 23 return 0; 24 } 25 26 // Default constructor 27 CThread::CThread() 28 { 29 bTerminated = FALSE; 30 // Points to the class that is executed within thread 31 Data.ClassPtr = this; 32 // Create synchronization event 33 Data.hFinished = CreateEvent(NULL, TRUE, FALSE, NULL); 34 35 // FIXME: Do some error handling 36 assert(Data.hFinished != NULL); 37 38 // Create thread 39 hThread = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)ThreadEntry, &Data, 0, &dwThreadId); 40 41 // FIXME: Do some error handling 42 assert(hThread != NULL); 43 } 44 45 // Default destructor 46 CThread::~CThread() 47 { 48 if ((hThread != NULL) && (Data.hFinished != NULL)) { 49 if (!bTerminated) 50 Terminate(); 51 WaitForSingleObject(Data.hFinished, INFINITE); 52 CloseHandle(Data.hFinished); 53 CloseHandle(hThread); 54 hThread = NULL; 55 } 56 } 57 58 // Execute thread code 59 void CThread::Execute() 60 { 61 while (!bTerminated) Sleep(0); 62 } 63 64 // Post a message to the thread's message queue 65 BOOL CThread::PostMessage(UINT Msg, WPARAM wParam, LPARAM lParam) 66 { 67 return PostThreadMessage(dwThreadId, Msg, wParam, lParam); 68 } 69 70 // Gracefully terminate thread 71 void CThread::Terminate() 72 { 73 bTerminated = TRUE; 74 } 75 76 // Returns TRUE if thread is terminated, FALSE if not 77 BOOL CThread::Terminated() 78 { 79 return bTerminated; 80 } 81