Home | History | Annotate | Download | only in source
      1 /*
      2  *  Copyright (c) 2012 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_SYSTEM_WRAPPERS_SOURCE_THREAD_POSIX_H_
     12 #define WEBRTC_SYSTEM_WRAPPERS_SOURCE_THREAD_POSIX_H_
     13 
     14 #include "thread_wrapper.h"
     15 #include <pthread.h>
     16 
     17 namespace webrtc {
     18 
     19 class CriticalSectionWrapper;
     20 class EventWrapper;
     21 
     22 class ThreadPosix : public ThreadWrapper
     23 {
     24 public:
     25     static ThreadWrapper* Create(ThreadRunFunction func, ThreadObj obj,
     26                                  ThreadPriority prio, const char* threadName);
     27 
     28     ThreadPosix(ThreadRunFunction func, ThreadObj obj, ThreadPriority prio,
     29                 const char* threadName);
     30     ~ThreadPosix();
     31 
     32     // From ThreadWrapper
     33     virtual void SetNotAlive();
     34     virtual bool Start(unsigned int& id);
     35     // Not implemented on Mac
     36     virtual bool SetAffinity(const int* processorNumbers,
     37                              unsigned int amountOfProcessors);
     38     virtual bool Stop();
     39     virtual bool Shutdown();
     40 
     41     void Run();
     42 
     43 private:
     44     int Construct();
     45 
     46 private:
     47     // processing function
     48     ThreadRunFunction   _runFunction;
     49     ThreadObj           _obj;
     50 
     51     // internal state
     52     CriticalSectionWrapper* _crit_state;  // Protects _alive and _dead
     53     bool                    _alive;
     54     bool                    _dead;
     55     ThreadPriority          _prio;
     56     EventWrapper*           _event;
     57 
     58     // zero-terminated thread name string
     59     char                    _name[kThreadMaxNameLength];
     60     bool                    _setThreadName;
     61 
     62     // handle to thread
     63 #if (defined(WEBRTC_LINUX) || defined(WEBRTC_ANDROID))
     64     pid_t                   _pid;
     65 #endif
     66     pthread_attr_t          _attr;
     67     pthread_t               _thread;
     68 };
     69 } // namespace webrtc
     70 
     71 #endif // WEBRTC_SYSTEM_WRAPPERS_SOURCE_THREAD_POSIX_H_
     72