1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-26 10:07:44 +00:00

LibC: Implement `flockfile and funlockfile`

To do this, we must set the type attribute when initializing a FILE to
``__PTHREAD_MUTEX_RECURSIVE``.
This commit is contained in:
Conor Byrne 2022-01-01 00:18:21 +00:00 committed by Brian Gianforcaro
parent aa0db4e4b0
commit 8515d7d5ab
2 changed files with 11 additions and 10 deletions

View file

@ -19,7 +19,8 @@ public:
: m_fd(fd) : m_fd(fd)
, m_mode(mode) , m_mode(mode)
{ {
__pthread_mutex_init(&m_mutex, nullptr); pthread_mutexattr_t attr = { __PTHREAD_MUTEX_RECURSIVE };
__pthread_mutex_init(&m_mutex, &attr);
} }
~FILE(); ~FILE();
@ -31,6 +32,9 @@ public:
void purge(); void purge();
bool close(); bool close();
void lock();
void unlock();
int fileno() const { return m_fd; } int fileno() const { return m_fd; }
bool eof() const { return m_eof; } bool eof() const { return m_eof; }
int mode() const { return m_mode; } int mode() const { return m_mode; }
@ -115,9 +119,6 @@ private:
// Flush *some* data from the buffer. // Flush *some* data from the buffer.
bool write_from_buffer(); bool write_from_buffer();
void lock();
void unlock();
int m_fd { -1 }; int m_fd { -1 };
int m_mode { 0 }; int m_mode { 0 };
u8 m_flags { Flags::None }; u8 m_flags { Flags::None };
@ -126,8 +127,6 @@ private:
pid_t m_popen_child { -1 }; pid_t m_popen_child { -1 };
Buffer m_buffer; Buffer m_buffer;
__pthread_mutex_t m_mutex; __pthread_mutex_t m_mutex;
friend class ScopedFileLock;
}; };
class ScopedFileLock { class ScopedFileLock {

View file

@ -1228,15 +1228,17 @@ int vscanf(const char* fmt, va_list ap)
} }
// https://pubs.opengroup.org/onlinepubs/9699919799/functions/flockfile.html // https://pubs.opengroup.org/onlinepubs/9699919799/functions/flockfile.html
void flockfile([[maybe_unused]] FILE* filehandle) void flockfile(FILE* filehandle)
{ {
dbgln("FIXME: Implement flockfile()"); VERIFY(filehandle);
filehandle->lock();
} }
// https://pubs.opengroup.org/onlinepubs/9699919799/functions/funlockfile.html // https://pubs.opengroup.org/onlinepubs/9699919799/functions/funlockfile.html
void funlockfile([[maybe_unused]] FILE* filehandle) void funlockfile(FILE* filehandle)
{ {
dbgln("FIXME: Implement funlockfile()"); VERIFY(filehandle);
filehandle->unlock();
} }
// https://pubs.opengroup.org/onlinepubs/9699919799/functions/tmpfile.html // https://pubs.opengroup.org/onlinepubs/9699919799/functions/tmpfile.html