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


rplidar_ros
Author(s):
autogenerated on Fri Dec 16 2016 03:59:16