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