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 "SkOnce.h" | 12 #include "SkOncePtr.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); \ |
50 template <> \ | 51 template <> \ |
51 SkMessageBus<Message>* SkMessageBus<Message>::Get() { \ | 52 SkMessageBus<Message>* SkMessageBus<Message>::Get() { \ |
52 static SkOnce once; \ | 53 return bus.get([]{ return new SkMessageBus<Message>(); }); \ |
53 static SkMessageBus<Message>* bus; \ | |
54 once([] { bus = new SkMessageBus<Message>(); }); \ | |
55 return bus; \ | |
56 } | 54 } |
57 | 55 |
58 // ----------------------- Implementation of SkMessageBus::Inbox -------------
---------- | 56 // ----------------------- Implementation of SkMessageBus::Inbox -------------
---------- |
59 | 57 |
60 template<typename Message> | 58 template<typename Message> |
61 SkMessageBus<Message>::Inbox::Inbox() { | 59 SkMessageBus<Message>::Inbox::Inbox() { |
62 // Register ourselves with the corresponding message bus. | 60 // Register ourselves with the corresponding message bus. |
63 SkMessageBus<Message>* bus = SkMessageBus<Message>::Get(); | 61 SkMessageBus<Message>* bus = SkMessageBus<Message>::Get(); |
64 SkAutoMutexAcquire lock(bus->fInboxesMutex); | 62 SkAutoMutexAcquire lock(bus->fInboxesMutex); |
65 bus->fInboxes.push(this); | 63 bus->fInboxes.push(this); |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
101 template <typename Message> | 99 template <typename Message> |
102 /*static*/ void SkMessageBus<Message>::Post(const Message& m) { | 100 /*static*/ void SkMessageBus<Message>::Post(const Message& m) { |
103 SkMessageBus<Message>* bus = SkMessageBus<Message>::Get(); | 101 SkMessageBus<Message>* bus = SkMessageBus<Message>::Get(); |
104 SkAutoMutexAcquire lock(bus->fInboxesMutex); | 102 SkAutoMutexAcquire lock(bus->fInboxesMutex); |
105 for (int i = 0; i < bus->fInboxes.count(); i++) { | 103 for (int i = 0; i < bus->fInboxes.count(); i++) { |
106 bus->fInboxes[i]->receive(m); | 104 bus->fInboxes[i]->receive(m); |
107 } | 105 } |
108 } | 106 } |
109 | 107 |
110 #endif // SkMessageBus_DEFINED | 108 #endif // SkMessageBus_DEFINED |
OLD | NEW |