• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2015-2016 Espressif Systems (Shanghai) PTE LTD
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 
7 //     http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 #pragma once
15 
16 #ifdef LINUX_TARGET
17 namespace nvs
18 {
19 class Lock
20 {
21 public:
Lock()22     Lock() { }
~Lock()23     ~Lock() { }
init()24     static void init() {}
uninit()25     static void uninit() {}
26 };
27 } // namespace nvs
28 
29 #else // LINUX_TARGET
30 
31 #include "esp_osal/esp_osal.h"
32 #include "esp_osal/semphr.h"
33 
34 namespace nvs
35 {
36 
37 class Lock
38 {
39 public:
Lock()40     Lock()
41     {
42         if (mSemaphore) {
43             xSemaphoreTake(mSemaphore, portMAX_DELAY);
44         }
45     }
46 
~Lock()47     ~Lock()
48     {
49         if (mSemaphore) {
50             xSemaphoreGive(mSemaphore);
51         }
52     }
53 
init()54     static esp_err_t init()
55     {
56         if (mSemaphore) {
57             return ESP_OK;
58         }
59         mSemaphore = xSemaphoreCreateMutex();
60         if (!mSemaphore) {
61             return ESP_ERR_NO_MEM;
62         }
63         return ESP_OK;
64     }
65 
uninit()66     static void uninit()
67     {
68         if (mSemaphore) {
69             vSemaphoreDelete(mSemaphore);
70         }
71         mSemaphore = nullptr;
72     }
73 
74     static SemaphoreHandle_t mSemaphore;
75 };
76 } // namespace nvs
77 
78 #endif // LINUX_TARGET
79