/* * Copyright (c) 1996 Gunther Schadow. All rights reserved. * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */ #ifndef CXX_TIMER_H #define CXX_TIMER_H #pragma interface #include #include #include // Just a C++ wrapper to attach a few calculus operators to // struct timeval class Timeval: public timeval { public: Timeval() { tv_sec = 0; tv_usec = 0; } Timeval(const timeval &x) { tv_sec=x.tv_sec; tv_usec=x.tv_usec; } Timeval(u_int sec, u_int usec = 0); const Timeval &operator += (const Timeval &x); const Timeval &operator -= (const Timeval &x); const Timeval &operator *= (u_int x); static const Timeval null; }; // NOTE: the class Timer should currently not be instantiated // on the heap since this may lead to restoring the wrong timer // on delete. class Timer { public: typedef void (*alrmhdl_t)(Timer&); Timer(u_int vsec, u_int vusec = 0, u_int isec = 0, u_int iusec = 0, alrmhdl_t hdl = NULL); ~Timer(); void get(u_int *vsec, u_int *vusec = NULL, u_int *isec = NULL, u_int *iusec = NULL) const; int expired() { return _alrm_cnt; } static void sleep(u_int vsec, u_int vusec = 0); private: u_long _alrm_cnt; itimerval _val; itimerval _orig_val; Timeval _init_val; alrmhdl_t _handler; class Timer *_orig_timer; static class Signal _sigalrm; static class Timer *_active_timer; static int alrmsuperhandler(class Signal&); private: Timer(const Timer &); const Timer &operator=(const Timer &); }; #endif