diff options
Diffstat (limited to 'lib/libc_r/uthread/uthread_fd.c')
-rw-r--r-- | lib/libc_r/uthread/uthread_fd.c | 51 |
1 files changed, 35 insertions, 16 deletions
diff --git a/lib/libc_r/uthread/uthread_fd.c b/lib/libc_r/uthread/uthread_fd.c index ad6e9f3..4c74080 100644 --- a/lib/libc_r/uthread/uthread_fd.c +++ b/lib/libc_r/uthread/uthread_fd.c @@ -47,23 +47,26 @@ _thread_fd_table_init(int fd) _thread_kern_sig_block(&status); /* Check if the file descriptor is out of range: */ - if (fd < 0 || fd >= _thread_dtablesize) { + if (fd < 0 || fd >= _thread_dtablesize) /* Return a bad file descriptor error: */ - errno = EBADF; - ret = -1; - } + ret = EBADF; + /* * Check if memory has already been allocated for this file * descriptor: */ else if (_thread_fd_table[fd] != NULL) { + /* Memory has already been allocated. */ } /* Allocate memory for the file descriptor table entry: */ - else if ((_thread_fd_table[fd] = (struct fd_table_entry *) malloc(sizeof(struct fd_table_entry))) == NULL) { + else if ((_thread_fd_table[fd] = (struct fd_table_entry *) + malloc(sizeof(struct fd_table_entry))) == NULL) /* Return a bad file descriptor error: */ - errno = EBADF; - ret = -1; - } else { + ret = EBADF; + else { + /* Assume that the operation will succeed: */ + ret = 0; + /* Initialise the file locks: */ _thread_fd_table[fd]->r_owner = NULL; _thread_fd_table[fd]->w_owner = NULL; @@ -79,16 +82,32 @@ _thread_fd_table_init(int fd) _thread_queue_init(&_thread_fd_table[fd]->w_queue); /* Get the flags for the file: */ - if ((_thread_fd_table[fd]->flags = _thread_sys_fcntl(fd, F_GETFL, 0)) == -1) { - ret = -1; + if (fd >= 3 && (_thread_fd_table[fd]->flags = + _thread_sys_fcntl(fd, F_GETFL, 0)) == -1) + ret = errno; + + else { + /* Check if a stdio descriptor: */ + if (fd < 3) + /* + * Use the stdio flags read by + * _pthread_init() to avoid + * mistaking the non-blocking + * flag that, when set on one + * stdio fd, is set on all stdio + * fds. + */ + _thread_fd_table[fd]->flags = + _pthread_stdio_flags[fd]; - /* Make the file descriptor non-blocking: */ - } else { - _thread_sys_fcntl(fd, F_SETFL, _thread_fd_table[fd]->flags | O_NONBLOCK); + /* Make the file descriptor non-blocking: */ + if (_thread_sys_fcntl(fd, F_SETFL, + _thread_fd_table[fd]->flags | O_NONBLOCK) == -1) + ret = errno; } /* Check if one of the fcntl calls failed: */ - if (ret == -1) { + if (ret != 0) { /* Free the file descriptor table entry: */ free(_thread_fd_table[fd]); _thread_fd_table[fd] = NULL; @@ -143,7 +162,7 @@ _thread_fd_unlock(int fd, int lock_type) * Set the state of the new owner of * the thread to running: */ - _thread_fd_table[fd]->r_owner->state = PS_RUNNING; + PTHREAD_NEW_STATE(_thread_fd_table[fd]->r_owner,PS_RUNNING); /* * Reset the number of read locks. @@ -181,7 +200,7 @@ _thread_fd_unlock(int fd, int lock_type) * Set the state of the new owner of * the thread to running: */ - _thread_fd_table[fd]->w_owner->state = PS_RUNNING; + PTHREAD_NEW_STATE(_thread_fd_table[fd]->w_owner,PS_RUNNING); /* * Reset the number of write locks. |