1 : // Copyright (c) 2006-2008 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_PLATFORM_THREAD_H_
10 : #define BASE_PLATFORM_THREAD_H_
11 :
12 : #include "base/basictypes.h"
13 :
14 : // PlatformThreadHandle should not be assumed to be a numeric type, since the
15 : // standard intends to allow pthread_t to be a structure. This means you
16 : // should not initialize it to a value, like 0. If it's a member variable, the
17 : // constructor can safely "value initialize" using () in the initializer list.
18 : #if defined(OS_WIN)
19 : #include <windows.h>
20 : typedef DWORD PlatformThreadId;
21 : typedef void* PlatformThreadHandle; // HANDLE
22 : #elif defined(OS_POSIX)
23 : #include <pthread.h>
24 : typedef pthread_t PlatformThreadHandle;
25 : #if defined(OS_LINUX)
26 : #include <unistd.h>
27 : typedef pid_t PlatformThreadId;
28 : #elif defined(OS_MACOSX)
29 : #include <mach/mach.h>
30 : typedef mach_port_t PlatformThreadId;
31 : #endif
32 : #endif
33 :
34 : // A namespace for low-level thread functions.
35 : class PlatformThread {
36 : public:
37 : // Gets the current thread id, which may be useful for logging purposes.
38 : static PlatformThreadId CurrentId();
39 :
40 : // Yield the current thread so another thread can be scheduled.
41 : static void YieldCurrentThread();
42 :
43 : // Sleeps for the specified duration (units are milliseconds).
44 : static void Sleep(int duration_ms);
45 :
46 : // Sets the thread name visible to a debugger. This has no effect otherwise.
47 : static void SetName(const char* name);
48 :
49 : // Implement this interface to run code on a background thread. Your
50 : // ThreadMain method will be called on the newly created thread.
51 1420 : class Delegate {
52 : public:
53 2838 : virtual ~Delegate() {}
54 : virtual void ThreadMain() = 0;
55 : };
56 :
57 : // Creates a new thread. The |stack_size| parameter can be 0 to indicate
58 : // that the default stack size should be used. Upon success,
59 : // |*thread_handle| will be assigned a handle to the newly created thread,
60 : // and |delegate|'s ThreadMain method will be executed on the newly created
61 : // thread.
62 : // NOTE: When you are done with the thread handle, you must call Join to
63 : // release system resources associated with the thread. You must ensure that
64 : // the Delegate object outlives the thread.
65 : static bool Create(size_t stack_size, Delegate* delegate,
66 : PlatformThreadHandle* thread_handle);
67 :
68 : // CreateNonJoinable() does the same thing as Create() except the thread
69 : // cannot be Join()'d. Therefore, it also does not output a
70 : // PlatformThreadHandle.
71 : static bool CreateNonJoinable(size_t stack_size, Delegate* delegate);
72 :
73 : // Joins with a thread created via the Create function. This function blocks
74 : // the caller until the designated thread exits. This will invalidate
75 : // |thread_handle|.
76 : static void Join(PlatformThreadHandle thread_handle);
77 :
78 : private:
79 : DISALLOW_IMPLICIT_CONSTRUCTORS(PlatformThread);
80 : };
81 :
82 : #endif // BASE_PLATFORM_THREAD_H_
|