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