diff options
-rw-r--r-- | base/SConscript | 3 | ||||
-rw-r--r-- | base/build/base.vcproj | 12 | ||||
-rw-r--r-- | base/build/base_unittests.vcproj | 8 | ||||
-rw-r--r-- | base/call_wrapper.h | 239 | ||||
-rw-r--r-- | base/call_wrapper_unittest.cc | 204 | ||||
-rw-r--r-- | base/simple_thread.cc | 78 | ||||
-rw-r--r-- | base/simple_thread.h | 163 | ||||
-rw-r--r-- | base/simple_thread_unittest.cc | 113 |
8 files changed, 0 insertions, 820 deletions
diff --git a/base/SConscript b/base/SConscript index 88c2020..b6214e3 100644 --- a/base/SConscript +++ b/base/SConscript @@ -77,7 +77,6 @@ input_files = [ 'revocable_store.cc', 'ref_counted.cc', 'sha2.cc', - 'simple_thread.cc', 'stats_table.cc', 'string_escape.cc', 'string_piece.cc', @@ -245,7 +244,6 @@ if env['PLATFORM'] == 'win32': # cross-platform live below. test_files = [ 'at_exit_unittest.cc', - 'call_wrapper_unittest.cc', 'command_line_unittest.cc', 'json_reader_unittest.cc', 'json_writer_unittest.cc', @@ -256,7 +254,6 @@ test_files = [ 'ref_counted_unittest.cc', 'run_all_unittests.cc', 'sha2_unittest.cc', - 'simple_thread_unittest.cc', 'singleton_unittest.cc', 'stack_container_unittest.cc', 'string_escape_unittest.cc', diff --git a/base/build/base.vcproj b/base/build/base.vcproj index ad52b1d..35295b9 100644 --- a/base/build/base.vcproj +++ b/base/build/base.vcproj @@ -206,10 +206,6 @@ > </File> <File - RelativePath="..\call_wrapper.h" - > - </File> - <File RelativePath="..\clipboard.h" > </File> @@ -614,14 +610,6 @@ > </File> <File - RelativePath="..\simple_thread.h" - > - </File> - <File - RelativePath="..\simple_thread.cc" - > - </File> - <File RelativePath="..\singleton.h" > </File> diff --git a/base/build/base_unittests.vcproj b/base/build/base_unittests.vcproj index 29997f4..fa32036 100644 --- a/base/build/base_unittests.vcproj +++ b/base/build/base_unittests.vcproj @@ -168,10 +168,6 @@ > </File> <File - RelativePath="..\call_wrapper_unittest.cc" - > - </File> - <File RelativePath="..\clipboard_unittest.cc" > </File> @@ -268,10 +264,6 @@ > </File> <File - RelativePath="..\simple_thread_unittest.cc" - > - </File> - <File RelativePath="..\singleton_unittest.cc" > </File> diff --git a/base/call_wrapper.h b/base/call_wrapper.h deleted file mode 100644 index ae9738d0..0000000 --- a/base/call_wrapper.h +++ /dev/null @@ -1,239 +0,0 @@ -// Copyright 2008, Google Inc. -// All rights reserved. -// -// Redistribution and use in source and binary forms, with or without -// modification, are permitted provided that the following conditions are -// met: -// -// * Redistributions of source code must retain the above copyright -// notice, this list of conditions and the following disclaimer. -// * Redistributions in binary form must reproduce the above -// copyright notice, this list of conditions and the following disclaimer -// in the documentation and/or other materials provided with the -// distribution. -// * Neither the name of Google Inc. nor the names of its -// contributors may be used to endorse or promote products derived from -// this software without specific prior written permission. -// -// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS -// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT -// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR -// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT -// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, -// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT -// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, -// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY -// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - -#ifndef BASE_CALL_WRAPPER_H_ -#define BASE_CALL_WRAPPER_H_ - -// NOTE: If you want a callback that takes at-call-time parameters, you should -// use Callback (see task.h). CallWrapper only supports creation-time binding. -// -// A function / method call invocation wrapper. This creates a "closure" of -// sorts, storing a function pointer (or method pointer), along with a possible -// list of arguments. The objects have a single method Run(), which will call -// the function / method with the arguments unpacked. The arguments to the -// wrapped call are supplied at CallWrapper creation time, and no arguments can -// be supplied to the Run() method. -// -// All wrappers should be constructed through the two factory methods: -// CallWrapper* NewFunctionCallWrapper(funcptr, ...); -// CallWrapper* NewMethodCallWrapper(obj, &Klass::Method, ...); -// -// The classes memory should be managed like any other dynamically allocated -// object, and deleted when no longer in use. It is of course wrong to destroy -// the object while a Run() is in progress, or to call Run() after the object -// has been destroyed. Arguments are copied by value, and kept within the -// CallWrapper object. You should only pass-by-value simple parameters, or -// pointers to more complex types. If your parameters need custom memory -// management, then you should probably do this at the same point you destroy -// the CallWrapper. You should be aware of how your CallWrapper is used, and -// it is valid for Run() to be called 0 or more times. -// -// Some example usage: -// CallWrapper* wrapper = NewFunctionCallWrapper(&my_func); -// wrapper->Run(); // my_func(); -// delete wrapper; -// -// scoped_ptr<CallWrapper> wrapper(NewFunctionCallWrapper(&my_func, 10)); -// wrapper->Run(); // my_func(10); -// -// MyObject obj; -// scoped_ptr<CallWrapper> wrapper( -// NewMethodCallWrapper(&obj, &MyObject::Foo, 1, 2)); -// wrapper->Run(); // obj->Foo(1, 2); - -#include "base/tuple.h" - -namespace base { - -class CallWrapper { - public: - virtual ~CallWrapper() { } - virtual void Run() = 0; - protected: - CallWrapper() { } -}; - -// Wraps a function / static method call. -template <typename FuncType, typename TupleType> -class FunctionCallWrapper : public CallWrapper { - public: - FunctionCallWrapper(FuncType func, TupleType params) - : func_(func), params_(params) { } - virtual void Run() { - DispatchToFunction(func_, params_); - } - private: - FuncType func_; - // We copy from the const& constructor argument to our own copy here. - TupleType params_; -}; - -// Wraps a method invocation on an object. -template <typename ObjType, typename MethType, typename TupleType> -class MethodCallWrapper : public CallWrapper { - public: - MethodCallWrapper(ObjType* obj, MethType meth, TupleType params) - : obj_(obj), meth_(meth), params_(params) { } - virtual void Run() { - DispatchToMethod(obj_, meth_, params_); - } - private: - ObjType* obj_; - MethType meth_; - // We copy from the const& constructor argument to our own copy here. - TupleType params_; -}; - -// Factory functions, conveniently creating a Tuple for 0-5 arguments. -template <typename FuncType> -inline CallWrapper* NewFunctionCallWrapper(FuncType func) { - typedef Tuple0 TupleType; - return new FunctionCallWrapper<FuncType, TupleType>( - func, MakeTuple()); -} - -template <typename FuncType, typename Arg0Type> -inline CallWrapper* NewFunctionCallWrapper(FuncType func, - const Arg0Type& arg0) { - typedef Tuple1<Arg0Type> TupleType; - return new FunctionCallWrapper<FuncType, TupleType>( - func, MakeTuple(arg0)); -} - -template <typename FuncType, typename Arg0Type, typename Arg1Type> -inline CallWrapper* NewFunctionCallWrapper(FuncType func, - const Arg0Type& arg0, - const Arg1Type& arg1) { - typedef Tuple2<Arg0Type, Arg1Type> TupleType; - return new FunctionCallWrapper<FuncType, TupleType>( - func, MakeTuple(arg0, arg1)); -} - -template <typename FuncType, typename Arg0Type, typename Arg1Type, - typename Arg2Type> -inline CallWrapper* NewFunctionCallWrapper(FuncType func, - const Arg0Type& arg0, - const Arg1Type& arg1, - const Arg2Type& arg2) { - typedef Tuple3<Arg0Type, Arg1Type, Arg2Type> TupleType; - return new FunctionCallWrapper<FuncType, TupleType>( - func, MakeTuple(arg0, arg1, arg2)); -} - -template <typename FuncType, typename Arg0Type, typename Arg1Type, - typename Arg2Type, typename Arg3Type> -inline CallWrapper* NewFunctionCallWrapper(FuncType func, - const Arg0Type& arg0, - const Arg1Type& arg1, - const Arg2Type& arg2, - const Arg3Type& arg3) { - typedef Tuple4<Arg0Type, Arg1Type, Arg2Type, Arg3Type> TupleType; - return new FunctionCallWrapper<FuncType, TupleType>( - func, MakeTuple(arg0, arg1, arg2, arg3)); -} - -template <typename FuncType, typename Arg0Type, typename Arg1Type, - typename Arg2Type, typename Arg3Type, typename Arg4Type> -inline CallWrapper* NewFunctionCallWrapper(FuncType func, - const Arg0Type& arg0, - const Arg1Type& arg1, - const Arg2Type& arg2, - const Arg3Type& arg3, - const Arg4Type& arg4) { - typedef Tuple5<Arg0Type, Arg1Type, Arg2Type, Arg3Type, Arg4Type> TupleType; - return new FunctionCallWrapper<FuncType, TupleType>( - func, MakeTuple(arg0, arg1, arg2, arg3, arg4)); -} - - -template <typename ObjType, typename MethType> -inline CallWrapper* NewMethodCallWrapper(ObjType* obj, MethType meth) { - typedef Tuple0 TupleType; - return new MethodCallWrapper<ObjType, MethType, TupleType>( - obj, meth, MakeTuple()); -} - -template <typename ObjType, typename MethType, typename Arg0Type> -inline CallWrapper* NewMethodCallWrapper(ObjType* obj, MethType meth, - const Arg0Type& arg0) { - typedef Tuple1<Arg0Type> TupleType; - return new MethodCallWrapper<ObjType, MethType, TupleType>( - obj, meth, MakeTuple(arg0)); -} - -template <typename ObjType, typename MethType, typename Arg0Type, - typename Arg1Type> -inline CallWrapper* NewMethodCallWrapper(ObjType* obj, MethType meth, - const Arg0Type& arg0, - const Arg1Type& arg1) { - typedef Tuple2<Arg0Type, Arg1Type> TupleType; - return new MethodCallWrapper<ObjType, MethType, TupleType>( - obj, meth, MakeTuple(arg0, arg1)); -} - -template <typename ObjType, typename MethType, typename Arg0Type, - typename Arg1Type, typename Arg2Type> -inline CallWrapper* NewMethodCallWrapper(ObjType* obj, MethType meth, - const Arg0Type& arg0, - const Arg1Type& arg1, - const Arg2Type& arg2) { - typedef Tuple3<Arg0Type, Arg1Type, Arg2Type> TupleType; - return new MethodCallWrapper<ObjType, MethType, TupleType>( - obj, meth, MakeTuple(arg0, arg1, arg2)); -} - -template <typename ObjType, typename MethType, typename Arg0Type, - typename Arg1Type, typename Arg2Type, typename Arg3Type> -inline CallWrapper* NewMethodCallWrapper(ObjType* obj, MethType meth, - const Arg0Type& arg0, - const Arg1Type& arg1, - const Arg2Type& arg2, - const Arg3Type& arg3) { - typedef Tuple4<Arg0Type, Arg1Type, Arg2Type, Arg3Type> TupleType; - return new MethodCallWrapper<ObjType, MethType, TupleType>( - obj, meth, MakeTuple(arg0, arg1, arg2, arg3)); -} - -template <typename ObjType, typename MethType, typename Arg0Type, - typename Arg1Type, typename Arg2Type, typename Arg3Type, - typename Arg4Type> -inline CallWrapper* NewMethodCallWrapper(ObjType* obj, MethType meth, - const Arg0Type& arg0, - const Arg1Type& arg1, - const Arg2Type& arg2, - const Arg3Type& arg3, - const Arg4Type& arg4) { - typedef Tuple5<Arg0Type, Arg1Type, Arg2Type, Arg3Type, Arg4Type> TupleType; - return new MethodCallWrapper<ObjType, MethType, TupleType>( - obj, meth, MakeTuple(arg0, arg1, arg2, arg3, arg4)); -} - -} // namespace base - -#endif // BASE_CALL_WRAPPER_H_ diff --git a/base/call_wrapper_unittest.cc b/base/call_wrapper_unittest.cc deleted file mode 100644 index b1e2518..0000000 --- a/base/call_wrapper_unittest.cc +++ /dev/null @@ -1,204 +0,0 @@ -// Copyright 2008, Google Inc. -// All rights reserved. -// -// Redistribution and use in source and binary forms, with or without -// modification, are permitted provided that the following conditions are -// met: -// -// * Redistributions of source code must retain the above copyright -// notice, this list of conditions and the following disclaimer. -// * Redistributions in binary form must reproduce the above -// copyright notice, this list of conditions and the following disclaimer -// in the documentation and/or other materials provided with the -// distribution. -// * Neither the name of Google Inc. nor the names of its -// contributors may be used to endorse or promote products derived from -// this software without specific prior written permission. -// -// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS -// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT -// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR -// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT -// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, -// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT -// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, -// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY -// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - -#include "base/call_wrapper.h" -#include "base/scoped_ptr.h" -#include "testing/gtest/include/gtest/gtest.h" - -namespace { - -int global_int = 0; - -void SetGlobalInt5() { - global_int = 5; -} -void SetGlobalInt(int x) { - global_int = x; -} -void SetInt(int* p, int x) { - *p = x; -} -void SetIntAdd2(int* p, int x, int y) { - *p = x + y; -} -void SetIntAdd3(int* p, int x, int y, int z) { - *p = x + y + z; -} -void SetIntAdd4(int* p, int x, int y, int z, int w) { - *p = x + y + z + w; -} - -} // namespace - -TEST(CallWrapperTest, FunctionCall) { - // Function call with 0 arguments. - { - EXPECT_EQ(0, global_int); - scoped_ptr<base::CallWrapper> wrapper( - base::NewFunctionCallWrapper(&SetGlobalInt5)); - - EXPECT_EQ(0, global_int); - wrapper->Run(); - EXPECT_EQ(5, global_int); - - global_int = 0; - wrapper->Run(); - EXPECT_EQ(5, global_int); - } - // Function call with 1 argument. - { - EXPECT_EQ(5, global_int); - scoped_ptr<base::CallWrapper> wrapper( - base::NewFunctionCallWrapper(&SetGlobalInt, 0)); - - EXPECT_EQ(5, global_int); - wrapper->Run(); - EXPECT_EQ(0, global_int); - - global_int = 5; - wrapper->Run(); - EXPECT_EQ(0, global_int); - } - // Function call with 2 arguments. - { - int stack_int = 4; - scoped_ptr<base::CallWrapper> wrapper; - - wrapper.reset(base::NewFunctionCallWrapper(&SetInt, &global_int, 8)); - EXPECT_EQ(4, stack_int); - EXPECT_EQ(0, global_int); - wrapper->Run(); - EXPECT_EQ(4, stack_int); - EXPECT_EQ(8, global_int); - - wrapper.reset(base::NewFunctionCallWrapper(&SetInt, &stack_int, 8)); - EXPECT_EQ(4, stack_int); - EXPECT_EQ(8, global_int); - wrapper->Run(); - EXPECT_EQ(8, stack_int); - EXPECT_EQ(8, global_int); - } - // Function call with 3-5 arguments. - { - int stack_int = 12; - scoped_ptr<base::CallWrapper> wrapper; - - wrapper.reset( - base::NewFunctionCallWrapper(&SetIntAdd2, &stack_int, 1, 6)); - EXPECT_EQ(12, stack_int); - wrapper->Run(); - EXPECT_EQ(7, stack_int); - - wrapper.reset( - base::NewFunctionCallWrapper(&SetIntAdd3, &stack_int, 1, 6, 2)); - EXPECT_EQ(7, stack_int); - wrapper->Run(); - EXPECT_EQ(9, stack_int); - - wrapper.reset( - base::NewFunctionCallWrapper(&SetIntAdd4, &stack_int, 1, 6, 2, 3)); - EXPECT_EQ(9, stack_int); - wrapper->Run(); - EXPECT_EQ(12, stack_int); - - global_int = 2; - wrapper->Run(); - EXPECT_EQ(12, stack_int); - } -} - -namespace { - -class Incrementer { - public: - Incrementer(int* ptr) : ptr_(ptr) { } - void IncrementBy(int x) { *ptr_ += x; } - void Increment() { IncrementBy(1); } - void SetIntAdd2(int x, int y) { *ptr_ = x + y; } - void SetIntAdd3(int x, int y, int z) { *ptr_ = x + y + z; } - void SetIntAdd4(int x, int y, int z, int w) { *ptr_ = x + y + z + w; } - private: - int* ptr_; -}; - -} // namespace - -TEST(CallWrapperTest, MethodCall) { - // Method call with 0 and 1 arguments. - { - int stack_int = 0; - Incrementer incr(&stack_int); - scoped_ptr<base::CallWrapper> wrapper; - - wrapper.reset( - base::NewMethodCallWrapper(&incr, &Incrementer::Increment)); - EXPECT_EQ(0, stack_int); - wrapper->Run(); - EXPECT_EQ(1, stack_int); - - wrapper.reset( - base::NewMethodCallWrapper(&incr, &Incrementer::IncrementBy, 10)); - EXPECT_EQ(1, stack_int); - wrapper->Run(); - EXPECT_EQ(11, stack_int); - wrapper->Run(); - EXPECT_EQ(21, stack_int); - wrapper->Run(); - EXPECT_EQ(31, stack_int); - } - // Method call with 2-5 arguments. - { - int stack_int = 0; - Incrementer incr(&stack_int); - scoped_ptr<base::CallWrapper> wrapper; - - wrapper.reset( - base::NewMethodCallWrapper(&incr, &Incrementer::SetIntAdd2, 1, 5)); - EXPECT_EQ(0, stack_int); - wrapper->Run(); - EXPECT_EQ(6, stack_int); - - wrapper.reset( - base::NewMethodCallWrapper(&incr, &Incrementer::SetIntAdd3, 1, 5, 7)); - EXPECT_EQ(6, stack_int); - wrapper->Run(); - EXPECT_EQ(13, stack_int); - - wrapper.reset( - base::NewMethodCallWrapper(&incr, - &Incrementer::SetIntAdd4, 1, 5, 7, 2)); - EXPECT_EQ(13, stack_int); - wrapper->Run(); - EXPECT_EQ(15, stack_int); - - stack_int = 2; - wrapper->Run(); - EXPECT_EQ(15, stack_int); - } -} diff --git a/base/simple_thread.cc b/base/simple_thread.cc deleted file mode 100644 index 1d88dada..0000000 --- a/base/simple_thread.cc +++ /dev/null @@ -1,78 +0,0 @@ -// Copyright 2008, Google Inc. -// All rights reserved. -// -// Redistribution and use in source and binary forms, with or without -// modification, are permitted provided that the following conditions are -// met: -// -// * Redistributions of source code must retain the above copyright -// notice, this list of conditions and the following disclaimer. -// * Redistributions in binary form must reproduce the above -// copyright notice, this list of conditions and the following disclaimer -// in the documentation and/or other materials provided with the -// distribution. -// * Neither the name of Google Inc. nor the names of its -// contributors may be used to endorse or promote products derived from -// this software without specific prior written permission. -// -// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS -// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT -// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR -// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT -// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, -// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT -// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, -// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY -// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - -#include "base/simple_thread.h" - -#include "base/call_wrapper.h" -#include "base/waitable_event.h" -#include "base/logging.h" -#include "base/platform_thread.h" -#include "base/string_util.h" - -namespace base { - -void SimpleThread::Start() { - DCHECK(!HasBeenStarted()) << "Tried to Start a thread multiple times."; - bool success = PlatformThread::Create(options_.stack_size(), this, &thread_); - CHECK(success); - event_.Wait(); // Wait for the thread to complete initialization. -} - -void SimpleThread::Join() { - DCHECK(HasBeenStarted()) << "Tried to Join a never-started thread."; - DCHECK(!HasBeenJoined()) << "Tried to Join a thread multiple times."; - PlatformThread::Join(thread_); - joined_ = true; -} - -void SimpleThread::ThreadMain() { - tid_ = PlatformThread::CurrentId(); - // Construct our full name of the form "name_prefix_/TID". - name_.push_back('/'); - name_.append(IntToString(tid_)); - PlatformThread::SetName(tid_, name_.c_str()); - - // We've initialized our new thread, signal that we're done to Start(). - event_.Signal(); - - Run(); -} - -SimpleThread::~SimpleThread() { - DCHECK(HasBeenStarted()) << "SimpleThread was never started."; - DCHECK(HasBeenJoined()) << "SimpleThread destroyed without being Join()ed."; -} - -void CallWrapperSimpleThread::Run() { - DCHECK(wrapper_) << "Tried to call Run without a wrapper (called twice?)"; - wrapper_->Run(); - wrapper_ = NULL; -} - -} // namespace base diff --git a/base/simple_thread.h b/base/simple_thread.h deleted file mode 100644 index 77754e9..0000000 --- a/base/simple_thread.h +++ /dev/null @@ -1,163 +0,0 @@ -// Copyright 2008, Google Inc. -// All rights reserved. -// -// Redistribution and use in source and binary forms, with or without -// modification, are permitted provided that the following conditions are -// met: -// -// * Redistributions of source code must retain the above copyright -// notice, this list of conditions and the following disclaimer. -// * Redistributions in binary form must reproduce the above -// copyright notice, this list of conditions and the following disclaimer -// in the documentation and/or other materials provided with the -// distribution. -// * Neither the name of Google Inc. nor the names of its -// contributors may be used to endorse or promote products derived from -// this software without specific prior written permission. -// -// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS -// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT -// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR -// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT -// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, -// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT -// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, -// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY -// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - -// WARNING: You should probably be using Thread (thread.h) instead. Thread is -// Chrome's message-loop based Thread abstraction, and if you are a -// thread running in the browser, there will likely be assumptions -// that your thread will have an associated message loop. -// -// This is a simple thread interface that backs to a native operating system -// thread. You should use this only when you want a thread that does not have -// an associated MessageLoop. Unittesting is the best example of this. -// -// The simplest interface to use is CallWrapperSimpleThread, which will create -// a new thread, and execute the CallWrapper in this new thread until it has -// completed, exiting the thread. See call_wrapper.h for that interface. -// -// NOTE: You *MUST* call Join on the thread to clean up the underlying thread -// resources. You are also responsible for destructing the SimpleThread object. -// It is invalid to destroy a SimpleThread while it is running, or without -// Start() having been called (and a thread never created). The CallWrapper -// object should live as long as a CallWrapperSimpleThread. -// -// Thread Safety: A SimpleThread is not completely thread safe. It is safe to -// access it from the creating thread or from the newly created thread. This -// implies that the creator thread should be the thread that calls Join. -// -// Example: -// scoped_ptr<CallWrapper> wrapper(NewMethodCallWrapper(obj, &Foo::Main)); -// scoped_ptr<SimpleThread> thread(new CallWrapperSimpleThread(wrapper)); -// thread->Start(); -// // Start will return after the Thread has been successfully started and -// // initialized. The newly created thread will invoke obj->Main, and run -// // until it returns. The CallWrapper will then delete itself. -// thread->Join(); // Wait until the thread has exited. You MUST Join! -// // The SimpleThread object is still valid, however you may not call Join -// // or Start again. In this example the scopers will destroy the objects. - -#ifndef BASE_SIMPLE_THREAD_H_ -#define BASE_SIMPLE_THREAD_H_ - -#include <string> - -#include "base/basictypes.h" -#include "base/waitable_event.h" -#include "base/platform_thread.h" - -namespace base { - -class CallWrapper; - -// This is the base SimpleThread. You can derive from it and implement the -// virtual Run method, or you can use the CallWrapperSimpleThread interface. -class SimpleThread : public PlatformThread::Delegate { - public: - class Options { - public: - Options() : stack_size_(0) { } - ~Options() { } - - // We use the standard compiler-supplied copy constructor. - - // A custom stack size, or 0 for the system default. - void set_stack_size(size_t size) { stack_size_ = size; } - size_t stack_size() const { return stack_size_; } - private: - size_t stack_size_; - }; - - // Create a SimpleThread. |options| should be used to manage any specific - // configuration involving the thread creation and management. - // Every thread has a name, in the form of |name_prefix|/TID, for example - // "my_thread/321". The thread will not be created until Start() is called. - SimpleThread(const Options& options, const std::string& name_prefix) - : name_prefix_(name_prefix), name_(name_prefix_), options_(options), - thread_(), event_(true, false), tid_(0), joined_(false) { } - - SimpleThread() - : name_prefix_("unnamed"), name_(name_prefix_), - thread_(), event_(true, false), tid_(0), joined_(false) { } - - virtual ~SimpleThread(); - - virtual void Start(); - virtual void Join(); - - // We follow the PlatformThread Delegate interface. - virtual void ThreadMain(); - - // Subclasses should override the Run method. - virtual void Run() = 0; - - // Return the thread name prefix, or "unnamed" if none was supplied. - std::string name_prefix() { return name_prefix_; } - - // Return the completed name including TID, only valid after Start(). - std::string name() { return name_; } - - // Return the thread id, only valid after Start(). - int tid() { return tid_; } - - // Return True if Start() has ever been called. - bool HasBeenStarted() { return event_.IsSignaled(); } - - // Return True if Join() has evern been called. - bool HasBeenJoined() { return joined_; } - - private: - const std::string name_prefix_; - std::string name_; - const Options options_; - PlatformThreadHandle thread_; // PlatformThread handle, invalid after Join! - WaitableEvent event_; // Signaled if Start() was ever called. - int tid_; // The backing thread's id. - bool joined_; // True if Join has been called. -}; - -class CallWrapperSimpleThread : public SimpleThread { - public: - typedef SimpleThread::Options Options; - - explicit CallWrapperSimpleThread(CallWrapper* wrapper) - : SimpleThread(), wrapper_(wrapper) { } - - CallWrapperSimpleThread(CallWrapper* wrapper, - const Options& options, - const std::string& name_prefix) - : SimpleThread(options, name_prefix), wrapper_(wrapper) { } - - virtual ~CallWrapperSimpleThread() { } - virtual void Run(); - private: - CallWrapper* wrapper_; -}; - -} // namespace base - -#endif // BASE_SIMPLE_THREAD_H_ diff --git a/base/simple_thread_unittest.cc b/base/simple_thread_unittest.cc deleted file mode 100644 index fcdabda..0000000 --- a/base/simple_thread_unittest.cc +++ /dev/null @@ -1,113 +0,0 @@ -// Copyright 2008, Google Inc. -// All rights reserved. -// -// Redistribution and use in source and binary forms, with or without -// modification, are permitted provided that the following conditions are -// met: -// -// * Redistributions of source code must retain the above copyright -// notice, this list of conditions and the following disclaimer. -// * Redistributions in binary form must reproduce the above -// copyright notice, this list of conditions and the following disclaimer -// in the documentation and/or other materials provided with the -// distribution. -// * Neither the name of Google Inc. nor the names of its -// contributors may be used to endorse or promote products derived from -// this software without specific prior written permission. -// -// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS -// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT -// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR -// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT -// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, -// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT -// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, -// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY -// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - -#include <vector> - -#include "base/call_wrapper.h" -#include "base/scoped_ptr.h" -#include "base/simple_thread.h" -#include "base/string_util.h" -#include "base/waitable_event.h" -#include "testing/gtest/include/gtest/gtest.h" - -namespace { - -void SetInt(int* p, int x) { - *p = x; -} - -void SignalEvent(base::WaitableEvent* event) { - EXPECT_FALSE(event->IsSignaled()); - event->Signal(); - EXPECT_TRUE(event->IsSignaled()); -} - -} // namespace - -TEST(SimpleThreadTest, CreateAndJoin) { - int stack_int = 0; - - scoped_ptr<base::CallWrapper> wrapper( - base::NewFunctionCallWrapper(SetInt, &stack_int, 7)); - EXPECT_EQ(0, stack_int); - scoped_ptr<base::SimpleThread> thread( - new base::CallWrapperSimpleThread(wrapper.get())); - EXPECT_FALSE(thread->HasBeenStarted()); - EXPECT_FALSE(thread->HasBeenJoined()); - EXPECT_EQ(0, stack_int); - - thread->Start(); - EXPECT_TRUE(thread->HasBeenStarted()); - EXPECT_FALSE(thread->HasBeenJoined()); - - thread->Join(); - EXPECT_TRUE(thread->HasBeenStarted()); - EXPECT_TRUE(thread->HasBeenJoined()); - EXPECT_EQ(7, stack_int); -} - -TEST(SimpleThreadTest, WaitForEvent) { - // Create a thread, and wait for it to signal us. - base::WaitableEvent event(true, false); - - scoped_ptr<base::CallWrapper> wrapper( - base::NewFunctionCallWrapper(SignalEvent, &event)); - scoped_ptr<base::SimpleThread> thread( - new base::CallWrapperSimpleThread(wrapper.get())); - - EXPECT_FALSE(event.IsSignaled()); - thread->Start(); - event.Wait(); - EXPECT_TRUE(event.IsSignaled()); - thread->Join(); -} - -TEST(SimpleThreadTest, Named) { - base::WaitableEvent event(true, false); - - base::SimpleThread::Options options; - scoped_ptr<base::CallWrapper> wrapper( - base::NewFunctionCallWrapper(SignalEvent, &event)); - scoped_ptr<base::SimpleThread> thread(new base::CallWrapperSimpleThread( - wrapper.get(), options, "testy")); - EXPECT_EQ(thread->name_prefix(), "testy"); - EXPECT_FALSE(event.IsSignaled()); - - thread->Start(); - EXPECT_EQ(thread->name_prefix(), "testy"); - EXPECT_EQ(thread->name(), std::string("testy/") + IntToString(thread->tid())); - event.Wait(); - - EXPECT_TRUE(event.IsSignaled()); - thread->Join(); - - // We keep the name and tid, even after the thread is gone. - EXPECT_EQ(thread->name_prefix(), "testy"); - EXPECT_EQ(thread->name(), std::string("testy/") + IntToString(thread->tid())); -} |