]> mj.ucw.cz Git - libucw.git/commitdiff
Main: Introduce functions for telling whether an object is active
authorMartin Mares <mj@ucw.cz>
Thu, 9 Jun 2011 16:16:33 +0000 (18:16 +0200)
committerMartin Mares <mj@ucw.cz>
Thu, 9 Jun 2011 16:16:33 +0000 (18:16 +0200)
This is an useful idiom and it helps avoid calling clist_is_linked()
on private parts of the objects.

ucw/main-rec.c
ucw/mainloop.c
ucw/mainloop.h

index 06c574944144d4b6b547323ae8c961df93d0e92f..3d6ad524e4392d44b1166e291a8f12e1eaa38e01 100644 (file)
@@ -52,7 +52,7 @@ void
 rec_io_del(struct main_rec_io *rio)
 {
   timer_del(&rio->timer);
-  if (clist_is_linked(&rio->start_read_hook.n))
+  if (hook_is_active(&rio->start_read_hook))
     hook_del(&rio->start_read_hook);
   file_del(&rio->file);
 
@@ -199,7 +199,7 @@ rec_io_recalc_read(struct main_rec_io *rio)
           * of the work to a main_hook, which will be called in the next iteration
           * of the main loop.
           */
-         if (!clist_is_linked(&rio->start_read_hook.n))
+         if (!hook_is_active(&rio->start_read_hook))
            {
              DBG("RIO: Scheduling start of reading");
              hook_add(&rio->start_read_hook);
@@ -207,7 +207,7 @@ rec_io_recalc_read(struct main_rec_io *rio)
        }
       else
        {
-         if (clist_is_linked(&rio->start_read_hook.n))
+         if (hook_is_active(&rio->start_read_hook))
            {
              DBG("RIO: Descheduling start of reading");
              hook_del(&rio->start_read_hook);
@@ -223,7 +223,7 @@ rec_io_recalc_read(struct main_rec_io *rio)
 void
 rec_io_start_read(struct main_rec_io *rio)
 {
-  ASSERT(clist_is_linked(&rio->file.n));
+  ASSERT(rec_io_is_active(rio));
   rio->read_started = 1;
   rec_io_recalc_read(rio);
 }
@@ -231,7 +231,7 @@ rec_io_start_read(struct main_rec_io *rio)
 void
 rec_io_stop_read(struct main_rec_io *rio)
 {
-  ASSERT(clist_is_linked(&rio->file.n));
+  ASSERT(rec_io_is_active(rio));
   rio->read_started = 0;
   rec_io_recalc_read(rio);
 }
@@ -311,7 +311,7 @@ void
 rec_io_write(struct main_rec_io *rio, void *data, uns len)
 {
   byte *bdata = data;
-  ASSERT(clist_is_linked(&rio->file.n));
+  ASSERT(rec_io_is_active(rio));
   if (!len)
     return;
 
@@ -420,7 +420,7 @@ main(void)
   main_loop();
   msg(L_INFO, "Finished.");
 
-  if (clist_is_linked(&rio.file.n))
+  if (file_is_active(&rio.file))
     rec_io_del(&rio);
   main_cleanup();
   return 0;
index 7eb8c8acdf35fe59861dc0fccb76a0a6ac0b9971..0d7d1e164278b118e06d64c818ca58b86f9d364a 100644 (file)
@@ -318,7 +318,7 @@ file_add(struct main_file *fi)
   struct main_context *m = main_current();
 
   DBG("MAIN: Adding file %p (fd=%d)", fi, fi->fd);
-  ASSERT(!clist_is_linked(&fi->n));
+  ASSERT(!file_is_active(fi));
   clist_add_tail(&m->file_list, &fi->n);
   m->file_cnt++;
 #ifdef CONFIG_UCW_EPOLL
@@ -355,7 +355,7 @@ file_del_ctx(struct main_context *m, struct main_file *fi)
   // XXX: Can be called on a non-current context
   DBG("MAIN: Deleting file %p (fd=%d)", fi, fi->fd);
 
-  ASSERT(clist_is_linked(&fi->n));
+  ASSERT(file_is_active(fi));
   clist_unlink(&fi->n);
   m->file_cnt--;
 #ifdef CONFIG_UCW_EPOLL
@@ -378,7 +378,7 @@ hook_add(struct main_hook *ho)
   struct main_context *m = main_current();
 
   DBG("MAIN: Adding hook %p", ho);
-  ASSERT(!clist_is_linked(&ho->n));
+  ASSERT(!hook_is_active(ho));
   clist_add_tail(&m->hook_list, &ho->n);
 }
 
@@ -386,7 +386,7 @@ void
 hook_del(struct main_hook *ho)
 {
   DBG("MAIN: Deleting hook %p", ho);
-  ASSERT(clist_is_linked(&ho->n));
+  ASSERT(hook_is_active(ho));
   clist_unlink(&ho->n);
 }
 
@@ -419,7 +419,7 @@ process_add(struct main_process *mp)
   struct main_context *m = main_current();
 
   DBG("MAIN: Adding process %p (pid=%d)", mp, mp->pid);
-  ASSERT(!clist_is_linked(&mp->n));
+  ASSERT(!process_is_active(mp));
   ASSERT(mp->handler);
   clist_add_tail(&m->process_list, &mp->n);
   if (!m->sigchld_handler)
@@ -436,7 +436,7 @@ void
 process_del(struct main_process *mp)
 {
   DBG("MAIN: Deleting process %p (pid=%d)", mp, mp->pid);
-  ASSERT(clist_is_linked(&mp->n));
+  ASSERT(process_is_active(mp));
   clist_unlink(&mp->n);
 }
 
@@ -543,7 +543,7 @@ signal_add(struct main_signal *ms)
 
   DBG("MAIN: Adding signal %p (sig=%d)", ms, ms->signum);
 
-  ASSERT(!clist_is_linked(&ms->n));
+  ASSERT(!signal_is_active(ms));
   // Adding at the head of the list is better if we are in the middle of walking the list.
   clist_add_head(&m->signal_list, &ms->n);
   if (m->sig_pipe_recv < 0)
@@ -568,7 +568,7 @@ signal_del_ctx(struct main_context *m, struct main_signal *ms)
   // XXX: Can be called on a non-current context
   DBG("MAIN: Deleting signal %p (sig=%d)", ms, ms->signum);
 
-  ASSERT(clist_is_linked(&ms->n));
+  ASSERT(signal_is_active(ms));
   clist_unlink(&ms->n);
 
   int another = 0;
index bf5cfcaef4f5737104be2c840d3a93ffe6b6f1e1..6de703d862f747687229d4cac5df5f03629083c2 100644 (file)
@@ -182,6 +182,12 @@ void timer_add_rel(struct main_timer *tm, timestamp_t expires_delta);
  **/
 void timer_del(struct main_timer *tm);
 
+/** Tells whether a timer is running. **/
+static inline int timer_is_active(struct main_timer *tm)
+{
+  return !!tm->expires;
+}
+
 /**
  * Forces refresh of the current timestamp cached in the active context.
  * You usually do not need to call this, since it is called every time the
@@ -250,6 +256,12 @@ void hook_add(struct main_hook *ho);
  **/
 void hook_del(struct main_hook *ho);
 
+/** Tells if a hook is active (i.e., added). **/
+static inline int hook_is_active(struct main_hook *ho)
+{
+  return clist_is_linked(&ho->n);
+}
+
 /** Show current state of a hook. Available only if LibUCW has been compiled with `CONFIG_DEBUG`. **/
 void hook_debug(struct main_hook *ho);
 
@@ -330,6 +342,12 @@ void file_chg(struct main_file *fi);
  **/
 void file_del(struct main_file *fi);
 
+/** Tells if a file is active (i.e., added). **/
+static inline int file_is_active(struct main_file *fi)
+{
+  return clist_is_linked(&fi->n);
+}
+
 /** Show current state of a file. Available only if LibUCW has been compiled with `CONFIG_DEBUG`. **/
 void file_debug(struct main_file *fi);
 
@@ -432,6 +450,12 @@ void block_io_write(struct main_block_io *bio, void *buf, uns len);
  **/
 void block_io_set_timeout(struct main_block_io *bio, timestamp_t expires_delta);
 
+/** Tells if a @bio is active (i.e., added). **/
+static inline int block_io_is_active(struct main_block_io *bio)
+{
+  return file_is_active(&bio->file);
+}
+
 /***
  * [[recordio]]
  * Asynchronous record I/O
@@ -543,6 +567,12 @@ enum rec_io_notify_status {
   RIO_EVENT_EOF = 3,                   /* Read: EOF seen */
 };
 
+/** Tells if a @rio is active (i.e., added). **/
+static inline int rec_io_is_active(struct main_rec_io *rio)
+{
+  return file_is_active(&rio->file);
+}
+
 /***
  * [[process]]
  * Child processes
@@ -596,6 +626,12 @@ void process_del(struct main_process *mp);
  **/
 int process_fork(struct main_process *mp);
 
+/** Tells if a process is active (i.e., added). **/
+static inline int process_is_active(struct main_process *mp)
+{
+  return clist_is_linked(&mp->n);
+}
+
 /** Show current state of a process. Available only if LibUCW has been compiled with `CONFIG_DEBUG`. **/
 void process_debug(struct main_process *pr);
 
@@ -639,6 +675,12 @@ void signal_add(struct main_signal *ms);
 /** Cancel a request for signal catching. **/
 void signal_del(struct main_signal *ms);
 
+/** Tells if a signal catcher is active (i.e., added). **/
+static inline int signal_is_active(struct main_signal *ms)
+{
+  return clist_is_linked(&ms->n);
+}
+
 /** Show current state of a signal catcher. Available only if LibUCW has been compiled with `CONFIG_DEBUG`. **/
 void signal_debug(struct main_signal *sg);