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
3 // found in the LICENSE file.
5 #ifndef MOJO_SYSTEM_WAITER_TEST_UTILS_H_
6 #define MOJO_SYSTEM_WAITER_TEST_UTILS_H_
8 #include "base/basictypes.h"
9 #include "base/compiler_specific.h"
10 #include "base/memory/ref_counted.h"
11 #include "base/threading/simple_thread.h"
12 #include "mojo/public/c/system/types.h"
13 #include "mojo/system/dispatcher.h"
14 #include "mojo/system/waiter.h"
20 // This is a very simple thread that has a |Waiter|, on which it waits
21 // indefinitely (and records the result). It will create and initialize the
22 // |Waiter| on creation, but the caller must start the thread with |Start()|. It
23 // will join the thread on destruction.
25 // One usually uses it like:
29 // WaiterList waiter_list;
30 // test::SimpleWaiterThread thread(&result);
31 // waiter_list.AddWaiter(thread.waiter(), ...);
33 // ... some stuff to wake the waiter ...
34 // waiter_list.RemoveWaiter(thread.waiter());
35 // } // Join |thread|.
36 // EXPECT_EQ(..., result);
38 // There's a bit of unrealism in its use: In this sort of usage, calls such as
39 // |Waiter::Init()|, |AddWaiter()|, and |RemoveWaiter()| are done in the main
40 // (test) thread, not the waiter thread (as would actually happen in real code).
41 // (We accept this unrealism for simplicity, since |WaiterList| is
42 // thread-unsafe so making it more realistic would require adding nontrivial
43 // synchronization machinery.)
44 class SimpleWaiterThread
: public base::SimpleThread
{
46 // For the duration of the lifetime of this object, |*result| belongs to it
47 // (in the sense that it will write to it whenever it wants).
48 explicit SimpleWaiterThread(MojoResult
* result
);
49 virtual ~SimpleWaiterThread(); // Joins the thread.
51 Waiter
* waiter() { return &waiter_
; }
54 virtual void Run() OVERRIDE
;
56 MojoResult
* const result_
;
59 DISALLOW_COPY_AND_ASSIGN(SimpleWaiterThread
);
62 // This is a more complex and realistic thread that has a |Waiter|, on which it
63 // waits for the given deadline (with the given flags). Unlike
64 // |SimpleWaiterThread|, it requires the machinery of |Dispatcher|.
65 class WaiterThread
: public base::SimpleThread
{
67 // Note: |*did_wait_out| and |*result| belong to this object while it's alive.
68 WaiterThread(scoped_refptr
<Dispatcher
> dispatcher
,
69 MojoWaitFlags wait_flags
,
70 MojoDeadline deadline
,
71 MojoResult success_result
,
73 MojoResult
* result_out
);
74 virtual ~WaiterThread();
77 virtual void Run() OVERRIDE
;
79 const scoped_refptr
<Dispatcher
> dispatcher_
;
80 const MojoWaitFlags wait_flags_
;
81 const MojoDeadline deadline_
;
82 const MojoResult success_result_
;
83 bool* const did_wait_out_
;
84 MojoResult
* const result_out_
;
88 DISALLOW_COPY_AND_ASSIGN(WaiterThread
);
95 #endif // MOJO_SYSTEM_WAITER_TEST_UTILS_H_