| 1 |
/* |
| 2 |
* libmowgli: A collection of useful routines for programming. |
| 3 |
* win32_mutexops.c: Windows mutex operations |
| 4 |
* |
| 5 |
* Copyright (c) 2011 Wilcox Technologies, LLC <awilcox -at- wilcox-tech.com> |
| 6 |
* |
| 7 |
* Permission to use, copy, modify, and/or distribute this software for any |
| 8 |
* purpose with or without fee is hereby granted, provided that the above |
| 9 |
* copyright notice and this permission notice is present in all copies. |
| 10 |
* |
| 11 |
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
| 12 |
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
| 13 |
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
| 14 |
* DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, |
| 15 |
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
| 16 |
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
| 17 |
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 18 |
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, |
| 19 |
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING |
| 20 |
* IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
| 21 |
* POSSIBILITY OF SUCH DAMAGE. |
| 22 |
*/ |
| 23 |
|
| 24 |
#include "mowgli.h" |
| 25 |
#include "thread/mutex_internal.h" |
| 26 |
|
| 27 |
/************* |
| 28 |
* This Windows implementation is guaranteed to work on Windows 95, |
| 29 |
* Windows NT 4, and anything later. |
| 30 |
*************/ |
| 31 |
#if defined(_WIN32) |
| 32 |
|
| 33 |
static int |
| 34 |
mowgli_win32_mutex_create(mowgli_mutex_t *mutex) |
| 35 |
{ |
| 36 |
mutex->mutex = CreateMutex(NULL, FALSE, NULL); |
| 37 |
|
| 38 |
if (mutex->mutex == NULL) |
| 39 |
return GetLastError(); |
| 40 |
|
| 41 |
return 0; |
| 42 |
} |
| 43 |
|
| 44 |
static int |
| 45 |
mowgli_win32_mutex_lock(mowgli_mutex_t *mutex) |
| 46 |
{ |
| 47 |
return WaitForSingleObject(mutex->mutex, INFINITE); |
| 48 |
} |
| 49 |
|
| 50 |
static int |
| 51 |
mowgli_win32_mutex_trylock(mowgli_mutex_t *mutex) |
| 52 |
{ |
| 53 |
return WaitForSingleObject(mutex->mutex, 0); |
| 54 |
} |
| 55 |
|
| 56 |
static int |
| 57 |
mowgli_win32_mutex_unlock(mowgli_mutex_t *mutex) |
| 58 |
{ |
| 59 |
if (ReleaseMutex(mutex->mutex) != 0) |
| 60 |
return 0; |
| 61 |
|
| 62 |
return GetLastError(); |
| 63 |
} |
| 64 |
|
| 65 |
static int |
| 66 |
mowgli_win32_mutex_destroy(mowgli_mutex_t *mutex) |
| 67 |
{ |
| 68 |
CloseHandle(mutex->mutex); |
| 69 |
return 0; |
| 70 |
} |
| 71 |
|
| 72 |
const mowgli_mutex_ops_t _mowgli_win32_mutex_ops = |
| 73 |
{ |
| 74 |
.mutex_create = mowgli_win32_mutex_create, |
| 75 |
.mutex_lock = mowgli_win32_mutex_lock, |
| 76 |
.mutex_trylock = mowgli_win32_mutex_trylock, |
| 77 |
.mutex_unlock = mowgli_win32_mutex_unlock, |
| 78 |
.mutex_destroy = mowgli_win32_mutex_destroy |
| 79 |
}; |
| 80 |
|
| 81 |
#endif |