3f463a9c0ff0a0b86db99681e680028b439d8b61
[iec.git] / src / type3_AndroidCloud / anbox-master / external / android-emugl / shared / emugl / 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 EMUGL_COMMON_MESSAGE_CHANNEL_H
16 #define EMUGL_COMMON_MESSAGE_CHANNEL_H
17
18 #include "emugl/common/condition_variable.h"
19 #include "emugl/common/mutex.h"
20
21 #include <stddef.h>
22
23 namespace emugl {
24
25 // Base non-templated class used to reduce the amount of template
26 // specialization.
27 class MessageChannelBase {
28 public:
29     // Constructor. |capacity| is the buffer capacity in messages.
30     MessageChannelBase(size_t capacity);
31
32     // Destructor.
33     ~MessageChannelBase();
34
35 protected:
36     // Call this method in the sender thread before writing a new message.
37     // This returns the position of the available slot in the message array
38     // where to copy the new fixed-size message. After the copy, call
39     // afterWrite().
40     size_t beforeWrite();
41
42     // To be called after beforeWrite() and copying a new fixed-size message
43     // into the array. This signal the receiver thread that there is a new
44     // incoming message.
45     void afterWrite();
46
47     // Call this method in the receiver thread before reading a new message.
48     // This returns the position in the message array where the new message
49     // can be read. Caller must process the message, then call afterRead().
50     size_t beforeRead();
51
52     // To be called in the receiver thread after beforeRead() and processing
53     // the corresponding message.
54     void afterRead();
55
56 private:
57     size_t mPos;
58     size_t mCount;
59     size_t mCapacity;
60     Mutex mLock;
61     ConditionVariable mCanRead;
62     ConditionVariable mCanWrite;
63 };
64
65 // Helper class used to implement an uni-directional IPC channel between
66 // two threads. The channel can be used to send fixed-size messages of type
67 // |T|, with an internal buffer size of |CAPACITY| items. All calls are
68 // blocking.
69 //
70 // Usage is pretty straightforward:
71 //
72 //   - From the sender thread, call send(msg);
73 //   - From the receiver thread, call receive(&msg);
74 //
75 template <typename T, size_t CAPACITY>
76 class MessageChannel : public MessageChannelBase {
77 public:
78     MessageChannel() : MessageChannelBase(CAPACITY) {}
79
80     void send(const T& msg) {
81         size_t pos = beforeWrite();
82         mItems[pos] = msg;
83         afterWrite();
84     }
85
86     void receive(T* msg) {
87         size_t pos = beforeRead();
88         *msg = mItems[pos];
89         afterRead();
90     }
91
92 private:
93     T mItems[CAPACITY];
94 };
95
96 }  // namespace emugl
97
98 #endif  // EMUGL_COMMON_MESSAGE_CHANNEL_H