• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
16 static inline int
get_sec_since_last_active(const struct timespec * last_active_ts)17 get_sec_since_last_active(const struct timespec *last_active_ts)
18 {
19 	struct timespec diff;
20 	subtract_timespecs(&now, last_active_ts, &diff);
21 	return diff.tv_sec;
22 }
23 
pic_update_current_time()24 void pic_update_current_time()
25 {
26 	clock_gettime(CLOCK_MONOTONIC_RAW, &now);
27 }
28 
pic_polled_interval_create(int interval_sec)29 struct polled_interval *pic_polled_interval_create(int interval_sec)
30 {
31 	struct polled_interval *pi;
32 	pi = malloc(sizeof(*pi));
33 	pi->last_interval_start_ts = now;
34 	pi->interval_sec = interval_sec;
35 	return pi;
36 }
37 
pic_polled_interval_destroy(struct polled_interval ** interval)38 void pic_polled_interval_destroy(struct polled_interval **interval)
39 {
40 	free(*interval);
41 	*interval = NULL;
42 }
43 
pic_interval_elapsed(const struct polled_interval * pi)44 int pic_interval_elapsed(const struct polled_interval *pi)
45 {
46 	return get_sec_since_last_active(&pi->last_interval_start_ts) >=
47 	       pi->interval_sec;
48 }
49 
pic_interval_reset(struct polled_interval * pi)50 void pic_interval_reset(struct polled_interval *pi)
51 {
52 	pi->last_interval_start_ts = now;
53 }