1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-25 20:47:45 +00:00

Generalize the SpinLock and move it to AK.

Add a separate lock to protect the VFS. I think this might be a good idea.
I'm not sure it's a good approach though. I'll fiddle with it as I go along.

It's really fun to figure out all these things on my own.
This commit is contained in:
Andreas Kling 2018-10-23 23:32:53 +02:00
parent e4bfcd2346
commit 018da1be11
11 changed files with 88 additions and 203 deletions

View file

@ -24,6 +24,8 @@ bool additionWouldOverflow(Unix::off_t a, Unix::off_t b)
int FileHandle::stat(Unix::stat* buffer)
{
Locker locker(VirtualFileSystem::lock());
if (!m_vnode)
return -EBADF;
@ -49,6 +51,8 @@ int FileHandle::stat(Unix::stat* buffer)
Unix::off_t FileHandle::seek(Unix::off_t offset, int whence)
{
Locker locker(VirtualFileSystem::lock());
if (!m_vnode)
return -EBADF;
@ -91,6 +95,8 @@ Unix::off_t FileHandle::seek(Unix::off_t offset, int whence)
Unix::ssize_t FileHandle::read(byte* buffer, Unix::size_t count)
{
Locker locker(VirtualFileSystem::lock());
if (m_vnode->isCharacterDevice()) {
// FIXME: What should happen to m_currentOffset?
return m_vnode->characterDevice()->read(buffer, count);
@ -102,6 +108,8 @@ Unix::ssize_t FileHandle::read(byte* buffer, Unix::size_t count)
ByteBuffer FileHandle::readEntireFile()
{
Locker locker(VirtualFileSystem::lock());
if (m_vnode->isCharacterDevice()) {
auto buffer = ByteBuffer::createUninitialized(1024);
Unix::ssize_t nread = m_vnode->characterDevice()->read(buffer.pointer(), buffer.size());

View file

@ -20,9 +20,18 @@ VirtualFileSystem& VirtualFileSystem::the()
return *s_the;
}
static SpinLock* s_vfsLock;
SpinLock& VirtualFileSystem::lock()
{
ASSERT(s_vfsLock);
return *s_vfsLock;
}
void VirtualFileSystem::initializeGlobals()
{
s_the = nullptr;
s_vfsLock = new SpinLock;
FileSystem::initializeGlobals();
}
@ -336,6 +345,8 @@ void VirtualFileSystem::listDirectoryRecursively(const String& path)
bool VirtualFileSystem::touch(const String& path)
{
Locker locker(VirtualFileSystem::lock());
auto inode = resolvePath(path);
if (!inode.isValid())
return false;
@ -344,6 +355,8 @@ bool VirtualFileSystem::touch(const String& path)
OwnPtr<FileHandle> VirtualFileSystem::open(const String& path)
{
Locker locker(VirtualFileSystem::lock());
auto inode = resolvePath(path);
if (!inode.isValid())
return nullptr;
@ -355,6 +368,8 @@ OwnPtr<FileHandle> VirtualFileSystem::open(const String& path)
OwnPtr<FileHandle> VirtualFileSystem::create(const String& path)
{
Locker locker(VirtualFileSystem::lock());
// FIXME: Do the real thing, not just this fake thing!
(void) path;
m_rootNode->fileSystem()->createInode(m_rootNode->fileSystem()->rootInode(), "empty", 0100644, 0);
@ -363,6 +378,8 @@ OwnPtr<FileHandle> VirtualFileSystem::create(const String& path)
OwnPtr<FileHandle> VirtualFileSystem::mkdir(const String& path)
{
Locker locker(VirtualFileSystem::lock());
// FIXME: Do the real thing, not just this fake thing!
(void) path;
m_rootNode->fileSystem()->makeDirectory(m_rootNode->fileSystem()->rootInode(), "mydir", 0400755);

View file

@ -5,6 +5,7 @@
#include <AK/RetainPtr.h>
#include <AK/String.h>
#include <AK/Vector.h>
#include <AK/Lock.h>
#include "InodeIdentifier.h"
#include "Limits.h"
@ -15,6 +16,7 @@ class FileSystem;
class VirtualFileSystem {
public:
static void initializeGlobals();
static SpinLock& lock();
struct Node {
InodeIdentifier inode;

View file

@ -21,6 +21,8 @@ int main(int c, char** v)
if (c >= 2)
filename = v[1];
VirtualFileSystem::initializeGlobals();
VirtualFileSystem vfs;
auto zero = make<ZeroDevice>();