Serenity Operating System
at master 57 lines 1.5 kB view raw
1/* 2 * Copyright (c) 2021, Spencer Dixon <spencercdixon@gmail.com> 3 * 4 * SPDX-License-Identifier: BSD-2-Clause 5 */ 6 7#include <LibTest/TestCase.h> 8#include <LibThreading/Thread.h> 9#include <unistd.h> 10 11TEST_CASE(threads_can_detach) 12{ 13 int should_be_42 = 0; 14 15 auto thread = Threading::Thread::construct([&should_be_42]() { 16 usleep(10 * 1000); 17 should_be_42 = 42; 18 return 0; 19 }); 20 thread->start(); 21 thread->detach(); 22 usleep(20 * 1000); 23 24 EXPECT(should_be_42 == 42); 25} 26 27TEST_CASE(joining_detached_thread_errors) 28{ 29 Atomic<bool> should_exit { false }; 30 auto thread = Threading::Thread::construct([&]() { 31 while (!should_exit.load()) 32 usleep(10 * 1000); 33 return 0; 34 }); 35 thread->start(); 36 thread->detach(); 37 38 // Because of how the crash test forks and removes the thread, we can't use that to verify that join() crashes. Instead, we check the join crash condition ourselves. 39 EXPECT(!thread->needs_to_be_joined()); 40 41 // FIXME: Dropping a running thread crashes because of the Function destructor. For now, force the detached thread to exit. 42 should_exit.store(true); 43 usleep(20 * 1000); 44} 45 46TEST_CASE(join_dead_thread) 47{ 48 auto thread = Threading::Thread::construct([&]() { return 0 /*nullptr*/; }); 49 thread->start(); 50 // The thread should have exited by then. 51 usleep(40 * 1000); 52 53 auto join_result = thread->join<int*>(); 54 55 EXPECT(!join_result.is_error()); 56 EXPECT_EQ(join_result.value(), static_cast<int*>(0)); 57}