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 "SkMutex.h" | 12 #include "SkMutex.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 10 matching lines...) Expand all Loading... |
33 SkMutex fMessagesMutex; | 33 SkMutex fMessagesMutex; |
34 | 34 |
35 friend class SkMessageBus; | 35 friend class SkMessageBus; |
36 void receive(const Message& m); // SkMessageBus is a friend only to cal
l this. | 36 void receive(const Message& m); // SkMessageBus is a friend only to cal
l this. |
37 }; | 37 }; |
38 | 38 |
39 private: | 39 private: |
40 SkMessageBus(); | 40 SkMessageBus(); |
41 static SkMessageBus* Get(); | 41 static SkMessageBus* Get(); |
42 | 42 |
| 43 // Allow SkLazyPtr to call SkMessageBus::SkMessageBus(). |
| 44 template <typename T> friend T* Private::sk_new(); |
| 45 |
43 SkTDArray<Inbox*> fInboxes; | 46 SkTDArray<Inbox*> fInboxes; |
44 SkMutex fInboxesMutex; | 47 SkMutex fInboxesMutex; |
45 }; | 48 }; |
46 | 49 |
47 // This must go in a single .cpp file, not some .h, or we risk creating more tha
n one global | 50 // 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. | 51 // SkMessageBus per type when using shared libraries. NOTE: at most one per fil
e will compile. |
49 #define DECLARE_SKMESSAGEBUS_MESSAGE(Message) \ | 52 #define DECLARE_SKMESSAGEBUS_MESSAGE(Message) \ |
50 SK_DECLARE_STATIC_ONCE_PTR(SkMessageBus<Message>, bus); \ | 53 SK_DECLARE_STATIC_LAZY_PTR(SkMessageBus<Message>, bus); \ |
51 template <> \ | 54 template <> \ |
52 SkMessageBus<Message>* SkMessageBus<Message>::Get() { \ | 55 SkMessageBus<Message>* SkMessageBus<Message>::Get() { \ |
53 return bus.get([]{ return new SkMessageBus<Message>(); }); \ | 56 return bus.get(); \ |
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 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
99 template <typename Message> | 102 template <typename Message> |
100 /*static*/ void SkMessageBus<Message>::Post(const Message& m) { | 103 /*static*/ void SkMessageBus<Message>::Post(const Message& m) { |
101 SkMessageBus<Message>* bus = SkMessageBus<Message>::Get(); | 104 SkMessageBus<Message>* bus = SkMessageBus<Message>::Get(); |
102 SkAutoMutexAcquire lock(bus->fInboxesMutex); | 105 SkAutoMutexAcquire lock(bus->fInboxesMutex); |
103 for (int i = 0; i < bus->fInboxes.count(); i++) { | 106 for (int i = 0; i < bus->fInboxes.count(); i++) { |
104 bus->fInboxes[i]->receive(m); | 107 bus->fInboxes[i]->receive(m); |
105 } | 108 } |
106 } | 109 } |
107 | 110 |
108 #endif // SkMessageBus_DEFINED | 111 #endif // SkMessageBus_DEFINED |
OLD | NEW |