Fix for x86_64 build fail
[platform/upstream/connectedhomeip.git] / src / system / SystemMutex.cpp
1 /*
2  *
3  *    Copyright (c) 2020 Project CHIP Authors
4  *    Copyright (c) 2016-2017 Nest Labs, Inc.
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 <system/SystemMutex.h>
27
28 // Include common private header
29 #include "SystemLayerPrivate.h"
30
31 #if !CHIP_SYSTEM_CONFIG_NO_LOCKING
32
33 // Include system headers
34 #include <errno.h>
35
36 namespace chip {
37 namespace System {
38
39 /**
40  * Initialize the mutual exclusion lock instance.
41  *
42  *  @param[in,out]  aThis   A zero-initialized object.
43  *
44  *  @retval         #CHIP_SYSTEM_NO_ERROR                  The mutual exclusion lock is ready to use.
45  *  @retval         #CHIP_SYSTEM_ERROR_NO_MEMORY           Insufficient system memory to allocate the mutual exclusion lock.
46  *  @retval         #CHIP_SYSTEM_ERROR_UNEXPECTED_STATE    An unexpected system error encountered during initialization.
47  */
48
49 #if CHIP_SYSTEM_CONFIG_POSIX_LOCKING
50 DLL_EXPORT Error Mutex::Init(Mutex & aThis)
51 {
52     int lSysError = pthread_mutex_init(&aThis.mPOSIXMutex, nullptr);
53     Error lError;
54
55     switch (lSysError)
56     {
57     case 0:
58         lError = CHIP_SYSTEM_NO_ERROR;
59         break;
60
61     case ENOMEM:
62         lError = CHIP_SYSTEM_ERROR_NO_MEMORY;
63         break;
64
65     default:
66         lError = CHIP_SYSTEM_ERROR_UNEXPECTED_STATE;
67         break;
68     }
69
70     return lError;
71 }
72 #endif // CHIP_SYSTEM_CONFIG_POSIX_LOCKING
73
74 #if CHIP_SYSTEM_CONFIG_FREERTOS_LOCKING
75 DLL_EXPORT Error Mutex::Init(Mutex & aThis)
76 {
77 restart:
78     if (__sync_bool_compare_and_swap(&aThis.mInitialized, 0, 1))
79     {
80 #if (configSUPPORT_STATIC_ALLOCATION == 1)
81         aThis.mFreeRTOSSemaphore = xSemaphoreCreateMutexStatic(&aThis.mFreeRTOSSemaphoreObj);
82 #else
83         aThis.mFreeRTOSSemaphore = xSemaphoreCreateMutex();
84 #endif
85         if (aThis.mFreeRTOSSemaphore == NULL)
86         {
87             aThis.mInitialized = 0;
88
89             return CHIP_SYSTEM_ERROR_NO_MEMORY;
90         }
91     }
92     else
93     {
94         while (aThis.mFreeRTOSSemaphore == NULL)
95         {
96             vTaskDelay(1);
97
98             if (aThis.mInitialized == 0)
99             {
100                 goto restart;
101             }
102         }
103     }
104
105     return CHIP_SYSTEM_NO_ERROR;
106 }
107
108 DLL_EXPORT void Mutex::Lock(void)
109 {
110     xSemaphoreTake(this->mFreeRTOSSemaphore, portMAX_DELAY);
111 }
112 #endif // CHIP_SYSTEM_CONFIG_FREERTOS_LOCKING
113
114 } // namespace System
115 } // namespace chip
116
117 #endif // !CHIP_SYSTEM_CONFIG_NO_LOCKING