1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /* ***** BEGIN LICENSE BLOCK *****
3 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
5 * The contents of this file are subject to the Mozilla Public License Version
6 * 1.1 (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 * http://www.mozilla.org/MPL/
10 * Software distributed under the License is distributed on an "AS IS" basis,
11 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 * for the specific language governing rights and limitations under the
15 * The Original Code is mozilla.org code.
17 * The Initial Developer of the Original Code is
18 * Netscape Communications Corporation.
19 * Portions created by the Initial Developer are Copyright (C) 1998
20 * the Initial Developer. All Rights Reserved.
24 * Alternatively, the contents of this file may be used under the terms of
25 * either the GNU General Public License Version 2 or later (the "GPL"), or
26 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
27 * in which case the provisions of the GPL or the LGPL are applicable instead
28 * of those above. If you wish to allow use of your version of this file only
29 * under the terms of either the GPL or the LGPL, and not to allow others to
30 * use your version of this file under the terms of the MPL, indicate your
31 * decision by deleting the provisions above and replace them with the notice
32 * and other provisions required by the GPL or the LGPL. If you do not delete
33 * the provisions above, a recipient may use your version of this file under
34 * the terms of any one of the MPL, the GPL or the LGPL.
36 * ***** END LICENSE BLOCK ***** */
45 static DWORD WINAPI
ThreadFunction(void * lp
)
50 CThread
* thread
= (CThread
*)lp
;
51 BOOL res
= thread
->init();
52 thread
->setInitEvent();
59 GetExitCodeThread(thread
->getHandle(), &ret
);
60 thread
->setShutEvent();
64 CThread::CThread(DWORD aInitTimeOut
, DWORD aShutTimeOut
) :
68 mEventThreadInitializationFinished(NULL
),
69 mEventThreadShutdownFinished(NULL
),
70 mInitTimeOut(aInitTimeOut
),
71 mShutTimeOut(aShutTimeOut
),
75 dbgOut1("CThread::CThread");
80 dbgOut1("CThread::~CThread");
83 BOOL
CThread::open(CThread
* aThread
)
85 dbgOut1("CThread::open");
87 mEventThreadInitializationFinished
= CreateEvent(NULL
, TRUE
, FALSE
, NULL
);
88 mEventThreadShutdownFinished
= CreateEvent(NULL
, TRUE
, FALSE
, NULL
);
89 mEventDo
= CreateEvent(NULL
, TRUE
, FALSE
, NULL
);
91 mThread
= (HANDLE
)::_beginthreadex(0, 1024, (UINT (__stdcall
*)(void *))ThreadFunction
, aThread
, 0L, (UINT
*)&mID
);
96 SetThreadPriority(mThread
, THREAD_PRIORITY_NORMAL
);
98 if(mEventThreadInitializationFinished
!= NULL
)
99 WaitForSingleObject(mEventThreadInitializationFinished
, mInitTimeOut
);
104 void CThread::close(CThread
* aThread
)
112 if(mEventThreadShutdownFinished
!= NULL
)
113 WaitForSingleObject(mEventThreadShutdownFinished
, mShutTimeOut
);
119 GetExitCodeThread(aThread
->getHandle(), &ret
);
120 TerminateThread(mThread
, ret
);
129 CloseHandle(mEventThreadInitializationFinished
);
130 CloseHandle(mEventThreadShutdownFinished
);
131 CloseHandle(mEventDo
);
132 dbgOut1("CThread::close");
135 BOOL
CThread::setInitEvent()
137 return SetEvent(mEventThreadInitializationFinished
);
140 BOOL
CThread::setShutEvent()
142 return SetEvent(mEventThreadShutdownFinished
);
145 BOOL
CThread::isAlive()
147 return (mThread
!= NULL
);
150 BOOL
CThread::isBusy()
152 return (mState
== ts_busy
);
155 HANDLE
CThread::getHandle()
164 while(mState
!= ts_dead
) {
165 WaitForSingleObject(mEventDo
, INFINITE
);
166 //dbgOut2("CThread::run(): Do event signalled, %u", mState);
167 ResetEvent(mEventDo
);
168 if(mState
== ts_dead
)
177 BOOL
CThread::tryAction(int aAction
)
179 if(mState
!= ts_ready
)
188 BOOL
CThread::doAction(int aAction
)
190 while(mState
!= ts_ready
) {
191 //dbgOut2("CThread::doAction %i -- waiting...", aAction);
192 if(mState
== ts_dead
)
199 //dbgOut2("CThread::doAction -- about to signal %i", aAction);