xorp

time_slice.hh

00001 // -*- c-basic-offset: 4; tab-width: 8; indent-tabs-mode: t -*-
00002 // vim:set sts=4 ts=8:
00003 
00004 // Copyright (c) 2001-2009 XORP, Inc.
00005 //
00006 // This program is free software; you can redistribute it and/or modify
00007 // it under the terms of the GNU Lesser General Public License, Version
00008 // 2.1, June 1999 as published by the Free Software Foundation.
00009 // Redistribution and/or modification of this program under the terms of
00010 // any other version of the GNU Lesser General Public License is not
00011 // permitted.
00012 // 
00013 // This program is distributed in the hope that it will be useful, but
00014 // WITHOUT ANY WARRANTY; without even the implied warranty of
00015 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. For more details,
00016 // see the GNU Lesser General Public License, Version 2.1, a copy of
00017 // which can be found in the XORP LICENSE.lgpl file.
00018 // 
00019 // XORP, Inc, 2953 Bunker Hill Lane, Suite 204, Santa Clara, CA 95054, USA;
00020 // http://xorp.net
00021 
00022 // $XORP: xorp/libxorp/time_slice.hh,v 1.13 2008/10/02 21:57:35 bms Exp $
00023 
00024 
00025 #ifndef __LIBXORP_TIME_SLICE_HH__
00026 #define __LIBXORP_TIME_SLICE_HH__
00027 
00028 //
00029 // Time-slice class declaration.
00030 //
00031 
00032 #ifdef HAVE_SYS_TIME_H
00033 #include <sys/time.h>
00034 #endif
00035 
00036 #include "timer.hh"
00037 #include "timeval.hh"
00038 
00039 
00040 //
00041 // Constants definitions
00042 //
00043 
00044 //
00045 // Structures/classes, typedefs and macros
00046 //
00047 
00072 class TimeSlice {
00073 public:
00085     TimeSlice(uint32_t usec_limit, size_t test_iter_frequency);
00086     
00090     void reset();
00091     
00100     bool is_expired();
00101     
00102 private:
00103     TimeVal     _time_slice_limit;  // The time slice to measure
00104     const size_t    _test_iter_frequency;   // The frequency of measuring
00105     
00106     TimeVal     _time_slice_start;  // When the time slice started
00107     size_t      _remain_iter;       // Remaning iterations to
00108                         // check the time
00109 };
00110 
00111 //
00112 // Deferred definitions
00113 //
00114 inline bool
00115 TimeSlice::is_expired()
00116 {
00117     if (--_remain_iter)
00118     return (false);     // Don't test the time yet
00119     
00120     _remain_iter = _test_iter_frequency;
00121     
00122     // Test if the time slice has expired
00123     TimeVal now;
00124     TimerList::system_gettimeofday(&now);
00125     if (now - _time_slice_limit < _time_slice_start)
00126     return (false);     // The time slice has not expired
00127     
00128     // The time slice has expired
00129     _time_slice_start = now;
00130     return (true);
00131 }
00132 
00133 //
00134 // Global variables
00135 //
00136 
00137 //
00138 // Global functions prototypes
00139 //
00140 
00141 #endif // __LIBXORP_TIME_SLICE_HH__
 All Classes Namespaces Functions Variables Typedefs Enumerations