blob: edb2ddc89e6dc44b2be17e25d7cad0d4ec7db7be [file] [log] [blame]
license.botf003cfe2008-08-24 09:55:55 +09001// 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.
initial.commit3f4a7322008-07-27 06:49:38 +09004
brettw@google.come3c034a2008-08-08 03:31:40 +09005#ifndef BASE_PLATFORM_THREAD_H_
6#define BASE_PLATFORM_THREAD_H_
initial.commit3f4a7322008-07-27 06:49:38 +09007
darin@google.comc18d7ae2008-08-21 18:46:32 +09008#include "base/basictypes.h"
brettw@google.come3c034a2008-08-08 03:31:40 +09009
deanm@google.comd8281e32008-08-23 02:22:49 +090010// PlatformThreadHandle should not be assumed to be a numeric type, since the
11// standard intends to allow pthread_t to be a structure. This means you
12// should not initialize it to a value, like 0. If it's a member variable, the
13// constructor can safely "value initialize" using () in the initializer list.
brettw@google.come3c034a2008-08-08 03:31:40 +090014#if defined(OS_WIN)
darin@google.comc18d7ae2008-08-21 18:46:32 +090015typedef void* PlatformThreadHandle; // HANDLE
brettw@google.come3c034a2008-08-08 03:31:40 +090016#elif defined(OS_POSIX)
initial.commit3f4a7322008-07-27 06:49:38 +090017#include <pthread.h>
18typedef pthread_t PlatformThreadHandle;
darin@google.comc18d7ae2008-08-21 18:46:32 +090019#endif
brettw@google.come3c034a2008-08-08 03:31:40 +090020
darin@google.comc18d7ae2008-08-21 18:46:32 +090021// A namespace for low-level thread functions.
initial.commit3f4a7322008-07-27 06:49:38 +090022class PlatformThread {
23 public:
darin@google.comc18d7ae2008-08-21 18:46:32 +090024 // Gets the current thread id, which may be useful for logging purposes.
pinkerton@google.com44159e42008-08-14 08:20:03 +090025 static int CurrentId();
initial.commit3f4a7322008-07-27 06:49:38 +090026
deanm@google.comc1c5cf52008-08-06 19:06:59 +090027 // Yield the current thread so another thread can be scheduled.
28 static void YieldCurrentThread();
29
paulg@google.comc8eeb752008-08-13 10:20:26 +090030 // Sleeps for the specified duration (units are milliseconds).
31 static void Sleep(int duration_ms);
32
darin@google.comc18d7ae2008-08-21 18:46:32 +090033 // Sets the thread name visible to a debugger. This has no effect otherwise.
34 // To set the name of the current thread, pass PlatformThread::CurrentId() as
35 // the thread_id parameter.
36 static void SetName(int thread_id, const char* name);
37
38 // Implement this interface to run code on a background thread. Your
39 // ThreadMain method will be called on the newly created thread.
40 class Delegate {
41 public:
42 virtual ~Delegate() {}
43 virtual void ThreadMain() = 0;
44 };
45
46 // Creates a new thread. The |stack_size| parameter can be 0 to indicate
47 // that the default stack size should be used. Upon success,
48 // |*thread_handle| will be assigned a handle to the newly created thread,
49 // and |delegate|'s ThreadMain method will be executed on the newly created
deanm@google.comc76a33a2008-08-22 19:49:15 +090050 // thread.
51 // NOTE: When you are done with the thread handle, you must call Join to
darin@google.comc18d7ae2008-08-21 18:46:32 +090052 // release system resources associated with the thread. You must ensure that
53 // the Delegate object outlives the thread.
54 static bool Create(size_t stack_size, Delegate* delegate,
55 PlatformThreadHandle* thread_handle);
56
57 // Joins with a thread created via the Create function. This function blocks
deanm@google.comc76a33a2008-08-22 19:49:15 +090058 // the caller until the designated thread exits. This will invalidate
59 // |thread_handle|.
darin@google.comc18d7ae2008-08-21 18:46:32 +090060 static void Join(PlatformThreadHandle thread_handle);
initial.commit3f4a7322008-07-27 06:49:38 +090061
62 private:
darin@google.comc18d7ae2008-08-21 18:46:32 +090063 DISALLOW_IMPLICIT_CONSTRUCTORS(PlatformThread);
initial.commit3f4a7322008-07-27 06:49:38 +090064};
65
brettw@google.come3c034a2008-08-08 03:31:40 +090066#endif // BASE_PLATFORM_THREAD_H_
license.botf003cfe2008-08-24 09:55:55 +090067