1 // Copyright (c) 2011 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 // WARNING: You should *NOT* be using this class directly. PlatformThread is
6 // the low-level platform-specific abstraction to the OS's threading interface.
7 // You should instead be using a message-loop driven Thread, see thread.h.
9 #ifndef BASE_THREADING_PLATFORM_THREAD_H_
10 #define BASE_THREADING_PLATFORM_THREAD_H_
13 #include "base/base_export.h"
14 #include "base/basictypes.h"
15 #include "build/build_config.h"
19 #elif defined(OS_POSIX)
21 #if defined(OS_MACOSX)
22 #include <mach/mach.h>
23 #else // OS_POSIX && !OS_MACOSX
30 // PlatformThreadHandle should not be assumed to be a numeric type, since the
31 // standard intends to allow pthread_t to be a structure. This means you
32 // should not initialize it to a value, like 0. If it's a member variable, the
33 // constructor can safely "value initialize" using () in the initializer list.
35 typedef DWORD PlatformThreadId;
36 typedef void* PlatformThreadHandle; // HANDLE
37 const PlatformThreadHandle kNullThreadHandle = NULL;
38 #elif defined(OS_POSIX)
39 typedef pthread_t PlatformThreadHandle;
40 const PlatformThreadHandle kNullThreadHandle = 0;
41 #if defined(OS_MACOSX)
42 typedef mach_port_t PlatformThreadId;
43 #else // OS_POSIX && !OS_MACOSX
44 typedef pid_t PlatformThreadId;
48 const PlatformThreadId kInvalidThreadId = 0;
50 // Valid values for SetThreadPriority()
52 kThreadPriority_Normal,
53 // Suitable for low-latency, glitch-resistant audio.
54 kThreadPriority_RealtimeAudio
57 // A namespace for low-level thread functions.
58 class BASE_EXPORT PlatformThread {
60 // Implement this interface to run code on a background thread. Your
61 // ThreadMain method will be called on the newly created thread.
62 class BASE_EXPORT Delegate {
64 virtual ~Delegate() {}
65 virtual void ThreadMain() = 0;
68 // Gets the current thread id, which may be useful for logging purposes.
69 static PlatformThreadId CurrentId();
71 // Yield the current thread so another thread can be scheduled.
72 static void YieldCurrentThread();
74 // Sleeps for the specified duration (units are milliseconds).
75 static void Sleep(int duration_ms);
77 // Sets the thread name visible to debuggers/tools. This has no effect
78 // otherwise. This name pointer is not copied internally. Thus, it must stay
79 // valid until the thread ends.
80 static void SetName(const char* name);
82 // Gets the thread name, if previously set by SetName.
83 static const char* GetName();
85 // Creates a new thread. The |stack_size| parameter can be 0 to indicate
86 // that the default stack size should be used. Upon success,
87 // |*thread_handle| will be assigned a handle to the newly created thread,
88 // and |delegate|'s ThreadMain method will be executed on the newly created
90 // NOTE: When you are done with the thread handle, you must call Join to
91 // release system resources associated with the thread. You must ensure that
92 // the Delegate object outlives the thread.
93 static bool Create(size_t stack_size, Delegate* delegate,
94 PlatformThreadHandle* thread_handle);
96 // CreateNonJoinable() does the same thing as Create() except the thread
97 // cannot be Join()'d. Therefore, it also does not output a
98 // PlatformThreadHandle.
99 static bool CreateNonJoinable(size_t stack_size, Delegate* delegate);
101 // Joins with a thread created via the Create function. This function blocks
102 // the caller until the designated thread exits. This will invalidate
104 static void Join(PlatformThreadHandle thread_handle);
106 static void SetThreadPriority(PlatformThreadHandle handle,
107 ThreadPriority priority);
110 DISALLOW_IMPLICIT_CONSTRUCTORS(PlatformThread);
115 #endif // BASE_THREADING_PLATFORM_THREAD_H_