1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283
|
/*********************************************************
* Copyright (C) 2003 VMware, Inc. All rights reserved.
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published
* by the Free Software Foundation version 2.1 and no later version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the Lesser GNU General Public
* License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
*********************************************************/
/*
* FoundryThreads.c --
*
* This is a simple cross-platform library for creating threads. It doesn't
* have all of the functionality of VThreads, but then it doesn't require
* all of the infrastructure either so it can be easily used in applications
* outside the VMX.
*
* On Windows, we sometimes run as a COM object in a DLL. DLL's cannot
* use thread-local-storage, even though they can use threads. As a
* result, we must build with no-threads, and we cannot use the
* VThread library. Instead, we must use our OS-specific threads.
*
*/
#include "vmware.h"
#include "vm_version.h"
#include "util.h"
#include "vix.h"
#include "foundryThreads.h"
#if _WIN32
#include <objbase.h.> // For CoInitializeEx
static DWORD WINAPI FoundryThreadWrapperProc(LPVOID lpParameter);
#else // Linux
#include <pthread.h>
static void *FoundryThreadWrapperProc(void *lpParameter);
#endif
/*
*-----------------------------------------------------------------------------
*
* FoundryThreads_StartThread --
*
* Start a worker thread.
*
* Results:
* FoundryWorkerThread *
*
* Side effects:
*
*-----------------------------------------------------------------------------
*/
FoundryWorkerThread *
FoundryThreads_StartThread(FoundryThreadProc proc, // IN
void *threadParam) // IN
{
VixError err = VIX_OK;
FoundryWorkerThread *threadState = NULL;
static const char *createThreadFailureMsg =
"%s: thread creation failed with error %d.\n";
ASSERT(proc);
/*
* Start the worker threads.
*/
threadState = (FoundryWorkerThread *) Util_SafeCalloc(1, sizeof(*threadState));
threadState->threadProc = proc;
threadState->threadParam = threadParam;
#ifdef _WIN32
threadState->threadHandle = CreateThread(NULL,
0,
FoundryThreadWrapperProc,
threadState,
0,
&(threadState->threadId));
if (NULL == threadState->threadHandle) {
Log(createThreadFailureMsg, __FUNCTION__, GetLastError());
err = VIX_E_OUT_OF_MEMORY;
goto abort;
}
#else // Linux
{
pthread_attr_t attr;
int result;
pthread_attr_init(&attr);
pthread_attr_setstacksize(&attr, 512 * 1024);
result = pthread_create(&(threadState->threadInfo),
&attr,
FoundryThreadWrapperProc,
threadState);
if (0 != result) {
Log(createThreadFailureMsg, __FUNCTION__, result);
err = VIX_E_OUT_OF_MEMORY;
goto abort;
}
}
#endif
abort:
if (VIX_OK != err) {
free(threadState);
threadState = NULL;
}
return threadState;
} // FoundryThreads_StartThread
/*
*-----------------------------------------------------------------------------
*
* FoundryThreads_StopThread --
*
* Shutdown a thread and destroys its thread state.
*
* Results:
* None.
*
* Side effects:
* May block while the given thread stops.
*
*-----------------------------------------------------------------------------
*/
void
FoundryThreads_StopThread(FoundryWorkerThread *threadState) // IN
{
#ifdef _WIN32
DWORD waitResult;
#endif
if (NULL == threadState) {
ASSERT(0);
return;
}
/*
* Stop the thread.
*/
threadState->stopThread = TRUE;
ASSERT(!FoundryThreads_IsCurrentThread(threadState));
#ifdef _WIN32
waitResult = WaitForSingleObject(threadState->threadHandle, 30000);
if (WAIT_OBJECT_0 != waitResult) {
TerminateThread(threadState->threadHandle, 0x1);
}
#else
pthread_join(threadState->threadInfo, NULL);
#endif
FoundryThreads_Free(threadState);
}
/*
*-----------------------------------------------------------------------------
*
* FoundryThreads_Free --
*
* Destroys the thread state.
*
* Results:
* None.
*
* Side effects:
* None.
*
*-----------------------------------------------------------------------------
*/
void
FoundryThreads_Free(FoundryWorkerThread *threadState) // IN
{
if (NULL != threadState) {
#ifdef _WIN32
CloseHandle(threadState->threadHandle);
threadState->threadHandle = NULL;
#else
threadState->threadInfo = 0;
#endif
free(threadState);
}
}
/*
*-----------------------------------------------------------------------------
*
* FoundryThreads_IsCurrentThread --
*
* Returns true if the thread state passed in refers to the current thread.
*
* Results:
*
*
* Side effects:
* None.
*
*-----------------------------------------------------------------------------
*/
Bool
FoundryThreads_IsCurrentThread(struct FoundryWorkerThread *threadState) // IN
{
if (NULL == threadState) {
return FALSE;
}
#ifdef _WIN32
return (GetCurrentThreadId() == threadState->threadId);
#else
return pthread_equal(pthread_self(), threadState->threadInfo);
#endif
}
/*
*-----------------------------------------------------------------------------
*
* FoundryThreadWrapperProc --
*
* This is a Windows-specific wrapper for a foundry thread procedure.
* It calls the platform-independent thread procedure.
*
* Results:
* Ignored; a thread termination status.
*
* Side effects:
*
*-----------------------------------------------------------------------------
*/
#if _WIN32
DWORD WINAPI
FoundryThreadWrapperProc(LPVOID threadParameter) // IN
#else // Linux
static void *
FoundryThreadWrapperProc(void *threadParameter) // IN
#endif
{
FoundryWorkerThread *threadState;
threadState = (FoundryWorkerThread *) threadParameter;
if (NULL == threadState) {
ASSERT(0);
goto abort;
}
if (NULL != threadState->threadProc) {
(*(threadState->threadProc))(threadState);
}
abort:
#if _WIN32
return 0;
#else // Linux
return NULL;
#endif
}
|