930d92cb268a31862517a06ca388433944bda32a
[iec.git] / src / type3_AndroidCloud / anbox-master / src / anbox / common / message_channel.h
1 // Copyright 2014 The Android Open Source Project
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 #ifndef ANBOX_COMMON_MESSAGE_CHANNEL_H
16 #define ANBOX_COMMON_MESSAGE_CHANNEL_H
17
18 #include <stddef.h>
19
20 #include <condition_variable>
21 #include <mutex>
22
23 namespace anbox {
24 namespace common {
25
26 // Base non-templated class used to reduce the amount of template
27 // specialization.
28 class MessageChannelBase {
29  public:
30   // Constructor. |capacity| is the buffer capacity in messages.
31   MessageChannelBase(size_t capacity);
32
33   // Destructor.
34   ~MessageChannelBase();
35
36  protected:
37   // Call this method in the sender thread before writing a new message.
38   // This returns the position of the available slot in the message array
39   // where to copy the new fixed-size message. After the copy, call
40   // afterWrite().
41   size_t before_write();
42
43   // To be called after beforeWrite() and copying a new fixed-size message
44   // into the array. This signal the receiver thread that there is a new
45   // incoming message.
46   void after_write();
47
48   // Call this method in the receiver thread before reading a new message.
49   // This returns the position in the message array where the new message
50   // can be read. Caller must process the message, then call afterRead().
51   size_t before_read();
52
53   // To be called in the receiver thread after beforeRead() and processing
54   // the corresponding message.
55   void after_read();
56
57  private:
58   size_t pos_;
59   size_t count_;
60   size_t capacity_;
61   std::mutex lock_;
62   std::condition_variable can_read_;
63   std::condition_variable can_write_;
64 };
65
66 // Helper class used to implement an uni-directional IPC channel between
67 // two threads. The channel can be used to send fixed-size messages of type
68 // |T|, with an internal buffer size of |CAPACITY| items. All calls are
69 // blocking.
70 //
71 // Usage is pretty straightforward:
72 //
73 //   - From the sender thread, call send(msg);
74 //   - From the receiver thread, call receive(&msg);
75 //
76 template <typename T, size_t CAPACITY>
77 class MessageChannel : public MessageChannelBase {
78  public:
79   MessageChannel() : MessageChannelBase(CAPACITY) {}
80
81   void send(const T& msg) {
82     size_t pos = before_write();
83     mItems[pos] = msg;
84     after_write();
85   }
86
87   void receive(T* msg) {
88     size_t pos = before_read();
89     *msg = mItems[pos];
90     after_read();
91   }
92
93  private:
94   T mItems[CAPACITY];
95 };
96 }  // namespace common
97 }  // namespace anbox
98
99 #endif