diff options
author | Daniel Bertalan <dani@danielbertalan.dev> | 2022-01-15 14:40:57 +0100 |
---|---|---|
committer | Brian Gianforcaro <b.gianfo@gmail.com> | 2022-01-16 14:59:21 -0800 |
commit | 6a6dbf5b0b23ce59ae4462f9aef93434f94f0226 (patch) | |
tree | d49d8ab2085b073d25e9fd796cda83790a5277e6 /Userland/Libraries/LibC/bits | |
parent | b4e864d02dfe84d5a2668f830b912c1b62f67c43 (diff) | |
download | serenity-6a6dbf5b0b23ce59ae4462f9aef93434f94f0226.zip |
LibC: Implement `fflush(nullptr)`
This caused all open file streams to be flushed.
This commit also changes `FILE::create` to handle buffer allocation
failure gracefully.
Diffstat (limited to 'Userland/Libraries/LibC/bits')
-rw-r--r-- | Userland/Libraries/LibC/bits/mutex_locker.h | 35 | ||||
-rw-r--r-- | Userland/Libraries/LibC/bits/stdio_file_implementation.h | 5 |
2 files changed, 40 insertions, 0 deletions
diff --git a/Userland/Libraries/LibC/bits/mutex_locker.h b/Userland/Libraries/LibC/bits/mutex_locker.h new file mode 100644 index 0000000000..e3771c3818 --- /dev/null +++ b/Userland/Libraries/LibC/bits/mutex_locker.h @@ -0,0 +1,35 @@ +/* + * Copyright (c) 2021, Daniel Bertalan <dani@danielbertalan.dev> + * + * SPDX-License-Identifier: BSD-2-Clause + */ + +#pragma once + +#include <pthread.h> + +// We don't want to bring LibThreading headers into LibC, so we use plain +// pthread mutexes and this RAII guard. +namespace LibC { + +class [[nodiscard]] MutexLocker { +public: + explicit MutexLocker(pthread_mutex_t& mutex) + : m_mutex(mutex) + { + lock(); + } + + ~MutexLocker() + { + unlock(); + } + + void lock() { pthread_mutex_lock(&m_mutex); } + void unlock() { pthread_mutex_unlock(&m_mutex); } + +private: + pthread_mutex_t& m_mutex; +}; + +} diff --git a/Userland/Libraries/LibC/bits/stdio_file_implementation.h b/Userland/Libraries/LibC/bits/stdio_file_implementation.h index 48029992ad..98e4820dcf 100644 --- a/Userland/Libraries/LibC/bits/stdio_file_implementation.h +++ b/Userland/Libraries/LibC/bits/stdio_file_implementation.h @@ -5,6 +5,7 @@ */ #include <AK/Array.h> +#include <AK/IntrusiveList.h> #include <AK/Types.h> #include <LibC/bits/FILE.h> #include <LibC/bits/pthread_integration.h> @@ -127,6 +128,10 @@ private: pid_t m_popen_child { -1 }; Buffer m_buffer; __pthread_mutex_t m_mutex; + IntrusiveListNode<FILE> m_list_node; + +public: + using List = IntrusiveList<&FILE::m_list_node>; }; class ScopedFileLock { |