summaryrefslogtreecommitdiff
path: root/Kernel/TTY
diff options
context:
space:
mode:
authorTom <tomut@yahoo.com>2020-09-29 16:26:13 -0600
committerAndreas Kling <kling@serenityos.org>2020-11-10 19:11:52 +0100
commit75f61fe3d9055b4e65deecc796401ea2f9e827b2 (patch)
treeed09858a8bf4071afe859d92178a9e2a77676d62 /Kernel/TTY
parent3c1ef744f6c55b40c3d89baaa942aac7e0c9110e (diff)
downloadserenity-75f61fe3d9055b4e65deecc796401ea2f9e827b2.zip
AK: Make RefPtr, NonnullRefPtr, WeakPtr thread safe
This makes most operations thread safe, especially so that they can safely be used in the Kernel. This includes obtaining a strong reference from a weak reference, which now requires an explicit call to WeakPtr::strong_ref(). Another major change is that Weakable::make_weak_ref() may require the explicit target type. Previously we used reinterpret_cast in WeakPtr, assuming that it can be properly converted. But WeakPtr does not necessarily have the knowledge to be able to do this. Instead, we now ask the class itself to deliver a WeakPtr to the type that we want. Also, WeakLink is no longer specific to a target type. The reason for this is that we want to be able to safely convert e.g. WeakPtr<T> to WeakPtr<U>, and before this we just reinterpret_cast the internal WeakLink<T> to WeakLink<U>, which is a bold assumption that it would actually produce the correct code. Instead, WeakLink now operates on just a raw pointer and we only make those constructors/operators available if we can verify that it can be safely cast. In order to guarantee thread safety, we now use the least significant bit in the pointer for locking purposes. This also means that only properly aligned pointers can be used.
Diffstat (limited to 'Kernel/TTY')
-rw-r--r--Kernel/TTY/TTY.cpp8
-rw-r--r--Kernel/TTY/TTY.h7
2 files changed, 10 insertions, 5 deletions
diff --git a/Kernel/TTY/TTY.cpp b/Kernel/TTY/TTY.cpp
index 7e9d8aefcf..69617991c9 100644
--- a/Kernel/TTY/TTY.cpp
+++ b/Kernel/TTY/TTY.cpp
@@ -166,8 +166,8 @@ void TTY::emit(u8 ch)
if (ch == m_termios.c_cc[VSUSP]) {
dbg() << tty_name() << ": VSUSP pressed!";
generate_signal(SIGTSTP);
- if (m_original_process_parent)
- (void)m_original_process_parent->send_signal(SIGCHLD, nullptr);
+ if (auto original_process_parent = m_original_process_parent.strong_ref())
+ (void)original_process_parent->send_signal(SIGCHLD, nullptr);
// TODO: Else send it to the session leader maybe?
return;
}
@@ -330,11 +330,11 @@ int TTY::ioctl(FileDescription&, unsigned request, FlatPtr arg)
return -EPERM;
if (process && pgid != process->pgid())
return -EPERM;
- m_pg = process_group->make_weak_ptr();
+ m_pg = *process_group;
if (process) {
if (auto parent = Process::from_pid(process->ppid())) {
- m_original_process_parent = parent->make_weak_ptr();
+ m_original_process_parent = *parent;
return 0;
}
}
diff --git a/Kernel/TTY/TTY.h b/Kernel/TTY/TTY.h
index 6b9d571336..aafbe17292 100644
--- a/Kernel/TTY/TTY.h
+++ b/Kernel/TTY/TTY.h
@@ -51,7 +51,12 @@ public:
unsigned short rows() const { return m_rows; }
unsigned short columns() const { return m_columns; }
- ProcessGroupID pgid() const { return m_pg ? m_pg->pgid() : 0; }
+ ProcessGroupID pgid() const
+ {
+ if (auto pg = m_pg.strong_ref())
+ return pg->pgid();
+ return 0;
+ }
void set_termios(const termios&);
bool should_generate_signals() const { return m_termios.c_lflag & ISIG; }