~mysql/mysql-server/mysql-6.0

« back to all changes in this revision

Viewing changes to mit-pthreads/pthreads/fd_pipe.c

  • Committer: bk at mysql
  • Date: 2000-07-31 19:29:14 UTC
  • Revision ID: sp1r-bk@work.mysql.com-20000731192914-08846
Import changeset

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* ==== fd_pipe.c ============================================================
 
2
 * Copyright (c) 1993, 1994 by Chris Provenzano, proven@mit.edu
 
3
 * All rights reserved.
 
4
 *
 
5
 * Redistribution and use in source and binary forms, with or without
 
6
 * modification, are permitted provided that the following conditions
 
7
 * are met:
 
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
 
18
 *        permission.
 
19
 *
 
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 
 
30
 * SUCH DAMAGE.
 
31
 *
 
32
 * Description : The new fast ITC pipe routines.
 
33
 *
 
34
 *  1.00 93/08/14 proven
 
35
 *      -Started coding this file.
 
36
 *
 
37
 *      1.01 93/11/13 proven
 
38
 *              -The functions readv() and writev() added.
 
39
 */
 
40
 
 
41
#ifndef lint
 
42
static const char rcsid[] = "$Id$";
 
43
#endif
 
44
 
 
45
#include <pthread.h>
 
46
#include <pthread/fd_pipe.h>
 
47
#include <sys/types.h>
 
48
#include <sys/socket.h>
 
49
#include <sys/time.h>
 
50
#include <fcntl.h>
 
51
#include <errno.h>
 
52
#include <pthread/posix.h>
 
53
#include <string.h>
 
54
#include <stdlib.h>
 
55
 
 
56
#ifndef MIN
 
57
#define MIN(a,b)        ((a)<(b)?(a):(b))
 
58
#endif
 
59
 
 
60
/* ==========================================================================
 
61
 * The pipe lock is never unlocked until all pthreads waiting are done with it
 
62
 * read()
 
63
 */
 
64
pthread_ssize_t __pipe_read(union fd_data fd_data, int flags, void *buf,
 
65
 size_t nbytes, struct timespec * timeout)
 
66
{
 
67
        struct __pipe *fd = (struct __pipe *)fd_data.ptr;
 
68
        struct pthread * pthread;
 
69
        int ret = 0;
 
70
 
 
71
        if (flags & O_ACCMODE) { return(NOTOK); }
 
72
 
 
73
        /* If there is nothing to read, go to sleep */
 
74
        if (fd->count == 0) {
 
75
                if (flags == WR_CLOSED) {
 
76
                        return(0);
 
77
                }
 
78
 
 
79
                pthread_sched_prevent();
 
80
 
 
81
                /* queue pthread for a FDR_WAIT */
 
82
                pthread_run->next = NULL;
 
83
                fd->wait = pthread_run;
 
84
                
 
85
                pthread_resched_resume(PS_FDR_WAIT);
 
86
                ret = fd->size;
 
87
        } else {
 
88
                ret = MIN(nbytes, fd->count);
 
89
                memcpy(buf, fd->buf + fd->offset, ret);
 
90
                if (!(fd->count -= ret)) {
 
91
                        fd->offset = 0;
 
92
                }
 
93
 
 
94
                if (pthread = fd->wait) {
 
95
                        fd->wait = NULL;
 
96
                        pthread_sched_prevent();
 
97
                        pthread_sched_other_resume(pthread);
 
98
                }
 
99
        }
 
100
        return(ret);
 
101
}
 
102
 
 
103
/* ==========================================================================
 
104
 * __pipe_write()
 
105
 *
 
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.
 
112
 */
 
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;
 
117
        int ret, count;
 
118
 
 
119
        if (!(flags & O_ACCMODE)) { return(NOTOK); }
 
120
 
 
121
        while (fd->flags != RD_CLOSED) {
 
122
                if (pthread = fd->wait) {
 
123
 
 
124
                        pthread_sched_prevent();
 
125
        
 
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;
 
131
                        ret = fd->wait_size;
 
132
                        fd->wait = NULL;
 
133
 
 
134
                        /* Wake up waiting pthread */   
 
135
                        pthread_sched_other_resume(pthread);
 
136
                }
 
137
 
 
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;
 
141
                        nbytes -= count;
 
142
                        ret += count;
 
143
                }
 
144
                if (nbytes) {
 
145
                        pthread_sched_prevent();
 
146
                        fd->wait = pthread_run;
 
147
                        pthread_resched_resume(PS_FDW_WAIT);
 
148
                } else {                
 
149
                    return(ret);
 
150
                }
 
151
        }
 
152
        return(NOTOK);
 
153
}
 
154
 
 
155
/* ==========================================================================
 
156
 * __pipe_close()
 
157
 *
 
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...
 
166
 */
 
167
int __pipe_close(struct __pipe *fd, int flags)
 
168
{
 
169
        struct pthread * pthread;
 
170
 
 
171
        if (!(fd->flags)) {
 
172
                if (pthread = fd->wait) {
 
173
                        if (flags & O_ACCMODE) {
 
174
                                fd->count = 0;
 
175
                                fd->wait = NULL;
 
176
                                fd->flags |= WR_CLOSED;
 
177
                                pthread_sched_prevent();
 
178
                                pthread_resched_resume(pthread);
 
179
                        } else {
 
180
                                /* Should send a signal */
 
181
                                fd->flags |= RD_CLOSED;
 
182
                        }
 
183
                }
 
184
        } else {
 
185
                free(fd);
 
186
                return(OK);
 
187
        }
 
188
}
 
189
 
 
190
/* ==========================================================================
 
191
 * For fcntl() which isn't implemented yet
 
192
 * __pipe_enosys()
 
193
 */
 
194
static int __pipe_enosys()
 
195
{
 
196
        SET_ERRNO(ENOSYS);
 
197
        return(NOTOK);
 
198
}
 
199
 
 
200
/* ==========================================================================
 
201
 * For writev() and readv() which aren't implemented yet
 
202
 * __pipe_enosys_v()
 
203
 */
 
204
static int __pipe_enosys_v(union fd_data fd, int flags,
 
205
                           const struct iovec *vec, int nvec,
 
206
                           struct timespec *timeout)
 
207
{
 
208
        SET_ERRNO(ENOSYS);
 
209
        return(NOTOK);
 
210
}
 
211
 
 
212
/* ==========================================================================
 
213
 * For lseek() which isn't implemented yet
 
214
 * __pipe_enosys_o()
 
215
 */
 
216
static off_t __pipe_enosys_o()
 
217
{
 
218
        SET_ERRNO(ENOSYS);
 
219
        return(NOTOK);
 
220
}
 
221
 
 
222
/*
 
223
 * File descriptor operations
 
224
 */
 
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 },
 
228
};
 
229
 
 
230
/* ==========================================================================
 
231
 * open()
 
232
 */
 
233
/* int __pipe_open(const char *path, int flags, ...) */
 
234
int newpipe(int fd[2])
 
235
{
 
236
        struct __pipe *fd_data;
 
237
 
 
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;
 
242
                fd_data->count = 0;
 
243
                fd_data->offset = 0;
 
244
 
 
245
                fd_data->wait = NULL;
 
246
                fd_data->flags = 0;
 
247
 
 
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;
 
252
 
 
253
                return(OK);
 
254
        }
 
255
        return(NOTOK);
 
256
}
 
257