Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(78)

Side by Side Diff: mojo/public/cpp/bindings/callback.h

Issue 1819463002: Make mojo::Callback safe to copy across threads. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: stuff Created 4 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2014 The Chromium Authors. All rights reserved. 1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #ifndef MOJO_PUBLIC_CPP_BINDINGS_CALLBACK_H_ 5 #ifndef MOJO_PUBLIC_CPP_BINDINGS_CALLBACK_H_
6 #define MOJO_PUBLIC_CPP_BINDINGS_CALLBACK_H_ 6 #define MOJO_PUBLIC_CPP_BINDINGS_CALLBACK_H_
7 7
8 #include <utility> 8 #include <utility>
9 9
10 #include "base/memory/ref_counted.h"
11 #include "base/memory/scoped_ptr.h"
10 #include "mojo/public/cpp/bindings/lib/callback_internal.h" 12 #include "mojo/public/cpp/bindings/lib/callback_internal.h"
11 #include "mojo/public/cpp/bindings/lib/shared_ptr.h"
12 #include "mojo/public/cpp/bindings/lib/template_util.h" 13 #include "mojo/public/cpp/bindings/lib/template_util.h"
13 14
14 namespace mojo { 15 namespace mojo {
15 16
16 template <typename Sig> 17 template <typename Sig>
17 class Callback; 18 class Callback;
18 19
19 // Represents a callback with any number of parameters and no return value. The 20 // Represents a callback with any number of parameters and no return value. The
20 // callback is executed by calling its Run() method. The callback may be "null", 21 // callback is executed by calling its Run() method. The callback may be "null",
21 // meaning it does nothing. 22 // meaning it does nothing.
(...skipping 11 matching lines...) Expand all
33 // ForwardType). 34 // ForwardType).
34 typename internal::Callback_ParamTraits<Args>::ForwardType...) 35 typename internal::Callback_ParamTraits<Args>::ForwardType...)
35 const = 0; 36 const = 0;
36 }; 37 };
37 38
38 // Constructs a "null" callback that does nothing. 39 // Constructs a "null" callback that does nothing.
39 Callback() {} 40 Callback() {}
40 41
41 // Constructs a callback that will run |runnable|. The callback takes 42 // Constructs a callback that will run |runnable|. The callback takes
42 // ownership of |runnable|. 43 // ownership of |runnable|.
43 explicit Callback(Runnable* runnable) : sink_(runnable) {} 44 explicit Callback(Runnable* runnable) : sink_(new RunnableHolder(runnable)) {}
44 45
45 // As above, but can take an object that isn't derived from Runnable, so long 46 // As above, but can take an object that isn't derived from Runnable, so long
46 // as it has a compatible operator() or Run() method. operator() will be 47 // as it has a compatible operator() or Run() method. operator() will be
47 // preferred if the type has both. 48 // preferred if the type has both.
48 template <typename Sink> 49 template <typename Sink>
49 Callback(const Sink& sink) { 50 Callback(const Sink& sink) {
50 using sink_type = typename internal::Conditional< 51 using sink_type = typename internal::Conditional<
51 internal::HasCompatibleCallOperator<Sink, Args...>::value, 52 internal::HasCompatibleCallOperator<Sink, Args...>::value,
52 FunctorAdapter<Sink>, RunnableAdapter<Sink>>::type; 53 FunctorAdapter<Sink>, RunnableAdapter<Sink>>::type;
53 sink_ = internal::SharedPtr<Runnable>(new sink_type(sink)); 54 sink_ = new RunnableHolder(new sink_type(sink));
54 } 55 }
55 56
56 // As above, but can take a compatible function pointer. 57 // As above, but can take a compatible function pointer.
57 Callback(void (*function_ptr)( 58 Callback(void (*function_ptr)(
58 typename internal::Callback_ParamTraits<Args>::ForwardType...)) 59 typename internal::Callback_ParamTraits<Args>::ForwardType...))
59 : sink_(new FunctionPtrAdapter(function_ptr)) {} 60 : sink_(new RunnableHolder(new FunctionPtrAdapter(function_ptr))) {}
60 61
61 // Executes the callback function. 62 // Executes the callback function.
62 void Run(typename internal::Callback_ParamTraits<Args>::ForwardType... args) 63 void Run(typename internal::Callback_ParamTraits<Args>::ForwardType... args)
63 const { 64 const {
64 if (sink_.get()) 65 if (sink_)
65 sink_->Run(std::forward< 66 sink_->runnable->Run(std::forward<
66 typename internal::Callback_ParamTraits<Args>::ForwardType>( 67 typename internal::Callback_ParamTraits<Args>::ForwardType>(
67 args)...); 68 args)...);
68 } 69 }
69 70
70 bool is_null() const { return !sink_.get(); } 71 bool is_null() const { return !sink_.get(); }
71 72
72 // Resets the callback to the "null" state. 73 // Resets the callback to the "null" state.
73 void reset() { sink_.reset(); } 74 void reset() { sink_ = nullptr; }
74 75
75 private: 76 private:
76 // Adapts a class that has a Run() method but is not derived from Runnable to 77 // Adapts a class that has a Run() method but is not derived from Runnable to
77 // be callable by Callback. 78 // be callable by Callback.
78 template <typename Sink> 79 template <typename Sink>
79 struct RunnableAdapter : public Runnable { 80 struct RunnableAdapter : public Runnable {
80 explicit RunnableAdapter(const Sink& sink) : sink(sink) {} 81 explicit RunnableAdapter(const Sink& sink) : sink(sink) {}
81 virtual void Run( 82 virtual void Run(
82 typename internal::Callback_ParamTraits<Args>::ForwardType... args) 83 typename internal::Callback_ParamTraits<Args>::ForwardType... args)
83 const override { 84 const override {
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after
116 typename internal::Callback_ParamTraits<Args>::ForwardType... args) 117 typename internal::Callback_ParamTraits<Args>::ForwardType... args)
117 const override { 118 const override {
118 (*function_ptr)( 119 (*function_ptr)(
119 std::forward< 120 std::forward<
120 typename internal::Callback_ParamTraits<Args>::ForwardType>( 121 typename internal::Callback_ParamTraits<Args>::ForwardType>(
121 args)...); 122 args)...);
122 } 123 }
123 FunctionPtr function_ptr; 124 FunctionPtr function_ptr;
124 }; 125 };
125 126
126 internal::SharedPtr<Runnable> sink_; 127 struct RunnableHolder : public base::RefCountedThreadSafe<RunnableHolder> {
128 explicit RunnableHolder(Runnable* runnable) : runnable(runnable) {}
129
130 scoped_ptr<Runnable> runnable;
131
132 private:
133 friend class base::RefCountedThreadSafe<RunnableHolder>;
134 ~RunnableHolder() {}
135 };
136
137 scoped_refptr<RunnableHolder> sink_;
127 }; 138 };
128 139
129 // A specialization of Callback which takes no parameters. 140 // A specialization of Callback which takes no parameters.
130 typedef Callback<void()> Closure; 141 typedef Callback<void()> Closure;
131 142
132 } // namespace mojo 143 } // namespace mojo
133 144
134 #endif // MOJO_PUBLIC_CPP_BINDINGS_CALLBACK_H_ 145 #endif // MOJO_PUBLIC_CPP_BINDINGS_CALLBACK_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698