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 "SkMutex.h" | 11 #include "SkMutex.h" |
12 #include "SkOncePtr.h" | 12 #include "SkOnce.h" |
13 #include "SkTArray.h" | 13 #include "SkTArray.h" |
14 #include "SkTDArray.h" | 14 #include "SkTDArray.h" |
15 #include "SkTypes.h" | 15 #include "SkTypes.h" |
16 | 16 |
17 template <typename Message> | 17 template <typename Message> |
18 class SkMessageBus : SkNoncopyable { | 18 class SkMessageBus : SkNoncopyable { |
19 public: | 19 public: |
20 // 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. |
21 static void Post(const Message& m); | 21 static void Post(const Message& m); |
22 | 22 |
(...skipping 17 matching lines...) Expand all Loading... |
40 SkMessageBus(); | 40 SkMessageBus(); |
41 static SkMessageBus* Get(); | 41 static SkMessageBus* Get(); |
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. NOTE: at most one per fil
e will compile. | 48 // SkMessageBus per type when using shared libraries. NOTE: at most one per fil
e will compile. |
49 #define DECLARE_SKMESSAGEBUS_MESSAGE(Message) \ | 49 #define DECLARE_SKMESSAGEBUS_MESSAGE(Message) \ |
50 SK_DECLARE_STATIC_ONCE_PTR(SkMessageBus<Message>, bus); \ | |
51 template <> \ | 50 template <> \ |
52 SkMessageBus<Message>* SkMessageBus<Message>::Get() { \ | 51 SkMessageBus<Message>* SkMessageBus<Message>::Get() { \ |
53 return bus.get([]{ return new SkMessageBus<Message>(); }); \ | 52 static SkOnce once; \ |
| 53 static SkMessageBus<Message>* bus; \ |
| 54 once([] { bus = new SkMessageBus<Message>(); }); \ |
| 55 return bus; \ |
54 } | 56 } |
55 | 57 |
56 // ----------------------- Implementation of SkMessageBus::Inbox -------------
---------- | 58 // ----------------------- Implementation of SkMessageBus::Inbox -------------
---------- |
57 | 59 |
58 template<typename Message> | 60 template<typename Message> |
59 SkMessageBus<Message>::Inbox::Inbox() { | 61 SkMessageBus<Message>::Inbox::Inbox() { |
60 // Register ourselves with the corresponding message bus. | 62 // Register ourselves with the corresponding message bus. |
61 SkMessageBus<Message>* bus = SkMessageBus<Message>::Get(); | 63 SkMessageBus<Message>* bus = SkMessageBus<Message>::Get(); |
62 SkAutoMutexAcquire lock(bus->fInboxesMutex); | 64 SkAutoMutexAcquire lock(bus->fInboxesMutex); |
63 bus->fInboxes.push(this); | 65 bus->fInboxes.push(this); |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
99 template <typename Message> | 101 template <typename Message> |
100 /*static*/ void SkMessageBus<Message>::Post(const Message& m) { | 102 /*static*/ void SkMessageBus<Message>::Post(const Message& m) { |
101 SkMessageBus<Message>* bus = SkMessageBus<Message>::Get(); | 103 SkMessageBus<Message>* bus = SkMessageBus<Message>::Get(); |
102 SkAutoMutexAcquire lock(bus->fInboxesMutex); | 104 SkAutoMutexAcquire lock(bus->fInboxesMutex); |
103 for (int i = 0; i < bus->fInboxes.count(); i++) { | 105 for (int i = 0; i < bus->fInboxes.count(); i++) { |
104 bus->fInboxes[i]->receive(m); | 106 bus->fInboxes[i]->receive(m); |
105 } | 107 } |
106 } | 108 } |
107 | 109 |
108 #endif // SkMessageBus_DEFINED | 110 #endif // SkMessageBus_DEFINED |
OLD | NEW |