summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBrian Gianforcaro <b.gianfo@gmail.com>2020-04-26 02:40:24 -0700
committerAndreas Kling <kling@serenityos.org>2020-04-26 21:31:52 +0200
commit25a620a5738470644983def20041ac79d8b85333 (patch)
treea0a5afe1bee7255bab3ab9936529dc0ead037122
parentfaf15e37213f3b023b2d029c06dcaab6871f8f13 (diff)
downloadserenity-25a620a5738470644983def20041ac79d8b85333.zip
Kernel: Enable timeout support for sys$futex(FUTEX_WAIT)
Utilize the new Thread::wait_on timeout parameter to implement timeout support for FUTEX_WAIT. As we compute the relative time from the user specified absolute time, we try to delay that computation as long as possible before we call into Thread::wait_on(..). To enable this a small bit of refactoring was done pull futex_queue fetching out and timeout fetch and calculation separation.
-rw-r--r--Kernel/Process.cpp43
1 files changed, 34 insertions, 9 deletions
diff --git a/Kernel/Process.cpp b/Kernel/Process.cpp
index 1ee32709f8..31dacfbfc5 100644
--- a/Kernel/Process.cpp
+++ b/Kernel/Process.cpp
@@ -2327,6 +2327,19 @@ timeval kgettimeofday()
return const_cast<const timeval&>(((KernelInfoPage*)s_info_page_address_for_kernel.as_ptr())->now);
}
+void compute_relative_timeout_from_absolute(const timeval& absolute_time, timeval& relative_time)
+{
+ // Convert absolute time to relative time of day.
+ timeval_sub(absolute_time, kgettimeofday(), relative_time);
+}
+
+void compute_relative_timeout_from_absolute(const timespec& absolute_time, timeval& relative_time)
+{
+ timeval tv_absolute_time;
+ timespec_to_timeval(absolute_time, tv_absolute_time);
+ compute_relative_timeout_from_absolute(tv_absolute_time, relative_time);
+}
+
void kgettimeofday(timeval& tv)
{
tv = kgettimeofday();
@@ -4660,21 +4673,33 @@ int Process::sys$futex(const Syscall::SC_futex_params* user_params)
if (user_timeout && !validate_read_typed(user_timeout))
return -EFAULT;
- timespec timeout { 0, 0 };
- if (user_timeout)
- copy_from_user(&timeout, user_timeout);
-
- i32 user_value;
-
switch (futex_op) {
- case FUTEX_WAIT:
+ case FUTEX_WAIT: {
+ i32 user_value;
copy_from_user(&user_value, userspace_address);
if (user_value != value)
return -EAGAIN;
+
+ timespec ts_abstimeout { 0, 0 };
+ if (user_timeout && !validate_read_and_copy_typed(&ts_abstimeout, user_timeout))
+ return -EFAULT;
+
+ WaitQueue& wait_queue = futex_queue(userspace_address);
+ timeval* optional_timeout = nullptr;
+ timeval relative_timeout { 0, 0 };
+ if (user_timeout) {
+ compute_relative_timeout_from_absolute(ts_abstimeout, relative_timeout);
+ optional_timeout = &relative_timeout;
+ }
+
// FIXME: This is supposed to be interruptible by a signal, but right now WaitQueue cannot be interrupted.
- // FIXME: Support timeout!
- Thread::current->wait_on(futex_queue(userspace_address));
+ Thread::BlockResult result = Thread::current->wait_on(wait_queue, optional_timeout);
+ if (result == Thread::BlockResult::InterruptedByTimeout) {
+ return -ETIMEDOUT;
+ }
+
break;
+ }
case FUTEX_WAKE:
if (value == 0)
return 0;