| 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 "azure_c_shared_utility/lock.h" |
| 5 |
#include "azure_c_shared_utility/xlogging.h" |
| 6 |
#include "rtos.h" |
| 7 |
|
| 8 |
|
| 9 |
LOCK_HANDLE Lock_Init(void) |
| 10 |
{ |
| 11 |
/* Codes_SRS_LOCK_10_002: [Lock_Init on success shall return a valid lock handle which should be a non NULL value] */ |
| 12 |
/* Codes_SRS_LOCK_10_003: [Lock_Init on error shall return NULL] */ |
| 13 |
Mutex* result = new Mutex(); |
| 14 |
if (result == NULL) |
| 15 |
{ |
| 16 |
LogError("Failed to instantiate a new Mutex object."); |
| 17 |
} |
| 18 |
|
| 19 |
return (LOCK_HANDLE)result; |
| 20 |
} |
| 21 |
|
| 22 |
LOCK_RESULT Lock(LOCK_HANDLE handle) |
| 23 |
{ |
| 24 |
LOCK_RESULT result; |
| 25 |
if (handle == NULL) |
| 26 |
{ |
| 27 |
/* Codes_SRS_LOCK_10_007: [Lock on NULL handle passed returns LOCK_ERROR] */ |
| 28 |
LogError("Invalid argument; handle is NULL."); |
| 29 |
result = LOCK_ERROR; |
| 30 |
} |
| 31 |
else |
| 32 |
{ |
| 33 |
Mutex* lock_mtx = (Mutex*)handle; |
| 34 |
if (lock_mtx->lock() == osOK) |
| 35 |
{ |
| 36 |
/* Codes_SRS_LOCK_10_005: [Lock on success shall return LOCK_OK] */ |
| 37 |
result = LOCK_OK; |
| 38 |
} |
| 39 |
else |
| 40 |
{ |
| 41 |
/* Codes_SRS_LOCK_10_006: [Lock on error shall return LOCK_ERROR] */ |
| 42 |
LogError("Mutex(%p)->lock() failed.", handle); |
| 43 |
result = LOCK_ERROR; |
| 44 |
} |
| 45 |
} |
| 46 |
|
| 47 |
return result; |
| 48 |
} |
| 49 |
|
| 50 |
LOCK_RESULT Unlock(LOCK_HANDLE handle) |
| 51 |
{ |
| 52 |
LOCK_RESULT result; |
| 53 |
if (handle == NULL) |
| 54 |
{ |
| 55 |
/* Codes_SRS_LOCK_10_007: [Unlock on NULL handle passed returns LOCK_ERROR] */ |
| 56 |
LogError("Invalid argument; handle is NULL."); |
| 57 |
result = LOCK_ERROR; |
| 58 |
} |
| 59 |
else |
| 60 |
{ |
| 61 |
Mutex* lock_mtx = (Mutex*)handle; |
| 62 |
if (lock_mtx->unlock() == osOK) |
| 63 |
{ |
| 64 |
/* Codes_SRS_LOCK_10_009: [Unlock on success shall return LOCK_OK] */ |
| 65 |
result = LOCK_OK; |
| 66 |
} |
| 67 |
else |
| 68 |
{ |
| 69 |
/* Codes_SRS_LOCK_10_010: [Unlock on error shall return LOCK_ERROR] */ |
| 70 |
LogError("Mutex(%p)->unlock() failed.", handle); |
| 71 |
result = LOCK_ERROR; |
| 72 |
} |
| 73 |
} |
| 74 |
|
| 75 |
return result; |
| 76 |
} |
| 77 |
|
| 78 |
LOCK_RESULT Lock_Deinit(LOCK_HANDLE handle) |
| 79 |
{ |
| 80 |
LOCK_RESULT result; |
| 81 |
if (NULL == handle) |
| 82 |
{ |
| 83 |
/* Codes_SRS_LOCK_10_007: [Lock_Deinit on NULL handle passed returns LOCK_ERROR] */ |
| 84 |
LogError("Invalid argument; handle is NULL."); |
| 85 |
result = LOCK_ERROR; |
| 86 |
} |
| 87 |
else |
| 88 |
{ |
| 89 |
/* Codes_SRS_LOCK_10_012: [Lock_Deinit frees the memory pointed by handle] */ |
| 90 |
Mutex* lock_mtx = (Mutex*)handle; |
| 91 |
delete lock_mtx; |
| 92 |
result = LOCK_OK; |
| 93 |
} |
| 94 |
|
| 95 |
return result; |
| 96 |
} |