cc: Make picture pile base thread safe.
[chromium-blink-merge.git] / mojo / public / cpp / utility / thread.h
blobb7d10ee7037e4e628edeaf5445c3e9aac8af459d
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.
5 #ifndef MOJO_PUBLIC_CPP_UTILITY_THREAD_H_
6 #define MOJO_PUBLIC_CPP_UTILITY_THREAD_H_
8 #ifdef _WIN32
9 #error "Not implemented: See crbug.com/342893."
10 #endif
12 #include <pthread.h>
13 #include <stddef.h>
15 #include "mojo/public/cpp/system/macros.h"
17 namespace mojo {
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:
27 Options() : stack_size_(0) {}
29 // A stack size of 0 means the default.
30 size_t stack_size() const { return stack_size_; }
31 void set_stack_size(size_t stack_size) { stack_size_ = stack_size; }
33 private:
34 size_t stack_size_;
36 // Copy and assign allowed.
39 // TODO(vtl): Add name or name prefix?
40 Thread();
41 explicit Thread(const Options& options);
42 virtual ~Thread();
44 void Start();
45 void Join();
47 virtual void Run() = 0;
49 private:
50 static void* ThreadRunTrampoline(void* arg);
52 const Options options_;
53 pthread_t thread_;
54 bool started_;
55 bool joined_;
57 MOJO_DISALLOW_COPY_AND_ASSIGN(Thread);
60 } // namespace mojo
62 #endif // MOJO_PUBLIC_CPP_UTILITY_THREAD_H_