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 esp_err_t init()
25     {
26         return ESP_OK;
27     }
28 
uninit()29     static void uninit() {}
30 };
31 } // namespace nvs
32 
33 #else // LINUX_TARGET
34 
35 #include "freertos/FreeRTOS.h"
36 #include "freertos/semphr.h"
37 
38 namespace nvs
39 {
40 
41 class Lock
42 {
43 public:
Lock()44     Lock()
45     {
46         if (mSemaphore) {
47             xSemaphoreTake(mSemaphore, portMAX_DELAY);
48         }
49     }
50 
~Lock()51     ~Lock()
52     {
53         if (mSemaphore) {
54             xSemaphoreGive(mSemaphore);
55         }
56     }
57 
init()58     static esp_err_t init()
59     {
60         if (mSemaphore) {
61             return ESP_OK;
62         }
63         mSemaphore = xSemaphoreCreateMutex();
64         if (!mSemaphore) {
65             return ESP_ERR_NO_MEM;
66         }
67         return ESP_OK;
68     }
69 
uninit()70     static void uninit()
71     {
72         if (mSemaphore) {
73             vSemaphoreDelete(mSemaphore);
74         }
75         mSemaphore = nullptr;
76     }
77 
78     static SemaphoreHandle_t mSemaphore;
79 };
80 } // namespace nvs
81 
82 #endif // LINUX_TARGET
83