| 1 |
// Copyright (c) Microsoft. All rights reserved. |
| 2 |
// Licensed under the MIT license. See LICENSE file in the project root for full license information. |
| 3 |
|
| 4 |
#include <stdlib.h> |
| 5 |
#include <windows.h> |
| 6 |
#include "azure_c_shared_utility/lock.h" |
| 7 |
#include "azure_c_shared_utility/gballoc.h" |
| 8 |
#include "azure_c_shared_utility/xlogging.h" |
| 9 |
|
| 10 |
#include "azure_macro_utils/macro_utils.h" |
| 11 |
|
| 12 |
LOCK_HANDLE Lock_Init(void) |
| 13 |
{ |
| 14 |
/* Codes_SRS_LOCK_10_002: [Lock_Init on success shall return a valid lock handle which should be a non NULL value] */ |
| 15 |
/* Codes_SRS_LOCK_10_003: [Lock_Init on error shall return NULL ] */ |
| 16 |
SRWLOCK* result = malloc(sizeof(SRWLOCK)); |
| 17 |
if (result == NULL) |
| 18 |
{ |
| 19 |
LogError("Allocate SRWLOCK failed."); |
| 20 |
} |
| 21 |
else |
| 22 |
{ |
| 23 |
InitializeSRWLock(result); |
| 24 |
} |
| 25 |
|
| 26 |
return (LOCK_HANDLE)result; |
| 27 |
} |
| 28 |
|
| 29 |
LOCK_RESULT Lock_Deinit(LOCK_HANDLE handle) |
| 30 |
{ |
| 31 |
LOCK_RESULT result; |
| 32 |
if (handle == NULL) |
| 33 |
{ |
| 34 |
/* Codes_SRS_LOCK_10_007: [Lock_Deinit on NULL handle passed returns LOCK_ERROR] */ |
| 35 |
LogError("Invalid argument; handle is NULL."); |
| 36 |
result = LOCK_ERROR; |
| 37 |
} |
| 38 |
else |
| 39 |
{ |
| 40 |
/* Codes_SRS_LOCK_10_012: [Lock_Deinit frees the memory pointed by handle] */ |
| 41 |
free(handle); |
| 42 |
result = LOCK_OK; |
| 43 |
} |
| 44 |
|
| 45 |
return result; |
| 46 |
} |
| 47 |
|
| 48 |
LOCK_RESULT Lock(LOCK_HANDLE handle) |
| 49 |
{ |
| 50 |
LOCK_RESULT result; |
| 51 |
if (handle == NULL) |
| 52 |
{ |
| 53 |
/* Codes_SRS_LOCK_10_007: [Lock on NULL handle passed returns LOCK_ERROR] */ |
| 54 |
LogError("Invalid argument; handle is NULL."); |
| 55 |
result = LOCK_ERROR; |
| 56 |
} |
| 57 |
else |
| 58 |
{ |
| 59 |
AcquireSRWLockExclusive((SRWLOCK*)handle); |
| 60 |
/* Codes_SRS_LOCK_10_005: [Lock on success shall return LOCK_OK] */ |
| 61 |
result = LOCK_OK; |
| 62 |
|
| 63 |
// Cannot fail |
| 64 |
/* Codes_SRS_LOCK_10_006: [Lock on error shall return LOCK_ERROR] */ |
| 65 |
} |
| 66 |
|
| 67 |
return result; |
| 68 |
} |
| 69 |
|
| 70 |
LOCK_RESULT Unlock(LOCK_HANDLE handle) |
| 71 |
{ |
| 72 |
LOCK_RESULT result; |
| 73 |
if (handle == NULL) |
| 74 |
{ |
| 75 |
/* Codes_SRS_LOCK_10_007: [Unlock on NULL handle passed returns LOCK_ERROR] */ |
| 76 |
LogError("Invalid argument; handle is NULL."); |
| 77 |
result = LOCK_ERROR; |
| 78 |
} |
| 79 |
else |
| 80 |
{ |
| 81 |
#ifdef _MSC_VER |
| 82 |
#pragma warning(disable:26110) // Warning C26110: Caller failing to hold lock 'handle' before calling function 'ReleaseSRWLockExclusive'. |
| 83 |
#endif |
| 84 |
ReleaseSRWLockExclusive((SRWLOCK*)handle); |
| 85 |
#ifdef _MSC_VER |
| 86 |
#pragma warning (default:26110) |
| 87 |
#endif |
| 88 |
/* Codes_SRS_LOCK_10_009: [Unlock on success shall return LOCK_OK] */ |
| 89 |
result = LOCK_OK; |
| 90 |
|
| 91 |
// Cannot fail |
| 92 |
/* Codes_SRS_LOCK_10_010: [Unlock on error shall return LOCK_ERROR] */ |
| 93 |
} |
| 94 |
|
| 95 |
return result; |
| 96 |
} |