Quellcode durchsuchen

schev: Working on schev subsystem

Serj Kalichev vor 3 Jahren
Ursprung
Commit
859d270a96
3 geänderte Dateien mit 97 neuen und 29 gelöschten Zeilen
  1. 65 26
      faux/schev/ev.c
  2. 7 0
      faux/schev/private.h
  3. 25 3
      faux/schev/schev.c

+ 65 - 26
faux/schev/ev.c

@@ -47,18 +47,12 @@ faux_ev_t *faux_ev_new(const struct timespec *time, int ev_id, void *data)
 		return NULL;
 
 	// Initialize
-	if (time) {
-		ev->time = *time;
-	} else {
-		struct timespec t = {};
-		clock_gettime(FAUX_SCHEV_CLOCK_SOURCE, &t);
-		ev->time = t;
-	}
 	ev->id = ev_id;
 	ev->data = data;
 	ev->periodic = FAUX_SCHEV_ONCE; // Not periodic by default
 	ev->cycles_num = 0;
 	faux_nsec_to_timespec(&ev->interval, 0l);
+	faux_ev_reschedule(ev, time);
 
 	return ev;
 }
@@ -74,49 +68,94 @@ void faux_ev_free(void *ptr)
 }
 
 
-int faux_ev_periodic(faux_ev_t *schev,
-	struct timespec *interval, int cycles_num)
+int faux_ev_periodic(faux_ev_t *ev,
+	const struct timespec *interval, int cycles_num)
 {
-	assert(schev);
+	assert(ev);
 	assert(interval);
 	// When cycles_num == 0 then periodic has no meaning
-	if (!schev || !interval || cycles_num == 0)
+	if (!ev || !interval || cycles_num == 0)
 		return -1;
 
-	schev->periodic = FAUX_SCHEV_PERIODIC;
-	schev->cycles_num = cycles_num;
-	schev->interval = *interval;
+	ev->periodic = FAUX_SCHEV_PERIODIC;
+	ev->cycles_num = cycles_num;
+	ev->interval = *interval;
 
 	return 0;
 }
 
 
-faux_schev_periodic_t faux_ev_is_periodic(faux_ev_t *schev)
+faux_schev_periodic_t faux_ev_is_periodic(faux_ev_t *ev)
 {
-	assert(schev);
-	if (!schev)
+	assert(ev);
+	if (!ev)
 		return FAUX_SCHEV_ONCE;
 
-	return schev->periodic;
+	return ev->periodic;
 }
 
-int faux_ev_dec_cycles(faux_ev_t *schev, int *new_cycles_num)
+
+int faux_ev_dec_cycles(faux_ev_t *ev, int *new_cycles_num)
 {
-	assert(schev);
-	if (!schev)
+	assert(ev);
+	if (!ev)
 		return -1;
-	if (schev->periodic != FAUX_SCHEV_PERIODIC)
+	if (ev->periodic != FAUX_SCHEV_PERIODIC)
 		return -1; // Non-periodic event
-	if ((schev->cycles_num != FAUX_SCHEV_CYCLES_INFINITE) &&
-		(schev->cycles_num > 0))
-		schev->cycles_num--;
+	if ((ev->cycles_num != FAUX_SCHEV_CYCLES_INFINITE) &&
+		(ev->cycles_num > 0))
+		ev->cycles_num--;
 
 	if (new_cycles_num)
-		*new_cycles_num = schev->cycles_num;
+		*new_cycles_num = ev->cycles_num;
 
 	return 0;
 }
 
