Backed out changeset b71c8c052463 (bug 1943846) for causing mass failures. CLOSED...
[gecko.git] / ipc / chromium / src / base / platform_thread.h
blobf3a65e8797cdd5d115ee5fb4cef21fe49044b1b4
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.
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.
11 #ifndef BASE_PLATFORM_THREAD_H_
12 #define BASE_PLATFORM_THREAD_H_
14 #include "base/basictypes.h"
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(XP_WIN)
21 # include <windows.h>
22 typedef DWORD PlatformThreadId;
23 typedef void* PlatformThreadHandle; // HANDLE
24 #else
25 # include <pthread.h>
26 typedef pthread_t PlatformThreadHandle;
27 # if defined(XP_LINUX) || defined(XP_OPENBSD) || defined(XP_SOLARIS) || \
28 defined(__GLIBC__)
29 # include <unistd.h>
30 typedef pid_t PlatformThreadId;
31 # elif defined(__DragonFly__) || defined(XP_FREEBSD) || defined(XP_NETBSD)
32 # include <sys/types.h>
33 typedef lwpid_t PlatformThreadId;
34 # elif defined(XP_DARWIN)
35 # include <mach/mach.h>
36 typedef mach_port_t PlatformThreadId;
37 # endif
38 #endif
40 // A namespace for low-level thread functions.
41 class PlatformThread {
42 public:
43 // Gets the current thread id, which may be useful for logging purposes.
44 static PlatformThreadId CurrentId();
46 // Yield the current thread so another thread can be scheduled.
47 static void YieldCurrentThread();
49 // Sleeps for the specified duration (units are milliseconds).
50 static void Sleep(int duration_ms);
52 // Sets the thread name visible to a debugger. This has no effect otherwise.
53 static void SetName(const char* name);
55 // Implement this interface to run code on a background thread. Your
56 // ThreadMain method will be called on the newly created thread.
57 class Delegate {
58 public:
59 virtual ~Delegate() {}
60 virtual void ThreadMain() = 0;
63 // Creates a new thread. The |stack_size| parameter can be 0 to indicate
64 // that the default stack size should be used. Upon success,
65 // |*thread_handle| will be assigned a handle to the newly created thread,
66 // and |delegate|'s ThreadMain method will be executed on the newly created
67 // thread.
68 // NOTE: When you are done with the thread handle, you must call Join to
69 // release system resources associated with the thread. You must ensure that
70 // the Delegate object outlives the thread.
71 static bool Create(size_t stack_size, Delegate* delegate,
72 PlatformThreadHandle* thread_handle);
74 // CreateNonJoinable() does the same thing as Create() except the thread
75 // cannot be Join()'d. Therefore, it also does not output a
76 // PlatformThreadHandle.
77 static bool CreateNonJoinable(size_t stack_size, Delegate* delegate);
79 // Joins with a thread created via the Create function. This function blocks
80 // the caller until the designated thread exits. This will invalidate
81 // |thread_handle|.
82 static void Join(PlatformThreadHandle thread_handle);
84 private:
85 DISALLOW_IMPLICIT_CONSTRUCTORS(PlatformThread);
88 #endif // BASE_PLATFORM_THREAD_H_