1/**************************************************************************\ 2 * Copyright (c) Kongsberg Oil & Gas Technologies AS 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions are 7 * met: 8 * 9 * Redistributions of source code must retain the above copyright notice, 10 * this list of conditions and the following disclaimer. 11 * 12 * Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 16 * Neither the name of the copyright holder nor the names of its 17 * contributors may be used to endorse or promote products derived from 18 * this software without specific prior written permission. 19 * 20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 21 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 23 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 24 * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 25 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 26 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 27 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 28 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 30 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 31\**************************************************************************/ 32 33/* this file should only be included from thread.c */ 34 35#include "glue/win32api.h" 36 37static DWORD WINAPI 38cc_w32thread_thread_proc(LPVOID lpParameter) 39{ 40 cc_thread *thread = (cc_thread *)lpParameter; 41 return (DWORD) reinterpret_cast<uintptr_t>(thread->func(thread->closure)); 42} 43 44static int 45internal_init(cc_thread * thread) 46{ 47 DWORD threadid_unused; 48 49 thread->w32thread.threadhandle = CreateThread(NULL, 0, 50 cc_w32thread_thread_proc, (LPVOID) thread, 0, &threadid_unused); 51 52 /* threadid_unused - see PlatformSDK doc. for CreateThread */ 53 54 /* FIXME: thammer 20011108, check PlatformSDK doc for 55 * _beginthreadex, _endthreadex, and note about using these with 56 * LIBCMT.LIB "A thread that uses functions from the C run-time 57 * libraries should use the beginthread and endthread C run-time 58 * functions for thread management rather than CreateThread and 59 * ExitThread. Failure to do so results in small memory leaks when 60 * ExitThread is called. " */ 61 62 if (thread->w32thread.threadhandle == NULL) { 63 if (COIN_DEBUG) { 64 cc_win32_print_error("internal_init", "CreateThread()", GetLastError()); 65 } 66 return CC_ERROR; 67 } 68 return CC_OK; 69} 70 71static int 72internal_clean(cc_thread * thread_struct) 73{ 74 /* FIXME: Is there really nothing to do here? pederb, 2001-12-10 */ 75 return CC_OK; 76} 77 78static int 79internal_join(cc_thread * thread, 80 void ** retvalptr) 81{ 82 DWORD status; 83 BOOL bstatus; 84 DWORD exitcode; 85 86 status = WaitForSingleObject(thread->w32thread.threadhandle, INFINITE); 87 if (status == WAIT_FAILED) { 88 if (COIN_DEBUG) { 89 cc_win32_print_error("internal_join", "WaitForSingleObject()", 90 GetLastError()); 91 } 92 return CC_ERROR; 93 } 94 else if (status != WAIT_OBJECT_0) { 95 if (COIN_DEBUG) { 96 cc_debugerror_post("internal_join", 97 "WaitForSingleObject() - unknown return value: %d\n", 98 status); 99 } 100 return CC_ERROR; 101 } 102 bstatus = GetExitCodeThread(thread->w32thread.threadhandle, &exitcode); 103 if (bstatus == FALSE) { 104 if (COIN_DEBUG) { 105 cc_win32_print_error("internal_join", "GetExitCodeThread()", 106 GetLastError()); 107 } 108 } 109 else if (retvalptr) { 110 *retvalptr = (void *)exitcode; 111 } 112 /* termination could be forced with TerminateThread() - but this 113 * will result in memory leaks - or bigger problems - see Platform 114 * SDK doc. */ 115 CloseHandle(thread->w32thread.threadhandle); 116 thread->w32thread.threadhandle = NULL; 117 118 return bstatus ? CC_OK : CC_ERROR; 119} 120