1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/ipc/chromium/src/base/platform_thread.h Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,85 @@ 1.4 +// Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. 1.5 +// Use of this source code is governed by a BSD-style license that can be 1.6 +// found in the LICENSE file. 1.7 + 1.8 +// WARNING: You should *NOT* be using this class directly. PlatformThread is 1.9 +// the low-level platform-specific abstraction to the OS's threading interface. 1.10 +// You should instead be using a message-loop driven Thread, see thread.h. 1.11 + 1.12 +#ifndef BASE_PLATFORM_THREAD_H_ 1.13 +#define BASE_PLATFORM_THREAD_H_ 1.14 + 1.15 +#include "base/basictypes.h" 1.16 + 1.17 +// PlatformThreadHandle should not be assumed to be a numeric type, since the 1.18 +// standard intends to allow pthread_t to be a structure. This means you 1.19 +// should not initialize it to a value, like 0. If it's a member variable, the 1.20 +// constructor can safely "value initialize" using () in the initializer list. 1.21 +#if defined(OS_WIN) 1.22 +#include <windows.h> 1.23 +typedef DWORD PlatformThreadId; 1.24 +typedef void* PlatformThreadHandle; // HANDLE 1.25 +#elif defined(OS_POSIX) 1.26 +#include <pthread.h> 1.27 +typedef pthread_t PlatformThreadHandle; 1.28 +#if defined(OS_LINUX) || defined(OS_OPENBSD) || defined(__GLIBC__) 1.29 +#include <unistd.h> 1.30 +typedef pid_t PlatformThreadId; 1.31 +#elif defined(OS_BSD) 1.32 +#include <sys/types.h> 1.33 +typedef lwpid_t PlatformThreadId; 1.34 +#elif defined(OS_MACOSX) 1.35 +#include <mach/mach.h> 1.36 +typedef mach_port_t PlatformThreadId; 1.37 +#endif 1.38 +#endif 1.39 + 1.40 +// A namespace for low-level thread functions. 1.41 +class PlatformThread { 1.42 + public: 1.43 + // Gets the current thread id, which may be useful for logging purposes. 1.44 + static PlatformThreadId CurrentId(); 1.45 + 1.46 + // Yield the current thread so another thread can be scheduled. 1.47 + static void YieldCurrentThread(); 1.48 + 1.49 + // Sleeps for the specified duration (units are milliseconds). 1.50 + static void Sleep(int duration_ms); 1.51 + 1.52 + // Sets the thread name visible to a debugger. This has no effect otherwise. 1.53 + static void SetName(const char* name); 1.54 + 1.55 + // Implement this interface to run code on a background thread. Your 1.56 + // ThreadMain method will be called on the newly created thread. 1.57 + class Delegate { 1.58 + public: 1.59 + virtual ~Delegate() {} 1.60 + virtual void ThreadMain() = 0; 1.61 + }; 1.62 + 1.63 + // Creates a new thread. The |stack_size| parameter can be 0 to indicate 1.64 + // that the default stack size should be used. Upon success, 1.65 + // |*thread_handle| will be assigned a handle to the newly created thread, 1.66 + // and |delegate|'s ThreadMain method will be executed on the newly created 1.67 + // thread. 1.68 + // NOTE: When you are done with the thread handle, you must call Join to 1.69 + // release system resources associated with the thread. You must ensure that 1.70 + // the Delegate object outlives the thread. 1.71 + static bool Create(size_t stack_size, Delegate* delegate, 1.72 + PlatformThreadHandle* thread_handle); 1.73 + 1.74 + // CreateNonJoinable() does the same thing as Create() except the thread 1.75 + // cannot be Join()'d. Therefore, it also does not output a 1.76 + // PlatformThreadHandle. 1.77 + static bool CreateNonJoinable(size_t stack_size, Delegate* delegate); 1.78 + 1.79 + // Joins with a thread created via the Create function. This function blocks 1.80 + // the caller until the designated thread exits. This will invalidate 1.81 + // |thread_handle|. 1.82 + static void Join(PlatformThreadHandle thread_handle); 1.83 + 1.84 + private: 1.85 + DISALLOW_IMPLICIT_CONSTRUCTORS(PlatformThread); 1.86 +}; 1.87 + 1.88 +#endif // BASE_PLATFORM_THREAD_H_