struct timeval __fd_kern_poll_timeout = { 0, 0 }; struct pthread **pthread; semaphore *lock; int count;   lock = &fd_wait_lock; if (SEMAPHORE_TEST_AND_SET(lock)) { return; } if (fd_wait_read || fd_wait_write) { for (pthread = &fd_wait_read; *pthread; pthread = &((*pthread)->next)) { FD_SET((*pthread)->fd, &fd_set_read); } for (pthread = &fd_wait_write; *pthread; pthread = &((*pthread)->next)) { FD_SET((*pthread)->fd, &fd_set_write); }  if ((count = machdep_sys_select(dtablesize, &fd_set_read, &fd_set_write, NULL, &__fd_kern_poll_timeout)) < OK) { if (count == -EINTR) { SEMAPHORE_RESET(lock); return; } PANIC(); }  for (pthread = &fd_wait_read; count && *pthread; ) { if (FD_ISSET((*pthread)->fd, &fd_set_read)) {   (*pthread)->state = PS_RUNNING; *pthread = (*pthread)->next; count--; continue; } pthread = &((*pthread)->next); }  for (pthread = &fd_wait_write; count && *pthread; ) { if (FD_ISSET((*pthread)->fd, &fd_set_write)) { semaphore *plock;   plock = &(*pthread)->lock; if (!(SEMAPHORE_TEST_AND_SET(plock))) {  (*pthread)->state = PS_RUNNING; *pthread = (*pthread)->next; SEMAPHORE_RESET(plock); } count--; continue; } pthread = &((*pthread)->next); } } SEMAPHORE_RESET(lock);
fd_kern_select(&__fd_kern_poll_timeout);
void fd_kern_poll(){	struct timeval __fd_kern_poll_timeout = { 0, 0 };	struct pthread **pthread;	semaphore *lock;	int count;	/* If someone has the lock then they are in RUNNING state, just return */	lock = &fd_wait_lock;	if (SEMAPHORE_TEST_AND_SET(lock)) {		return;	}	if (fd_wait_read || fd_wait_write) {		for (pthread = &fd_wait_read; *pthread; pthread = &((*pthread)->next)) {			FD_SET((*pthread)->fd, &fd_set_read);		}		for (pthread = &fd_wait_write; *pthread; pthread = &((*pthread)->next)) {			FD_SET((*pthread)->fd, &fd_set_write);		}		if ((count = machdep_sys_select(dtablesize, &fd_set_read,		  &fd_set_write, NULL, &__fd_kern_poll_timeout)) < OK) {			if (count == -EINTR) {				SEMAPHORE_RESET(lock);				return;			}			PANIC();		}			for (pthread = &fd_wait_read; count && *pthread; ) {			if (FD_ISSET((*pthread)->fd, &fd_set_read)) {				/* Get lock on thread */				(*pthread)->state = PS_RUNNING;				*pthread = (*pthread)->next;				count--;				continue;			} 			pthread = &((*pthread)->next);		}							for (pthread = &fd_wait_write; count && *pthread; ) {			if (FD_ISSET((*pthread)->fd, &fd_set_write)) {				semaphore *plock;				/* Get lock on thread */				plock = &(*pthread)->lock;				if (!(SEMAPHORE_TEST_AND_SET(plock))) {					/* Thread locked, skip it. */					(*pthread)->state = PS_RUNNING;					*pthread = (*pthread)->next;					SEMAPHORE_RESET(plock);				}				count--;				continue;			} 			pthread = &((*pthread)->next);		}	}	SEMAPHORE_RESET(lock);}