int NaClSimpleServiceAcceptAndSpawnHandler(
    struct NaClSimpleService *self) {
  struct NaClSimpleServiceConnection  *conn = NULL;
  int                                 status;

  NaClLog(4, "Entered NaClSimpleServiceAcceptAndSpawnHandler\n");
  NaClLog(4,
          ("NaClSimpleServiceAcceptAndSpawnHandler:"
           " &conn is 0x%"NACL_PRIxPTR"\n"),
          (uintptr_t) &conn);
  status = (*NACL_VTBL(NaClSimpleService, self)->AcceptConnection)(
      self,
      &conn);
  NaClLog(4, "AcceptConnection vfn returned %d\n", status);
  if (0 != status) {
    goto abort;
  }
  NaClLog(4,
          "NaClSimpleServiceAcceptAndSpawnHandler: conn is 0x%"NACL_PRIxPTR"\n",
          (uintptr_t) conn);
  NaClLog(4, "NaClSimpleServiceAcceptAndSpawnHandler: spawning thread\n");

  CHECK(NULL == conn->thread);

  /* ownership of |conn| reference is passed to the thread */
  if (!NaClThreadInterfaceConstructAndStartThread(
          self->thread_factory_fn,
          self->thread_factory_data,
          RpcHandlerBase,
          conn,
          NACL_KERN_STACK_SIZE,
          &conn->thread)) {
    NaClLog(4, "NaClSimpleServiceAcceptAndSpawnHandler: no thread\n");
    NaClRefCountUnref((struct NaClRefCount *) conn);
    conn = NULL;
    conn->thread = NULL;
    status = -NACL_ABI_EAGAIN;
    goto abort;
  }
  status = 0;
abort:
  NaClLog(4,
          "Leaving NaClSimpleServiceAcceptAndSpawnHandler, status %d\n",
          status);
  return status;
}
int NaClSimpleServiceStartServiceThread(struct NaClSimpleService *server) {
  NaClLog(4, "NaClSimpleServiceStartServiceThread: spawning thread\n");
  CHECK(server->acceptor == NULL);

  if (!NaClThreadInterfaceConstructAndStartThread(
          server->thread_factory_fn,
          server->thread_factory_data,
          AcceptThread,
          NaClRefCountRef(&server->base),
          NACL_KERN_STACK_SIZE,
          &server->acceptor)) {
    NaClLog(4, "NaClSimpleServiceStartServiceThread: no thread\n");
    NaClRefCountUnref(&server->base);  /* undo ref in Ctor call arglist */
    return 0;
  }
  NaClLog(4, "NaClSimpleServiceStartServiceThread: success\n");
  return 1;
}