1 /*
2  * Copyright 2013 Google Inc.
3  *
4  * Use of this source code is governed by a BSD-style license that can be
5  * found in the LICENSE file.
6  */
7 
8 #ifndef SkMessageBus_DEFINED
9 #define SkMessageBus_DEFINED
10 
11 #include "SkMutex.h"
12 #include "SkOncePtr.h"
13 #include "SkTArray.h"
14 #include "SkTDArray.h"
15 #include "SkTypes.h"
16 
17 template <typename Message>
18 class SkMessageBus : SkNoncopyable {
19 public:
20     // Post a message to be received by all Inboxes for this Message type.  Threadsafe.
21     static void Post(const Message& m);
22 
23     class Inbox {
24     public:
25         Inbox();
26         ~Inbox();
27 
28         // Overwrite out with all the messages we've received since the last call.  Threadsafe.
29         void poll(SkTArray<Message>* out);
30 
31     private:
32         SkTArray<Message>  fMessages;
33         SkMutex            fMessagesMutex;
34 
35         friend class SkMessageBus;
36         void receive(const Message& m);  // SkMessageBus is a friend only to call this.
37     };
38 
39 private:
40     SkMessageBus();
41     static SkMessageBus* Get();
42 
43     SkTDArray<Inbox*> fInboxes;
44     SkMutex           fInboxesMutex;
45 };
46 
47 // This must go in a single .cpp file, not some .h, or we risk creating more than one global
48 // SkMessageBus per type when using shared libraries.  NOTE: at most one per file will compile.
49 #define DECLARE_SKMESSAGEBUS_MESSAGE(Message)                      \
50     SK_DECLARE_STATIC_ONCE_PTR(SkMessageBus<Message>, bus);        \
51     template <>                                                    \
52     SkMessageBus<Message>* SkMessageBus<Message>::Get() {          \
53         return bus.get([]{ return new SkMessageBus<Message>(); }); \
54     }
55 
56 //   ----------------------- Implementation of SkMessageBus::Inbox -----------------------
57 
58 template<typename Message>
Inbox()59 SkMessageBus<Message>::Inbox::Inbox() {
60     // Register ourselves with the corresponding message bus.
61     SkMessageBus<Message>* bus = SkMessageBus<Message>::Get();
62     SkAutoMutexAcquire lock(bus->fInboxesMutex);
63     bus->fInboxes.push(this);
64 }
65 
66 template<typename Message>
~Inbox()67 SkMessageBus<Message>::Inbox::~Inbox() {
68     // Remove ourselves from the corresponding message bus.
69     SkMessageBus<Message>* bus = SkMessageBus<Message>::Get();
70     SkAutoMutexAcquire lock(bus->fInboxesMutex);
71     // This is a cheaper fInboxes.remove(fInboxes.find(this)) when order doesn't matter.
72     for (int i = 0; i < bus->fInboxes.count(); i++) {
73         if (this == bus->fInboxes[i]) {
74             bus->fInboxes.removeShuffle(i);
75             break;
76         }
77     }
78 }
79 
80 template<typename Message>
receive(const Message & m)81 void SkMessageBus<Message>::Inbox::receive(const Message& m) {
82     SkAutoMutexAcquire lock(fMessagesMutex);
83     fMessages.push_back(m);
84 }
85 
86 template<typename Message>
poll(SkTArray<Message> * messages)87 void SkMessageBus<Message>::Inbox::poll(SkTArray<Message>* messages) {
88     SkASSERT(messages);
89     messages->reset();
90     SkAutoMutexAcquire lock(fMessagesMutex);
91     fMessages.swap(messages);
92 }
93 
94 //   ----------------------- Implementation of SkMessageBus -----------------------
95 
96 template <typename Message>
SkMessageBus()97 SkMessageBus<Message>::SkMessageBus() {}
98 
99 template <typename Message>
Post(const Message & m)100 /*static*/ void SkMessageBus<Message>::Post(const Message& m) {
101     SkMessageBus<Message>* bus = SkMessageBus<Message>::Get();
102     SkAutoMutexAcquire lock(bus->fInboxesMutex);
103     for (int i = 0; i < bus->fInboxes.count(); i++) {
104         bus->fInboxes[i]->receive(m);
105     }
106 }
107 
108 #endif  // SkMessageBus_DEFINED
109