[comp.sys.isis] X and ISIS event loops

wright@hsi86.hsi.com (Gary Wright) (06/04/91)

I have been experimenting with ISIS and X using the main loop structure
indicated in the ISIS manual (Section 16.8 Xt version).  The
recommended loop causes excessive CPU usage since when no events exist,
it turns in to a busy wait.

I vaguely remembered reading about a change to the recommended looping
structure.  I was unable to find it in the comp.sys.isis archives nor
in the postings I have saved over the last few weeks.  Am I just
imagining this or can some one point me towards it?
-- 
Gary Wright                                                 ...!uunet!hsi!wright
3M Health Information Systems					  wright@hsi.com

glade@cs.cornell.edu (Bradford Glade) (06/05/91)

>
>I have been experimenting with ISIS and X using the main loop structure
>indicated in the ISIS manual (Section 16.8 Xt version).  The
>recommended loop causes excessive CPU usage since when no events exist,
>it turns in to a busy wait.
>
>I vaguely remembered reading about a change to the recommended looping
>structure.  I was unable to find it in the comp.sys.isis archives nor
>in the postings I have saved over the last few weeks.  Am I just
>imagining this or can some one point me towards it?
>-- 
>Gary Wright                                             ...!uunet!hsi!wright
>3M Health Information Systems		  	         wright@hsi.com

You are correct, the solution outlined in that version of the manual
proposes a poor solution which busy-waits!  The following solution
is related but does not busy-wait.

void isis_x_main_loop(arg)

  void *arg;

{
  Display *dpy = arg;
  XEvent  event;
  int     imask;

  for(;;) {
    do {
      /* Do *some* of the X work available. */
      int nevents = 0;
      while ((XtPending() != 0) && (++nevents < 50)) {
	XtNextEvent(&event);
	XtDispatchEvent(&event);
      }
      /* Do all Isis work available. */
      isis_accept_events(0);
    } while (XtPending() != 0); 
    
    /* Keep looping while there is still more X work. This could
       be because the inner loop hit 50 iterations, or because
       the Isis tasks generated more X work. 
    */
    
    imask = 1<<dpy->fd;
    isis_select(32, &imask, (int *)0, (int *)0, (struct timeval *)0);
  } 
}

The call to isis_select will cause this thread to wait for the next X
event to come along, and thus not busy-wait.  Note: The argument
passed to this routine should be a pointer to the X Display on which
the program is running.  A single process which uses multiple displays
will need to add the additional X input file descriptors to imask.
Note: isis_start_done should be called before entering the infinite
loop above.  This routine and the Display can be passed as the
argument to isis_mainloop if desired.

-Brad.

-- 
Bradford B. Glade             |  glade@cs.cornell.edu    
Dept. of Computer Science     |  Tel. (607) 255-2219 (Office)
4162 Upson Hall Cornell Univ. |  Tel. (607) 539-6801 (Home)
Ithaca, NY 14850              |  FAX  (607) 255-4428 (CS Dept.)