+/**
+ *
+ * Note: faux_ev_new() use it. Be carefull.
+ */
+int faux_ev_reschedule(faux_ev_t *ev, const struct timespec *new_time)
+{
+	assert(ev);
+	if (!ev)
+		return -1;
+
+	if (new_time) {
+		ev->time = *new_time;
+	} else { // Time isn't given so use "NOW"
+		struct timespec t = {};
+		clock_gettime(FAUX_SCHEV_CLOCK_SOURCE, &t);
+		ev->time = t;
+	}
+
+	return 0;
+}
+
+
+int faux_ev_reschedule_interval(faux_ev_t *ev)
+{
+	struct timespec new_time = {};
+
+	assert(ev);
+	if (!ev)
+		return -1;
+	if (ev->periodic != FAUX_SCHEV_PERIODIC)
+		return -1;
+	if (0 == ev->cycles_num)
+		return -1;
+
+	faux_timespec_sum(&new_time, &ev->time, &ev->interval);
+	faux_ev_reschedule(ev, &new_time);
+
+	if (ev->cycles_num != FAUX_SCHEV_CYCLES_INFINITE)
+		faux_ev_dec_cycles(ev, NULL);
+
+	return 0;
+}
+
+
 int faux_ev_id(const faux_ev_t *ev)
 {
 	assert(ev);

+ 7 - 0
faux/schev/private.h

@@ -29,8 +29,15 @@ int faux_ev_compare_data(const void *key, const void *list_item);
 faux_ev_t *faux_ev_new(const struct timespec *time, int ev_id, void *data);
 void faux_ev_free(void *ptr);
 
+int faux_ev_periodic(faux_ev_t *ev,
+	const struct timespec *interval, int cycles_num);
+int faux_ev_dec_cycles(faux_ev_t *ev, int *new_cycles_num);
+int faux_ev_reschedule(faux_ev_t *ev, const struct timespec *new_time);
+int faux_ev_reschedule_interval(faux_ev_t *ev);
+
 int faux_ev_id(const faux_ev_t *ev);
 void *faux_ev_data(const faux_ev_t *ev);
 const struct timespec *faux_ev_time(const faux_ev_t *ev);
+faux_schev_periodic_t faux_ev_is_periodic(faux_ev_t *ev);
 
 C_DECL_END

+ 25 - 3
faux/schev/schev.c

@@ -52,16 +52,20 @@ void faux_schev_free(faux_schev_t *schev)
 }
 
 
-/** @brief Adds event to scheduling list using absolute time.
+/** @brief Internal function to add event to scheduling list.
  *
  * @param [in] sched Allocated and initialized sched object.
  * @param [in] time Absolute time of future event.
  * @param [in] Event ID.
  * @param [in] Pointer to arbitrary data linked to event.
+ * @param [in] Periodic flag.
+ * @param [in] Number of cycles (FAUX_SCHEV_CYCLES_INFINITE for infinite).
+ * @param [in] Periodic interval.
  * @return 0 - success, < 0 on error.
  */
-int faux_schev_schedule(
-	faux_schev_t *schev, const struct timespec *time, int ev_id, void *data)
+static int _schev_schedule(faux_schev_t *schev, const struct timespec *time,
+	int ev_id, void *data, faux_schev_periodic_t periodic,
+	const struct timespec *interval, int cycles_num)
 {
 	faux_ev_t *ev = NULL;
 	faux_list_node_t *node = NULL;
@@ -74,6 +78,8 @@ int faux_schev_schedule(
 	assert(ev);
 	if (!ev)
 		return -1;
+	if (FAUX_SCHEV_PERIODIC == periodic)
+		faux_ev_periodic(ev, interval, cycles_num);
 
 	node = faux_list_add(schev->list, ev);
 	if (!node) { // Something went wrong
@@ -85,6 +91,22 @@ int faux_schev_schedule(
 }
 
 
+/** @brief Adds non-periodic event to scheduling list using absolute time.
+ *
+ * @param [in] sched Allocated and initialized sched object.
+ * @param [in] time Absolute time of future event.
+ * @param [in] Event ID.
+ * @param [in] Pointer to arbitrary data linked to event.
+ * @return 0 - success, < 0 on error.
+ */
+int faux_schev_schedule(
+	faux_schev_t *schev, const struct timespec *time, int ev_id, void *data)
+{
+	return _schev_schedule(schev, time, ev_id, data,
+		FAUX_SCHEV_ONCE, NULL, 0);
+}
+
+
 /** @brief Adds event to scheduling list using interval.
  *
  * Add interval to the list. The absolute time is calculated by