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