Skip to content

Commit f8cfa3d

Browse files
committed
add _lockFS()/_unlockFS() to Adafruit_LittleFS
1 parent d643515 commit f8cfa3d

File tree

4 files changed

+92
-89
lines changed

4 files changed

+92
-89
lines changed

libraries/Adafruit_LittleFS/src/Adafruit_LittleFS.cpp

Lines changed: 31 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,8 @@ Adafruit_LittleFS::~Adafruit_LittleFS ()
5757
// User should format the disk and try again
5858
bool Adafruit_LittleFS::begin (struct lfs_config * cfg)
5959
{
60-
xSemaphoreTake(_mutex, portMAX_DELAY);
60+
_lockFS();
61+
6162
bool ret;
6263
// not a loop, just an quick way to short-circuit on error
6364
do {
@@ -70,27 +71,31 @@ bool Adafruit_LittleFS::begin (struct lfs_config * cfg)
7071
_mounted = (err == LFS_ERR_OK);
7172
ret = _mounted;
7273
} while(0);
73-
xSemaphoreGive(_mutex);
74+
75+
_unlockFS();
7476
return ret;
7577
}
7678

7779
// Tear down and unmount file system
7880
void Adafruit_LittleFS::end(void)
7981
{
80-
xSemaphoreTake(_mutex, portMAX_DELAY);
82+
_lockFS();
83+
8184
if (_mounted)
8285
{
8386
_mounted = false;
8487
int err = lfs_unmount(&_lfs);
8588
PRINT_LFS_ERR(err);
8689
(void)err;
8790
}
88-
xSemaphoreGive(_mutex);
91+
92+
_unlockFS();
8993
}
9094

9195
bool Adafruit_LittleFS::format (void)
9296
{
93-
xSemaphoreTake(_mutex, portMAX_DELAY);
97+
_lockFS();
98+
9499
int err = LFS_ERR_OK;
95100
bool attemptMount = _mounted;
96101
// not a loop, just an quick way to short-circuit on error
@@ -114,7 +119,8 @@ bool Adafruit_LittleFS::format (void)
114119
}
115120
// success!
116121
} while(0);
117-
xSemaphoreGive(_mutex);
122+
123+
_unlockFS();
118124
return LFS_ERR_OK == err;
119125
}
120126

@@ -129,9 +135,11 @@ Adafruit_LittleFS_Namespace::File Adafruit_LittleFS::open (char const *filepath,
129135
bool Adafruit_LittleFS::exists (char const *filepath)
130136
{
131137
struct lfs_info info;
132-
xSemaphoreTake(_mutex, portMAX_DELAY);
138+
_lockFS();
139+
133140
bool ret = (0 == lfs_stat(&_lfs, filepath, &info));
134-
xSemaphoreGive(_mutex);
141+
142+
_unlockFS();
135143
return ret;
136144
}
137145

@@ -143,7 +151,7 @@ bool Adafruit_LittleFS::mkdir (char const *filepath)
143151
const char* slash = filepath;
144152
if ( slash[0] == '/' ) slash++; // skip root '/'
145153

146-
xSemaphoreTake(_mutex, portMAX_DELAY);
154+
_lockFS();
147155

148156
// make intermediate parent directory(ies)
149157
while ( NULL != (slash = strchr(slash, '/')) )
@@ -170,27 +178,32 @@ bool Adafruit_LittleFS::mkdir (char const *filepath)
170178
ret = false;
171179
}
172180
}
173-
xSemaphoreGive(_mutex);
181+
182+
_unlockFS();
174183
return ret;
175184
}
176185

177186
// Remove a file
178187
bool Adafruit_LittleFS::remove (char const *filepath)
179188
{
180-
xSemaphoreTake(_mutex, portMAX_DELAY);
189+
_lockFS();
190+
181191
int err = lfs_remove(&_lfs, filepath);
182192
PRINT_LFS_ERR(err);
183-
xSemaphoreGive(_mutex);
193+
194+
_unlockFS();
184195
return LFS_ERR_OK == err;
185196
}
186197

187198
// Remove a folder
188199
bool Adafruit_LittleFS::rmdir (char const *filepath)
189200
{
190-
xSemaphoreTake(_mutex, portMAX_DELAY);
201+
_lockFS();
202+
191203
int err = lfs_remove(&_lfs, filepath);
192204
PRINT_LFS_ERR(err);
193-
xSemaphoreGive(_mutex);
205+
206+
_unlockFS();
194207
return LFS_ERR_OK == err;
195208
}
196209

@@ -203,10 +216,12 @@ bool Adafruit_LittleFS::rmdir_r (char const *filepath)
203216
to see if issues (such as the orphans in threaded linked list) are resolved.
204217
https://github.com/ARMmbed/littlefs/issues/43
205218
*/
206-
xSemaphoreTake(_mutex, portMAX_DELAY);
219+
_lockFS();
220+
207221
int err = lfs_remove(&_lfs, filepath);
208222
PRINT_LFS_ERR(err);
209-
xSemaphoreGive(_mutex);
223+
224+
_unlockFS();
210225
return LFS_ERR_OK == err;
211226
}
212227

libraries/Adafruit_LittleFS/src/Adafruit_LittleFS.h

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -67,20 +67,19 @@ class Adafruit_LittleFS
6767
// format file system
6868
bool format (void);
6969

70-
lfs_t* getFS(void)
71-
{
72-
return &_lfs;
73-
}
70+
/*------------------------------------------------------------------*/
71+
/* INTERNAL USAGE ONLY
72+
* Although declare as public, it is meant to be invoked by internal
73+
* code. User should not call these directly
74+
*------------------------------------------------------------------*/
75+
lfs_t* _getFS (void) { return &_lfs; }
76+
void _lockFS (void) { xSemaphoreTake(_mutex, portMAX_DELAY); }
77+
void _unlockFS(void) { xSemaphoreGive(_mutex); }
7478

7579
protected:
7680
bool _mounted;
7781
struct lfs_config* _lfs_cfg;
7882
lfs_t _lfs;
79-
80-
// these two functions need access to the private _mutex variable:
81-
friend void Adafruit_LittleFS_Namespace::File::_LockFilesystem(void);
82-
friend void Adafruit_LittleFS_Namespace::File::_UnlockFilesystem(void);
83-
//static_assert(configSUPPORT_STATIC_ALLOCATION == 1, "Currently only supports configuration with STATIC_ALLOCATION enabled");
8483
SemaphoreHandle_t _mutex;
8584

8685
private:

0 commit comments

Comments
 (0)