| 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 <pthread.h> |
| 6 |
#include "azure_c_shared_utility/lock.h" |
| 7 |
#include "azure_c_shared_utility/xlogging.h" |
| 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 |
pthread_mutex_t* result = (pthread_mutex_t*)malloc(sizeof(pthread_mutex_t)); |
| 13 |
if (result == NULL) |
| 14 |
{ |
| 15 |
LogError("malloc failed."); |
| 16 |
} |
| 17 |
else |
| 18 |
{ |
| 19 |
if (pthread_mutex_init(result, NULL) != 0) |
| 20 |
{ |
| 21 |
/* Codes_SRS_LOCK_10_003: [Lock_Init on error shall return NULL ] */ |
| 22 |
LogError("pthread_mutex_init failed."); |
| 23 |
free(result); |
| 24 |
result = NULL; |
| 25 |
} |
| 26 |
} |
| 27 |
|
| 28 |
return (LOCK_HANDLE)result; |
| 29 |
} |
| 30 |
|
| 31 |
LOCK_RESULT Lock(LOCK_HANDLE handle) |
| 32 |
{ |
| 33 |
LOCK_RESULT result; |
| 34 |
if (handle == NULL) |
| 35 |
{ |
| 36 |
/* Codes_SRS_LOCK_10_007: [Lock on NULL handle passed returns LOCK_ERROR] */ |
| 37 |
LogError("Invalid argument; handle is NULL."); |
| 38 |
result = LOCK_ERROR; |
| 39 |
} |
| 40 |
else |
| 41 |
{ |
| 42 |
int return_code = pthread_mutex_lock((pthread_mutex_t*)handle); |
| 43 |
if (return_code == 0) |
| 44 |
{ |
| 45 |
/* Codes_SRS_LOCK_10_005: [Lock on success shall return LOCK_OK] */ |
| 46 |
result = LOCK_OK; |
| 47 |
} |
| 48 |
else |
| 49 |
{ |
| 50 |
/* Codes_SRS_LOCK_10_006: [Lock on error shall return LOCK_ERROR] */ |
| 51 |
LogError("pthread_mutex_lock failed (%d).", return_code); |
| 52 |
result = LOCK_ERROR; |
| 53 |
} |
| 54 |
} |
| 55 |
|
| 56 |
return result; |
| 57 |
} |
| 58 |
|
| 59 |
LOCK_RESULT Unlock(LOCK_HANDLE handle) |
| 60 |
{ |
| 61 |
LOCK_RESULT result; |
| 62 |
if (handle == NULL) |
| 63 |
{ |
| 64 |
/* Codes_SRS_LOCK_10_007: [Unlock on NULL handle passed returns LOCK_ERROR] */ |
| 65 |
LogError("Invalid argument; handle is NULL."); |
| 66 |
result = LOCK_ERROR; |
| 67 |
} |
| 68 |
else |
| 69 |
{ |
| 70 |
int return_code = pthread_mutex_unlock((pthread_mutex_t*)handle); |
| 71 |
if (return_code == 0) |
| 72 |
{ |
| 73 |
/* Codes_SRS_LOCK_10_009: [Unlock on success shall return LOCK_OK] */ |
| 74 |
result = LOCK_OK; |
| 75 |
} |
| 76 |
else |
| 77 |
{ |
| 78 |
/* Codes_SRS_LOCK_10_010: [Unlock on error shall return LOCK_ERROR] */ |
| 79 |
LogError("pthread_mutex_unlock failed (%d).", return_code); |
| 80 |
result = LOCK_ERROR; |
| 81 |
} |
| 82 |
} |
| 83 |
|
| 84 |
return result; |
| 85 |
} |
| 86 |
|
| 87 |
LOCK_RESULT Lock_Deinit(LOCK_HANDLE handle) |
| 88 |
{ |
| 89 |
LOCK_RESULT result; |
| 90 |
if (NULL == handle) |
| 91 |
{ |
| 92 |
/* Codes_SRS_LOCK_10_007: [Lock_Deinit on NULL handle passed returns LOCK_ERROR] */ |
| 93 |
LogError("Invalid argument; handle is NULL."); |
| 94 |
result = LOCK_ERROR; |
| 95 |
} |
| 96 |
else |
| 97 |
{ |
| 98 |
/* Codes_SRS_LOCK_10_012: [Lock_Deinit frees the memory pointed by handle] */ |
| 99 |
if(pthread_mutex_destroy((pthread_mutex_t*)handle) == 0) |
| 100 |
{ |
| 101 |
free(handle); |
| 102 |
handle = NULL; |
| 103 |
result = LOCK_OK; |
| 104 |
} |
| 105 |
else |
| 106 |
{ |
| 107 |
LogError("pthread_mutex_destroy failed;"); |
| 108 |
result = LOCK_ERROR; |
| 109 |
} |
| 110 |
} |
| 111 |
|
| 112 |
return result; |
| 113 |
} |