123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231 |
- #ifndef BASE_THREADING_SIMPLE_THREAD_H_
- #define BASE_THREADING_SIMPLE_THREAD_H_
- #include <stddef.h>
- #include <string>
- #include <vector>
- #include "base/base_export.h"
- #include "base/compiler_specific.h"
- #include "base/containers/queue.h"
- #include "base/macros.h"
- #include "base/synchronization/lock.h"
- #include "base/synchronization/waitable_event.h"
- #include "base/threading/platform_thread.h"
- namespace base {
- class BASE_EXPORT SimpleThread : public PlatformThread::Delegate {
- public:
- struct BASE_EXPORT Options {
- public:
- Options() = default;
- explicit Options(ThreadPriority priority_in) : priority(priority_in) {}
- ~Options() = default;
-
- Options(const Options& other) = default;
- Options& operator=(const Options& other) = default;
-
- size_t stack_size = 0;
- ThreadPriority priority = ThreadPriority::NORMAL;
-
-
-
-
- bool joinable = true;
- };
-
-
-
-
- explicit SimpleThread(const std::string& name);
- SimpleThread(const std::string& name, const Options& options);
- ~SimpleThread() override;
-
-
- void Start();
-
-
- void Join();
-
-
-
- void StartAsync();
-
- virtual void Run() = 0;
-
-
-
-
- PlatformThreadId tid();
-
-
-
-
- bool HasBeenStarted();
-
- bool HasBeenJoined() const { return joined_; }
-
- bool HasStartBeenAttempted() { return start_called_; }
-
- void ThreadMain() override;
- private:
-
-
- virtual void BeforeStart() {}
-
-
- virtual void BeforeRun() {}
-
-
- virtual void BeforeJoin() {}
- const std::string name_;
- const Options options_;
- PlatformThreadHandle thread_;
- WaitableEvent event_;
- PlatformThreadId tid_ = kInvalidThreadId;
- bool joined_ = false;
-
- bool start_called_ = false;
- DISALLOW_COPY_AND_ASSIGN(SimpleThread);
- };
- class BASE_EXPORT DelegateSimpleThread : public SimpleThread {
- public:
- class BASE_EXPORT Delegate {
- public:
- virtual ~Delegate() = default;
- virtual void Run() = 0;
- };
- DelegateSimpleThread(Delegate* delegate,
- const std::string& name_prefix);
- DelegateSimpleThread(Delegate* delegate,
- const std::string& name_prefix,
- const Options& options);
- ~DelegateSimpleThread() override;
- void Run() override;
- private:
- Delegate* delegate_;
- DISALLOW_COPY_AND_ASSIGN(DelegateSimpleThread);
- };
- class BASE_EXPORT DelegateSimpleThreadPool
- : public DelegateSimpleThread::Delegate {
- public:
- typedef DelegateSimpleThread::Delegate Delegate;
- DelegateSimpleThreadPool(const std::string& name_prefix, int num_threads);
- ~DelegateSimpleThreadPool() override;
-
-
- void Start();
-
-
- void JoinAll();
-
-
- void AddWork(Delegate* work, int repeat_count);
- void AddWork(Delegate* work) {
- AddWork(work, 1);
- }
-
- void Run() override;
- private:
- const std::string name_prefix_;
- int num_threads_;
- std::vector<DelegateSimpleThread*> threads_;
- base::queue<Delegate*> delegates_;
- base::Lock lock_;
- WaitableEvent dry_;
- DISALLOW_COPY_AND_ASSIGN(DelegateSimpleThreadPool);
- };
- }
- #endif
|