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 "SkOnce.h" | 11 #include "SkLazyPtr.h" |
12 #include "SkTDArray.h" | 12 #include "SkTDArray.h" |
13 #include "SkThread.h" | 13 #include "SkThread.h" |
14 #include "SkTypes.h" | 14 #include "SkTypes.h" |
15 | 15 |
16 template <typename Message> | 16 template <typename Message> |
17 class SkMessageBus : SkNoncopyable { | 17 class SkMessageBus : SkNoncopyable { |
18 public: | 18 public: |
19 // Post a message to be received by all Inboxes for this Message type. Thre
adsafe. | 19 // Post a message to be received by all Inboxes for this Message type. Thre
adsafe. |
20 static void Post(const Message& m); | 20 static void Post(const Message& m); |
21 | 21 |
22 class Inbox { | 22 class Inbox { |
23 public: | 23 public: |
24 Inbox(); | 24 Inbox(); |
25 ~Inbox(); | 25 ~Inbox(); |
26 | 26 |
27 // Overwrite out with all the messages we've received since the last cal
l. Threadsafe. | 27 // Overwrite out with all the messages we've received since the last cal
l. Threadsafe. |
28 void poll(SkTDArray<Message>* out); | 28 void poll(SkTDArray<Message>* out); |
29 | 29 |
30 private: | 30 private: |
31 SkTDArray<Message> fMessages; | 31 SkTDArray<Message> fMessages; |
32 SkMutex fMessagesMutex; | 32 SkMutex fMessagesMutex; |
33 | 33 |
34 friend class SkMessageBus; | 34 friend class SkMessageBus; |
35 void receive(const Message& m); // SkMessageBus is a friend only to cal
l this. | 35 void receive(const Message& m); // SkMessageBus is a friend only to cal
l this. |
36 }; | 36 }; |
37 | 37 |
38 private: | 38 private: |
39 SkMessageBus(); | 39 SkMessageBus(); |
40 static SkMessageBus* Get(); | 40 static SkMessageBus* Get(); |
41 static void New(SkMessageBus**); | 41 static SkMessageBus* New(); |
42 | 42 |
43 SkTDArray<Inbox*> fInboxes; | 43 SkTDArray<Inbox*> fInboxes; |
44 SkMutex fInboxesMutex; | 44 SkMutex fInboxesMutex; |
45 }; | 45 }; |
46 | 46 |
47 // This must go in a single .cpp file, not some .h, or we risk creating more tha
n one global | 47 // This must go in a single .cpp file, not some .h, or we risk creating more tha
n one global |
48 // SkMessageBus per type when using shared libraries. | 48 // SkMessageBus per type when using shared libraries. |
49 #define DECLARE_SKMESSAGEBUS_MESSAGE(Message) \ | 49 #define DECLARE_SKMESSAGEBUS_MESSAGE(Message) \ |
50 template <> \ | 50 template <> \ |
51 SkMessageBus<Message>* SkMessageBus<Message>::Get() { \ | 51 SkMessageBus<Message>* SkMessageBus<Message>::Get() { \ |
52 static SkMessageBus<Message>* bus = NULL; \ | 52 SK_DECLARE_STATIC_LAZY_PTR(SkMessageBus<Message>, bus, New); \ |
53 SK_DECLARE_STATIC_ONCE(once); \ | 53 return bus.get(); \ |
54 SkOnce(&once, &New, &bus); \ | |
55 SkASSERT(bus != NULL); \ | |
56 return bus; \ | |
57 } | 54 } |
58 | 55 |
59 // ----------------------- Implementation of SkMessageBus::Inbox -------------
---------- | 56 // ----------------------- Implementation of SkMessageBus::Inbox -------------
---------- |
60 | 57 |
61 template<typename Message> | 58 template<typename Message> |
62 SkMessageBus<Message>::Inbox::Inbox() { | 59 SkMessageBus<Message>::Inbox::Inbox() { |
63 // Register ourselves with the corresponding message bus. | 60 // Register ourselves with the corresponding message bus. |
64 SkMessageBus<Message>* bus = SkMessageBus<Message>::Get(); | 61 SkMessageBus<Message>* bus = SkMessageBus<Message>::Get(); |
65 SkAutoMutexAcquire lock(bus->fInboxesMutex); | 62 SkAutoMutexAcquire lock(bus->fInboxesMutex); |
66 bus->fInboxes.push(this); | 63 bus->fInboxes.push(this); |
(...skipping 26 matching lines...) Expand all Loading... |
93 SkAutoMutexAcquire lock(fMessagesMutex); | 90 SkAutoMutexAcquire lock(fMessagesMutex); |
94 messages->swap(fMessages); | 91 messages->swap(fMessages); |
95 } | 92 } |
96 | 93 |
97 // ----------------------- Implementation of SkMessageBus --------------------
--- | 94 // ----------------------- Implementation of SkMessageBus --------------------
--- |
98 | 95 |
99 template <typename Message> | 96 template <typename Message> |
100 SkMessageBus<Message>::SkMessageBus() {} | 97 SkMessageBus<Message>::SkMessageBus() {} |
101 | 98 |
102 template <typename Message> | 99 template <typename Message> |
103 /*static*/ void SkMessageBus<Message>::New(SkMessageBus<Message>** bus) { | 100 /*static*/ SkMessageBus<Message>* SkMessageBus<Message>::New() { |
104 *bus = new SkMessageBus<Message>(); | 101 return SkNEW(SkMessageBus<Message>); |
105 } | 102 } |
106 | 103 |
107 template <typename Message> | 104 template <typename Message> |
108 /*static*/ void SkMessageBus<Message>::Post(const Message& m) { | 105 /*static*/ void SkMessageBus<Message>::Post(const Message& m) { |
109 SkMessageBus<Message>* bus = SkMessageBus<Message>::Get(); | 106 SkMessageBus<Message>* bus = SkMessageBus<Message>::Get(); |
110 SkAutoMutexAcquire lock(bus->fInboxesMutex); | 107 SkAutoMutexAcquire lock(bus->fInboxesMutex); |
111 for (int i = 0; i < bus->fInboxes.count(); i++) { | 108 for (int i = 0; i < bus->fInboxes.count(); i++) { |
112 bus->fInboxes[i]->receive(m); | 109 bus->fInboxes[i]->receive(m); |
113 } | 110 } |
114 } | 111 } |
115 | 112 |
116 #endif // SkMessageBus_DEFINED | 113 #endif // SkMessageBus_DEFINED |
OLD | NEW |