/**************************************************************************\
 * Copyright (c) Kongsberg Oil & Gas Technologies AS
 * All rights reserved.
 * 
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are
 * met:
 * 
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * 
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * 
 * Neither the name of the copyright holder nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * 
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
\**************************************************************************/

/* this file should only be included from thread.c */

#include "glue/win32api.h"

static DWORD WINAPI
cc_w32thread_thread_proc(LPVOID lpParameter)
{
  cc_thread *thread = (cc_thread *)lpParameter;
  return (DWORD) reinterpret_cast<uintptr_t>(thread->func(thread->closure));
}

static int
internal_init(cc_thread * thread)
{
  DWORD threadid_unused;

  thread->w32thread.threadhandle = CreateThread(NULL, 0,
    cc_w32thread_thread_proc, (LPVOID) thread, 0, &threadid_unused);

  /* threadid_unused - see PlatformSDK doc. for CreateThread */

  /* FIXME: thammer 20011108, check PlatformSDK doc for
   * _beginthreadex, _endthreadex, and note about using these with
   * LIBCMT.LIB "A thread that uses functions from the C runtime
   * libraries should use the beginthread and endthread C runtime
   * functions for thread management rather than CreateThread and
   * ExitThread. Failure to do so results in small memory leaks when
   * ExitThread is called. " */

  if (thread->w32thread.threadhandle == NULL) {
    if (COIN_DEBUG) {
      cc_win32_print_error("internal_init", "CreateThread()", GetLastError());
    }
    return CC_ERROR;
  }
  return CC_OK;
}

static int
internal_clean(cc_thread * thread_struct)
{
  /* FIXME: Is there really nothing to do here? pederb, 2001-12-10 */
  return CC_OK;
}

static int
internal_join(cc_thread * thread,
               void ** retvalptr)
{
  DWORD status;
  BOOL bstatus;
  DWORD exitcode;

  status = WaitForSingleObject(thread->w32thread.threadhandle, INFINITE);
  if (status == WAIT_FAILED) {
    if (COIN_DEBUG) {
      cc_win32_print_error("internal_join", "WaitForSingleObject()",
                           GetLastError());
    }
    return CC_ERROR;
  }
  else if (status != WAIT_OBJECT_0) {
    if (COIN_DEBUG) {
      cc_debugerror_post("internal_join",
                         "WaitForSingleObject() - unknown return value: %d\n",
                         status);
    }
    return CC_ERROR;
  }
  bstatus = GetExitCodeThread(thread->w32thread.threadhandle, &exitcode);
  if (bstatus == FALSE) {
    if (COIN_DEBUG) {
      cc_win32_print_error("internal_join", "GetExitCodeThread()",
                           GetLastError());
    }
  }
  else if (retvalptr) {
    *retvalptr = reinterpret_cast<void *>(static_cast<uintptr_t>(exitcode));
  }
  /* termination could be forced with TerminateThread() - but this
   * will result in memory leaks - or bigger problems - see Platform
   * SDK doc. */
  CloseHandle(thread->w32thread.threadhandle);
  thread->w32thread.threadhandle = NULL;

  return bstatus ? CC_OK : CC_ERROR;
}
