Home | History | Annotate | Download | only in source
      1 /*
      2  *  Copyright (c) 2011 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_MODULES_UTILITY_SOURCE_PROCESS_THREAD_IMPL_H_
     12 #define WEBRTC_MODULES_UTILITY_SOURCE_PROCESS_THREAD_IMPL_H_
     13 
     14 #include <list>
     15 #include <queue>
     16 
     17 #include "webrtc/base/criticalsection.h"
     18 #include "webrtc/base/platform_thread.h"
     19 #include "webrtc/base/thread_checker.h"
     20 #include "webrtc/modules/utility/include/process_thread.h"
     21 #include "webrtc/system_wrappers/include/event_wrapper.h"
     22 #include "webrtc/typedefs.h"
     23 
     24 namespace webrtc {
     25 
     26 class ProcessThreadImpl : public ProcessThread {
     27  public:
     28   explicit ProcessThreadImpl(const char* thread_name);
     29   ~ProcessThreadImpl() override;
     30 
     31   void Start() override;
     32   void Stop() override;
     33 
     34   void WakeUp(Module* module) override;
     35   void PostTask(rtc::scoped_ptr<ProcessTask> task) override;
     36 
     37   void RegisterModule(Module* module) override;
     38   void DeRegisterModule(Module* module) override;
     39 
     40  protected:
     41   static bool Run(void* obj);
     42   bool Process();
     43 
     44  private:
     45   struct ModuleCallback {
     46     ModuleCallback() : module(nullptr), next_callback(0) {}
     47     ModuleCallback(const ModuleCallback& cb)
     48         : module(cb.module), next_callback(cb.next_callback) {}
     49     ModuleCallback(Module* module) : module(module), next_callback(0) {}
     50     bool operator==(const ModuleCallback& cb) const {
     51       return cb.module == module;
     52     }
     53 
     54     Module* const module;
     55     int64_t next_callback;  // Absolute timestamp.
     56 
     57    private:
     58     ModuleCallback& operator=(ModuleCallback&);
     59   };
     60 
     61   typedef std::list<ModuleCallback> ModuleList;
     62 
     63   // Warning: For some reason, if |lock_| comes immediately before |modules_|
     64   // with the current class layout, we will  start to have mysterious crashes
     65   // on Mac 10.9 debug.  I (Tommi) suspect we're hitting some obscure alignemnt
     66   // issues, but I haven't figured out what they are, if there are alignment
     67   // requirements for mutexes on Mac or if there's something else to it.
     68   // So be careful with changing the layout.
     69   rtc::CriticalSection lock_;  // Used to guard modules_, tasks_ and stop_.
     70 
     71   rtc::ThreadChecker thread_checker_;
     72   const rtc::scoped_ptr<EventWrapper> wake_up_;
     73   // TODO(pbos): Remove scoped_ptr and stop recreating the thread.
     74   rtc::scoped_ptr<rtc::PlatformThread> thread_;
     75 
     76   ModuleList modules_;
     77   // TODO(tommi): Support delayed tasks.
     78   std::queue<ProcessTask*> queue_;
     79   bool stop_;
     80   const char* thread_name_;
     81 };
     82 
     83 }  // namespace webrtc
     84 
     85 #endif // WEBRTC_MODULES_UTILITY_SOURCE_PROCESS_THREAD_IMPL_H_
     86