/src/openweave-core/src/system/SystemMutex.cpp
Line | Count | Source |
1 | | /* |
2 | | * |
3 | | * Copyright (c) 2016-2017 Nest Labs, Inc. |
4 | | * All rights reserved. |
5 | | * |
6 | | * Licensed under the Apache License, Version 2.0 (the "License"); |
7 | | * you may not use this file except in compliance with the License. |
8 | | * You may obtain a copy of the License at |
9 | | * |
10 | | * http://www.apache.org/licenses/LICENSE-2.0 |
11 | | * |
12 | | * Unless required by applicable law or agreed to in writing, software |
13 | | * distributed under the License is distributed on an "AS IS" BASIS, |
14 | | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
15 | | * See the License for the specific language governing permissions and |
16 | | * limitations under the License. |
17 | | */ |
18 | | |
19 | | /** |
20 | | * @file |
21 | | * This file defines the abstraction of mutual exclusion locks |
22 | | * offered by the target platform. |
23 | | */ |
24 | | |
25 | | // Include module header |
26 | | #include <SystemLayer/SystemMutex.h> |
27 | | |
28 | | // Include common private header |
29 | | #include "SystemLayerPrivate.h" |
30 | | |
31 | | #if !WEAVE_SYSTEM_CONFIG_NO_LOCKING |
32 | | |
33 | | // Include system headers |
34 | | #include <errno.h> |
35 | | |
36 | | namespace nl { |
37 | | namespace Weave { |
38 | | namespace System { |
39 | | |
40 | | /** |
41 | | * Initialize the mutual exclusion lock instance. |
42 | | * |
43 | | * @param[inout] aThis A zero-initialized object. |
44 | | * |
45 | | * @retval #WEAVE_SYSTEM_NO_ERROR The mutual exclusion lock is ready to use. |
46 | | * @retval #WEAVE_SYSTEM_ERROR_NO_MEMORY Insufficient system memory to allocate the mutual exclusion lock. |
47 | | * @retval #WEAVE_SYSTEM_ERROR_UNEXPECTED_STATE An unexpected system error encountered during initialization. |
48 | | */ |
49 | | |
50 | | #if WEAVE_SYSTEM_CONFIG_POSIX_LOCKING |
51 | | NL_DLL_EXPORT Error Mutex::Init(Mutex& aThis) |
52 | 12 | { |
53 | 12 | int lSysError = pthread_mutex_init(&aThis.mPOSIXMutex, NULL); |
54 | 12 | Error lError; |
55 | | |
56 | 12 | switch (lSysError) |
57 | 12 | { |
58 | 12 | case 0: |
59 | 12 | lError = WEAVE_SYSTEM_NO_ERROR; |
60 | 12 | break; |
61 | | |
62 | 0 | case ENOMEM: |
63 | 0 | lError = WEAVE_SYSTEM_ERROR_NO_MEMORY; |
64 | 0 | break; |
65 | | |
66 | 0 | default: |
67 | 0 | lError = WEAVE_SYSTEM_ERROR_UNEXPECTED_STATE; |
68 | 0 | break; |
69 | 12 | } |
70 | | |
71 | 12 | return lError; |
72 | 12 | } |
73 | | #endif // WEAVE_SYSTEM_CONFIG_POSIX_LOCKING |
74 | | |
75 | | #if WEAVE_SYSTEM_CONFIG_FREERTOS_LOCKING |
76 | | NL_DLL_EXPORT Error Mutex::Init(Mutex& aThis) |
77 | | { |
78 | | restart: |
79 | | if (__sync_bool_compare_and_swap(&aThis.mInitialized, 0, 1)) |
80 | | { |
81 | | #if (configSUPPORT_STATIC_ALLOCATION == 1) |
82 | | aThis.mFreeRTOSSemaphore = xSemaphoreCreateMutexStatic(&aThis.mFreeRTOSSemaphoreObj); |
83 | | #else |
84 | | aThis.mFreeRTOSSemaphore = xSemaphoreCreateMutex(); |
85 | | #endif |
86 | | if (aThis.mFreeRTOSSemaphore == NULL) |
87 | | { |
88 | | aThis.mInitialized = 0; |
89 | | |
90 | | return WEAVE_SYSTEM_ERROR_NO_MEMORY; |
91 | | } |
92 | | } else { |
93 | | while (aThis.mFreeRTOSSemaphore == NULL) |
94 | | { |
95 | | vTaskDelay(1); |
96 | | |
97 | | if (aThis.mInitialized == 0) |
98 | | { |
99 | | goto restart; |
100 | | } |
101 | | } |
102 | | } |
103 | | |
104 | | return WEAVE_SYSTEM_NO_ERROR; |
105 | | } |
106 | | |
107 | | NL_DLL_EXPORT void Mutex::Lock(void) |
108 | | { |
109 | | xSemaphoreTake(this->mFreeRTOSSemaphore, portMAX_DELAY); |
110 | | } |
111 | | #endif // WEAVE_SYSTEM_CONFIG_FREERTOS_LOCKING |
112 | | |
113 | | } // namespace System |
114 | | } // namespace Weave |
115 | | } // namespace nl |
116 | | |
117 | | #endif // !WEAVE_SYSTEM_CONFIG_NO_LOCKING |