Browse Source

faux.time: faux_timespec_now() and faux_timespec_before_now()

Serj Kalichev 3 years ago
parent
commit
83084e7807
6 changed files with 75 additions and 17 deletions
  1. 19 0
      faux/sched.h
  2. 2 4
      faux/sched/ev.c
  3. 1 3
      faux/sched/private.h
  4. 7 9
      faux/sched/sched.c
  5. 5 1
      faux/time.h
  6. 41 0
      faux/time/time.c

+ 19 - 0
faux/sched.h

@@ -9,6 +9,7 @@
 #include <faux/time.h>
 
 #define FAUX_SCHED_NOW NULL
+#define FAUX_SCHED_CYCLES_INFINITE (-1)
 
 typedef enum {
 	FAUX_SCHED_PERIODIC = BOOL_TRUE,
@@ -22,6 +23,24 @@ typedef faux_list_node_t faux_sched_node_t;
 
 C_DECL_BEGIN
 
+faux_sched_t *faux_sched_new(void);
+void faux_sched_free(faux_sched_t *sched);
+
+int faux_sched_once(
+	faux_sched_t *sched, const struct timespec *time, int ev_id, void *data);
+int faux_sched_once_delayed(faux_sched_t *sched,
+	const struct timespec *interval, int ev_id, void *data);
+int faux_sched_periodic(
+	faux_sched_t *sched, const struct timespec *time, int ev_id, void *data,
+	const struct timespec *period, int cycle_num);
+int faux_sched_periodic_delayed(
+	faux_sched_t *sched, int ev_id, void *data,
+	const struct timespec *period, int cycle_num);
+int faux_sched_next_interval(faux_sched_t *sched, struct timespec *interval);
+void faux_sched_empty(faux_sched_t *sched);
+int faux_sched_pop(faux_sched_t *sched, int *ev_id, void **data);
+int faux_sched_remove_by_id(faux_sched_t *sched, int id);
+int faux_sched_remove_by_data(faux_sched_t *sched, void *data);
 
 C_DECL_END
 

+ 2 - 4
faux/sched/ev.c

@@ -125,9 +125,7 @@ int faux_ev_reschedule(faux_ev_t *ev, const struct timespec *new_time)
 	if (new_time) {
 		ev->time = *new_time;
 	} else { // Time isn't given so use "NOW"
-		struct timespec t = {};
-		clock_gettime(FAUX_SCHED_CLOCK_SOURCE, &t);
-		ev->time = t;
+		faux_timespec_now(&ev->time);
 	}
 
 	return 0;
@@ -165,7 +163,7 @@ int faux_ev_time_left(faux_ev_t *ev, struct timespec *left)
 	if (!ev || !left)
 		return -1;
 
-	clock_gettime(FAUX_SCHED_CLOCK_SOURCE, &now);
+	faux_timespec_now(&now);
 	if (faux_timespec_cmp(&now, &ev->time) > 0) { // Already happend
 		faux_nsec_to_timespec(left, 0l);
 		return 0;

+ 1 - 3
faux/sched/private.h

@@ -3,9 +3,6 @@
 #include "faux/time.h"
 #include "faux/sched.h"
 
-#define FAUX_SCHED_CLOCK_SOURCE CLOCK_MONOTONIC
-
-#define FAUX_SCHED_CYCLES_INFINITE (-1)
 
 struct faux_ev_s {
 	struct timespec time; // Planned time of event
@@ -20,6 +17,7 @@ struct faux_sched_s {
 	faux_list_t *list;
 };
 
+
 C_DECL_BEGIN
 
 int faux_ev_compare(const void *first, const void *second);

+ 7 - 9
faux/sched/sched.c

@@ -131,7 +131,7 @@ int faux_sched_once(
 int faux_sched_once_delayed(faux_sched_t *sched,
 	const struct timespec *interval, int ev_id, void *data)
 {
-	struct timespec t = {};
+	struct timespec now = {};
 	struct timespec plan = {};
 
 	assert(sched);
@@ -140,8 +140,8 @@ int faux_sched_once_delayed(faux_sched_t *sched,
 
 	if (!interval)
 		return faux_sched_once(sched, FAUX_SCHED_NOW, ev_id, data);
-	clock_gettime(FAUX_SCHED_CLOCK_SOURCE, &t);
-	faux_timespec_sum(&plan, &t, interval);
+	faux_timespec_now(&now);
+	faux_timespec_sum(&plan, &now, interval);
 
 	return faux_sched_once(sched, &plan, ev_id, data);
 }
@@ -179,7 +179,7 @@ int faux_sched_periodic_delayed(
 	faux_sched_t *sched, int ev_id, void *data,
 	const struct timespec *period, int cycle_num)
 {
-	struct timespec t = {};
+	struct timespec now = {};
 	struct timespec plan = {};
 
 	assert(sched);
@@ -187,8 +187,8 @@ int faux_sched_periodic_delayed(
 	if (!sched || !period)
 		return -1;
 
-	clock_gettime(FAUX_SCHED_CLOCK_SOURCE, &t);
-	faux_timespec_sum(&plan, &t, period);
+	faux_timespec_now(&now);
+	faux_timespec_sum(&plan, &now, period);
 	return faux_sched_periodic(sched, &plan, ev_id, data,
 		period, cycle_num);
 }
@@ -239,7 +239,6 @@ void faux_sched_empty(faux_sched_t *sched)
  */
 int faux_sched_pop(faux_sched_t *sched, int *ev_id, void **data)
 {
-	struct timespec now = {};
 	faux_list_node_t *iter = NULL;
 	faux_ev_t *ev = NULL;
 
@@ -251,8 +250,7 @@ int faux_sched_pop(faux_sched_t *sched, int *ev_id, void **data)
 	if (!iter)
 		return -1;
 	ev = (faux_ev_t *)faux_list_data(iter);
-	clock_gettime(FAUX_SCHED_CLOCK_SOURCE, &now);
-	if (faux_timespec_cmp(faux_ev_time(ev), &now) > 0)
+	if (!faux_timespec_before_now(faux_ev_time(ev)))
 		return -1; // No events for this time
 	faux_list_takeaway(sched->list, iter); // Remove entry from list
 

+ 5 - 1
faux/time.h

@@ -11,6 +11,8 @@
 
 #include <faux/faux.h>
 
+#define FAUX_CLOCK_SOURCE CLOCK_MONOTONIC
+
 C_DECL_BEGIN
 
 // Operations for struct timespec
@@ -19,8 +21,10 @@ int faux_timespec_diff(struct timespec *res,
 	const struct timespec *val1, const struct timespec *val2);
 int faux_timespec_sum(struct timespec *res,
 	const struct timespec *val1, const struct timespec *val2);
+int faux_timespec_now(struct timespec *now);
+bool_t faux_timespec_before_now(const struct timespec *ts);
 
-// Conversations of struct timespec
+// Convertions of struct timespec
 uint64_t faux_timespec_to_nsec(const struct timespec *ts);
 void faux_nsec_to_timespec(struct timespec *ts, uint64_t nsec);
 

+ 41 - 0
faux/time/time.c

@@ -136,3 +136,44 @@ void faux_nsec_to_timespec(struct timespec *ts, uint64_t nsec)
 	ts->tv_sec = (time_t)(nsec / 1000000000l);
 	ts->tv_nsec = (long)(nsec % 1000000000l);
 }
+
+
+/** @brief Returns current time (now).
+ *
+ * @param [out] now The struct timespec to save current time.
+ * @return 0 - success, < 0 on error.
+ */
+int faux_timespec_now(struct timespec *now)
+{
+	assert(now);
+	if (!now)
+		return -1;
+
+	clock_gettime(FAUX_CLOCK_SOURCE, now);
+
+	return 0;
+}
+
+
+/** @brief Indicates if specified struct timespec is before now.
+ *
+ * The equality to current time (now) is considered as already
+ * coming time.
+ *
+ * @param [in] ts The struct timespec to compare.
+ * @return BOOL_TRUE if timespec is before now else BOOL_FALSE.
+ */
+bool_t faux_timespec_before_now(const struct timespec *ts)
+{
+	struct timespec now = {};
+
+	assert(ts);
+	if (!ts)
+		return BOOL_FALSE;
+
+	faux_timespec_now(&now);
+	if (faux_timespec_cmp(&now, ts) >= 0) // Already happend
+		return BOOL_TRUE;
+
+	return BOOL_FALSE;
+}