Home | History | Annotate | Download | only in include
      1 /******************************************************************************
      2  *
      3  *  Copyright (C) 2014 Google, Inc.
      4  *
      5  *  Licensed under the Apache License, Version 2.0 (the "License");
      6  *  you may not use this file except in compliance with the License.
      7  *  You may obtain a copy of the License at:
      8  *
      9  *  http://www.apache.org/licenses/LICENSE-2.0
     10  *
     11  *  Unless required by applicable law or agreed to in writing, software
     12  *  distributed under the License is distributed on an "AS IS" BASIS,
     13  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     14  *  See the License for the specific language governing permissions and
     15  *  limitations under the License.
     16  *
     17  ******************************************************************************/
     18 
     19 #pragma once
     20 
     21 #include <stdbool.h>
     22 
     23 #include "alarm.h"
     24 
     25 typedef struct non_repeating_timer_t non_repeating_timer_t;
     26 
     27 // Creates a new non repeating timer of |duration| with callback |cb|. |cb| will be
     28 // called back in the context of an upspecified thread, and may not be NULL. |data|
     29 // is a context variable for the callback and may be NULL. The returned object must
     30 // be freed by calling |non_repeating_timer_free|. Returns NULL on failure.
     31 non_repeating_timer_t *non_repeating_timer_new(period_ms_t duration, alarm_callback_t cb, void *data);
     32 
     33 // Frees a non repeating timer created by |non_repeating_timer_new|. |timer| may be
     34 // NULL. If the timer is currently running, it will be cancelled. It is not safe to
     35 // call |non_repeating_timer_free| from inside the callback of a non repeating timer.
     36 void non_repeating_timer_free(non_repeating_timer_t *timer);
     37 
     38 // Restarts the non repeating timer. If it is currently running, the timer is reset.
     39 // |timer| may not be NULL.
     40 void non_repeating_timer_restart(non_repeating_timer_t *timer);
     41 
     42 // Restarts the non repeating timer if |condition| is true, otherwise ensures it is
     43 // not running. |timer| may not be NULL.
     44 void non_repeating_timer_restart_if(non_repeating_timer_t *timer, bool condition);
     45 
     46 // Cancels the non repeating timer if it is currently running. All the semantics of
     47 // |alarm_cancel| apply here. |timer| may not be NULL.
     48 void non_repeating_timer_cancel(non_repeating_timer_t *timer);
     49