Debian Codegraph @

src/vendor/azure-uamqp-c/deps/azure-c-shared-utility/pal/freertos/lock.c

raw
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
7
8 #include "freertos/FreeRTOS.h"
9 #include "freertos/semphr.h"
10
11 LOCK_HANDLE Lock_Init(void)
12 {
13 /* Codes_SRS_LOCK_10_002: [Lock_Init on success shall return a valid lock handle which should be a non NULL value] */
14 /* Codes_SRS_LOCK_10_003: [Lock_Init on error shall return NULL ] */
15 // xSemaphoreCreateMutex is the obvious choice, but it returns a recursive
16 // sync object, which we do not want for the lock adapter.
17 SemaphoreHandle_t result = xSemaphoreCreateBinary();
18 if (result != NULL)
19 {
20 int gv = xSemaphoreGive(result);
21 if (gv != pdTRUE)
22 {
23 LogError("xSemaphoreGive failed after create.");
24 vSemaphoreDelete(result);
25 result = NULL;
26 }
27 }
28 else
29 {
30 LogError("xSemaphoreCreateBinary failed.");
31 }
32
33 return (LOCK_HANDLE)result;
34 }
35
36 LOCK_RESULT Lock_Deinit(LOCK_HANDLE handle)
37 {
38 LOCK_RESULT result;
39 if (handle == NULL)
40 {
41 /* Codes_SRS_LOCK_10_007: [Lock_Deinit on NULL handle passed returns LOCK_ERROR] */
42 LogError("Invalid argument; handle is NULL.");
43 result = LOCK_ERROR;
44 }
45 else
46 {
47 /* Codes_SRS_LOCK_10_012: [Lock_Deinit frees the memory pointed by handle] */
48 vSemaphoreDelete((SemaphoreHandle_t)handle);
49 result = LOCK_OK;
50 }
51
52 return result;
53 }
54
55 LOCK_RESULT Lock(LOCK_HANDLE handle)
56 {
57 LOCK_RESULT result;
58 if (handle == NULL)
59 {
60 /* Codes_SRS_LOCK_10_007: [Lock on NULL handle passed returns LOCK_ERROR] */
61 LogError("Invalid argument; handle is NULL.");
62 result = LOCK_ERROR;
63 }
64 else
65 {
66 int rv = xSemaphoreTake((SemaphoreHandle_t)handle, portMAX_DELAY);
67 switch (rv)
68 {
69 case pdTRUE:
70 /* Codes_SRS_LOCK_10_005: [Lock on success shall return LOCK_OK] */
71 result = LOCK_OK;
72 break;
73 default:
74 LogError("xSemaphoreTake failed.");
75 /* Codes_SRS_LOCK_10_006: [Lock on error shall return LOCK_ERROR] */
76 result = LOCK_ERROR;
77 break;
78 }
79 }
80
81 return result;
82 }
83
84 LOCK_RESULT Unlock(LOCK_HANDLE handle)
85 {
86 LOCK_RESULT result;
87 if (handle == NULL)
88 {
89 /* Codes_SRS_LOCK_10_007: [Unlock on NULL handle passed returns LOCK_ERROR] */
90 LogError("Invalid argument; handle is NULL.");
91 result = LOCK_ERROR;
92 }
93 else
94 {
95 int rv = xSemaphoreGive((SemaphoreHandle_t)handle);
96 switch (rv)
97 {
98 case pdTRUE:
99 /* Codes_SRS_LOCK_10_005: [Lock on success shall return LOCK_OK] */
100 result = LOCK_OK;
101 break;
102 default:
103 LogError("xSemaphoreGive failed.");
104 /* Codes_SRS_LOCK_10_006: [Lock on error shall return LOCK_ERROR] */
105 result = LOCK_ERROR;
106 break;
107 }
108 }
109
110 return result;
111 }