commit-bot@chromium.org | 50a3043 | 2013-10-24 17:44:27 +0000 | [diff] [blame] | 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 | |
mtklein | 1b24933 | 2015-07-07 12:21:21 -0700 | [diff] [blame] | 11 | #include "SkMutex.h" |
mtklein | ffa4a92 | 2016-05-05 16:05:56 -0700 | [diff] [blame] | 12 | #include "SkOnce.h" |
bsalomon | 23e619c | 2015-02-06 11:54:28 -0800 | [diff] [blame] | 13 | #include "SkTArray.h" |
commit-bot@chromium.org | 50a3043 | 2013-10-24 17:44:27 +0000 | [diff] [blame] | 14 | #include "SkTDArray.h" |
commit-bot@chromium.org | 50a3043 | 2013-10-24 17:44:27 +0000 | [diff] [blame] | 15 | #include "SkTypes.h" |
| 16 | |
| 17 | template <typename Message> |
| 18 | class SkMessageBus : SkNoncopyable { |
| 19 | public: |
Jim Van Verth | 474d687 | 2017-12-14 13:00:05 -0500 | [diff] [blame] | 20 | // Post a message to be received by Inboxes for this Message type. Threadsafe. |
| 21 | // If id is SK_InvalidUniqueID then it will be sent to all inboxes. |
| 22 | // Otherwise it will be sent to the inbox with that id. |
| 23 | static void Post(const Message& m, uint32_t destID = SK_InvalidUniqueID); |
commit-bot@chromium.org | 50a3043 | 2013-10-24 17:44:27 +0000 | [diff] [blame] | 24 | |
| 25 | class Inbox { |
| 26 | public: |
Jim Van Verth | 474d687 | 2017-12-14 13:00:05 -0500 | [diff] [blame] | 27 | Inbox(uint32_t uniqueID = SK_InvalidUniqueID); |
commit-bot@chromium.org | 50a3043 | 2013-10-24 17:44:27 +0000 | [diff] [blame] | 28 | ~Inbox(); |
| 29 | |
| 30 | // Overwrite out with all the messages we've received since the last call. Threadsafe. |
bsalomon | 23e619c | 2015-02-06 11:54:28 -0800 | [diff] [blame] | 31 | void poll(SkTArray<Message>* out); |
commit-bot@chromium.org | 50a3043 | 2013-10-24 17:44:27 +0000 | [diff] [blame] | 32 | |
| 33 | private: |
bsalomon | 23e619c | 2015-02-06 11:54:28 -0800 | [diff] [blame] | 34 | SkTArray<Message> fMessages; |
commit-bot@chromium.org | 50a3043 | 2013-10-24 17:44:27 +0000 | [diff] [blame] | 35 | SkMutex fMessagesMutex; |
Jim Van Verth | 474d687 | 2017-12-14 13:00:05 -0500 | [diff] [blame] | 36 | uint32_t fUniqueID; |
commit-bot@chromium.org | 50a3043 | 2013-10-24 17:44:27 +0000 | [diff] [blame] | 37 | |
| 38 | friend class SkMessageBus; |
| 39 | void receive(const Message& m); // SkMessageBus is a friend only to call this. |
| 40 | }; |
| 41 | |
| 42 | private: |
| 43 | SkMessageBus(); |
| 44 | static SkMessageBus* Get(); |
mtklein | 148ec59 | 2014-10-13 13:17:56 -0700 | [diff] [blame] | 45 | |
commit-bot@chromium.org | 50a3043 | 2013-10-24 17:44:27 +0000 | [diff] [blame] | 46 | SkTDArray<Inbox*> fInboxes; |
| 47 | SkMutex fInboxesMutex; |
| 48 | }; |
| 49 | |
commit-bot@chromium.org | c665804 | 2014-01-15 23:09:01 +0000 | [diff] [blame] | 50 | // This must go in a single .cpp file, not some .h, or we risk creating more than one global |
mtklein | 148ec59 | 2014-10-13 13:17:56 -0700 | [diff] [blame] | 51 | // SkMessageBus per type when using shared libraries. NOTE: at most one per file will compile. |
mtklein | 6c59d80 | 2015-09-09 09:09:53 -0700 | [diff] [blame] | 52 | #define DECLARE_SKMESSAGEBUS_MESSAGE(Message) \ |
mtklein | 6c59d80 | 2015-09-09 09:09:53 -0700 | [diff] [blame] | 53 | template <> \ |
| 54 | SkMessageBus<Message>* SkMessageBus<Message>::Get() { \ |
mtklein | ffa4a92 | 2016-05-05 16:05:56 -0700 | [diff] [blame] | 55 | static SkOnce once; \ |
| 56 | static SkMessageBus<Message>* bus; \ |
| 57 | once([] { bus = new SkMessageBus<Message>(); }); \ |
| 58 | return bus; \ |
commit-bot@chromium.org | c665804 | 2014-01-15 23:09:01 +0000 | [diff] [blame] | 59 | } |
| 60 | |
commit-bot@chromium.org | 50a3043 | 2013-10-24 17:44:27 +0000 | [diff] [blame] | 61 | // ----------------------- Implementation of SkMessageBus::Inbox ----------------------- |
| 62 | |
| 63 | template<typename Message> |
Jim Van Verth | 474d687 | 2017-12-14 13:00:05 -0500 | [diff] [blame] | 64 | SkMessageBus<Message>::Inbox::Inbox(uint32_t uniqueID) : fUniqueID(uniqueID) { |
commit-bot@chromium.org | 50a3043 | 2013-10-24 17:44:27 +0000 | [diff] [blame] | 65 | // Register ourselves with the corresponding message bus. |
| 66 | SkMessageBus<Message>* bus = SkMessageBus<Message>::Get(); |
| 67 | SkAutoMutexAcquire lock(bus->fInboxesMutex); |
| 68 | bus->fInboxes.push(this); |
| 69 | } |
| 70 | |
| 71 | template<typename Message> |
| 72 | SkMessageBus<Message>::Inbox::~Inbox() { |
| 73 | // Remove ourselves from the corresponding message bus. |
| 74 | SkMessageBus<Message>* bus = SkMessageBus<Message>::Get(); |
| 75 | SkAutoMutexAcquire lock(bus->fInboxesMutex); |
| 76 | // This is a cheaper fInboxes.remove(fInboxes.find(this)) when order doesn't matter. |
| 77 | for (int i = 0; i < bus->fInboxes.count(); i++) { |
| 78 | if (this == bus->fInboxes[i]) { |
| 79 | bus->fInboxes.removeShuffle(i); |
| 80 | break; |
| 81 | } |
| 82 | } |
| 83 | } |
| 84 | |
| 85 | template<typename Message> |
| 86 | void SkMessageBus<Message>::Inbox::receive(const Message& m) { |
| 87 | SkAutoMutexAcquire lock(fMessagesMutex); |
bsalomon | 23e619c | 2015-02-06 11:54:28 -0800 | [diff] [blame] | 88 | fMessages.push_back(m); |
commit-bot@chromium.org | 50a3043 | 2013-10-24 17:44:27 +0000 | [diff] [blame] | 89 | } |
| 90 | |
| 91 | template<typename Message> |
bsalomon | 23e619c | 2015-02-06 11:54:28 -0800 | [diff] [blame] | 92 | void SkMessageBus<Message>::Inbox::poll(SkTArray<Message>* messages) { |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 93 | SkASSERT(messages); |
commit-bot@chromium.org | 50a3043 | 2013-10-24 17:44:27 +0000 | [diff] [blame] | 94 | messages->reset(); |
| 95 | SkAutoMutexAcquire lock(fMessagesMutex); |
bsalomon | 23e619c | 2015-02-06 11:54:28 -0800 | [diff] [blame] | 96 | fMessages.swap(messages); |
commit-bot@chromium.org | 50a3043 | 2013-10-24 17:44:27 +0000 | [diff] [blame] | 97 | } |
| 98 | |
| 99 | // ----------------------- Implementation of SkMessageBus ----------------------- |
| 100 | |
| 101 | template <typename Message> |
| 102 | SkMessageBus<Message>::SkMessageBus() {} |
| 103 | |
| 104 | template <typename Message> |
Jim Van Verth | 474d687 | 2017-12-14 13:00:05 -0500 | [diff] [blame] | 105 | /*static*/ void SkMessageBus<Message>::Post(const Message& m, uint32_t destID) { |
commit-bot@chromium.org | 50a3043 | 2013-10-24 17:44:27 +0000 | [diff] [blame] | 106 | SkMessageBus<Message>* bus = SkMessageBus<Message>::Get(); |
| 107 | SkAutoMutexAcquire lock(bus->fInboxesMutex); |
| 108 | for (int i = 0; i < bus->fInboxes.count(); i++) { |
Jim Van Verth | 474d687 | 2017-12-14 13:00:05 -0500 | [diff] [blame] | 109 | if (SK_InvalidUniqueID == destID || bus->fInboxes[i]->fUniqueID == destID) { |
| 110 | bus->fInboxes[i]->receive(m); |
| 111 | } |
commit-bot@chromium.org | 50a3043 | 2013-10-24 17:44:27 +0000 | [diff] [blame] | 112 | } |
| 113 | } |
| 114 | |
| 115 | #endif // SkMessageBus_DEFINED |