Browse Source

faux.eloop: Del all functions for signals, fds, sched

Serj Kalichev 2 years ago
parent
commit
45d9fa9d94
3 changed files with 73 additions and 0 deletions
  1. 3 0
      faux/eloop.h
  2. 67 0
      faux/eloop/eloop.c
  3. 3 0
      faux/faux.map

+ 3 - 0
faux/eloop.h

@@ -45,10 +45,12 @@ bool_t faux_eloop_loop(faux_eloop_t *eloop);
 bool_t faux_eloop_add_fd(faux_eloop_t *eloop, int fd, short events,
 	faux_eloop_cb_fn event_cb, void *user_data);
 bool_t faux_eloop_del_fd(faux_eloop_t *eloop, int fd);
+bool_t faux_eloop_del_fd_all(faux_eloop_t *eloop);
 
 bool_t faux_eloop_add_signal(faux_eloop_t *eloop, int signo,
 	faux_eloop_cb_fn event_cb, void *user_data);
 bool_t faux_eloop_del_signal(faux_eloop_t *eloop, int signo);
+bool_t faux_eloop_del_signal_all(faux_eloop_t *eloop);
 
 faux_ev_t *faux_eloop_add_sched_once(faux_eloop_t *eloop, const struct timespec *time,
 	int ev_id, faux_eloop_cb_fn event_cb, void *data);
@@ -62,6 +64,7 @@ faux_ev_t *faux_eloop_add_sched_periodic_delayed(faux_eloop_t *eloop,
 	const struct timespec *period, unsigned int cycle_num);
 ssize_t faux_eloop_del_sched(faux_eloop_t *eloop, faux_ev_t *ev);
 ssize_t faux_eloop_del_sched_by_id(faux_eloop_t *eloop, int ev_id);
+bool_t faux_eloop_del_sched_all(faux_eloop_t *eloop);
 bool_t faux_eloop_include_fd_event(faux_eloop_t *eloop, int fd, short event);
 bool_t faux_eloop_exclude_fd_event(faux_eloop_t *eloop, int fd, short event);
 

+ 67 - 0
faux/eloop/eloop.c

@@ -557,6 +557,31 @@ bool_t faux_eloop_del_fd(faux_eloop_t *eloop, int fd)
 }
 
 
+/** @brief Unregisters all file descriptors.
+ *
+ * @param [in] eloop Allocated and initialized event loop object.
+ * @return BOOL_TRUE - success, BOOL_FALSE - error.
+ */
+bool_t faux_eloop_del_fd_all(faux_eloop_t *eloop)
+{
+	faux_list_node_t *iter = NULL;
+
+	if (!eloop)
+		return BOOL_FALSE;
+
+	// "Del all" function is so complex because pollfd object
+	// contains not user added fds only. It contains special fd for signals,
+	// service pipe and may be something else. So del all fds one by one.
+	while ((iter = faux_list_tail(eloop->fds))) {
+		faux_eloop_fd_t *entry = NULL;
+		entry = (faux_eloop_fd_t *)faux_list_data(iter);
+		faux_eloop_del_fd(eloop, entry->fd);
+	}
+
+	return BOOL_TRUE;
+}
+
+
 /** @brief Registers signal to wait for.
  *
  * @param [in] eloop Allocated and initialized event loop object.
@@ -653,6 +678,31 @@ bool_t faux_eloop_del_signal(faux_eloop_t *eloop, int signo)
 }
 
 
+/** @brief Unregisters all signals to wait for.
+ *
+ * @param [in] eloop Allocated and initialized event loop object.
+ * @return BOOL_TRUE - success, BOOL_FALSE - error.
+ */
+bool_t faux_eloop_del_signal_all(faux_eloop_t *eloop)
+{
+	faux_list_node_t *iter = NULL;
+
+	if (!eloop)
+		return BOOL_FALSE;
+
+	// "Del all" function is so complex because signals can be set now
+	// and deletion is not only removing from list.
+	// So del all signals one by one.
+	while ((iter = faux_list_tail(eloop->signals))) {
+		faux_eloop_signal_t *entry = NULL;
+		entry = (faux_eloop_signal_t *)faux_list_data(iter);
+		faux_eloop_del_signal(eloop, entry->signo);
+	}
+
+	return BOOL_TRUE;
+}
+
+
 /** @brief Service function to create new context for event.
  *
  * @param [in] event_cb Callback for event.
@@ -835,6 +885,23 @@ ssize_t faux_eloop_del_sched(faux_eloop_t *eloop, faux_ev_t *ev)
 }
 
 
+/** @brief Unregisters all scheduled time events.
+ *
+ * @param [in] eloop Allocated and initialized event loop object.
+ * @return BOOL_TRUE - success, BOOL_FALSE - error.
+ */
+bool_t faux_eloop_del_sched_all(faux_eloop_t *eloop)
+{
+	assert(eloop);
+	if (!eloop)
+		return BOOL_FALSE;
+
+	faux_sched_del_all(eloop->sched);
+
+	return BOOL_TRUE;
+}
+
+
 /** @brief Unregisters scheduled time event by event ID.
  *
  * @param [in] eloop Allocated and initialized event loop object.

+ 3 - 0
faux/faux.map

@@ -48,14 +48,17 @@ FAUX_2.0 {
 		faux_eloop_loop;
 		faux_eloop_add_fd;
 		faux_eloop_del_fd;
+		faux_eloop_del_fd_all;
 		faux_eloop_add_signal;
 		faux_eloop_del_signal;
+		faux_eloop_del_signal_all;
 		faux_eloop_add_sched_once;
 		faux_eloop_add_sched_once_delayed;
 		faux_eloop_add_sched_periodic;
 		faux_eloop_add_sched_periodic_delayed;
 		faux_eloop_del_sched;
 		faux_eloop_del_sched_by_id;
+		faux_eloop_del_sched_all;
 		faux_eloop_include_fd_event;
 		faux_eloop_exclude_fd_event;