1  | 
     | 
     | 
    /*	$OpenBSD: timer.c,v 1.1 2016/07/19 16:54:26 reyk Exp $	*/  | 
    
    
    2  | 
     | 
     | 
     | 
    
    
    3  | 
     | 
     | 
    /*  | 
    
    
    4  | 
     | 
     | 
     * Copyright (c) 2010-2016 Reyk Floeter <reyk@openbsd.org>  | 
    
    
    5  | 
     | 
     | 
     *  | 
    
    
    6  | 
     | 
     | 
     * Permission to use, copy, modify, and distribute this software for any  | 
    
    
    7  | 
     | 
     | 
     * purpose with or without fee is hereby granted, provided that the above  | 
    
    
    8  | 
     | 
     | 
     * copyright notice and this permission notice appear in all copies.  | 
    
    
    9  | 
     | 
     | 
     *  | 
    
    
    10  | 
     | 
     | 
     * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES  | 
    
    
    11  | 
     | 
     | 
     * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF  | 
    
    
    12  | 
     | 
     | 
     * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR  | 
    
    
    13  | 
     | 
     | 
     * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES  | 
    
    
    14  | 
     | 
     | 
     * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN  | 
    
    
    15  | 
     | 
     | 
     * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF  | 
    
    
    16  | 
     | 
     | 
     * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.  | 
    
    
    17  | 
     | 
     | 
     */  | 
    
    
    18  | 
     | 
     | 
     | 
    
    
    19  | 
     | 
     | 
    #include <sys/queue.h>  | 
    
    
    20  | 
     | 
     | 
    #include <sys/socket.h>  | 
    
    
    21  | 
     | 
     | 
    #include <sys/uio.h>  | 
    
    
    22  | 
     | 
     | 
     | 
    
    
    23  | 
     | 
     | 
    #include <stdio.h>  | 
    
    
    24  | 
     | 
     | 
    #include <stdlib.h>  | 
    
    
    25  | 
     | 
     | 
    #include <unistd.h>  | 
    
    
    26  | 
     | 
     | 
    #include <string.h>  | 
    
    
    27  | 
     | 
     | 
    #include <errno.h>  | 
    
    
    28  | 
     | 
     | 
    #include <fcntl.h>  | 
    
    
    29  | 
     | 
     | 
    #include <ctype.h>  | 
    
    
    30  | 
     | 
     | 
    #include <event.h>  | 
    
    
    31  | 
     | 
     | 
     | 
    
    
    32  | 
     | 
     | 
    #include "switchd.h"  | 
    
    
    33  | 
     | 
     | 
     | 
    
    
    34  | 
     | 
     | 
    void	 timer_callback(int, short, void *);  | 
    
    
    35  | 
     | 
     | 
     | 
    
    
    36  | 
     | 
     | 
    void  | 
    
    
    37  | 
     | 
     | 
    timer_set(struct switchd *sc, struct timer *tmr,  | 
    
    
    38  | 
     | 
     | 
        void (*cb)(struct switchd *, void *), void *arg)  | 
    
    
    39  | 
     | 
     | 
    { | 
    
    
    40  | 
     | 
     | 
    	tmr->tmr_sc = sc;  | 
    
    
    41  | 
     | 
     | 
    	tmr->tmr_cb = cb;  | 
    
    
    42  | 
     | 
     | 
    	tmr->tmr_cbarg = arg;  | 
    
    
    43  | 
     | 
     | 
    	evtimer_set(&tmr->tmr_ev, timer_callback, tmr);  | 
    
    
    44  | 
     | 
     | 
    }  | 
    
    
    45  | 
     | 
     | 
     | 
    
    
    46  | 
     | 
     | 
    void  | 
    
    
    47  | 
     | 
     | 
    timer_add(struct switchd *sc, struct timer *tmr, int timeout)  | 
    
    
    48  | 
     | 
     | 
    { | 
    
    
    49  | 
     | 
     | 
    	struct timeval		 tv = { timeout }; | 
    
    
    50  | 
     | 
     | 
     | 
    
    
    51  | 
     | 
     | 
    	if (evtimer_initialized(&tmr->tmr_ev) &&  | 
    
    
    52  | 
     | 
     | 
    	    evtimer_pending(&tmr->tmr_ev, NULL))  | 
    
    
    53  | 
     | 
     | 
    		evtimer_del(&tmr->tmr_ev);  | 
    
    
    54  | 
     | 
     | 
     | 
    
    
    55  | 
     | 
     | 
    	evtimer_add(&tmr->tmr_ev, &tv);  | 
    
    
    56  | 
     | 
     | 
    }  | 
    
    
    57  | 
     | 
     | 
     | 
    
    
    58  | 
     | 
     | 
    void  | 
    
    
    59  | 
     | 
     | 
    timer_del(struct switchd *sc, struct timer *tmr)  | 
    
    
    60  | 
     | 
     | 
    { | 
    
    
    61  | 
     | 
     | 
    	if (tmr->tmr_sc == sc && tmr->tmr_cb &&  | 
    
    
    62  | 
     | 
     | 
    	    evtimer_initialized(&tmr->tmr_ev))  | 
    
    
    63  | 
     | 
     | 
    		evtimer_del(&tmr->tmr_ev);  | 
    
    
    64  | 
     | 
     | 
    }  | 
    
    
    65  | 
     | 
     | 
     | 
    
    
    66  | 
     | 
     | 
    void  | 
    
    
    67  | 
     | 
     | 
    timer_callback(int fd, short event, void *arg)  | 
    
    
    68  | 
     | 
     | 
    { | 
    
    
    69  | 
     | 
     | 
    	struct timer	*tmr = arg;  | 
    
    
    70  | 
     | 
     | 
     | 
    
    
    71  | 
     | 
     | 
    	if (tmr->tmr_cb)  | 
    
    
    72  | 
     | 
     | 
    		tmr->tmr_cb(tmr->tmr_sc, tmr->tmr_cbarg);  | 
    
    
    73  | 
     | 
     | 
    }  |