int accept(int fd, struct sockaddr *addr, socklen_t *addrlen) { pthread_t self = pthread_self(); int rv; _enter_cancel(self); rv = _thread_sys_accept(fd, addr, addrlen); _leave_cancel(self); return (rv); }
int accept(int fd, struct sockaddr * name, socklen_t *namelen) { struct pthread *curthread = _get_curthread(); int ret; int newfd; enum fd_entry_mode init_mode; /* This is a cancellation point: */ _thread_enter_cancellation_point(); /* Lock the file descriptor: */ if ((ret = _FD_LOCK(fd, FD_RDWR, NULL)) == 0) { /* Enter a loop to wait for a connection request: */ while ((ret = _thread_sys_accept(fd, name, namelen)) < 0) { /* Check if the socket is to block: */ if ((_thread_fd_table[fd]->status_flags->flags & O_NONBLOCK) == 0 && (errno == EWOULDBLOCK || errno == EAGAIN)) { /* Save the socket file descriptor: */ curthread->data.fd.fd = fd; curthread->data.fd.fname = __FILE__; curthread->data.fd.branch = __LINE__; /* Set the timeout: */ _thread_kern_set_timeout(NULL); curthread->interrupted = 0; curthread->closing_fd = 0; /* Schedule the next thread: */ _thread_kern_sched_state(PS_FDR_WAIT, __FILE__, __LINE__); /* Check if the wait was interrupted: */ if (curthread->interrupted) { /* Return an error status: */ errno = EINTR; ret = -1; break; } else if (curthread->closing_fd) { /* Return an error status: */ errno = EBADF; ret = -1; break; } } else { /* * Another error has occurred, so exit the * loop here: */ break; } } /* * If no errors initialize the file descriptor table * for the new socket. If the client's view of the * status_flags for fd is blocking, then force newfd * to be viewed as blocking too. */ if (ret != -1) { newfd = ret; if ((_thread_fd_table[fd]->status_flags->flags & O_NONBLOCK) == 0) init_mode = FD_INIT_BLOCKING; else init_mode = FD_INIT_NEW; if((ret = _thread_fd_table_init(newfd, init_mode, NULL)) != -1) ret = newfd; else { /* quitely close the fd */ _thread_sys_close(ret); } } /* Unlock the file descriptor: */ _FD_UNLOCK(fd, FD_RDWR); } /* No longer in a cancellation point: */ _thread_leave_cancellation_point(); /* Return the socket file descriptor or -1 on error: */ return (ret); }