1 // Copyright (c) 2012 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_
12 #include "base/base_export.h"
13 #include "base/basictypes.h"
14 #include "base/time.h"
15 #include "build/build_config.h"
19 #elif defined(OS_POSIX)
26 // PlatformThreadHandle should not be assumed to be a numeric type, since the
27 // standard intends to allow pthread_t to be a structure. This means you
28 // should not initialize it to a value, like 0. If it's a member variable, the
29 // constructor can safely "value initialize" using () in the initializer list.
31 typedef DWORD PlatformThreadId
;
32 typedef void* PlatformThreadHandle
; // HANDLE
33 const PlatformThreadHandle kNullThreadHandle
= NULL
;
34 #elif defined(OS_POSIX)
35 typedef pthread_t PlatformThreadHandle
;
36 const PlatformThreadHandle kNullThreadHandle
= 0;
37 typedef pid_t PlatformThreadId
;
40 const PlatformThreadId kInvalidThreadId
= 0;
42 // Valid values for SetThreadPriority()
44 kThreadPriority_Normal
,
45 // Suitable for low-latency, glitch-resistant audio.
46 kThreadPriority_RealtimeAudio
49 // A namespace for low-level thread functions.
50 class BASE_EXPORT PlatformThread
{
52 // Implement this interface to run code on a background thread. Your
53 // ThreadMain method will be called on the newly created thread.
54 class BASE_EXPORT Delegate
{
56 virtual void ThreadMain() = 0;
59 virtual ~Delegate() {}
62 // Gets the current thread id, which may be useful for logging purposes.
63 static PlatformThreadId
CurrentId();
65 // Yield the current thread so another thread can be scheduled.
66 static void YieldCurrentThread();
68 // Sleeps for the specified duration.
69 static void Sleep(base::TimeDelta duration
);
71 // Sets the thread name visible to debuggers/tools. This has no effect
72 // otherwise. This name pointer is not copied internally. Thus, it must stay
73 // valid until the thread ends.
74 static void SetName(const char* name
);
76 // Gets the thread name, if previously set by SetName.
77 static const char* GetName();
79 // Creates a new thread. The |stack_size| parameter can be 0 to indicate
80 // that the default stack size should be used. Upon success,
81 // |*thread_handle| will be assigned a handle to the newly created thread,
82 // and |delegate|'s ThreadMain method will be executed on the newly created
84 // NOTE: When you are done with the thread handle, you must call Join to
85 // release system resources associated with the thread. You must ensure that
86 // the Delegate object outlives the thread.
87 static bool Create(size_t stack_size
, Delegate
* delegate
,
88 PlatformThreadHandle
* thread_handle
);
90 // CreateWithPriority() does the same thing as Create() except the priority of
91 // the thread is set based on |priority|. Can be used in place of Create()
92 // followed by SetThreadPriority(). SetThreadPriority() has not been
93 // implemented on the Linux platform yet, this is the only way to get a high
94 // priority thread on Linux.
95 static bool CreateWithPriority(size_t stack_size
, Delegate
* delegate
,
96 PlatformThreadHandle
* thread_handle
,
97 ThreadPriority priority
);
99 // CreateNonJoinable() does the same thing as Create() except the thread
100 // cannot be Join()'d. Therefore, it also does not output a
101 // PlatformThreadHandle.
102 static bool CreateNonJoinable(size_t stack_size
, Delegate
* delegate
);
104 // Joins with a thread created via the Create function. This function blocks
105 // the caller until the designated thread exits. This will invalidate
107 static void Join(PlatformThreadHandle thread_handle
);
109 // Sets the priority of the thread specified in |handle| to |priority|.
110 // This does not work on Linux, use CreateWithPriority() instead.
111 static void SetThreadPriority(PlatformThreadHandle handle
,
112 ThreadPriority priority
);
115 DISALLOW_IMPLICIT_CONSTRUCTORS(PlatformThread
);
120 #endif // BASE_THREADING_PLATFORM_THREAD_H_