Home | History | Annotate | Download | only in base
      1 /*
      2  *  Copyright (c) 2015 The WebRTC project authors. All Rights Reserved.
      3  *
      4  *  Use of this source code is governed by a BSD-style license
      5  *  that can be found in the LICENSE file in the root of the source
      6  *  tree. An additional intellectual property rights grant can be found
      7  *  in the file PATENTS.  All contributing project authors may
      8  *  be found in the AUTHORS file in the root of the source tree.
      9  */
     10 
     11 #ifndef WEBRTC_BASE_PLATFORM_THREAD_H_
     12 #define WEBRTC_BASE_PLATFORM_THREAD_H_
     13 
     14 #include <string>
     15 
     16 #include "webrtc/base/constructormagic.h"
     17 #include "webrtc/base/event.h"
     18 #include "webrtc/base/platform_thread_types.h"
     19 #include "webrtc/base/scoped_ptr.h"
     20 #include "webrtc/base/thread_checker.h"
     21 
     22 namespace rtc {
     23 
     24 PlatformThreadId CurrentThreadId();
     25 PlatformThreadRef CurrentThreadRef();
     26 
     27 // Compares two thread identifiers for equality.
     28 bool IsThreadRefEqual(const PlatformThreadRef& a, const PlatformThreadRef& b);
     29 
     30 // Sets the current thread name.
     31 void SetCurrentThreadName(const char* name);
     32 
     33 // Callback function that the spawned thread will enter once spawned.
     34 // A return value of false is interpreted as that the function has no
     35 // more work to do and that the thread can be released.
     36 typedef bool (*ThreadRunFunction)(void*);
     37 
     38 enum ThreadPriority {
     39 #ifdef WEBRTC_WIN
     40   kLowPriority = THREAD_PRIORITY_BELOW_NORMAL,
     41   kNormalPriority = THREAD_PRIORITY_NORMAL,
     42   kHighPriority = THREAD_PRIORITY_ABOVE_NORMAL,
     43   kHighestPriority = THREAD_PRIORITY_HIGHEST,
     44   kRealtimePriority = THREAD_PRIORITY_TIME_CRITICAL
     45 #else
     46   kLowPriority = 1,
     47   kNormalPriority = 2,
     48   kHighPriority = 3,
     49   kHighestPriority = 4,
     50   kRealtimePriority = 5
     51 #endif
     52 };
     53 
     54 // Represents a simple worker thread.  The implementation must be assumed
     55 // to be single threaded, meaning that all methods of the class, must be
     56 // called from the same thread, including instantiation.
     57 class PlatformThread {
     58  public:
     59   PlatformThread(ThreadRunFunction func, void* obj, const char* thread_name);
     60   virtual ~PlatformThread();
     61 
     62   // Spawns a thread and tries to set thread priority according to the priority
     63   // from when CreateThread was called.
     64   void Start();
     65 
     66   bool IsRunning() const;
     67 
     68   // Stops (joins) the spawned thread.
     69   void Stop();
     70 
     71   // Set the priority of the thread. Must be called when thread is running.
     72   bool SetPriority(ThreadPriority priority);
     73 
     74  private:
     75   void Run();
     76 
     77   ThreadRunFunction const run_function_;
     78   void* const obj_;
     79   // TODO(pbos): Make sure call sites use string literals and update to a const
     80   // char* instead of a std::string.
     81   const std::string name_;
     82   rtc::ThreadChecker thread_checker_;
     83 #if defined(WEBRTC_WIN)
     84   static DWORD WINAPI StartThread(void* param);
     85 
     86   bool stop_;
     87   HANDLE thread_;
     88 #else
     89   static void* StartThread(void* param);
     90 
     91   rtc::Event stop_event_;
     92 
     93   pthread_t thread_;
     94 #endif  // defined(WEBRTC_WIN)
     95   RTC_DISALLOW_COPY_AND_ASSIGN(PlatformThread);
     96 };
     97 
     98 }  // namespace rtc
     99 
    100 #endif  // WEBRTC_BASE_PLATFORM_THREAD_H_
    101