1
/* ==== fd_pipe.c ============================================================
2
* Copyright (c) 1993, 1994 by Chris Provenzano, proven@mit.edu
5
* Redistribution and use in source and binary forms, with or without
6
* modification, are permitted provided that the following conditions
8
* 1. Redistributions of source code must retain the above copyright
9
* notice, this list of conditions and the following disclaimer.
10
* 2. Redistributions in binary form must reproduce the above copyright
11
* notice, this list of conditions and the following disclaimer in the
12
* documentation and/or other materials provided with the distribution.
13
* 3. All advertising materials mentioning features or use of this software
14
* must display the following acknowledgement:
15
* This product includes software developed by Chris Provenzano.
16
* 4. The name of Chris Provenzano may not be used to endorse or promote
17
* products derived from this software without specific prior written
20
* THIS SOFTWARE IS PROVIDED BY CHRIS PROVENZANO ``AS IS'' AND
21
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23
* ARE DISCLAIMED. IN NO EVENT SHALL CHRIS PROVENZANO BE LIABLE FOR ANY
24
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
25
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
26
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
27
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32
* Description : The new fast ITC pipe routines.
34
* 1.00 93/08/14 proven
35
* -Started coding this file.
37
* 1.01 93/11/13 proven
38
* -The functions readv() and writev() added.
42
static const char rcsid[] = "$Id$";
46
#include <pthread/fd_pipe.h>
47
#include <sys/types.h>
48
#include <sys/socket.h>
52
#include <pthread/posix.h>
57
#define MIN(a,b) ((a)<(b)?(a):(b))
60
/* ==========================================================================
61
* The pipe lock is never unlocked until all pthreads waiting are done with it
64
pthread_ssize_t __pipe_read(union fd_data fd_data, int flags, void *buf,
65
size_t nbytes, struct timespec * timeout)
67
struct __pipe *fd = (struct __pipe *)fd_data.ptr;
68
struct pthread * pthread;
71
if (flags & O_ACCMODE) { return(NOTOK); }
73
/* If there is nothing to read, go to sleep */
75
if (flags == WR_CLOSED) {
79
pthread_sched_prevent();
81
/* queue pthread for a FDR_WAIT */
82
pthread_run->next = NULL;
83
fd->wait = pthread_run;
85
pthread_resched_resume(PS_FDR_WAIT);
88
ret = MIN(nbytes, fd->count);
89
memcpy(buf, fd->buf + fd->offset, ret);
90
if (!(fd->count -= ret)) {
94
if (pthread = fd->wait) {
96
pthread_sched_prevent();
97
pthread_sched_other_resume(pthread);
103
/* ==========================================================================
106
* First check to see if the read side is still open, then
107
* check to see if there is a thread in a read wait for this pipe, if so
108
* copy as much data as possible directly into the read waiting threads
109
* buffer. The write thread(whether or not there was a read thread)
110
* copies as much data as it can into the pipe buffer and it there
111
* is still data it goes to sleep.
113
pthread_ssize_t __pipe_write(union fd_data fd_data, int flags, const void *buf,
114
size_t nbytes, struct timespec * timeout) {
115
struct __pipe *fd = (struct __pipe *)fd_data.ptr;
116
struct pthread * pthread;
119
if (!(flags & O_ACCMODE)) { return(NOTOK); }
121
while (fd->flags != RD_CLOSED) {
122
if (pthread = fd->wait) {
124
pthread_sched_prevent();
126
/* Copy data directly into waiting pthreads buf */
127
fd->wait_size = MIN(nbytes, fd->wait_size);
128
memcpy(fd->wait_buf, buf, fd->wait_size);
129
buf = (const char *)buf + fd->wait_size;
130
nbytes -= fd->wait_size;
134
/* Wake up waiting pthread */
135
pthread_sched_other_resume(pthread);
138
if (count = MIN(nbytes, fd->size - (fd->offset + fd->count))) {
139
memcpy(fd->buf + (fd->offset + fd->count), buf, count);
140
buf = (const char *)buf + count;
145
pthread_sched_prevent();
146
fd->wait = pthread_run;
147
pthread_resched_resume(PS_FDW_WAIT);
155
/* ==========================================================================
158
* The whole close procedure is a bit odd and needs a bit of a rethink.
159
* For now close() locks the fd, calls fd_free() which checks to see if
160
* there are any other fd values poinging to the same real fd. If so
161
* It breaks the wait queue into two sections those that are waiting on fd
162
* and those waiting on other fd's. Those that are waiting on fd are connected
163
* to the fd_table[fd] queue, and the count is set to zero, (BUT THE LOCK IS NOT
164
* RELEASED). close() then calls fd_unlock which give the fd to the next queued
165
* element which determins that the fd is closed and then calls fd_unlock etc...
167
int __pipe_close(struct __pipe *fd, int flags)
169
struct pthread * pthread;
172
if (pthread = fd->wait) {
173
if (flags & O_ACCMODE) {
176
fd->flags |= WR_CLOSED;
177
pthread_sched_prevent();
178
pthread_resched_resume(pthread);
180
/* Should send a signal */
181
fd->flags |= RD_CLOSED;
190
/* ==========================================================================
191
* For fcntl() which isn't implemented yet
194
static int __pipe_enosys()
200
/* ==========================================================================
201
* For writev() and readv() which aren't implemented yet
204
static int __pipe_enosys_v(union fd_data fd, int flags,
205
const struct iovec *vec, int nvec,
206
struct timespec *timeout)
212
/* ==========================================================================
213
* For lseek() which isn't implemented yet
216
static off_t __pipe_enosys_o()
223
* File descriptor operations
225
struct fd_ops fd_ops[] = {
226
{ __pipe_write, __pipe_read, __pipe_close, __pipe_enosys,
227
__pipe_enosys_v, __pipe_enosys_v, __pipe_enosys_o, 0 },
230
/* ==========================================================================
233
/* int __pipe_open(const char *path, int flags, ...) */
234
int newpipe(int fd[2])
236
struct __pipe *fd_data;
238
if ((!((fd[0] = fd_allocate()) < OK)) && (!((fd[1] = fd_allocate()) < OK))) {
239
fd_data = malloc(sizeof(struct __pipe));
240
fd_data->buf = malloc(4096);
241
fd_data->size = 4096;
245
fd_data->wait = NULL;
248
fd_table[fd[0]]->fd.ptr = fd_data;
249
fd_table[fd[0]]->flags = O_RDONLY;
250
fd_table[fd[1]]->fd.ptr = fd_data;
251
fd_table[fd[1]]->flags = O_WRONLY;