| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef MOJO_EDK_SYSTEM_WAITER_TEST_UTILS_H_ | 5 #ifndef MOJO_EDK_SYSTEM_WAITER_TEST_UTILS_H_ |
| 6 #define MOJO_EDK_SYSTEM_WAITER_TEST_UTILS_H_ | 6 #define MOJO_EDK_SYSTEM_WAITER_TEST_UTILS_H_ |
| 7 | 7 |
| 8 #include <stdint.h> | 8 #include <stdint.h> |
| 9 | 9 |
| 10 #include "base/memory/ref_counted.h" | 10 #include "base/memory/ref_counted.h" |
| 11 #include "base/threading/simple_thread.h" | |
| 12 #include "mojo/edk/system/dispatcher.h" | 11 #include "mojo/edk/system/dispatcher.h" |
| 13 #include "mojo/edk/system/handle_signals_state.h" | 12 #include "mojo/edk/system/handle_signals_state.h" |
| 14 #include "mojo/edk/system/waiter.h" | 13 #include "mojo/edk/system/waiter.h" |
| 14 #include "mojo/edk/test/simple_test_thread.h" |
| 15 #include "mojo/public/c/system/types.h" | 15 #include "mojo/public/c/system/types.h" |
| 16 #include "mojo/public/cpp/system/macros.h" | 16 #include "mojo/public/cpp/system/macros.h" |
| 17 | 17 |
| 18 namespace mojo { | 18 namespace mojo { |
| 19 namespace system { | 19 namespace system { |
| 20 namespace test { | 20 namespace test { |
| 21 | 21 |
| 22 // This is a very simple thread that has a |Waiter|, on which it waits | 22 // This is a very simple thread that has a |Waiter|, on which it waits |
| 23 // indefinitely (and records the result). It will create and initialize the | 23 // indefinitely (and records the result). It will create and initialize the |
| 24 // |Waiter| on creation, but the caller must start the thread with |Start()|. It | 24 // |Waiter| on creation, but the caller must start the thread with |Start()|. It |
| (...skipping 11 matching lines...) Expand all Loading... |
| 36 // awakable_list.Remove(thread.waiter()); | 36 // awakable_list.Remove(thread.waiter()); |
| 37 // } // Join |thread|. | 37 // } // Join |thread|. |
| 38 // EXPECT_EQ(..., result); | 38 // EXPECT_EQ(..., result); |
| 39 // | 39 // |
| 40 // There's a bit of unrealism in its use: In this sort of usage, calls such as | 40 // There's a bit of unrealism in its use: In this sort of usage, calls such as |
| 41 // |Waiter::Init()|, |AddAwakable()|, and |RemoveAwakable()| are done in the | 41 // |Waiter::Init()|, |AddAwakable()|, and |RemoveAwakable()| are done in the |
| 42 // main (test) thread, not the waiter thread (as would actually happen in real | 42 // main (test) thread, not the waiter thread (as would actually happen in real |
| 43 // code). (We accept this unrealism for simplicity, since |AwakableList| is | 43 // code). (We accept this unrealism for simplicity, since |AwakableList| is |
| 44 // thread-unsafe so making it more realistic would require adding nontrivial | 44 // thread-unsafe so making it more realistic would require adding nontrivial |
| 45 // synchronization machinery.) | 45 // synchronization machinery.) |
| 46 class SimpleWaiterThread : public base::SimpleThread { | 46 class SimpleWaiterThread : public mojo::test::SimpleTestThread { |
| 47 public: | 47 public: |
| 48 // For the duration of the lifetime of this object, |*result| belongs to it | 48 // For the duration of the lifetime of this object, |*result| belongs to it |
| 49 // (in the sense that it will write to it whenever it wants). | 49 // (in the sense that it will write to it whenever it wants). |
| 50 SimpleWaiterThread(MojoResult* result, uint32_t* context); | 50 SimpleWaiterThread(MojoResult* result, uint32_t* context); |
| 51 ~SimpleWaiterThread() override; // Joins the thread. | 51 ~SimpleWaiterThread() override; // Joins the thread. |
| 52 | 52 |
| 53 Waiter* waiter() { return &waiter_; } | 53 Waiter* waiter() { return &waiter_; } |
| 54 | 54 |
| 55 private: | 55 private: |
| 56 void Run() override; | 56 void Run() override; |
| 57 | 57 |
| 58 MojoResult* const result_; | 58 MojoResult* const result_; |
| 59 uint32_t* const context_; | 59 uint32_t* const context_; |
| 60 Waiter waiter_; | 60 Waiter waiter_; |
| 61 | 61 |
| 62 MOJO_DISALLOW_COPY_AND_ASSIGN(SimpleWaiterThread); | 62 MOJO_DISALLOW_COPY_AND_ASSIGN(SimpleWaiterThread); |
| 63 }; | 63 }; |
| 64 | 64 |
| 65 // This is a more complex and realistic thread that has a |Waiter|, on which it | 65 // This is a more complex and realistic thread that has a |Waiter|, on which it |
| 66 // waits for the given deadline (with the given flags). Unlike | 66 // waits for the given deadline (with the given flags). Unlike |
| 67 // |SimpleWaiterThread|, it requires the machinery of |Dispatcher|. | 67 // |SimpleWaiterThread|, it requires the machinery of |Dispatcher|. |
| 68 class WaiterThread : public base::SimpleThread { | 68 class WaiterThread : public mojo::test::SimpleTestThread { |
| 69 public: | 69 public: |
| 70 // Note: |*did_wait_out|, |*result_out|, |*context_out| and | 70 // Note: |*did_wait_out|, |*result_out|, |*context_out| and |
| 71 // |*signals_state_out| "belong" to this object (i.e., may be modified by, on | 71 // |*signals_state_out| "belong" to this object (i.e., may be modified by, on |
| 72 // some other thread) while it's alive. | 72 // some other thread) while it's alive. |
| 73 WaiterThread(scoped_refptr<Dispatcher> dispatcher, | 73 WaiterThread(scoped_refptr<Dispatcher> dispatcher, |
| 74 MojoHandleSignals handle_signals, | 74 MojoHandleSignals handle_signals, |
| 75 MojoDeadline deadline, | 75 MojoDeadline deadline, |
| 76 uint32_t context, | 76 uint32_t context, |
| 77 bool* did_wait_out, | 77 bool* did_wait_out, |
| 78 MojoResult* result_out, | 78 MojoResult* result_out, |
| (...skipping 16 matching lines...) Expand all Loading... |
| 95 Waiter waiter_; | 95 Waiter waiter_; |
| 96 | 96 |
| 97 MOJO_DISALLOW_COPY_AND_ASSIGN(WaiterThread); | 97 MOJO_DISALLOW_COPY_AND_ASSIGN(WaiterThread); |
| 98 }; | 98 }; |
| 99 | 99 |
| 100 } // namespace test | 100 } // namespace test |
| 101 } // namespace system | 101 } // namespace system |
| 102 } // namespace mojo | 102 } // namespace mojo |
| 103 | 103 |
| 104 #endif // MOJO_EDK_SYSTEM_WAITER_TEST_UTILS_H_ | 104 #endif // MOJO_EDK_SYSTEM_WAITER_TEST_UTILS_H_ |
| OLD | NEW |