コード例 #1
0
ファイル: event_loop.cpp プロジェクト: jiangzw/gazellemq
int EventLoop::handle_events()
{
    int nevents = get_ready_events();
    assert(nevents);
    log_debug("ready events: " << nevents);

    int result = handle_timer_events();
    if (result > 0)
        return result;

    result = handle_io_events();
    return result;
}
コード例 #2
0
ファイル: events.c プロジェクト: freestylem2m/start
int event_loop( long timeout )
{
	long max_fd = 0;

	fd_set fds_read, fds_write, fds_exception;
	if( ! create_event_set( &fds_read, &fds_write, &fds_exception, &max_fd ) )
		return -1;

	time_t now = rel_time(0L);
	long alarm_time = alarm_getnext();

	// Reduce 'timeout' to ensure the next scheduled alarm occurs on time
	if( alarm_time >= 0 ) {
		if( alarm_time > now ) {
			if( timeout > ( alarm_time - now ))
				timeout = alarm_time - now;
		} else
			timeout = 0;
	}

#ifdef USE_PSELECT
	// If I know there are signals which need processing, reset the select timeout to 0
	// Signals are normally blocked, so these would be manufactured events.
	struct timespec tm = { timeout / 1000, (timeout % 1000) * 1000 * 1000 };

	if( event_signals.event_signal_pending_count )
		tm.tv_nsec = tm.tv_sec = 0;

	int rc = pselect( (int) max_fd, &fds_read, &fds_write, &fds_exception, &tm, &event_signals.event_signal_default );
#else
	// expect queued signals to occur immediately
	sigprocmask( SIG_SETMASK, &event_signals.event_signal_default, NULL );
	sleep(0);

	if( event_signals.event_signal_pending_count )
		timeout = 0;

	struct timeval tm = { timeout / 1000, (timeout % 1000) * 1000 };

#if 0
#ifndef NDEBUG
	int i = 0;
	printf("READ: ");
	for(i=0;i<64;i++)
		if(FD_ISSET(i,&fds_read)) {
			printf("%d ",i);
			if( fcntl( i, F_GETFL ) < 0 )
				printf("*");
		}
	printf("WRITE: ");
	for(i=0;i<64;i++)
		if(FD_ISSET(i,&fds_write)) {
			printf("%d ",i);
			if( fcntl( i, F_GETFL ) < 0 )
				printf("*");
		}
	printf("EXCEPTION: ");
	for(i=0;i<64;i++)
		if(FD_ISSET(i,&fds_exception)) {
			printf("%d ",i);
			if( fcntl( i, F_GETFL ) < 0 )
				printf("*");
		}
	printf("\n");
#endif
#endif

#ifndef NDEBUG
	//printf("calling select...");fflush(stdout);
#endif
	int rc = select( max_fd, &fds_read, &fds_write, &fds_exception, &tm );
#ifndef NDEBUG
	//printf("done\n");fflush(stdout);
#endif
#endif

	sigprocmask(SIG_BLOCK, &event_signals.event_signal_mask, NULL);

	if( rc < 0 ) {
		if( (errno != EINTR)
#ifdef mips
				&& (errno != ENOENT)
#endif
		  ) {
			d_printf("(p)select returned %d (errno = %d - %s)\n",rc, errno, strerror(errno));
			exit(0);
			return -1;
		} else {
			return 0;  // timeout - nothing to do
		}
	}

	if( event_signals.event_signal_pending_count ) {
		//d_printf("Calling handle_pending_signals()\n");
		handle_pending_signals();
	}

	//d_printf("Calling handle_event_set()\n");
	rc = handle_event_set( &fds_read, &fds_write, &fds_exception );

	if( !rc ) {
		//d_printf("Calling handle_timer_events()\n");
		handle_timer_events();
	}

	return 0;
}