license.bot | f003cfe | 2008-08-24 09:55:55 +0900 | [diff] [blame^] | 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. |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 4 | |
brettw@google.com | e3c034a | 2008-08-08 03:31:40 +0900 | [diff] [blame] | 5 | #ifndef BASE_PLATFORM_THREAD_H_ |
| 6 | #define BASE_PLATFORM_THREAD_H_ |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 7 | |
darin@google.com | c18d7ae | 2008-08-21 18:46:32 +0900 | [diff] [blame] | 8 | #include "base/basictypes.h" |
brettw@google.com | e3c034a | 2008-08-08 03:31:40 +0900 | [diff] [blame] | 9 | |
deanm@google.com | d8281e3 | 2008-08-23 02:22:49 +0900 | [diff] [blame] | 10 | // 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.com | e3c034a | 2008-08-08 03:31:40 +0900 | [diff] [blame] | 14 | #if defined(OS_WIN) |
darin@google.com | c18d7ae | 2008-08-21 18:46:32 +0900 | [diff] [blame] | 15 | typedef void* PlatformThreadHandle; // HANDLE |
brettw@google.com | e3c034a | 2008-08-08 03:31:40 +0900 | [diff] [blame] | 16 | #elif defined(OS_POSIX) |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 17 | #include <pthread.h> |
| 18 | typedef pthread_t PlatformThreadHandle; |
darin@google.com | c18d7ae | 2008-08-21 18:46:32 +0900 | [diff] [blame] | 19 | #endif |
brettw@google.com | e3c034a | 2008-08-08 03:31:40 +0900 | [diff] [blame] | 20 | |
darin@google.com | c18d7ae | 2008-08-21 18:46:32 +0900 | [diff] [blame] | 21 | // A namespace for low-level thread functions. |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 22 | class PlatformThread { |
| 23 | public: |
darin@google.com | c18d7ae | 2008-08-21 18:46:32 +0900 | [diff] [blame] | 24 | // Gets the current thread id, which may be useful for logging purposes. |
pinkerton@google.com | 44159e4 | 2008-08-14 08:20:03 +0900 | [diff] [blame] | 25 | static int CurrentId(); |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 26 | |
deanm@google.com | c1c5cf5 | 2008-08-06 19:06:59 +0900 | [diff] [blame] | 27 | // Yield the current thread so another thread can be scheduled. |
| 28 | static void YieldCurrentThread(); |
| 29 | |
paulg@google.com | c8eeb75 | 2008-08-13 10:20:26 +0900 | [diff] [blame] | 30 | // Sleeps for the specified duration (units are milliseconds). |
| 31 | static void Sleep(int duration_ms); |
| 32 | |
darin@google.com | c18d7ae | 2008-08-21 18:46:32 +0900 | [diff] [blame] | 33 | // 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.com | c76a33a | 2008-08-22 19:49:15 +0900 | [diff] [blame] | 50 | // thread. |
| 51 | // NOTE: When you are done with the thread handle, you must call Join to |
darin@google.com | c18d7ae | 2008-08-21 18:46:32 +0900 | [diff] [blame] | 52 | // 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.com | c76a33a | 2008-08-22 19:49:15 +0900 | [diff] [blame] | 58 | // the caller until the designated thread exits. This will invalidate |
| 59 | // |thread_handle|. |
darin@google.com | c18d7ae | 2008-08-21 18:46:32 +0900 | [diff] [blame] | 60 | static void Join(PlatformThreadHandle thread_handle); |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 61 | |
| 62 | private: |
darin@google.com | c18d7ae | 2008-08-21 18:46:32 +0900 | [diff] [blame] | 63 | DISALLOW_IMPLICIT_CONSTRUCTORS(PlatformThread); |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 64 | }; |
| 65 | |
brettw@google.com | e3c034a | 2008-08-08 03:31:40 +0900 | [diff] [blame] | 66 | #endif // BASE_PLATFORM_THREAD_H_ |
license.bot | f003cfe | 2008-08-24 09:55:55 +0900 | [diff] [blame^] | 67 | |