mirror of
https://github.com/JorjBauer/aiie.git
synced 2024-11-26 11:49:19 +00:00
added mutexes
This commit is contained in:
parent
7773577c42
commit
3a1d0219dc
@ -4,6 +4,7 @@
|
|||||||
#include "teensy-filemanager.h"
|
#include "teensy-filemanager.h"
|
||||||
#include <string.h> // strcpy
|
#include <string.h> // strcpy
|
||||||
#include <TeensyThreads.h>
|
#include <TeensyThreads.h>
|
||||||
|
#include "teensy-println.h"
|
||||||
|
|
||||||
Threads::Mutex fslock;
|
Threads::Mutex fslock;
|
||||||
|
|
||||||
@ -23,6 +24,8 @@ TeensyFileManager::~TeensyFileManager()
|
|||||||
|
|
||||||
int8_t TeensyFileManager::openFile(const char *name)
|
int8_t TeensyFileManager::openFile(const char *name)
|
||||||
{
|
{
|
||||||
|
Threads::Scope locker(fslock);
|
||||||
|
|
||||||
if (cacheFd != -1) {
|
if (cacheFd != -1) {
|
||||||
cacheFile.close();
|
cacheFile.close();
|
||||||
cacheFd = -1;
|
cacheFd = -1;
|
||||||
@ -54,10 +57,9 @@ int8_t TeensyFileManager::openFile(const char *name)
|
|||||||
|
|
||||||
void TeensyFileManager::closeFile(int8_t fd)
|
void TeensyFileManager::closeFile(int8_t fd)
|
||||||
{
|
{
|
||||||
|
Threads::Scope locker(fslock);
|
||||||
if (cacheFd != -1) {
|
if (cacheFd != -1) {
|
||||||
fslock.lock();
|
|
||||||
cacheFile.close();
|
cacheFile.close();
|
||||||
fslock.unlock();
|
|
||||||
cacheFd = -1;
|
cacheFd = -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -80,6 +82,7 @@ const char *TeensyFileManager::fileName(int8_t fd)
|
|||||||
// suffix may be comma-separated
|
// suffix may be comma-separated
|
||||||
int8_t TeensyFileManager::readDir(const char *where, const char *suffix, char *outputFN, int8_t startIdx, uint16_t maxlen)
|
int8_t TeensyFileManager::readDir(const char *where, const char *suffix, char *outputFN, int8_t startIdx, uint16_t maxlen)
|
||||||
{
|
{
|
||||||
|
Threads::Scope locker(fslock);
|
||||||
// ... open, read, save next name, close, return name. Horribly
|
// ... open, read, save next name, close, return name. Horribly
|
||||||
// inefficient but hopefully won't break the sd layer. And if it
|
// inefficient but hopefully won't break the sd layer. And if it
|
||||||
// works then we can make this more efficient later.
|
// works then we can make this more efficient later.
|
||||||
@ -158,20 +161,15 @@ bool TeensyFileManager::_prepCache(int8_t fd)
|
|||||||
// Not our cached file, or we have no cached file
|
// Not our cached file, or we have no cached file
|
||||||
if (cacheFd != -1) {
|
if (cacheFd != -1) {
|
||||||
// Close the old one if we had one
|
// Close the old one if we had one
|
||||||
fslock.lock();
|
|
||||||
cacheFile.close();
|
cacheFile.close();
|
||||||
fslock.unlock();
|
|
||||||
cacheFd = -1;
|
cacheFd = -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Open the new one
|
// Open the new one
|
||||||
fslock.lock();
|
|
||||||
cacheFile = sd.open(cachedNames[fd], O_RDWR | O_CREAT);
|
cacheFile = sd.open(cachedNames[fd], O_RDWR | O_CREAT);
|
||||||
if (!cacheFile.isOpen()) {
|
if (!cacheFile.isOpen()) {
|
||||||
fslock.unlock();
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
fslock.unlock();
|
|
||||||
cacheFd = fd; // cache is live
|
cacheFd = fd; // cache is live
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -184,6 +182,7 @@ void TeensyFileManager::getRootPath(char *toWhere, int8_t maxLen)
|
|||||||
// strncpy(toWhere, "/A2DISKS/", maxLen);
|
// strncpy(toWhere, "/A2DISKS/", maxLen);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// FIXME: this should be private
|
||||||
bool TeensyFileManager::setSeekPosition(int8_t fd, uint32_t pos)
|
bool TeensyFileManager::setSeekPosition(int8_t fd, uint32_t pos)
|
||||||
{
|
{
|
||||||
seekToEnd(fd);
|
seekToEnd(fd);
|
||||||
@ -196,7 +195,7 @@ bool TeensyFileManager::setSeekPosition(int8_t fd, uint32_t pos)
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// FIXME: this should be private
|
||||||
void TeensyFileManager::seekToEnd(int8_t fd)
|
void TeensyFileManager::seekToEnd(int8_t fd)
|
||||||
{
|
{
|
||||||
FatFile f = sd.open(cachedNames[fd], FILE_READ);
|
FatFile f = sd.open(cachedNames[fd], FILE_READ);
|
||||||
@ -210,6 +209,7 @@ void TeensyFileManager::seekToEnd(int8_t fd)
|
|||||||
|
|
||||||
int TeensyFileManager::write(int8_t fd, const void *buf, int nbyte)
|
int TeensyFileManager::write(int8_t fd, const void *buf, int nbyte)
|
||||||
{
|
{
|
||||||
|
Threads::Scope locker(fslock);
|
||||||
// open, seek, write, close.
|
// open, seek, write, close.
|
||||||
if (fd < 0 || fd >= numCached) {
|
if (fd < 0 || fd >= numCached) {
|
||||||
return -1;
|
return -1;
|
||||||
@ -223,25 +223,22 @@ int TeensyFileManager::write(int8_t fd, const void *buf, int nbyte)
|
|||||||
|
|
||||||
uint32_t pos = fileSeekPositions[fd];
|
uint32_t pos = fileSeekPositions[fd];
|
||||||
|
|
||||||
fslock.lock();
|
|
||||||
if (!cacheFile.seekSet(pos)) {
|
if (!cacheFile.seekSet(pos)) {
|
||||||
fslock.unlock();
|
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (cacheFile.write(buf, nbyte) != nbyte) {
|
if (cacheFile.write(buf, nbyte) != nbyte) {
|
||||||
fslock.unlock();
|
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
fileSeekPositions[fd] += nbyte;
|
fileSeekPositions[fd] += nbyte;
|
||||||
cacheFile.close();
|
cacheFile.close();
|
||||||
fslock.unlock();
|
|
||||||
return nbyte;
|
return nbyte;
|
||||||
};
|
};
|
||||||
|
|
||||||
int TeensyFileManager::read(int8_t fd, void *buf, int nbyte)
|
int TeensyFileManager::read(int8_t fd, void *buf, int nbyte)
|
||||||
{
|
{
|
||||||
|
Threads::Scope locker(fslock);
|
||||||
// open, seek, read, close.
|
// open, seek, read, close.
|
||||||
if (fd < 0 || fd >= numCached) {
|
if (fd < 0 || fd >= numCached) {
|
||||||
return -1;
|
return -1;
|
||||||
@ -254,24 +251,21 @@ int TeensyFileManager::read(int8_t fd, void *buf, int nbyte)
|
|||||||
_prepCache(fd);
|
_prepCache(fd);
|
||||||
|
|
||||||
uint32_t pos = fileSeekPositions[fd];
|
uint32_t pos = fileSeekPositions[fd];
|
||||||
fslock.lock();
|
|
||||||
if (!cacheFile.seekSet(pos)) {
|
if (!cacheFile.seekSet(pos)) {
|
||||||
fslock.unlock();
|
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
fileSeekPositions[fd] += nbyte;
|
fileSeekPositions[fd] += nbyte;
|
||||||
|
|
||||||
if (cacheFile.read(buf, nbyte) != nbyte) {
|
if (cacheFile.read(buf, nbyte) != nbyte) {
|
||||||
fslock.unlock();
|
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
fslock.unlock();
|
|
||||||
return nbyte;
|
return nbyte;
|
||||||
};
|
};
|
||||||
|
|
||||||
int TeensyFileManager::lseek(int8_t fd, int offset, int whence)
|
int TeensyFileManager::lseek(int8_t fd, int offset, int whence)
|
||||||
{
|
{
|
||||||
|
Threads::Scope locker(fslock);
|
||||||
if (whence == SEEK_CUR && offset == 0) {
|
if (whence == SEEK_CUR && offset == 0) {
|
||||||
return fileSeekPositions[fd];
|
return fileSeekPositions[fd];
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user