From e6bb34532a8ae839506cc5d012ae4a5a0460ed39 Mon Sep 17 00:00:00 2001 From: Augustin Cavalier Date: Tue, 8 Jan 2019 23:48:18 -0500 Subject: [PATCH] kernel/thread: Properly implement has_data for non-current threads. This was left unimplemented since the dawn of Haiku; but it's so rarely used nobody seems to have noticed. I've taken care to leave it optimized for the current thread case. Change-Id: Ib028a37963b2da6d0ca9b4dbd5a5f4a74ecf25b4 Reviewed-on: https://review.haiku-os.org/c/860 Reviewed-by: Adrien Destugues --- src/system/kernel/thread.cpp | 34 +++++++++++++++++++++++++++------- 1 file changed, 27 insertions(+), 7 deletions(-) diff --git a/src/system/kernel/thread.cpp b/src/system/kernel/thread.cpp index 470f4d71e4..01a14442a3 100644 --- a/src/system/kernel/thread.cpp +++ b/src/system/kernel/thread.cpp @@ -3041,20 +3041,40 @@ receive_data(thread_id *sender, void *buffer, size_t bufferSize) } -bool -has_data(thread_id thread) +static bool +thread_has_data(thread_id id, bool kernel) { - // TODO: The thread argument is ignored. - int32 count; + Thread* currentThread = thread_get_current_thread(); + Thread* thread; + BReference threadReference; + if (id == currentThread->id) { + thread = currentThread; + } else { + thread = Thread::Get(id); + if (thread == NULL) + return false; - if (get_sem_count(thread_get_current_thread()->msg.read_sem, - &count) != B_OK) + threadReference.SetTo(thread, true); + } + + if (!kernel && thread->team != currentThread->team) + return false; + + int32 count; + if (get_sem_count(thread->msg.read_sem, &count) != B_OK) return false; return count == 0 ? false : true; } +bool +has_data(thread_id thread) +{ + return thread_has_data(thread, true); +} + + status_t _get_thread_info(thread_id id, thread_info *info, size_t size) { @@ -3581,7 +3601,7 @@ _user_wait_for_thread(thread_id id, status_t *userReturnCode) bool _user_has_data(thread_id thread) { - return has_data(thread); + return thread_has_data(thread, false); }