Line data Source code
1 : /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 : /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 : // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved.
4 : // Use of this source code is governed by a BSD-style license that can be
5 : // found in the LICENSE file.
6 :
7 : // WARNING: You should *NOT* be using this class directly. PlatformThread is
8 : // the low-level platform-specific abstraction to the OS's threading interface.
9 : // You should instead be using a message-loop driven Thread, see thread.h.
10 :
11 : #ifndef BASE_PLATFORM_THREAD_H_
12 : #define BASE_PLATFORM_THREAD_H_
13 :
14 : #include "base/basictypes.h"
15 :
16 : // PlatformThreadHandle should not be assumed to be a numeric type, since the
17 : // standard intends to allow pthread_t to be a structure. This means you
18 : // should not initialize it to a value, like 0. If it's a member variable, the
19 : // constructor can safely "value initialize" using () in the initializer list.
20 : #if defined(OS_WIN)
21 : #include <windows.h>
22 : typedef DWORD PlatformThreadId;
23 : typedef void* PlatformThreadHandle; // HANDLE
24 : #elif defined(OS_POSIX)
25 : #include <pthread.h>
26 : typedef pthread_t PlatformThreadHandle;
27 : #if defined(OS_LINUX) || defined(OS_OPENBSD) || defined(OS_SOLARIS) || defined(__GLIBC__)
28 : #include <unistd.h>
29 : typedef pid_t PlatformThreadId;
30 : #elif defined(OS_BSD)
31 : #include <sys/types.h>
32 : typedef lwpid_t PlatformThreadId;
33 : #elif defined(OS_MACOSX)
34 : #include <mach/mach.h>
35 : typedef mach_port_t PlatformThreadId;
36 : #endif
37 : #endif
38 :
39 : // A namespace for low-level thread functions.
40 : class PlatformThread {
41 : public:
42 : // Gets the current thread id, which may be useful for logging purposes.
43 : static PlatformThreadId CurrentId();
44 :
45 : // Yield the current thread so another thread can be scheduled.
46 : static void YieldCurrentThread();
47 :
48 : // Sleeps for the specified duration (units are milliseconds).
49 : static void Sleep(int duration_ms);
50 :
51 : // Sets the thread name visible to a debugger. This has no effect otherwise.
52 : static void SetName(const char* name);
53 :
54 : // Implement this interface to run code on a background thread. Your
55 : // ThreadMain method will be called on the newly created thread.
56 8 : class Delegate {
57 : public:
58 0 : virtual ~Delegate() {}
59 : virtual void ThreadMain() = 0;
60 : };
61 :
62 : // Creates a new thread. The |stack_size| parameter can be 0 to indicate
63 : // that the default stack size should be used. Upon success,
64 : // |*thread_handle| will be assigned a handle to the newly created thread,
65 : // and |delegate|'s ThreadMain method will be executed on the newly created
66 : // thread.
67 : // NOTE: When you are done with the thread handle, you must call Join to
68 : // release system resources associated with the thread. You must ensure that
69 : // the Delegate object outlives the thread.
70 : static bool Create(size_t stack_size, Delegate* delegate,
71 : PlatformThreadHandle* thread_handle);
72 :
73 : // CreateNonJoinable() does the same thing as Create() except the thread
74 : // cannot be Join()'d. Therefore, it also does not output a
75 : // PlatformThreadHandle.
76 : static bool CreateNonJoinable(size_t stack_size, Delegate* delegate);
77 :
78 : // Joins with a thread created via the Create function. This function blocks
79 : // the caller until the designated thread exits. This will invalidate
80 : // |thread_handle|.
81 : static void Join(PlatformThreadHandle thread_handle);
82 :
83 : private:
84 : DISALLOW_IMPLICIT_CONSTRUCTORS(PlatformThread);
85 : };
86 :
87 : #endif // BASE_PLATFORM_THREAD_H_
|