Initialize Tizen 2.3
[external/chromium.git] / base / threading / platform_thread.h
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.
4
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.
8
9 #ifndef BASE_THREADING_PLATFORM_THREAD_H_
10 #define BASE_THREADING_PLATFORM_THREAD_H_
11 #pragma once
12
13 #include "base/base_export.h"
14 #include "base/basictypes.h"
15 #include "build/build_config.h"
16
17 #if defined(OS_WIN)
18 #include <windows.h>
19 #elif defined(OS_POSIX)
20 #include <pthread.h>
21 #if defined(OS_MACOSX)
22 #include <mach/mach.h>
23 #else  // OS_POSIX && !OS_MACOSX
24 #include <unistd.h>
25 #endif
26 #endif
27
28 namespace base {
29
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.
34 #if defined(OS_WIN)
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;
45 #endif
46 #endif
47
48 const PlatformThreadId kInvalidThreadId = 0;
49
50 // Valid values for SetThreadPriority()
51 enum ThreadPriority{
52   kThreadPriority_Normal,
53   // Suitable for low-latency, glitch-resistant audio.
54   kThreadPriority_RealtimeAudio
55 };
56
57 // A namespace for low-level thread functions.
58 class BASE_EXPORT PlatformThread {
59  public:
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 {
63    public:
64     virtual ~Delegate() {}
65     virtual void ThreadMain() = 0;
66   };
67
68   // Gets the current thread id, which may be useful for logging purposes.
69   static PlatformThreadId CurrentId();
70
71   // Yield the current thread so another thread can be scheduled.
72   static void YieldCurrentThread();
73
74   // Sleeps for the specified duration (units are milliseconds).
75   static void Sleep(int duration_ms);
76
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);
81
82   // Gets the thread name, if previously set by SetName.
83   static const char* GetName();
84
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
89   // thread.
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);
95
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);
100
101   // Joins with a thread created via the Create function.  This function blocks
102   // the caller until the designated thread exits.  This will invalidate
103   // |thread_handle|.
104   static void Join(PlatformThreadHandle thread_handle);
105
106   static void SetThreadPriority(PlatformThreadHandle handle,
107                                 ThreadPriority priority);
108
109  private:
110   DISALLOW_IMPLICIT_CONSTRUCTORS(PlatformThread);
111 };
112
113 }  // namespace base
114
115 #endif  // BASE_THREADING_PLATFORM_THREAD_H_