• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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
3 // found in the LICENSE file.
4 
5 #ifndef MOJO_PUBLIC_CPP_UTILITY_THREAD_H_
6 #define MOJO_PUBLIC_CPP_UTILITY_THREAD_H_
7 
8 #ifdef _WIN32
9 #error "Not implemented: See crbug.com/342893."
10 #endif
11 
12 #include <pthread.h>
13 #include <stddef.h>
14 
15 #include "mojo/public/cpp/system/macros.h"
16 
17 namespace mojo {
18 
19 // This class is thread-friendly, not thread-safe (e.g., you mustn't call
20 // |Join()| from multiple threads and/or simultaneously try to destroy the
21 // object).
22 class Thread {
23  public:
24   // TODO(vtl): Support non-joinable? priority?
25   class Options {
26    public:
Options()27     Options() : stack_size_(0) {}
28 
29     // A stack size of 0 means the default.
stack_size()30     size_t stack_size() const { return stack_size_; }
set_stack_size(size_t stack_size)31     void set_stack_size(size_t stack_size) { stack_size_ = stack_size; }
32 
33    private:
34     size_t stack_size_;
35 
36     // Copy and assign allowed.
37   };
38 
39   // TODO(vtl): Add name or name prefix?
40   Thread();
41   explicit Thread(const Options& options);
42   virtual ~Thread();
43 
44   void Start();
45   void Join();
46 
47   virtual void Run() = 0;
48 
49  private:
50   static void* ThreadRunTrampoline(void* arg);
51 
52   const Options options_;
53   pthread_t thread_;
54   bool started_;
55   bool joined_;
56 
57   MOJO_DISALLOW_COPY_AND_ASSIGN(Thread);
58 };
59 
60 }  // namespace mojo
61 
62 #endif  // MOJO_PUBLIC_CPP_UTILITY_THREAD_H_
63