thr_writev.c revision 24518
113546Sjulian/* 213546Sjulian * Copyright (c) 1995 John Birrell <jb@cimlogic.com.au>. 313546Sjulian * All rights reserved. 413546Sjulian * 513546Sjulian * Redistribution and use in source and binary forms, with or without 613546Sjulian * modification, are permitted provided that the following conditions 713546Sjulian * are met: 813546Sjulian * 1. Redistributions of source code must retain the above copyright 913546Sjulian * notice, this list of conditions and the following disclaimer. 1013546Sjulian * 2. Redistributions in binary form must reproduce the above copyright 1113546Sjulian * notice, this list of conditions and the following disclaimer in the 1213546Sjulian * documentation and/or other materials provided with the distribution. 1313546Sjulian * 3. All advertising materials mentioning features or use of this software 1413546Sjulian * must display the following acknowledgement: 1513546Sjulian * This product includes software developed by John Birrell. 1613546Sjulian * 4. Neither the name of the author nor the names of any co-contributors 1713546Sjulian * may be used to endorse or promote products derived from this software 1813546Sjulian * without specific prior written permission. 1913546Sjulian * 2013546Sjulian * THIS SOFTWARE IS PROVIDED BY JOHN BIRRELL AND CONTRIBUTORS ``AS IS'' AND 2113546Sjulian * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 2213546Sjulian * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 2313546Sjulian * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 2413546Sjulian * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 2513546Sjulian * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 2613546Sjulian * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 2713546Sjulian * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 2813546Sjulian * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 2913546Sjulian * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 3013546Sjulian * SUCH DAMAGE. 3113546Sjulian * 3224518Sjb * $Id$ 3324518Sjb * 3413546Sjulian */ 3513546Sjulian#include <sys/types.h> 3613546Sjulian#include <sys/fcntl.h> 3713546Sjulian#include <sys/uio.h> 3813546Sjulian#include <errno.h> 3913546Sjulian#include <unistd.h> 4013546Sjulian#ifdef _THREAD_SAFE 4113546Sjulian#include <pthread.h> 4213546Sjulian#include "pthread_private.h" 4313546Sjulian 4413546Sjulianssize_t 4513546Sjulianwritev(int fd, const struct iovec * iov, int iovcnt) 4613546Sjulian{ 4722315Sjulian int ret; 4822315Sjulian int status; 4922315Sjulian 5022315Sjulian /* Lock the file descriptor for read and write: */ 5122315Sjulian if ((ret = _thread_fd_lock(fd, FD_RDWR, NULL, 5222315Sjulian __FILE__, __LINE__)) == 0) { 5322315Sjulian /* Perform a non-blocking writev syscall: */ 5413546Sjulian while ((ret = _thread_sys_writev(fd, iov, iovcnt)) < 0) { 5524518Sjb if ((_thread_fd_table[fd]->flags & O_NONBLOCK) == 0 && 5622315Sjulian (errno == EWOULDBLOCK || errno == EAGAIN)) { 5713546Sjulian _thread_kern_sig_block(&status); 5813546Sjulian _thread_run->data.fd.fd = fd; 5913546Sjulian _thread_kern_set_timeout(NULL); 6022315Sjulian 6122315Sjulian /* Reset the interrupted operation flag: */ 6222315Sjulian _thread_run->interrupted = 0; 6322315Sjulian 6422315Sjulian _thread_kern_sched_state(PS_FDW_WAIT, 6522315Sjulian __FILE__, __LINE__); 6622315Sjulian 6722315Sjulian /* 6822315Sjulian * Check if the operation was 6922315Sjulian * interrupted by a signal 7022315Sjulian */ 7122315Sjulian if (_thread_run->interrupted) { 7213546Sjulian ret = -1; 7313546Sjulian break; 7413546Sjulian } 7513546Sjulian } else { 7613546Sjulian break; 7713546Sjulian } 7813546Sjulian } 7922315Sjulian _thread_fd_unlock(fd, FD_RDWR); 8013546Sjulian } 8113546Sjulian return (ret); 8213546Sjulian} 8313546Sjulian#endif 84