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_SYSTEM_MESSAGE_PIPE_H_ | 5 #ifndef MOJO_SYSTEM_MESSAGE_PIPE_H_ |
6 #define MOJO_SYSTEM_MESSAGE_PIPE_H_ | 6 #define MOJO_SYSTEM_MESSAGE_PIPE_H_ |
7 | 7 |
8 #include <deque> | |
9 | |
10 #include "base/basictypes.h" | 8 #include "base/basictypes.h" |
11 #include "base/memory/ref_counted.h" | 9 #include "base/memory/ref_counted.h" |
12 #include "base/memory/scoped_ptr.h" | 10 #include "base/memory/scoped_ptr.h" |
13 #include "base/synchronization/lock.h" | 11 #include "base/synchronization/lock.h" |
14 #include "mojo/public/system/core.h" | 12 #include "mojo/public/system/core.h" |
15 #include "mojo/public/system/system_export.h" | 13 #include "mojo/public/system/system_export.h" |
16 #include "mojo/system/waiter_list.h" | |
17 | 14 |
18 namespace mojo { | 15 namespace mojo { |
19 namespace system { | 16 namespace system { |
20 | 17 |
21 class MessageInTransit; | 18 class MessagePipeEndpoint; |
22 class Waiter; | 19 class Waiter; |
23 | 20 |
24 // |MessagePipe| is the secondary object implementing a message pipe (see the | 21 // |MessagePipe| is the secondary object implementing a message pipe (see the |
25 // explanatory comment in core_impl.cc), and is jointly owned by the two | 22 // explanatory comment in core_impl.cc). It is typically owned by the |
26 // dispatchers passed in to the constructor. This class is thread-safe. | 23 // dispatcher(s) corresponding to the local endpoints. This class is |
| 24 // thread-safe. |
27 class MOJO_SYSTEM_EXPORT MessagePipe : | 25 class MOJO_SYSTEM_EXPORT MessagePipe : |
28 public base::RefCountedThreadSafe<MessagePipe> { | 26 public base::RefCountedThreadSafe<MessagePipe> { |
29 public: | 27 public: |
| 28 MessagePipe(scoped_ptr<MessagePipeEndpoint> endpoint_0, |
| 29 scoped_ptr<MessagePipeEndpoint> endpoint_1); |
| 30 |
| 31 // Convenience constructor that constructs a |MessagePipe| with two new |
| 32 // |LocalMessagePipeEndpoint|s. |
30 MessagePipe(); | 33 MessagePipe(); |
31 | 34 |
32 // These are called by the dispatcher to implement its methods of | 35 // These are called by the dispatcher to implement its methods of |
33 // corresponding names. In all cases, the port |port| must be open. | 36 // corresponding names. In all cases, the port |port| must be open. |
34 void CancelAllWaiters(unsigned port); | 37 void CancelAllWaiters(unsigned port); |
35 void Close(unsigned port); | 38 void Close(unsigned port); |
36 // Unlike |MessagePipeDispatcher::WriteMessage()|, this does not validate its | 39 // Unlike |MessagePipeDispatcher::WriteMessage()|, this does not validate its |
37 // arguments. |bytes|/|num_bytes| and |handles|/|num_handles| must be valid. | 40 // arguments. |bytes|/|num_bytes| and |handles|/|num_handles| must be valid. |
38 MojoResult WriteMessage(unsigned port, | 41 MojoResult WriteMessage(unsigned port, |
39 const void* bytes, uint32_t num_bytes, | 42 const void* bytes, uint32_t num_bytes, |
40 const MojoHandle* handles, uint32_t num_handles, | 43 const MojoHandle* handles, uint32_t num_handles, |
41 MojoWriteMessageFlags flags); | 44 MojoWriteMessageFlags flags); |
42 // Unlike |MessagePipeDispatcher::ReadMessage()|, this does not validate its | 45 // Unlike |MessagePipeDispatcher::ReadMessage()|, this does not validate its |
43 // arguments. |bytes|/|num_bytes| and |handles|/|num_handles| must be valid. | 46 // arguments. |bytes|/|num_bytes| and |handles|/|num_handles| must be valid. |
44 MojoResult ReadMessage(unsigned port, | 47 MojoResult ReadMessage(unsigned port, |
45 void* bytes, uint32_t* num_bytes, | 48 void* bytes, uint32_t* num_bytes, |
46 MojoHandle* handles, uint32_t* num_handles, | 49 MojoHandle* handles, uint32_t* num_handles, |
47 MojoReadMessageFlags flags); | 50 MojoReadMessageFlags flags); |
48 MojoResult AddWaiter(unsigned port, | 51 MojoResult AddWaiter(unsigned port, |
49 Waiter* waiter, | 52 Waiter* waiter, |
50 MojoWaitFlags flags, | 53 MojoWaitFlags flags, |
51 MojoResult wake_result); | 54 MojoResult wake_result); |
52 void RemoveWaiter(unsigned port, Waiter* waiter); | 55 void RemoveWaiter(unsigned port, Waiter* waiter); |
53 | 56 |
54 private: | 57 private: |
55 friend class base::RefCountedThreadSafe<MessagePipe>; | 58 friend class base::RefCountedThreadSafe<MessagePipe>; |
56 virtual ~MessagePipe(); | 59 virtual ~MessagePipe(); |
57 | 60 |
58 MojoWaitFlags SatisfiedFlagsNoLock(unsigned port); | |
59 MojoWaitFlags SatisfiableFlagsNoLock(unsigned port); | |
60 | |
61 base::Lock lock_; // Protects the following members. | 61 base::Lock lock_; // Protects the following members. |
62 bool is_open_[2]; | 62 scoped_ptr<MessagePipeEndpoint> endpoints_[2]; |
63 // These are *incoming* queues for their corresponding ports. It owns its | |
64 // contents. | |
65 // TODO(vtl): When possible (with C++11), convert the plain pointers to | |
66 // scoped_ptr/unique_ptr. | |
67 std::deque<MessageInTransit*> message_queues_[2]; | |
68 WaiterList waiter_lists_[2]; | |
69 | 63 |
70 DISALLOW_COPY_AND_ASSIGN(MessagePipe); | 64 DISALLOW_COPY_AND_ASSIGN(MessagePipe); |
71 }; | 65 }; |
72 | 66 |
73 } // namespace system | 67 } // namespace system |
74 } // namespace mojo | 68 } // namespace mojo |
75 | 69 |
76 #endif // MOJO_SYSTEM_MESSAGE_PIPE_H_ | 70 #endif // MOJO_SYSTEM_MESSAGE_PIPE_H_ |
OLD | NEW |