| 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 "SkOnce.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 SkMessageBus* New(); | 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 | 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 SK_DECLARE_STATIC_LAZY_PTR(SkMessageBus<Message>, bus, New); \ | 52 static SkMessageBus<Message>* bus = NULL; \ |
| 53 return bus.get(); \ | 53 SK_DECLARE_STATIC_ONCE(once); \ |
| 54 SkOnce(&once, &New, &bus); \ |
| 55 SkASSERT(bus != NULL); \ |
| 56 return bus; \ |
| 54 } | 57 } |
| 55 | 58 |
| 56 // ----------------------- Implementation of SkMessageBus::Inbox -------------
---------- | 59 // ----------------------- Implementation of SkMessageBus::Inbox -------------
---------- |
| 57 | 60 |
| 58 template<typename Message> | 61 template<typename Message> |
| 59 SkMessageBus<Message>::Inbox::Inbox() { | 62 SkMessageBus<Message>::Inbox::Inbox() { |
| 60 // Register ourselves with the corresponding message bus. | 63 // Register ourselves with the corresponding message bus. |
| 61 SkMessageBus<Message>* bus = SkMessageBus<Message>::Get(); | 64 SkMessageBus<Message>* bus = SkMessageBus<Message>::Get(); |
| 62 SkAutoMutexAcquire lock(bus->fInboxesMutex); | 65 SkAutoMutexAcquire lock(bus->fInboxesMutex); |
| 63 bus->fInboxes.push(this); | 66 bus->fInboxes.push(this); |
| (...skipping 26 matching lines...) Expand all Loading... |
| 90 SkAutoMutexAcquire lock(fMessagesMutex); | 93 SkAutoMutexAcquire lock(fMessagesMutex); |
| 91 messages->swap(fMessages); | 94 messages->swap(fMessages); |
| 92 } | 95 } |
| 93 | 96 |
| 94 // ----------------------- Implementation of SkMessageBus --------------------
--- | 97 // ----------------------- Implementation of SkMessageBus --------------------
--- |
| 95 | 98 |
| 96 template <typename Message> | 99 template <typename Message> |
| 97 SkMessageBus<Message>::SkMessageBus() {} | 100 SkMessageBus<Message>::SkMessageBus() {} |
| 98 | 101 |
| 99 template <typename Message> | 102 template <typename Message> |
| 100 /*static*/ SkMessageBus<Message>* SkMessageBus<Message>::New() { | 103 /*static*/ void SkMessageBus<Message>::New(SkMessageBus<Message>** bus) { |
| 101 return SkNEW(SkMessageBus<Message>); | 104 *bus = new SkMessageBus<Message>(); |
| 102 } | 105 } |
| 103 | 106 |
| 104 template <typename Message> | 107 template <typename Message> |
| 105 /*static*/ void SkMessageBus<Message>::Post(const Message& m) { | 108 /*static*/ void SkMessageBus<Message>::Post(const Message& m) { |
| 106 SkMessageBus<Message>* bus = SkMessageBus<Message>::Get(); | 109 SkMessageBus<Message>* bus = SkMessageBus<Message>::Get(); |
| 107 SkAutoMutexAcquire lock(bus->fInboxesMutex); | 110 SkAutoMutexAcquire lock(bus->fInboxesMutex); |
| 108 for (int i = 0; i < bus->fInboxes.count(); i++) { | 111 for (int i = 0; i < bus->fInboxes.count(); i++) { |
| 109 bus->fInboxes[i]->receive(m); | 112 bus->fInboxes[i]->receive(m); |
| 110 } | 113 } |
| 111 } | 114 } |
| 112 | 115 |
| 113 #endif // SkMessageBus_DEFINED | 116 #endif // SkMessageBus_DEFINED |
| OLD | NEW |