security/sandbox/chromium/base/threading/platform_thread.h

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/security/sandbox/chromium/base/threading/platform_thread.h	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,160 @@
     1.4 +// Copyright (c) 2012 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_THREADING_PLATFORM_THREAD_H_
    1.13 +#define BASE_THREADING_PLATFORM_THREAD_H_
    1.14 +
    1.15 +#include "base/base_export.h"
    1.16 +#include "base/basictypes.h"
    1.17 +#include "base/time/time.h"
    1.18 +#include "build/build_config.h"
    1.19 +
    1.20 +#if defined(OS_WIN)
    1.21 +#include <windows.h>
    1.22 +#elif defined(OS_POSIX)
    1.23 +#include <pthread.h>
    1.24 +#include <unistd.h>
    1.25 +#endif
    1.26 +
    1.27 +namespace base {
    1.28 +
    1.29 +#if defined(OS_WIN)
    1.30 +typedef DWORD PlatformThreadId;
    1.31 +#elif defined(OS_POSIX)
    1.32 +typedef pid_t PlatformThreadId;
    1.33 +#endif
    1.34 +
    1.35 +class PlatformThreadHandle {
    1.36 + public:
    1.37 +#if defined(OS_WIN)
    1.38 +  typedef void* Handle;
    1.39 +#elif defined(OS_POSIX)
    1.40 +  typedef pthread_t Handle;
    1.41 +#endif
    1.42 +
    1.43 +  PlatformThreadHandle()
    1.44 +      : handle_(0),
    1.45 +        id_(0) {
    1.46 +  }
    1.47 +
    1.48 +  explicit PlatformThreadHandle(Handle handle)
    1.49 +      : handle_(handle),
    1.50 +        id_(0) {
    1.51 +  }
    1.52 +
    1.53 +  PlatformThreadHandle(Handle handle,
    1.54 +                       PlatformThreadId id)
    1.55 +      : handle_(handle),
    1.56 +        id_(id) {
    1.57 +  }
    1.58 +
    1.59 +  bool is_equal(const PlatformThreadHandle& other) {
    1.60 +    return handle_ == other.handle_;
    1.61 +  }
    1.62 +
    1.63 +  bool is_null() {
    1.64 +    return !handle_;
    1.65 +  }
    1.66 +
    1.67 +  Handle platform_handle() {
    1.68 +    return handle_;
    1.69 +  }
    1.70 +
    1.71 + private:
    1.72 +  friend class PlatformThread;
    1.73 +
    1.74 +  Handle handle_;
    1.75 +  PlatformThreadId id_;
    1.76 +};
    1.77 +
    1.78 +const PlatformThreadId kInvalidThreadId(0);
    1.79 +
    1.80 +// Valid values for SetThreadPriority()
    1.81 +enum ThreadPriority{
    1.82 +  kThreadPriority_Normal,
    1.83 +  // Suitable for low-latency, glitch-resistant audio.
    1.84 +  kThreadPriority_RealtimeAudio,
    1.85 +  // Suitable for threads which generate data for the display (at ~60Hz).
    1.86 +  kThreadPriority_Display,
    1.87 +  // Suitable for threads that shouldn't disrupt high priority work.
    1.88 +  kThreadPriority_Background
    1.89 +};
    1.90 +
    1.91 +// A namespace for low-level thread functions.
    1.92 +class BASE_EXPORT PlatformThread {
    1.93 + public:
    1.94 +  // Implement this interface to run code on a background thread.  Your
    1.95 +  // ThreadMain method will be called on the newly created thread.
    1.96 +  class BASE_EXPORT Delegate {
    1.97 +   public:
    1.98 +    virtual void ThreadMain() = 0;
    1.99 +
   1.100 +   protected:
   1.101 +    virtual ~Delegate() {}
   1.102 +  };
   1.103 +
   1.104 +  // Gets the current thread id, which may be useful for logging purposes.
   1.105 +  static PlatformThreadId CurrentId();
   1.106 +
   1.107 +  // Get the current handle.
   1.108 +  static PlatformThreadHandle CurrentHandle();
   1.109 +
   1.110 +  // Yield the current thread so another thread can be scheduled.
   1.111 +  static void YieldCurrentThread();
   1.112 +
   1.113 +  // Sleeps for the specified duration.
   1.114 +  static void Sleep(base::TimeDelta duration);
   1.115 +
   1.116 +  // Sets the thread name visible to debuggers/tools. This has no effect
   1.117 +  // otherwise. This name pointer is not copied internally. Thus, it must stay
   1.118 +  // valid until the thread ends.
   1.119 +  static void SetName(const char* name);
   1.120 +
   1.121 +  // Gets the thread name, if previously set by SetName.
   1.122 +  static const char* GetName();
   1.123 +
   1.124 +  // Creates a new thread.  The |stack_size| parameter can be 0 to indicate
   1.125 +  // that the default stack size should be used.  Upon success,
   1.126 +  // |*thread_handle| will be assigned a handle to the newly created thread,
   1.127 +  // and |delegate|'s ThreadMain method will be executed on the newly created
   1.128 +  // thread.
   1.129 +  // NOTE: When you are done with the thread handle, you must call Join to
   1.130 +  // release system resources associated with the thread.  You must ensure that
   1.131 +  // the Delegate object outlives the thread.
   1.132 +  static bool Create(size_t stack_size, Delegate* delegate,
   1.133 +                     PlatformThreadHandle* thread_handle);
   1.134 +
   1.135 +  // CreateWithPriority() does the same thing as Create() except the priority of
   1.136 +  // the thread is set based on |priority|.  Can be used in place of Create()
   1.137 +  // followed by SetThreadPriority().  SetThreadPriority() has not been
   1.138 +  // implemented on the Linux platform yet, this is the only way to get a high
   1.139 +  // priority thread on Linux.
   1.140 +  static bool CreateWithPriority(size_t stack_size, Delegate* delegate,
   1.141 +                                 PlatformThreadHandle* thread_handle,
   1.142 +                                 ThreadPriority priority);
   1.143 +
   1.144 +  // CreateNonJoinable() does the same thing as Create() except the thread
   1.145 +  // cannot be Join()'d.  Therefore, it also does not output a
   1.146 +  // PlatformThreadHandle.
   1.147 +  static bool CreateNonJoinable(size_t stack_size, Delegate* delegate);
   1.148 +
   1.149 +  // Joins with a thread created via the Create function.  This function blocks
   1.150 +  // the caller until the designated thread exits.  This will invalidate
   1.151 +  // |thread_handle|.
   1.152 +  static void Join(PlatformThreadHandle thread_handle);
   1.153 +
   1.154 +  static void SetThreadPriority(PlatformThreadHandle handle,
   1.155 +                                ThreadPriority priority);
   1.156 +
   1.157 + private:
   1.158 +  DISALLOW_IMPLICIT_CONSTRUCTORS(PlatformThread);
   1.159 +};
   1.160 +
   1.161 +}  // namespace base
   1.162 +
   1.163 +#endif  // BASE_THREADING_PLATFORM_THREAD_H_

mercurial