vfs experiments 1 slightly better

This commit is contained in:
2024-02-24 14:12:31 +01:00
parent 548fd6511d
commit 96aff20678
5 changed files with 49 additions and 4 deletions

View File

@@ -23,6 +23,30 @@ public:
private:
T *lock;
};
template<typename T>
class LockGuardTry {
public:
LockGuardTry(T &lock) : _lock(&lock) {
assert2(are_interrupts_enabled(), "Trying to lock with disabled interrupts!");
suc = _lock->try_lock();
}
~LockGuardTry() {
if (suc)
_lock->unlock();
}
LockGuardTry(LockGuardTry const &d) = delete;
bool locked() { return suc; }
void lock() {
_lock->lock();
suc = true;
}
private:
T *_lock;
bool suc;
};
#endif//OS2_LOCKGUARD_H

View File

@@ -25,12 +25,21 @@ NodeFile *File::file() {
}
uint64_t File::seek(uint64_t pos) {
_pos = pos;
return pos;
}
uint64_t File::read(char *buf, uint64_t size) {
if (file()) return file()->read(buf, _pos, size);
if (file()) {
file()->read(buf, _pos, size);
_pos += size;
return size;
}
}
uint64_t File::write(const char *buf, uint64_t size) {
if (file()) return file()->write(buf, _pos, size);
if (file()) {
file()->write(buf, _pos, size);
_pos += size;
return size;
}
}
uint64_t File::size() {
if (file()) return file()->size();

View File

@@ -5,7 +5,7 @@
#include "MemFs.hpp"
#include "LockGuard.hpp"
Vector<Node *> MemFs::MemFsNodeDir::children() {
// assert(_lock.owner() == cur_task());
assert(_lock.owner() == cur_task());
Vector<Node *> out;
for (auto c: _children) {

View File

@@ -9,7 +9,11 @@
Node::~Node() = default;
Node *Node::traverse(const Path &path) {
// lock
LockGuardTry l(_lock);
// FIXME: This is bad
if (!l.locked() && _lock.owner() != cur_task())
l.lock();
NodeDir &nodeDir = static_cast<NodeDir &>(*this);
if (nodeDir._mount) return nodeDir._mount->root()->traverse(path);
if (path.empty()) return this;

View File

@@ -39,4 +39,12 @@ void VFSTester::test() {
cf->read(t.data(), cf->size());
assert(t == "hello wooooorld");
}
{
String t("aaaaaaaaaaaaaaaaaaaa");
File *cf = VFSApi::get(c);
cf->seek(0);
cf->read(t.data(), 9);
cf->read(t.data() + 9, cf->size() - 9);
assert(t == "hello wooooorld");
}
}