Main Page | Modules | Alphabetical List | Data Structures | Directories | File List | Data Fields | Globals | Related Pages

timer_resched.c

Go to the documentation of this file.
00001 /*
00002 ** Copyright (C) 2005 by Kevin L. Mitchell <klmitch@mit.edu>
00003 **
00004 ** This program is free software; you can redistribute it and/or modify
00005 ** it under the terms of the GNU General Public License as published by
00006 ** the Free Software Foundation; either version 2 of the License, or
00007 ** (at your option) any later version.
00008 **
00009 ** This program is distributed in the hope that it will be useful,
00010 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
00011 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012 ** GNU General Public License for more details.
00013 **
00014 ** You should have received a copy of the GNU General Public License
00015 ** along with this program; if not, write to the Free Software
00016 ** Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00017 **
00018 ** @(#)$Id: timer_resched.c,v 1.2 2005/09/15 18:14:11 klmitch Exp $
00019 */
00027 #include "engines_int.h"
00028 
00029 RCSTAG("@(#)$Id: timer_resched.c,v 1.2 2005/09/15 18:14:11 klmitch Exp $");
00030 
00031 ev_err_t
00032 timer_resched(ev_ctx_t *ctx, ev_tim_t *tim, ev_timtype_t type,
00033               struct timeval *value)
00034 {
00035   ev_engine_t *eng;
00036   ev_err_t err = 0;
00037   struct timeval ov;
00038   int tmp;
00039 
00040   ev_init(); /* make sure library is initialized... */
00041 
00042   /* sanity-check the arguments... */
00043   if (!ec_verify(ctx) || !ti_verify(tim) ||
00044       (type != TT_ABSOLUTE && type != TT_RELATIVE && type != TT_PERIODIC) ||
00045       !value || eg_context(tim) != ctx)
00046     ev_return(EINVAL);
00047 
00048   /* if the generator's been deleted, it's already been removed from the
00049    * engines and from the timer tree...
00050    */
00051   if (!(eg_flags(tim) & EV_GEN_DELETED))
00052     /* Walk through all the timer engines (last to first)... */
00053     for (eng = ctx->ec_timer.el_last; eng;
00054          eng = eng->eng_timer.eti_active.el_prev)
00055       if ((err = eng_tim_rem(ctx, eng, tim))) { /* remove the timer */
00056         for (; eng; eng = eng->eng_timer.eti_active.el_prev)
00057           eng_tim_rem(ctx, eng, tim); /* remove from remaining engines */
00058 
00059         _timer_untree(ctx, tim); /* remove timer from tree */
00060 
00061         eg_flags_set(tim, EV_GEN_DELETED); /* got to delete it... */
00062 
00063         eg_ref_dec(ctx, tim); /* decrement reference count */
00064 
00065         ev_return(err); /* Return the error */
00066       }
00067 
00068   ov = tim->ti_expire; /* save the old timer value... */
00069 
00070   /* Re-configure timer value and recompute expiration time */
00071   tim->ti_type = type;
00072   tim->ti_value = *value;
00073   ti_expire_comp(tim); /* compute expiration time */
00074 
00075   /* Re-add timer to all the engines... */
00076   for (eng = ctx->ec_timer.el_first; eng;
00077        eng = eng->eng_timer.eti_active.el_next)
00078     if ((err = eng_tim_add(ctx, eng, tim))) { /* add to engine */
00079       /* oops, error occurred.  Walk engines backwards and remove timer */
00080       for (; eng; eng = eng->eng_timer.eti_active.el_prev)
00081         eng_tim_rem(ctx, eng, tim);
00082 
00083       _timer_untree(ctx, tim); /* remove timer from tree */
00084 
00085       eg_flags_set(tim, EV_GEN_DELETED); /* got to delete it... */
00086 
00087       eg_ref_dec(ctx, tim); /* decrement reference count */
00088 
00089       ev_return(err); /* return the error */
00090     }
00091 
00092   /* Update the tree... */
00093   if (eg_flags(tim) & EV_GEN_DELETED) /* was timer deleted? */
00094     err = _timer_insert(ctx, tim); /* then re-insert the timer */
00095   else if ((tmp = tv_comp(&tim->ti_expire, &ov)) < 0)
00096     err = _timer_heapify_down(ctx, tim); /* new value is earlier time... */
00097   else if (tmp > 0) /* new value is later time... */
00098     err = _timer_heapify_up(ctx, tim);
00099 
00100   if (err) /* did an error occur? */
00101     timer_destroy(ctx, tim); /* OK, destroy the timer. */
00102   else
00103     eg_flags_clr(tim, EV_GEN_DELETED); /* clear the deleted flag */
00104 
00105   ev_return(err);
00106 }

Generated on Wed Dec 28 23:36:56 2005 for event by  doxygen 1.4.4