/**************************************************************************\ * * This file is part of the Coin 3D visualization library. * Copyright (C) 1998-2007 by Systems in Motion. All rights reserved. * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License * ("GPL") version 2 as published by the Free Software Foundation. * See the file LICENSE.GPL at the root directory of this source * distribution for additional information about the GNU GPL. * * For using Coin with software that can not be combined with the GNU * GPL, and for taking advantage of the additional benefits of our * support services, please contact Systems in Motion about acquiring * a Coin Professional Edition License. * * See http://www.coin3d.org/ for more information. * * Systems in Motion, Postboks 1283, Pirsenteret, 7462 Trondheim, NORWAY. * http://www.sim.no/ sales@sim.no coin-support@coin3d.org * \**************************************************************************/ /* this file should only be included from mutex.c */ #include /* FIXME: some of the cases where we are robust and catch and return error codes should probably be replaced by asserts. Should audit the code versus the Win32 API documentation and fix. 20030604 mortene. */ static int win32_mutex_struct_init(cc_mutex * mutex_struct) { mutex_struct->w32thread.mutexhandle = CreateMutex(NULL, FALSE, NULL); if (mutex_struct->w32thread.mutexhandle == NULL) { if (COIN_DEBUG) { cc_win32_print_error("win32_mutex_struct_init", "CreateMutex()", GetLastError()); } return CC_ERROR; } return CC_OK; } static int win32_mutex_struct_clean(cc_mutex * mutex_struct) { BOOL status = CloseHandle(mutex_struct->w32thread.mutexhandle); if (status == FALSE) { if (COIN_DEBUG) { cc_win32_print_error("win32_mutex_struct_clean", "CloseHandle()", GetLastError()); } return CC_ERROR; } return CC_OK; } static int win32_mutex_lock(cc_mutex * mutex) { DWORD status; status = WaitForSingleObject(mutex->w32thread.mutexhandle, INFINITE); if (status == WAIT_FAILED) { if (COIN_DEBUG) { cc_win32_print_error("win32_mutex_lock", "WaitSingleObject()", GetLastError()); } return CC_ERROR; } return CC_OK; } static int win32_mutex_try_lock(cc_mutex * mutex) { DWORD status; status = WaitForSingleObject(mutex->w32thread.mutexhandle, 0); if (status == WAIT_TIMEOUT) return CC_BUSY; else if ((status == WAIT_OBJECT_0) || (status == WAIT_ABANDONED)) return CC_OK; /* if we get here there was an error */ if (COIN_DEBUG) { cc_win32_print_error("win32_mutex_try_lock", "WaitSingleObject()", GetLastError()); } return CC_ERROR; } static int win32_mutex_unlock(cc_mutex * mutex) { BOOL status = ReleaseMutex(mutex->w32thread.mutexhandle); if (status == FALSE) { if (COIN_DEBUG) { cc_win32_print_error("win32_mutex_unlock", "ReleaseMutex()", GetLastError()); } return CC_ERROR; } return CC_OK; }