OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2013 Google Inc. | 2 * Copyright 2013 Google Inc. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
6 */ | 6 */ |
7 | 7 |
8 #ifndef SkMessageBus_DEFINED | 8 #ifndef SkMessageBus_DEFINED |
9 #define SkMessageBus_DEFINED | 9 #define SkMessageBus_DEFINED |
10 | 10 |
11 #include "SkLazyPtr.h" | 11 #include "SkLazyPtr.h" |
| 12 #include "SkTArray.h" |
12 #include "SkTDArray.h" | 13 #include "SkTDArray.h" |
13 #include "SkThread.h" | 14 #include "SkThread.h" |
14 #include "SkTypes.h" | 15 #include "SkTypes.h" |
15 | 16 |
16 template <typename Message> | 17 template <typename Message> |
17 class SkMessageBus : SkNoncopyable { | 18 class SkMessageBus : SkNoncopyable { |
18 public: | 19 public: |
19 // Post a message to be received by all Inboxes for this Message type. Thre
adsafe. | 20 // Post a message to be received by all Inboxes for this Message type. Thre
adsafe. |
20 static void Post(const Message& m); | 21 static void Post(const Message& m); |
21 | 22 |
22 class Inbox { | 23 class Inbox { |
23 public: | 24 public: |
24 Inbox(); | 25 Inbox(); |
25 ~Inbox(); | 26 ~Inbox(); |
26 | 27 |
27 // Overwrite out with all the messages we've received since the last cal
l. Threadsafe. | 28 // Overwrite out with all the messages we've received since the last cal
l. Threadsafe. |
28 void poll(SkTDArray<Message>* out); | 29 void poll(SkTArray<Message>* out); |
29 | 30 |
30 private: | 31 private: |
31 SkTDArray<Message> fMessages; | 32 SkTArray<Message> fMessages; |
32 SkMutex fMessagesMutex; | 33 SkMutex fMessagesMutex; |
33 | 34 |
34 friend class SkMessageBus; | 35 friend class SkMessageBus; |
35 void receive(const Message& m); // SkMessageBus is a friend only to cal
l this. | 36 void receive(const Message& m); // SkMessageBus is a friend only to cal
l this. |
36 }; | 37 }; |
37 | 38 |
38 private: | 39 private: |
39 SkMessageBus(); | 40 SkMessageBus(); |
40 static SkMessageBus* Get(); | 41 static SkMessageBus* Get(); |
41 | 42 |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
75 if (this == bus->fInboxes[i]) { | 76 if (this == bus->fInboxes[i]) { |
76 bus->fInboxes.removeShuffle(i); | 77 bus->fInboxes.removeShuffle(i); |
77 break; | 78 break; |
78 } | 79 } |
79 } | 80 } |
80 } | 81 } |
81 | 82 |
82 template<typename Message> | 83 template<typename Message> |
83 void SkMessageBus<Message>::Inbox::receive(const Message& m) { | 84 void SkMessageBus<Message>::Inbox::receive(const Message& m) { |
84 SkAutoMutexAcquire lock(fMessagesMutex); | 85 SkAutoMutexAcquire lock(fMessagesMutex); |
85 fMessages.push(m); | 86 fMessages.push_back(m); |
86 } | 87 } |
87 | 88 |
88 template<typename Message> | 89 template<typename Message> |
89 void SkMessageBus<Message>::Inbox::poll(SkTDArray<Message>* messages) { | 90 void SkMessageBus<Message>::Inbox::poll(SkTArray<Message>* messages) { |
90 SkASSERT(messages); | 91 SkASSERT(messages); |
91 messages->reset(); | 92 messages->reset(); |
92 SkAutoMutexAcquire lock(fMessagesMutex); | 93 SkAutoMutexAcquire lock(fMessagesMutex); |
93 messages->swap(fMessages); | 94 fMessages.swap(messages); |
94 } | 95 } |
95 | 96 |
96 // ----------------------- Implementation of SkMessageBus --------------------
--- | 97 // ----------------------- Implementation of SkMessageBus --------------------
--- |
97 | 98 |
98 template <typename Message> | 99 template <typename Message> |
99 SkMessageBus<Message>::SkMessageBus() {} | 100 SkMessageBus<Message>::SkMessageBus() {} |
100 | 101 |
101 template <typename Message> | 102 template <typename Message> |
102 /*static*/ void SkMessageBus<Message>::Post(const Message& m) { | 103 /*static*/ void SkMessageBus<Message>::Post(const Message& m) { |
103 SkMessageBus<Message>* bus = SkMessageBus<Message>::Get(); | 104 SkMessageBus<Message>* bus = SkMessageBus<Message>::Get(); |
104 SkAutoMutexAcquire lock(bus->fInboxesMutex); | 105 SkAutoMutexAcquire lock(bus->fInboxesMutex); |
105 for (int i = 0; i < bus->fInboxes.count(); i++) { | 106 for (int i = 0; i < bus->fInboxes.count(); i++) { |
106 bus->fInboxes[i]->receive(m); | 107 bus->fInboxes[i]->receive(m); |
107 } | 108 } |
108 } | 109 } |
109 | 110 |
110 #endif // SkMessageBus_DEFINED | 111 #endif // SkMessageBus_DEFINED |
OLD | NEW |