Home | History | Annotate | Download | only in hdr
      1 /*
      2  * Copyright (C) 2014 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 #ifndef DMTHREADHELPER_INCLUDE
     18 #define DMTHREADHELPER_INCLUDE
     19 
     20 #include <pthread.h>
     21 #include <unistd.h>
     22 #include <assert.h>
     23 #include <memory.h>
     24 #include <errno.h>
     25 
     26 #ifdef DEBUG
     27   #define IMASSERT(expression) assert(expression)
     28   //#define NRES int nRes=
     29   #define NRES(foo) int nRes=(foo)
     30 #else
     31   #define IMASSERT(expression)
     32   //#define NRES
     33   #define NRES(foo) foo
     34 #endif
     35 
     36 
     37 class DMCriticalSection
     38 {
     39 public:
     40   /**
     41   * Default constructor
     42   */
     43   DMCriticalSection();
     44 
     45   /**
     46   * Destructor
     47   */
     48   ~DMCriticalSection();
     49 
     50   /**
     51   * Locks mutex object
     52   */
     53   void Enter();
     54 
     55   /**
     56   * Unlocks mutex object
     57   */
     58   void Leave();
     59 
     60   /**
     61   * Tries to lock mutex object
     62   * \return TRUE if mutex object is locked
     63   */
     64   bool TryEnter();
     65 
     66   /**
     67   * Retrieves handler on mutex object
     68   */
     69   pthread_mutex_t& GetHandle() { return m_section;}
     70 
     71 private:
     72   /* handler */
     73   pthread_mutex_t   m_section;
     74 };
     75 
     76 
     77 class DMThread
     78 {
     79  public:
     80 
     81   /**
     82   * Default constructor
     83   */
     84   DMThread()
     85   {
     86       m_bRunning = false;
     87       memset(&m_hThread, 0, sizeof(m_hThread));
     88   }
     89 
     90   /**
     91   * Destructor
     92   */
     93   virtual ~DMThread(){}
     94 
     95   /**
     96   * Starts thread
     97   * \return TRUE if success
     98   */
     99   bool StartThread();
    100 
    101   /**
    102   * Stops thread
    103   * \return TRUE if success
    104   */
    105   bool StopThread();
    106 
    107  protected:
    108   /**
    109   * Run method to be implemented in an inherited class
    110   */
    111   virtual void* Run() = 0;
    112  private:
    113 
    114   /**
    115   * Thread callback function
    116   */
    117   static void* ThreadProc(void *pArg);
    118 
    119 protected:
    120   /** Flag to specify if thread is running */
    121   bool  m_bRunning;
    122   /** Thread handler */
    123   pthread_t   m_hThread;
    124 };
    125 
    126 
    127 inline DMCriticalSection::DMCriticalSection( )
    128 {
    129   memset( &m_section, 0, sizeof( m_section ) );
    130 
    131   NRES(pthread_mutex_init( &m_section, 0 ));
    132   IMASSERT( nRes == 0 );
    133 }
    134 
    135 inline DMCriticalSection::~DMCriticalSection()
    136 {
    137   NRES(pthread_mutex_destroy( &m_section ));
    138   IMASSERT( nRes == 0 );
    139 }
    140 
    141 inline void DMCriticalSection::Enter()
    142 {
    143   NRES(pthread_mutex_lock( &m_section ));
    144   IMASSERT( nRes == 0 );
    145 }
    146 
    147 inline void DMCriticalSection::Leave()
    148 {
    149   NRES(pthread_mutex_unlock( &m_section ));
    150 
    151   IMASSERT( nRes == 0 );
    152 }
    153 
    154 inline bool DMCriticalSection::TryEnter()
    155 {
    156   int nRes = pthread_mutex_trylock( &m_section );
    157 
    158   IMASSERT( nRes == 0 || nRes == EBUSY );
    159 
    160   return nRes == 0;
    161 }
    162 
    163 
    164 class  DMSingleLock
    165 {
    166   DMCriticalSection& m_oSection;
    167 public:
    168   /**
    169   * Constructor
    170   * \param oSection [in] - critical section(mutex)
    171   */
    172   DMSingleLock( DMCriticalSection& oSection ): m_oSection( oSection )
    173   {
    174     m_oSection.Enter();
    175   }
    176 
    177   /**
    178   * Destructor
    179   */
    180   ~DMSingleLock()
    181   {
    182     m_oSection.Leave();
    183   }
    184 };
    185 
    186 #define DmThSleep(a)  usleep(a)
    187 
    188 #endif
    189