event.h
Go to the documentation of this file.
00001 /*
00002  * Copyright (c) 2014, RoboPeak
00003  * All rights reserved.
00004  *
00005  * Redistribution and use in source and binary forms, with or without 
00006  * modification, are permitted provided that the following conditions are met:
00007  *
00008  * 1. Redistributions of source code must retain the above copyright notice, 
00009  *    this list of conditions and the following disclaimer.
00010  *
00011  * 2. Redistributions in binary form must reproduce the above copyright notice, 
00012  *    this list of conditions and the following disclaimer in the documentation 
00013  *    and/or other materials provided with the distribution.
00014  *
00015  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 
00016  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, 
00017  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 
00018  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR 
00019  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 
00020  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 
00021  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; 
00022  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 
00023  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 
00024  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, 
00025  * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00026  *
00027  */
00028 /*
00029  *  RoboPeak LIDAR System
00030  *  Lock abstract layer
00031  *
00032  *  Copyright 2009 - 2014 RoboPeak Team
00033  *  http://www.robopeak.com
00034  * 
00035  */
00036 
00037 
00038 #pragma once
00039 namespace rp{ namespace hal{
00040 
00041 class Event
00042 {
00043 public:
00044     
00045     enum
00046     {
00047         EVENT_OK = 1,
00048         EVENT_TIMEOUT = -1,
00049         EVENT_FAILED = 0,
00050     };
00051     
00052     Event(bool isAutoReset = true, bool isSignal = false)
00053 #ifdef _WIN32
00054         : _event(NULL)
00055 #else
00056         : _is_signalled(isSignal)
00057         , _isAutoReset(isAutoReset)
00058 #endif
00059     {
00060 #ifdef _WIN32
00061         _event = CreateEvent(NULL, isAutoReset?FALSE:TRUE, isSignal?TRUE:FALSE, NULL); 
00062 #else
00063         pthread_mutex_init(&_cond_locker, NULL);
00064         pthread_cond_init(&_cond_var, NULL);
00065 #endif
00066     }
00067 
00068     ~ Event()
00069     {
00070         release();
00071     }
00072 
00073     void set( bool isSignal = true )
00074     {
00075         if (isSignal){
00076 #ifdef _WIN32
00077             SetEvent(_event);
00078 #else
00079             pthread_mutex_lock(&_cond_locker);
00080                
00081             if ( _is_signalled == false )
00082             {
00083                 _is_signalled = true;
00084                 pthread_cond_signal(&_cond_var);
00085             }
00086             pthread_mutex_unlock(&_cond_locker);
00087 #endif
00088         }
00089         else
00090         {
00091 #ifdef _WIN32
00092             ResetEvent(_event);
00093 #else
00094             pthread_mutex_lock(&_cond_locker);
00095             _is_signalled = false;
00096             pthread_mutex_unlock(&_cond_locker);
00097 #endif
00098         }
00099     }
00100     
00101     unsigned long wait( unsigned long timeout = 0xFFFFFFFF )
00102     {
00103 #ifdef _WIN32
00104         switch (WaitForSingleObject(_event, timeout==0xFFFFFFF?INFINITE:(DWORD)timeout))
00105         {
00106         case WAIT_FAILED:
00107             return EVENT_FAILED;
00108         case WAIT_OBJECT_0:
00109             return EVENT_OK;
00110         case WAIT_TIMEOUT:
00111             return EVENT_TIMEOUT;
00112         }
00113         return EVENT_OK;
00114 #else
00115         unsigned long ans = EVENT_OK;
00116         pthread_mutex_lock( &_cond_locker );
00117 
00118         if ( !_is_signalled )
00119         {
00120             
00121                 if (timeout == 0xFFFFFFFF){
00122                     pthread_cond_wait(&_cond_var,&_cond_locker);
00123                 }else
00124                 {
00125                     timespec wait_time;
00126                     timeval now;
00127                     gettimeofday(&now,NULL);
00128 
00129                     wait_time.tv_sec = timeout/1000 + now.tv_sec;
00130                     wait_time.tv_nsec = (timeout%1000)*1000000ULL + now.tv_usec*1000;
00131                 
00132                     if (wait_time.tv_nsec >= 1000000000)
00133                     {
00134                        ++wait_time.tv_sec;
00135                        wait_time.tv_nsec -= 1000000000;
00136                     }
00137                     switch (pthread_cond_timedwait(&_cond_var,&_cond_locker,&wait_time))
00138                     {
00139                     case 0:
00140                         // signalled
00141                         break;
00142                     case ETIMEDOUT:
00143                         // time up
00144                         ans = EVENT_TIMEOUT;
00145                         goto _final;
00146                         break;
00147                     default:
00148                         ans = EVENT_FAILED;
00149                         goto _final;
00150                     }
00151        
00152             }
00153         }
00154           
00155         assert(_is_signalled);
00156 
00157         if ( _isAutoReset )
00158         {
00159             _is_signalled = false;
00160         }
00161 _final:
00162         pthread_mutex_unlock( &_cond_locker );
00163 
00164         return ans;
00165 #endif
00166         
00167     }
00168 protected:
00169 
00170     void release()
00171     {
00172 #ifdef _WIN32
00173         CloseHandle(_event);
00174 #else
00175         pthread_mutex_destroy(&_cond_locker);
00176         pthread_cond_destroy(&_cond_var);
00177 #endif
00178     }
00179 
00180 #ifdef _WIN32
00181         HANDLE _event;
00182 #else
00183         pthread_cond_t         _cond_var;
00184         pthread_mutex_t        _cond_locker;
00185         bool                   _is_signalled;
00186         bool                   _isAutoReset;
00187 #endif
00188 };
00189 }}


rplidar_ros
Author(s):
autogenerated on Fri Aug 28 2015 12:46:43