Browse Source

faux.sched,eloop: Some refactoring

Serj Kalichev 3 years ago
parent
commit
73b4a340e8
4 changed files with 69 additions and 95 deletions
  1. 6 4
      faux/eloop.h
  2. 28 38
      faux/eloop/eloop.c
  3. 3 4
      faux/sched.h
  4. 32 49
      faux/sched/sched.c

+ 6 - 4
faux/eloop.h

@@ -6,6 +6,7 @@
 #define _faux_eloop_h
 
 #include <faux/faux.h>
+#include <faux/sched.h>
 
 typedef struct faux_eloop_s faux_eloop_t;
 
@@ -18,6 +19,7 @@ typedef enum {
 
 typedef struct {
 	int ev_id;
+	faux_ev_t *ev;
 } faux_eloop_info_sched_t;
 
 typedef struct {
@@ -45,14 +47,14 @@ bool_t faux_eloop_del_fd(faux_eloop_t *eloop, int fd);
 bool_t faux_eloop_add_signal(faux_eloop_t *eloop, int signo,
 	faux_eloop_cb_f event_cb, void *user_data);
 bool_t faux_eloop_del_signal(faux_eloop_t *eloop, int signo);
-bool_t faux_eloop_add_sched_once(faux_eloop_t *eloop, const struct timespec *time,
+faux_ev_t *faux_eloop_add_sched_once(faux_eloop_t *eloop, const struct timespec *time,
 	int ev_id, faux_eloop_cb_f event_cb, void *data);
-bool_t faux_eloop_add_sched_once_delayed(faux_eloop_t *eloop, const struct timespec *interval,
+faux_ev_t *faux_eloop_add_sched_once_delayed(faux_eloop_t *eloop, const struct timespec *interval,
 	int ev_id, faux_eloop_cb_f event_cb, void *data);
-bool_t faux_eloop_add_sched_periodic(faux_eloop_t *eloop, const struct timespec *time,
+faux_ev_t *faux_eloop_add_sched_periodic(faux_eloop_t *eloop, const struct timespec *time,
 	int ev_id, faux_eloop_cb_f event_cb, void *data,
 	const struct timespec *period, unsigned int cycle_num);
-bool_t faux_eloop_add_sched_periodic_delayed(faux_eloop_t *eloop,
+faux_ev_t *faux_eloop_add_sched_periodic_delayed(faux_eloop_t *eloop,
 	int ev_id, faux_eloop_cb_f event_cb, void *data,
 	const struct timespec *period, unsigned int cycle_num);
 bool_t faux_eloop_del_sched(faux_eloop_t *eloop, int id);

+ 28 - 38
faux/eloop/eloop.c

@@ -164,8 +164,7 @@ bool_t faux_eloop_loop(faux_eloop_t *eloop)
 	sigprocmask(SIG_SETMASK, &blocked_signals, &orig_sig_set);
 
 #ifdef HAVE_SIGNALFD
-	// Create Linux-specific signal file descriptor. Wait for all signals.
-	// Unneeded signals will be filtered out later.
+	// Create Linux-specific signal file descriptor. Wait for signals.
 	eloop->signal_fd = signalfd(eloop->signal_fd, &eloop->sig_set,
 		SIGNALFD_FLAGS);
 	faux_pollfd_add(eloop->pollfds, eloop->signal_fd, POLLIN);
@@ -248,14 +247,18 @@ bool_t faux_eloop_loop(faux_eloop_t *eloop)
 				faux_eloop_cb_f event_cb = context->event_cb;
 				void *user_data = context->user_data;
 
-				if (!faux_ev_is_busy(ev))
+				if (!faux_ev_is_busy(ev)) {
 					faux_ev_free(ev);
+					ev = NULL;
+				}
 				if (!event_cb)
 					event_cb = eloop->default_event_cb;
 				if (!event_cb) // Callback is not defined
 					continue;
 				info.ev_id = ev_id;
-
+				// Callback will get only rescheduled event object.
+				// If event is not scheduled, callback will get NULL.
+				info.ev = ev;
 				// Execute callback
 				r = event_cb(eloop, FAUX_ELOOP_SCHED, &info,
 					user_data);
@@ -266,7 +269,6 @@ bool_t faux_eloop_loop(faux_eloop_t *eloop)
 			continue;
 		}
 
-
 		// File descriptor
 		faux_pollfd_init_iterator(eloop->pollfds, &pollfd_iter);
 		while ((pollfd = faux_pollfd_each_active(eloop->pollfds, &pollfd_iter))) {
@@ -314,7 +316,7 @@ bool_t faux_eloop_loop(faux_eloop_t *eloop)
 				continue; // Another fds are common, not signal
 			}
 
-			// Prepare event data
+			// File descriptor
 			entry = (faux_eloop_fd_t *)faux_list_kfind(eloop->fds, &fd);
 			assert(entry);
 			if (!entry) // Something went wrong
@@ -512,7 +514,7 @@ static faux_eloop_context_t *faux_eloop_new_context(
 }
 
 
-bool_t faux_eloop_add_sched_once(faux_eloop_t *eloop, const struct timespec *time,
+faux_ev_t *faux_eloop_add_sched_once(faux_eloop_t *eloop, const struct timespec *time,
 	int ev_id, faux_eloop_cb_f event_cb, void *data)
 {
 	faux_eloop_context_t *context = NULL;
@@ -520,27 +522,24 @@ bool_t faux_eloop_add_sched_once(faux_eloop_t *eloop, const struct timespec *tim
 
 	assert(eloop);
 	if (!eloop)
-		return BOOL_FALSE;
-
-	if (faux_sched_id_exist(eloop->sched, ev_id))
-		return BOOL_FALSE; // ID must be unique
+		return NULL;
 
 	context = faux_eloop_new_context(event_cb, data);
 	assert(context);
 	if (!context)
-		return BOOL_FALSE;
+		return NULL;
 
 	if (!(ev = faux_sched_once(eloop->sched, time, ev_id, context))) {
 		faux_free(context);
-		return BOOL_FALSE;
+		return NULL;
 	}
 	faux_ev_set_free_data_cb(ev, faux_free);
 
-	return BOOL_TRUE;
+	return ev;
 }
 
 
-bool_t faux_eloop_add_sched_once_delayed(faux_eloop_t *eloop, const struct timespec *interval,
+faux_ev_t *faux_eloop_add_sched_once_delayed(faux_eloop_t *eloop, const struct timespec *interval,
 	int ev_id, faux_eloop_cb_f event_cb, void *data)
 {
 	faux_eloop_context_t *context = NULL;
@@ -548,27 +547,24 @@ bool_t faux_eloop_add_sched_once_delayed(faux_eloop_t *eloop, const struct times
 
 	assert(eloop);
 	if (!eloop)
-		return BOOL_FALSE;
-
-	if (faux_sched_id_exist(eloop->sched, ev_id))
-		return BOOL_FALSE; // ID must be unique
+		return NULL;
 
 	context = faux_eloop_new_context(event_cb, data);
 	assert(context);
 	if (!context)
-		return BOOL_FALSE;
+		return NULL;
 
 	if (!(ev = faux_sched_once_delayed(eloop->sched, interval, ev_id, context))) {
 		faux_free(context);
-		return BOOL_FALSE;
+		return NULL;
 	}
 	faux_ev_set_free_data_cb(ev, faux_free);
 
-	return BOOL_TRUE;
+	return ev;
 }
 
 
-bool_t faux_eloop_add_sched_periodic(faux_eloop_t *eloop, const struct timespec *time,
+faux_ev_t *faux_eloop_add_sched_periodic(faux_eloop_t *eloop, const struct timespec *time,
 	int ev_id, faux_eloop_cb_f event_cb, void *data,
 	const struct timespec *period, unsigned int cycle_num)
 {
@@ -577,28 +573,25 @@ bool_t faux_eloop_add_sched_periodic(faux_eloop_t *eloop, const struct timespec
 
 	assert(eloop);
 	if (!eloop)
-		return BOOL_FALSE;
-
-	if (faux_sched_id_exist(eloop->sched, ev_id))
-		return BOOL_FALSE; // ID must be unique
+		return NULL;
 
 	context = faux_eloop_new_context(event_cb, data);
 	assert(context);
 	if (!context)
-		return BOOL_FALSE;
+		return NULL;
 
 	if (!(ev = faux_sched_periodic(eloop->sched, time, ev_id, context,
 		period, cycle_num))) {
 		faux_free(context);
-		return BOOL_FALSE;
+		return NULL;
 	}
 	faux_ev_set_free_data_cb(ev, faux_free);
 
-	return BOOL_TRUE;
+	return ev;
 }
 
 
-bool_t faux_eloop_add_sched_periodic_delayed(faux_eloop_t *eloop,
+faux_ev_t *faux_eloop_add_sched_periodic_delayed(faux_eloop_t *eloop,
 	int ev_id, faux_eloop_cb_f event_cb, void *data,
 	const struct timespec *period, unsigned int cycle_num)
 {
@@ -607,24 +600,21 @@ bool_t faux_eloop_add_sched_periodic_delayed(faux_eloop_t *eloop,
 
 	assert(eloop);
 	if (!eloop)
-		return BOOL_FALSE;
-
-	if (faux_sched_id_exist(eloop->sched, ev_id))
-		return BOOL_FALSE; // ID must be unique
+		return NULL;
 
 	context = faux_eloop_new_context(event_cb, data);
 	assert(context);
 	if (!context)
-		return BOOL_FALSE;
+		return NULL;
 
 	if (!(ev = faux_sched_periodic_delayed(eloop->sched, ev_id, context,
 		period, cycle_num))) {
 		faux_free(context);
-		return BOOL_FALSE;
+		return NULL;
 	}
 	faux_ev_set_free_data_cb(ev, faux_free);
 
-	return BOOL_TRUE;
+	return ev;
 }
 
 

+ 3 - 4
faux/sched.h

@@ -58,10 +58,9 @@ faux_ev_t *faux_sched_pop(faux_sched_t *sched);
 ssize_t faux_sched_del(faux_sched_t *sched, faux_ev_t *ev);
 ssize_t faux_sched_del_by_id(faux_sched_t *sched, int id);
 ssize_t faux_sched_del_by_data(faux_sched_t *sched, void *data);
-
-const struct timespec *faux_sched_time_by_data(faux_sched_t *sched, void *data);
-bool_t faux_sched_id_exist(faux_sched_t *sched, int id);
-bool_t faux_sched_get_by_id(faux_sched_t *sched, int ev_id, void **data,
+faux_ev_t *faux_sched_get_by_id(faux_sched_t *sched, int ev_id,
+	faux_list_node_t **saved);
+faux_ev_t *faux_sched_get_by_data(faux_sched_t *sched, void *data,
 	faux_list_node_t **saved);
 
 C_DECL_END

+ 32 - 49
faux/sched/sched.c

@@ -367,78 +367,61 @@ ssize_t faux_sched_del_by_data(faux_sched_t *sched, void *data)
 }
 
 
-/** @brief Report does entry with specified ID already exist.
+/** @brief Get scheduled event by specified value.
  *
- * @param [in] sched Allocated and initialized sched object.
- * @param [in] id ID.
- * @return BOOL_TRUE - exist, BOOL_FALSE - doesn't exist.
- */
-bool_t faux_sched_id_exist(faux_sched_t *sched, int id)
-{
-	assert(sched);
-	if (!sched)
-		return -1;
-
-	if (faux_list_match_node(sched->list, faux_ev_compare_id, &id, NULL))
-		return BOOL_TRUE;
-
-	return BOOL_FALSE;
-}
-
-
-/** @brief Get sched entries with specified ID.
+ * Static function.
  *
  * @param [in] sched Allocated and initialized sched object.
- * @param [in] id ID to search for.
- * @param [out] data Return user data.
+ * @param [in] value Value to search for.
+ * @param [in] cmp_f Callback to compare key and entry.
  * @param [in,out] saved Iterator.
- * @return BOOL_TRUE - found, BOOL_FALSE - not found.
+ * @return Event (faux_ev_t) pointer or NULL on error or not found.
  */
-bool_t faux_sched_get_by_id(faux_sched_t *sched, int ev_id, void **data,
-	faux_list_node_t **saved)
+static faux_ev_t *faux_sched_get_by_something(faux_sched_t *sched, void *value,
+	faux_list_kcmp_fn cmp_f, faux_list_node_t **saved)
 {
 	faux_list_node_t *node = NULL;
 	faux_ev_t *ev = NULL;
 
 	assert(sched);
 	if (!sched)
-		return BOOL_FALSE;
+		return NULL;
 
-	node = faux_list_match_node(sched->list,
-		faux_ev_compare_id, &ev_id, saved);
+	node = faux_list_match_node(sched->list, cmp_f, value, saved);
 	if (!node)
-		return BOOL_FALSE;
+		return NULL;
 
 	ev = (faux_ev_t *)faux_list_data(node);
-	if (data)
-		*data = faux_ev_data(ev);
 
-	return BOOL_TRUE;
+	return ev;
 }
 
 
-/** @brief Get time of events with specified data pointer from list.
+/** @brief Get sched entries with specified event ID.
  *
  * @param [in] sched Allocated and initialized sched object.
- * @param [in] data Data to search entries to remove.
- * @return Number of removed entries or < 0 on error.
+ * @param [in] ev_id Event ID to search for.
+ * @param [in,out] saved Iterator.
+ * @return Event (faux_ev_t) pointer or NULL on error or not found.
  */
-const struct timespec *faux_sched_time_by_data(faux_sched_t *sched, void *data)
+faux_ev_t *faux_sched_get_by_id(faux_sched_t *sched, int ev_id,
+	faux_list_node_t **saved)
 {
-	faux_list_node_t *node = NULL;
-	faux_ev_t *ev = NULL;
-
-	assert(sched);
-	if (!sched)
-		return NULL;
-
-	node = faux_list_match_node(sched->list, faux_ev_compare_data, data, NULL);
-	if (!node)
-		return NULL;
+	return faux_sched_get_by_something(sched, &ev_id,
+		faux_ev_compare_id, saved);
+}
 
-	ev = faux_list_data(node);
-	if (!ev)
-		return NULL;
 
-	return faux_ev_time(ev);
+/** @brief Get sched entries with specified user data pointer.
+ *
+ * @param [in] sched Allocated and initialized sched object.
+ * @param [in] data Pointer to user data to search for.
+ * @param [in,out] saved Iterator.
+ * @return Event (faux_ev_t) pointer or NULL on error or not found.
+ */
+faux_ev_t *faux_sched_get_by_data(faux_sched_t *sched, void *data,
+	faux_list_node_t **saved)
+{
+	return faux_sched_get_by_something(sched, data,
+		faux_ev_compare_data, saved);
 }