1 /* Copyright 2018 The Chromium OS Authors. All rights reserved. 2 * Use of this source code is governed by a BSD-style license that can be 3 * found in the LICENSE file. 4 */ 5 6 #include "cras_util.h" 7 #include "polled_interval_checker.h" 8 9 struct polled_interval { 10 struct timespec last_interval_start_ts; 11 int interval_sec; 12 }; 13 14 static struct timespec now; 15 get_sec_since_last_active(const struct timespec * last_active_ts)16static inline int get_sec_since_last_active( 17 const struct timespec *last_active_ts) { 18 struct timespec diff; 19 subtract_timespecs(&now, last_active_ts, &diff); 20 return diff.tv_sec; 21 } 22 pic_update_current_time()23void pic_update_current_time() { 24 clock_gettime(CLOCK_MONOTONIC_RAW, &now); 25 } 26 pic_polled_interval_create(int interval_sec)27struct polled_interval *pic_polled_interval_create(int interval_sec) { 28 struct polled_interval *pi; 29 pi = malloc(sizeof(*pi)); 30 pi->last_interval_start_ts = now; 31 pi->interval_sec = interval_sec; 32 return pi; 33 } 34 pic_polled_interval_destroy(struct polled_interval ** interval)35void pic_polled_interval_destroy(struct polled_interval **interval) { 36 free(*interval); 37 *interval = NULL; 38 } 39 pic_interval_elapsed(const struct polled_interval * pi)40int pic_interval_elapsed(const struct polled_interval *pi) { 41 return get_sec_since_last_active(&pi->last_interval_start_ts) >= 42 pi->interval_sec; 43 } 44 pic_interval_reset(struct polled_interval * pi)45void pic_interval_reset(struct polled_interval *pi) { 46 pi->last_interval_start_ts = now; 47 }