Bug 470455 - test_database_sync_embed_visits.js leaks, r=sdwilsh
[wine-gecko.git] / modules / plugin / sdk / samples / npthread / windows / thread.cpp
blob304033ae181eccfa1c5b78787f8a2c49b118dbe7
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
13 * License.
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.
22 * Contributor(s):
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 ***** */
38 #include <windows.h>
39 #include <windowsx.h>
40 #include <process.h>
42 #include "thread.h"
43 #include "dbg.h"
45 static DWORD WINAPI ThreadFunction(void * lp)
47 if (lp == NULL)
48 return 0L;
50 CThread * thread = (CThread *)lp;
51 BOOL res = thread->init();
52 thread->setInitEvent();
54 if(res)
55 thread->run();
57 thread->shut();
58 DWORD ret;
59 GetExitCodeThread(thread->getHandle(), &ret);
60 thread->setShutEvent();
61 return ret;
64 CThread::CThread(DWORD aInitTimeOut, DWORD aShutTimeOut) :
65 mThread(NULL),
66 mID(0),
67 mState(ts_dead),
68 mEventThreadInitializationFinished(NULL),
69 mEventThreadShutdownFinished(NULL),
70 mInitTimeOut(aInitTimeOut),
71 mShutTimeOut(aShutTimeOut),
72 mEventDo(NULL),
73 mAction(0)
75 dbgOut1("CThread::CThread");
78 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);
93 if(mThread == NULL)
94 return FALSE;
96 SetThreadPriority(mThread, THREAD_PRIORITY_NORMAL);
98 if(mEventThreadInitializationFinished != NULL)
99 WaitForSingleObject(mEventThreadInitializationFinished, mInitTimeOut);
101 return TRUE;
104 void CThread::close(CThread * aThread)
106 switch (mState) {
107 case ts_dead:
108 break;
109 case ts_ready:
110 mState = ts_dead;
111 SetEvent(mEventDo);
112 if(mEventThreadShutdownFinished != NULL)
113 WaitForSingleObject(mEventThreadShutdownFinished, mShutTimeOut);
114 break;
115 case ts_busy:
117 aThread->shut();
118 DWORD ret;
119 GetExitCodeThread(aThread->getHandle(), &ret);
120 TerminateThread(mThread, ret);
121 mState = ts_dead;
122 break;
124 default:
125 break;
128 mThread = NULL;
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()
157 return mThread;
160 void CThread::run()
162 mState = ts_ready;
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)
169 return;
170 mState = ts_busy;
171 dispatch();
172 mState = ts_ready;
174 mState = ts_dead;
177 BOOL CThread::tryAction(int aAction)
179 if(mState != ts_ready)
180 return FALSE;
182 mAction = aAction;
183 SetEvent(mEventDo);
185 return TRUE;
188 BOOL CThread::doAction(int aAction)
190 while(mState != ts_ready) {
191 //dbgOut2("CThread::doAction %i -- waiting...", aAction);
192 if(mState == ts_dead)
193 return FALSE;
194 Sleep(0);
197 mState = ts_busy;
198 mAction = aAction;
199 //dbgOut2("CThread::doAction -- about to signal %i", aAction);
200 SetEvent(mEventDo);
202 return TRUE;