Home | History | Annotate | Download | only in base
      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 const PlatformThreadHandle kNullThreadHandle = NULL;
     23 #elif defined(OS_POSIX)
     24 #include <pthread.h>
     25 typedef pthread_t PlatformThreadHandle;
     26 const PlatformThreadHandle kNullThreadHandle = 0;
     27 #if defined(OS_MACOSX)
     28 #include <mach/mach.h>
     29 typedef mach_port_t PlatformThreadId;
     30 #else  // OS_POSIX && !OS_MACOSX
     31 #include <unistd.h>
     32 typedef pid_t PlatformThreadId;
     33 #endif
     34 #endif
     35 
     36 // A namespace for low-level thread functions.
     37 class PlatformThread {
     38  public:
     39   // Gets the current thread id, which may be useful for logging purposes.
     40   static PlatformThreadId CurrentId();
     41 
     42   // Yield the current thread so another thread can be scheduled.
     43   static void YieldCurrentThread();
     44 
     45   // Sleeps for the specified duration (units are milliseconds).
     46   static void Sleep(int duration_ms);
     47 
     48   // Sets the thread name visible to a debugger.  This has no effect otherwise.
     49   static void SetName(const char* name);
     50 
     51   // Implement this interface to run code on a background thread.  Your
     52   // ThreadMain method will be called on the newly created thread.
     53   class Delegate {
     54    public:
     55     virtual ~Delegate() {}
     56     virtual void ThreadMain() = 0;
     57   };
     58 
     59   // Creates a new thread.  The |stack_size| parameter can be 0 to indicate
     60   // that the default stack size should be used.  Upon success,
     61   // |*thread_handle| will be assigned a handle to the newly created thread,
     62   // and |delegate|'s ThreadMain method will be executed on the newly created
     63   // thread.
     64   // NOTE: When you are done with the thread handle, you must call Join to
     65   // release system resources associated with the thread.  You must ensure that
     66   // the Delegate object outlives the thread.
     67   static bool Create(size_t stack_size, Delegate* delegate,
     68                      PlatformThreadHandle* thread_handle);
     69 
     70   // CreateNonJoinable() does the same thing as Create() except the thread
     71   // cannot be Join()'d.  Therefore, it also does not output a
     72   // PlatformThreadHandle.
     73   static bool CreateNonJoinable(size_t stack_size, Delegate* delegate);
     74 
     75   // Joins with a thread created via the Create function.  This function blocks
     76   // the caller until the designated thread exits.  This will invalidate
     77   // |thread_handle|.
     78   static void Join(PlatformThreadHandle thread_handle);
     79 
     80  private:
     81   DISALLOW_IMPLICIT_CONSTRUCTORS(PlatformThread);
     82 };
     83 
     84 #endif  // BASE_PLATFORM_THREAD_H_
     85