Home | History | Annotate | Download | only in adaptation
      1 /******************************************************************************
      2  *
      3  *  Copyright (C) 2012 Broadcom Corporation
      4  *
      5  *  Licensed under the Apache License, Version 2.0 (the "License");
      6  *  you may not use this file except in compliance with the License.
      7  *  You may obtain a copy of the License at:
      8  *
      9  *  http://www.apache.org/licenses/LICENSE-2.0
     10  *
     11  *  Unless required by applicable law or agreed to in writing, software
     12  *  distributed under the License is distributed on an "AS IS" BASIS,
     13  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     14  *  See the License for the specific language governing permissions and
     15  *  limitations under the License.
     16  *
     17  ******************************************************************************/
     18 
     19 /******************************************************************************
     20  *
     21  *  Synchronize two or more threads using a condition variable and a mutex.
     22  *
     23  ******************************************************************************/
     24 #pragma once
     25 #include "CondVar.h"
     26 #include "Mutex.h"
     27 
     28 
     29 class SyncEvent
     30 {
     31 public:
     32     /*******************************************************************************
     33     **
     34     ** Function:        ~SyncEvent
     35     **
     36     ** Description:     Cleanup all resources.
     37     **
     38     ** Returns:         None.
     39     **
     40     *******************************************************************************/
     41     ~SyncEvent ()
     42     {
     43     }
     44 
     45 
     46     /*******************************************************************************
     47     **
     48     ** Function:        start
     49     **
     50     ** Description:     Start a synchronization operation.
     51     **
     52     ** Returns:         None.
     53     **
     54     *******************************************************************************/
     55     void start ()
     56     {
     57         mMutex.lock ();
     58     }
     59 
     60 
     61     /*******************************************************************************
     62     **
     63     ** Function:        wait
     64     **
     65     ** Description:     Block the thread and wait for the event to occur.
     66     **
     67     ** Returns:         None.
     68     **
     69     *******************************************************************************/
     70     void wait ()
     71     {
     72         mCondVar.wait (mMutex);
     73     }
     74 
     75 
     76     /*******************************************************************************
     77     **
     78     ** Function:        wait
     79     **
     80     ** Description:     Block the thread and wait for the event to occur.
     81     **                  millisec: Timeout in milliseconds.
     82     **
     83     ** Returns:         True if wait is successful; false if timeout occurs.
     84     **
     85     *******************************************************************************/
     86     bool wait (long millisec)
     87     {
     88         bool retVal = mCondVar.wait (mMutex, millisec);
     89         return retVal;
     90     }
     91 
     92 
     93     /*******************************************************************************
     94     **
     95     ** Function:        notifyOne
     96     **
     97     ** Description:     Notify a blocked thread that the event has occured. Unblocks it.
     98     **
     99     ** Returns:         None.
    100     **
    101     *******************************************************************************/
    102     void notifyOne ()
    103     {
    104         mCondVar.notifyOne ();
    105     }
    106 
    107 
    108     /*******************************************************************************
    109     **
    110     ** Function:        end
    111     **
    112     ** Description:     End a synchronization operation.
    113     **
    114     ** Returns:         None.
    115     **
    116     *******************************************************************************/
    117     void end ()
    118     {
    119         mMutex.unlock ();
    120     }
    121 
    122 private:
    123     CondVar mCondVar;
    124     Mutex mMutex;
    125 };
    126 
    127 
    128 /*****************************************************************************/
    129 /*****************************************************************************/
    130 
    131 
    132 /*****************************************************************************
    133 **
    134 **  Name:           SyncEventGuard
    135 **
    136 **  Description:    Automatically start and end a synchronization event.
    137 **
    138 *****************************************************************************/
    139 class SyncEventGuard
    140 {
    141 public:
    142     /*******************************************************************************
    143     **
    144     ** Function:        SyncEventGuard
    145     **
    146     ** Description:     Start a synchronization operation.
    147     **
    148     ** Returns:         None.
    149     **
    150     *******************************************************************************/
    151     SyncEventGuard (SyncEvent& event)
    152     :   mEvent (event)
    153     {
    154         event.start (); //automatically start operation
    155     };
    156 
    157 
    158     /*******************************************************************************
    159     **
    160     ** Function:        ~SyncEventGuard
    161     **
    162     ** Description:     End a synchronization operation.
    163     **
    164     ** Returns:         None.
    165     **
    166     *******************************************************************************/
    167     ~SyncEventGuard ()
    168     {
    169         mEvent.end (); //automatically end operation
    170     };
    171 
    172 private:
    173     SyncEvent& mEvent;
    174 };
    175 
    176