previous up next     contents index
Next: Integration with Threads Up: Threads Previous: Blocked Non-main Threads

Sleeping by Embedded MzScheme

 

    Sometimes, MzScheme needs to ``sleep'' -- i.e., block the main thread's execution -- for a certain number of seconds or until external input appears on some file descriptor. Generally, this should block the main event loop of the entire application. However, the way in which sleeping is performed may depend on the embedding application. The global function pointer  scheme_sleep should set by an embedding application to implement a blocking sleep.

A  scheme_sleep function takes two arguments: a float and a void * that is really a fd_set *. (The file descriptor set argument is only used for platforms with file descriptions.) If the float argument is non-zero, then the  scheme_sleep function blocks for the specified number of seconds. Otherwise, the  scheme_sleep function blocks until there is input on on of the specified file descriptors.

The following function mzsleep is an appropriate  scheme_sleep function for most any Unix application.

static void mzsleep(float v, void *fds)
{
  if (v) {
    usleep((unsigned)(v * 1000));
  } else {
    int limit;
    fd_set rd, ex;

    limit = getdtablesize();

    memcpy(&rd, fds, sizeof(fd_set));
    memcpy(&ex, fds, sizeof(fd_set));
    select(limit, &rd, NULL, &ex, NULL);
  }
}



PLT