1 // Copyright 2012 The Chromium Authors 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 // WARNING: You should *NOT* be using this class directly. PlatformThread is 6 // the low-level platform-specific abstraction to the OS's threading interface. 7 // You should instead be using a message-loop driven Thread, see thread.h. 8 9 #ifndef BASE_ALLOCATOR_PARTITION_ALLOCATOR_PARTITION_ALLOC_BASE_THREADING_PLATFORM_THREAD_FOR_TESTING_H_ 10 #define BASE_ALLOCATOR_PARTITION_ALLOCATOR_PARTITION_ALLOC_BASE_THREADING_PLATFORM_THREAD_FOR_TESTING_H_ 11 12 #include <stddef.h> 13 14 #include <iosfwd> 15 16 #include "base/allocator/partition_allocator/partition_alloc_base/threading/platform_thread.h" 17 #include "build/build_config.h" 18 19 namespace partition_alloc::internal::base { 20 21 // A namespace for low-level thread functions. 22 class PlatformThreadForTesting : public PlatformThread { 23 public: 24 // Implement this interface to run code on a background thread. Your 25 // ThreadMain method will be called on the newly created thread. 26 class Delegate { 27 public: 28 virtual void ThreadMain() = 0; 29 30 protected: 31 virtual ~Delegate() = default; 32 }; 33 34 PlatformThreadForTesting() = delete; 35 PlatformThreadForTesting(const PlatformThreadForTesting&) = delete; 36 PlatformThreadForTesting& operator=(const PlatformThreadForTesting&) = delete; 37 38 // Yield the current thread so another thread can be scheduled. 39 // 40 // Note: this is likely not the right call to make in most situations. If this 41 // is part of a spin loop, consider base::Lock, which likely has better tail 42 // latency. Yielding the thread has different effects depending on the 43 // platform, system load, etc., and can result in yielding the CPU for less 44 // than 1us, or many tens of ms. 45 static void YieldCurrentThread(); 46 47 // Creates a new thread. The `stack_size` parameter can be 0 to indicate 48 // that the default stack size should be used. Upon success, 49 // `*thread_handle` will be assigned a handle to the newly created thread, 50 // and `delegate`'s ThreadMain method will be executed on the newly created 51 // thread. 52 // NOTE: When you are done with the thread handle, you must call Join to 53 // release system resources associated with the thread. You must ensure that 54 // the Delegate object outlives the thread. 55 static bool Create(size_t stack_size, 56 Delegate* delegate, 57 PlatformThreadHandle* thread_handle); 58 59 // Joins with a thread created via the Create function. This function blocks 60 // the caller until the designated thread exits. This will invalidate 61 // `thread_handle`. 62 static void Join(PlatformThreadHandle thread_handle); 63 64 #if BUILDFLAG(IS_POSIX) || BUILDFLAG(IS_FUCHSIA) 65 // Returns the default thread stack size set by chrome. If we do not 66 // explicitly set default size then returns 0. 67 static size_t GetDefaultThreadStackSize(); 68 #endif 69 }; 70 71 } // namespace partition_alloc::internal::base 72 73 #endif // BASE_ALLOCATOR_PARTITION_ALLOCATOR_PARTITION_ALLOC_BASE_THREADING_PLATFORM_THREADD_FOR_TESTING_H_ 74