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 |
(...skipping 26 matching lines...) Expand all Loading... |
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 void New(SkMessageBus**); |
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 |
| 48 // SkMessageBus per type when using shared libraries. |
| 49 #define DECLARE_SKMESSAGEBUS_MESSAGE(Message) \ |
| 50 template <> \ |
| 51 SkMessageBus<Message>* SkMessageBus<Message>::Get() { \ |
| 52 static SkMessageBus<Message>* bus = NULL; \ |
| 53 SK_DECLARE_STATIC_ONCE(once); \ |
| 54 SkOnce(&once, &New, &bus); \ |
| 55 SkASSERT(bus != NULL); \ |
| 56 return bus; \ |
| 57 } |
| 58 |
47 // ----------------------- Implementation of SkMessageBus::Inbox -------------
---------- | 59 // ----------------------- Implementation of SkMessageBus::Inbox -------------
---------- |
48 | 60 |
49 template<typename Message> | 61 template<typename Message> |
50 SkMessageBus<Message>::Inbox::Inbox() { | 62 SkMessageBus<Message>::Inbox::Inbox() { |
51 // Register ourselves with the corresponding message bus. | 63 // Register ourselves with the corresponding message bus. |
52 SkMessageBus<Message>* bus = SkMessageBus<Message>::Get(); | 64 SkMessageBus<Message>* bus = SkMessageBus<Message>::Get(); |
53 SkAutoMutexAcquire lock(bus->fInboxesMutex); | 65 SkAutoMutexAcquire lock(bus->fInboxesMutex); |
54 bus->fInboxes.push(this); | 66 bus->fInboxes.push(this); |
55 } | 67 } |
56 | 68 |
(...skipping 29 matching lines...) Expand all Loading... |
86 | 98 |
87 template <typename Message> | 99 template <typename Message> |
88 SkMessageBus<Message>::SkMessageBus() {} | 100 SkMessageBus<Message>::SkMessageBus() {} |
89 | 101 |
90 template <typename Message> | 102 template <typename Message> |
91 /*static*/ void SkMessageBus<Message>::New(SkMessageBus<Message>** bus) { | 103 /*static*/ void SkMessageBus<Message>::New(SkMessageBus<Message>** bus) { |
92 *bus = new SkMessageBus<Message>(); | 104 *bus = new SkMessageBus<Message>(); |
93 } | 105 } |
94 | 106 |
95 template <typename Message> | 107 template <typename Message> |
96 /*static*/ SkMessageBus<Message>* SkMessageBus<Message>::Get() { | |
97 // The first time this method is called, create the singleton bus for this m
essage type. | |
98 static SkMessageBus<Message>* bus = NULL; | |
99 SK_DECLARE_STATIC_ONCE(once); | |
100 SkOnce(&once, &New, &bus); | |
101 | |
102 SkASSERT(bus != NULL); | |
103 return bus; | |
104 } | |
105 | |
106 template <typename Message> | |
107 /*static*/ void SkMessageBus<Message>::Post(const Message& m) { | 108 /*static*/ void SkMessageBus<Message>::Post(const Message& m) { |
108 SkMessageBus<Message>* bus = SkMessageBus<Message>::Get(); | 109 SkMessageBus<Message>* bus = SkMessageBus<Message>::Get(); |
109 SkAutoMutexAcquire lock(bus->fInboxesMutex); | 110 SkAutoMutexAcquire lock(bus->fInboxesMutex); |
110 for (int i = 0; i < bus->fInboxes.count(); i++) { | 111 for (int i = 0; i < bus->fInboxes.count(); i++) { |
111 bus->fInboxes[i]->receive(m); | 112 bus->fInboxes[i]->receive(m); |
112 } | 113 } |
113 } | 114 } |
114 | 115 |
115 #endif // SkMessageBus_DEFINED | 116 #endif // SkMessageBus_DEFINED |
OLD | NEW |