1*6777b538SAndroid Build Coastguard Worker // Copyright 2013 The Chromium Authors 2*6777b538SAndroid Build Coastguard Worker // Use of this source code is governed by a BSD-style license that can be 3*6777b538SAndroid Build Coastguard Worker // found in the LICENSE file. 4*6777b538SAndroid Build Coastguard Worker 5*6777b538SAndroid Build Coastguard Worker #ifndef BASE_TEST_TEST_IO_THREAD_H_ 6*6777b538SAndroid Build Coastguard Worker #define BASE_TEST_TEST_IO_THREAD_H_ 7*6777b538SAndroid Build Coastguard Worker 8*6777b538SAndroid Build Coastguard Worker #include "base/compiler_specific.h" 9*6777b538SAndroid Build Coastguard Worker #include "base/functional/callback_forward.h" 10*6777b538SAndroid Build Coastguard Worker #include "base/memory/ref_counted.h" 11*6777b538SAndroid Build Coastguard Worker #include "base/task/task_runner.h" 12*6777b538SAndroid Build Coastguard Worker #include "base/threading/thread.h" 13*6777b538SAndroid Build Coastguard Worker 14*6777b538SAndroid Build Coastguard Worker namespace base { 15*6777b538SAndroid Build Coastguard Worker 16*6777b538SAndroid Build Coastguard Worker // Create and run an IO thread with a MessageLoop, and 17*6777b538SAndroid Build Coastguard Worker // making the MessageLoop accessible from its client. 18*6777b538SAndroid Build Coastguard Worker // It also provides some ideomatic API like PostTaskAndWait(). 19*6777b538SAndroid Build Coastguard Worker // 20*6777b538SAndroid Build Coastguard Worker // This API is not thread-safe: 21*6777b538SAndroid Build Coastguard Worker // - Start()/Stop() should only be called from the main (creation) thread. 22*6777b538SAndroid Build Coastguard Worker // - PostTask()/message_loop()/task_runner() are also safe to call from the 23*6777b538SAndroid Build Coastguard Worker // underlying thread itself (to post tasks from other threads: get the 24*6777b538SAndroid Build Coastguard Worker // task_runner() from the main thread first, it is then safe to pass _it_ 25*6777b538SAndroid Build Coastguard Worker // around). 26*6777b538SAndroid Build Coastguard Worker class TestIOThread { 27*6777b538SAndroid Build Coastguard Worker public: 28*6777b538SAndroid Build Coastguard Worker enum Mode { kAutoStart, kManualStart }; 29*6777b538SAndroid Build Coastguard Worker explicit TestIOThread(Mode mode); 30*6777b538SAndroid Build Coastguard Worker 31*6777b538SAndroid Build Coastguard Worker TestIOThread(const TestIOThread&) = delete; 32*6777b538SAndroid Build Coastguard Worker TestIOThread& operator=(const TestIOThread&) = delete; 33*6777b538SAndroid Build Coastguard Worker 34*6777b538SAndroid Build Coastguard Worker // Stops the I/O thread if necessary. 35*6777b538SAndroid Build Coastguard Worker ~TestIOThread(); 36*6777b538SAndroid Build Coastguard Worker 37*6777b538SAndroid Build Coastguard Worker // After Stop(), Start() may be called again to start a new I/O thread. 38*6777b538SAndroid Build Coastguard Worker // Stop() may be called even when the I/O thread is not started. 39*6777b538SAndroid Build Coastguard Worker void Start(); 40*6777b538SAndroid Build Coastguard Worker void Stop(); 41*6777b538SAndroid Build Coastguard Worker 42*6777b538SAndroid Build Coastguard Worker // Post |task| to the IO thread. 43*6777b538SAndroid Build Coastguard Worker void PostTask(const Location& from_here, base::OnceClosure task); 44*6777b538SAndroid Build Coastguard Worker task_runner()45*6777b538SAndroid Build Coastguard Worker scoped_refptr<SingleThreadTaskRunner> task_runner() { 46*6777b538SAndroid Build Coastguard Worker return io_thread_.task_runner(); 47*6777b538SAndroid Build Coastguard Worker } 48*6777b538SAndroid Build Coastguard Worker 49*6777b538SAndroid Build Coastguard Worker private: 50*6777b538SAndroid Build Coastguard Worker base::Thread io_thread_; 51*6777b538SAndroid Build Coastguard Worker bool io_thread_started_; 52*6777b538SAndroid Build Coastguard Worker }; 53*6777b538SAndroid Build Coastguard Worker 54*6777b538SAndroid Build Coastguard Worker } // namespace base 55*6777b538SAndroid Build Coastguard Worker 56*6777b538SAndroid Build Coastguard Worker #endif // BASE_TEST_TEST_IO_THREAD_H_ 57