common.h

00001 /*****************************************************************************
00002 Copyright (c) 2001 - 2009, The Board of Trustees of the University of Illinois.
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
00007 met:
00008 
00009 * Redistributions of source code must retain the above
00010   copyright notice, this list of conditions and the
00011   following disclaimer.
00012 
00013 * Redistributions in binary form must reproduce the
00014   above copyright notice, this list of conditions
00015   and the following disclaimer in the documentation
00016   and/or other materials provided with the distribution.
00017 
00018 * Neither the name of the University of Illinois
00019   nor the names of its contributors may be used to
00020   endorse or promote products derived from this
00021   software without specific prior written permission.
00022 
00023 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
00024 IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
00025 THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
00026 PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
00027 CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
00028 EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
00029 PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
00030 PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
00031 LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
00032 NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
00033 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00034 *****************************************************************************/
00035 
00036 /*****************************************************************************
00037 written by
00038    Yunhong Gu, last updated 08/01/2009
00039 *****************************************************************************/
00040 
00041 #ifndef __UDT_COMMON_H__
00042 #define __UDT_COMMON_H__
00043 
00044 
00045 #ifndef WIN32
00046    #include <sys/time.h>
00047    #include <sys/uio.h>
00048    #include <pthread.h>
00049 #else
00050    #ifdef __MINGW__
00051       #include <stdint.h>
00052       #include <ws2tcpip.h>
00053    #endif
00054    #include <windows.h>
00055 #endif
00056 #include <cstdlib>
00057 #include "udt.h"
00058 
00059 
00060 #ifdef WIN32
00061    // Windows compability
00062    typedef HANDLE pthread_t;
00063    typedef HANDLE pthread_mutex_t;
00064    typedef HANDLE pthread_cond_t;
00065    typedef DWORD pthread_key_t;
00066 #endif
00067 
00068 
00070 
00071 class CTimer
00072 {
00073 public:
00074    CTimer();
00075    ~CTimer();
00076 
00077 public:
00078 
00079       // Functionality:
00080       //    Sleep for "interval" CCs.
00081       // Parameters:
00082       //    0) [in] interval: CCs to sleep.
00083       // Returned value:
00084       //    None.
00085 
00086    void sleep(uint64_t interval);
00087 
00088       // Functionality:
00089       //    Seelp until CC "nexttime".
00090       // Parameters:
00091       //    0) [in] nexttime: next time the caller is waken up.
00092       // Returned value:
00093       //    None.
00094 
00095    void sleepto(uint64_t nexttime);
00096 
00097       // Functionality:
00098       //    Stop the sleep() or sleepto() methods.
00099       // Parameters:
00100       //    None.
00101       // Returned value:
00102       //    None.
00103 
00104    void interrupt();
00105 
00106       // Functionality:
00107       //    trigger the clock for a tick, for better granuality in no_busy_waiting timer.
00108       // Parameters:
00109       //    None.
00110       // Returned value:
00111       //    None.
00112 
00113    void tick();
00114 
00115 public:
00116 
00117       // Functionality:
00118       //    Read the CPU clock cycle into x.
00119       // Parameters:
00120       //    0) [out] x: to record cpu clock cycles.
00121       // Returned value:
00122       //    None.
00123 
00124    static void rdtsc(uint64_t &x);
00125 
00126       // Functionality:
00127       //    return the CPU frequency.
00128       // Parameters:
00129       //    None.
00130       // Returned value:
00131       //    CPU frequency.
00132 
00133    static uint64_t getCPUFrequency();
00134 
00135       // Functionality:
00136       //    check the current time, 64bit, in microseconds.
00137       // Parameters:
00138       //    None.
00139       // Returned value:
00140       //    current time in microseconds.
00141 
00142    static uint64_t getTime();
00143 
00144       // Functionality:
00145       //    trigger an event such as new connection, close, new data, etc. for "select" call.
00146       // Parameters:
00147       //    None.
00148       // Returned value:
00149       //    None.
00150 
00151    static void triggerEvent();
00152 
00153       // Functionality:
00154       //    wait for an event to br triggered by "triggerEvent".
00155       // Parameters:
00156       //    None.
00157       // Returned value:
00158       //    None.
00159 
00160    static void waitForEvent();
00161 
00162       // Functionality:
00163       //    sleep for a short interval. exact sleep time does not matter
00164       // Parameters:
00165       //    None.
00166       // Returned value:
00167       //    None.
00168 
00169    static void sleep();
00170 
00171 private:
00172    uint64_t getTimeInMicroSec();
00173 
00174 private:
00175    uint64_t m_ullSchedTime;             // next schedulled time
00176 
00177    pthread_cond_t m_TickCond;
00178    pthread_mutex_t m_TickLock;
00179 
00180    static pthread_cond_t m_EventCond;
00181    static pthread_mutex_t m_EventLock;
00182 
00183 private:
00184    static uint64_t s_ullCPUFrequency;   // CPU frequency : clock cycles per microsecond
00185    static uint64_t readCPUFrequency();
00186    static bool m_bUseMicroSecond;       // No higher resolution timer available, use gettimeofday().
00187 };
00188 
00190 
00191 class CGuard
00192 {
00193 public:
00194    CGuard(pthread_mutex_t& lock);
00195    ~CGuard();
00196 
00197 public:
00198    static void enterCS(pthread_mutex_t& lock);
00199    static void leaveCS(pthread_mutex_t& lock);
00200 
00201    static void createMutex(pthread_mutex_t& lock);
00202    static void releaseMutex(pthread_mutex_t& lock);
00203 
00204    static void createCond(pthread_cond_t& cond);
00205    static void releaseCond(pthread_cond_t& cond);
00206 
00207 private:
00208    pthread_mutex_t& m_Mutex;            // Alias name of the mutex to be protected
00209    int m_iLocked;                       // Locking status
00210 
00211    CGuard& operator=(const CGuard&);
00212 };
00213 
00214 
00215 
00217 
00218 // UDT Sequence Number 0 - (2^31 - 1)
00219 
00220 // seqcmp: compare two seq#, considering the wraping
00221 // seqlen: length from the 1st to the 2nd seq#, including both
00222 // seqoff: offset from the 2nd to the 1st seq#
00223 // incseq: increase the seq# by 1
00224 // decseq: decrease the seq# by 1
00225 // incseq: increase the seq# by a given offset
00226 
00227 class CSeqNo
00228 {
00229 public:
00230    inline static int seqcmp(int32_t seq1, int32_t seq2)
00231    {return (abs(seq1 - seq2) < m_iSeqNoTH) ? (seq1 - seq2) : (seq2 - seq1);}
00232 
00233    inline static int seqlen(int32_t seq1, int32_t seq2)
00234    {return (seq1 <= seq2) ? (seq2 - seq1 + 1) : (seq2 - seq1 + m_iMaxSeqNo + 2);}
00235 
00236    inline static int seqoff(int32_t seq1, int32_t seq2)
00237    {
00238       if (abs(seq1 - seq2) < m_iSeqNoTH)
00239          return seq2 - seq1;
00240 
00241       if (seq1 < seq2)
00242          return seq2 - seq1 - m_iMaxSeqNo - 1;
00243 
00244       return seq2 - seq1 + m_iMaxSeqNo + 1;
00245    }
00246 
00247    inline static int32_t incseq(int32_t seq)
00248    {return (seq == m_iMaxSeqNo) ? 0 : seq + 1;}
00249 
00250    inline static int32_t decseq(int32_t seq)
00251    {return (seq == 0) ? m_iMaxSeqNo : seq - 1;}
00252 
00253    inline static int32_t incseq(int32_t seq, int32_t inc)
00254    {return (m_iMaxSeqNo - seq >= inc) ? seq + inc : seq - m_iMaxSeqNo + inc - 1;}
00255 
00256 public:
00257    static const int32_t m_iSeqNoTH;             // threshold for comparing seq. no.
00258    static const int32_t m_iMaxSeqNo;            // maximum sequence number used in UDT
00259 };
00260 
00262 
00263 // UDT ACK Sub-sequence Number: 0 - (2^31 - 1)
00264 
00265 class CAckNo
00266 {
00267 public:
00268    inline static int32_t incack(int32_t ackno)
00269    {return (ackno == m_iMaxAckSeqNo) ? 0 : ackno + 1;}
00270 
00271 public:
00272    static const int32_t m_iMaxAckSeqNo;         // maximum ACK sub-sequence number used in UDT
00273 };
00274 
00276 
00277 // UDT Message Number: 0 - (2^29 - 1)
00278 
00279 class CMsgNo
00280 {
00281 public:
00282    inline static int msgcmp(int32_t msgno1, int32_t msgno2)
00283    {return (abs(msgno1 - msgno2) < m_iMsgNoTH) ? (msgno1 - msgno2) : (msgno2 - msgno1);}
00284 
00285    inline static int msglen(int32_t msgno1, int32_t msgno2)
00286    {return (msgno1 <= msgno2) ? (msgno2 - msgno1 + 1) : (msgno2 - msgno1 + m_iMaxMsgNo + 2);}
00287 
00288    inline static int msgoff(int32_t msgno1, int32_t msgno2)
00289    {
00290       if (abs(msgno1 - msgno2) < m_iMsgNoTH)
00291          return msgno2 - msgno1;
00292 
00293       if (msgno1 < msgno2)
00294          return msgno2 - msgno1 - m_iMaxMsgNo - 1;
00295 
00296       return msgno2 - msgno1 + m_iMaxMsgNo + 1;
00297    }
00298 
00299    inline static int32_t incmsg(int32_t msgno)
00300    {return (msgno == m_iMaxMsgNo) ? 0 : msgno + 1;}
00301 
00302 public:
00303    static const int32_t m_iMsgNoTH;             // threshold for comparing msg. no.
00304    static const int32_t m_iMaxMsgNo;            // maximum message number used in UDT
00305 };
00306 
00308 
00309 struct CIPAddress
00310 {
00311    static bool ipcmp(const sockaddr* addr1, const sockaddr* addr2, int ver = AF_INET);
00312    static void ntop(const sockaddr* addr, uint32_t ip[4], int ver = AF_INET);
00313    static void pton(sockaddr* addr, const uint32_t ip[4], int ver = AF_INET);
00314 };
00315 
00317 
00318 struct CMD5
00319 {
00320    static void compute(const char* input, unsigned char result[16]);
00321 };
00322 
00323 
00324 #endif

Generated on 9 Feb 2013 for barchart-udt-core-2.2.2 by  doxygen 1.6.1