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 mutex.c */ 34 35#include "glue/win32api.h" 36 37/* FIXME: some of the cases where we are robust and catch and return 38 error codes should probably be replaced by asserts. Should audit 39 the code versus the Win32 API documentation and fix. 20030604 mortene. */ 40 41static int 42win32_mutex_struct_init(cc_mutex * mutex_struct) 43{ 44 mutex_struct->w32thread.mutexhandle = CreateMutex(NULL, FALSE, NULL); 45 if (mutex_struct->w32thread.mutexhandle == NULL) { 46 if (COIN_DEBUG) { 47 cc_win32_print_error("win32_mutex_struct_init", 48 "CreateMutex()", GetLastError()); 49 } 50 return CC_ERROR; 51 } 52 return CC_OK; 53} 54 55static int 56win32_mutex_struct_clean(cc_mutex * mutex_struct) 57{ 58 BOOL status = CloseHandle(mutex_struct->w32thread.mutexhandle); 59 if (status == FALSE) { 60 if (COIN_DEBUG) { 61 cc_win32_print_error("win32_mutex_struct_clean", 62 "CloseHandle()", GetLastError()); 63 } 64 return CC_ERROR; 65 } 66 return CC_OK; 67} 68 69static int 70win32_mutex_lock(cc_mutex * mutex) 71{ 72 DWORD status; 73 status = WaitForSingleObject(mutex->w32thread.mutexhandle, INFINITE); 74 if (status == WAIT_FAILED) { 75 if (COIN_DEBUG) { 76 cc_win32_print_error("win32_mutex_lock", 77 "WaitSingleObject()", GetLastError()); 78 } 79 return CC_ERROR; 80 } 81 return CC_OK; 82} 83 84static int 85win32_mutex_try_lock(cc_mutex * mutex) 86{ 87 DWORD status; 88 status = WaitForSingleObject(mutex->w32thread.mutexhandle, 0); 89 if (status == WAIT_TIMEOUT) 90 return CC_BUSY; 91 else if ((status == WAIT_OBJECT_0) || (status == WAIT_ABANDONED)) 92 return CC_OK; 93 94 /* if we get here there was an error */ 95 if (COIN_DEBUG) { 96 cc_win32_print_error("win32_mutex_try_lock", 97 "WaitSingleObject()", GetLastError()); 98 } 99 return CC_ERROR; 100} 101 102static int 103win32_mutex_unlock(cc_mutex * mutex) 104{ 105 BOOL status = ReleaseMutex(mutex->w32thread.mutexhandle); 106 if (status == FALSE) { 107 if (COIN_DEBUG) { 108 cc_win32_print_error("win32_mutex_unlock", 109 "ReleaseMutex()", GetLastError()); 110 } 111 return CC_ERROR; 112 } 113 return CC_OK; 114} 115