fd_pipe.c
上传用户:tsgydb
上传日期:2007-04-14
资源大小:10674k
文件大小:7k
源码类别:

MySQL数据库

开发平台:

Visual C++

  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. #ifndef lint
  41. static const char rcsid[] = "$Id$";
  42. #endif
  43. #include <pthread.h>
  44. #include <pthread/fd_pipe.h>
  45. #include <sys/types.h>
  46. #include <sys/socket.h>
  47. #include <sys/time.h>
  48. #include <fcntl.h>
  49. #include <errno.h>
  50. #include <pthread/posix.h>
  51. #include <string.h>
  52. #include <stdlib.h>
  53. #ifndef MIN
  54. #define MIN(a,b) ((a)<(b)?(a):(b))
  55. #endif
  56. /* ==========================================================================
  57.  * The pipe lock is never unlocked until all pthreads waiting are done with it
  58.  * read()
  59.  */
  60. pthread_ssize_t __pipe_read(union fd_data fd_data, int flags, void *buf,
  61.  size_t nbytes, struct timespec * timeout)
  62. {
  63. struct __pipe *fd = (struct __pipe *)fd_data.ptr;
  64. struct pthread * pthread;
  65. int ret = 0;
  66. if (flags & O_ACCMODE) { return(NOTOK); }
  67. /* If there is nothing to read, go to sleep */
  68. if (fd->count == 0) {
  69. if (flags == WR_CLOSED) {
  70. return(0);
  71. }
  72. pthread_sched_prevent();
  73. /* queue pthread for a FDR_WAIT */
  74. pthread_run->next = NULL;
  75. fd->wait = pthread_run;
  76. pthread_resched_resume(PS_FDR_WAIT);
  77. ret = fd->size;
  78. } else {
  79. ret = MIN(nbytes, fd->count);
  80. memcpy(buf, fd->buf + fd->offset, ret);
  81. if (!(fd->count -= ret)) {
  82. fd->offset = 0;
  83. }
  84. if (pthread = fd->wait) {
  85. fd->wait = NULL;
  86. pthread_sched_prevent();
  87. pthread_sched_other_resume(pthread);
  88. }
  89. }
  90. return(ret);
  91. }
  92. /* ==========================================================================
  93.  * __pipe_write()
  94.  *
  95.  * First check to see if the read side is still open, then
  96.  * check to see if there is a thread in a read wait for this pipe, if so
  97.  * copy as much data as possible directly into the read waiting threads
  98.  * buffer. The write thread(whether or not there was a read thread)
  99.  * copies as much data as it can into the pipe buffer and it there
  100.  * is still data it goes to sleep.
  101.  */
  102. pthread_ssize_t __pipe_write(union fd_data fd_data, int flags, const void *buf,
  103.  size_t nbytes, struct timespec * timeout) {
  104. struct __pipe *fd = (struct __pipe *)fd_data.ptr;
  105. struct pthread * pthread;
  106. int ret, count;
  107. if (!(flags & O_ACCMODE)) { return(NOTOK); }
  108. while (fd->flags != RD_CLOSED) {
  109. if (pthread = fd->wait) {
  110. pthread_sched_prevent();
  111. /* Copy data directly into waiting pthreads buf */
  112. fd->wait_size = MIN(nbytes, fd->wait_size);
  113. memcpy(fd->wait_buf, buf, fd->wait_size);
  114. buf = (const char *)buf + fd->wait_size;
  115. nbytes -= fd->wait_size;
  116. ret = fd->wait_size;
  117. fd->wait = NULL;
  118. /* Wake up waiting pthread */
  119. pthread_sched_other_resume(pthread);
  120. }
  121. if (count = MIN(nbytes, fd->size - (fd->offset + fd->count))) {
  122. memcpy(fd->buf + (fd->offset + fd->count), buf, count);
  123. buf = (const char *)buf + count;
  124. nbytes -= count;
  125. ret += count;
  126. }
  127. if (nbytes) {
  128. pthread_sched_prevent();
  129. fd->wait = pthread_run;
  130. pthread_resched_resume(PS_FDW_WAIT);
  131. } else {
  132.         return(ret);
  133. }
  134. }
  135. return(NOTOK);
  136. }
  137. /* ==========================================================================
  138.  * __pipe_close()
  139.  *
  140.  * The whole close procedure is a bit odd and needs a bit of a rethink.
  141.  * For now close() locks the fd, calls fd_free() which checks to see if
  142.  * there are any other fd values poinging to the same real fd. If so
  143.  * It breaks the wait queue into two sections those that are waiting on fd
  144.  * and those waiting on other fd's. Those that are waiting on fd are connected
  145.  * to the fd_table[fd] queue, and the count is set to zero, (BUT THE LOCK IS NOT
  146.  * RELEASED). close() then calls fd_unlock which give the fd to the next queued
  147.  * element which determins that the fd is closed and then calls fd_unlock etc...
  148.  */
  149. int __pipe_close(struct __pipe *fd, int flags)
  150. {
  151. struct pthread * pthread;
  152. if (!(fd->flags)) {
  153. if (pthread = fd->wait) {
  154. if (flags & O_ACCMODE) {
  155. fd->count = 0;
  156. fd->wait = NULL;
  157. fd->flags |= WR_CLOSED;
  158. pthread_sched_prevent();
  159. pthread_resched_resume(pthread);
  160. } else {
  161. /* Should send a signal */
  162. fd->flags |= RD_CLOSED;
  163. }
  164. }
  165. } else {
  166. free(fd);
  167. return(OK);
  168. }
  169. }
  170. /* ==========================================================================
  171.  * For fcntl() which isn't implemented yet
  172.  * __pipe_enosys()
  173.  */
  174. static int __pipe_enosys()
  175. {
  176. SET_ERRNO(ENOSYS);
  177. return(NOTOK);
  178. }
  179. /* ==========================================================================
  180.  * For writev() and readv() which aren't implemented yet
  181.  * __pipe_enosys_v()
  182.  */
  183. static int __pipe_enosys_v(union fd_data fd, int flags,
  184.    const struct iovec *vec, int nvec,
  185.    struct timespec *timeout)
  186. {
  187. SET_ERRNO(ENOSYS);
  188. return(NOTOK);
  189. }
  190. /* ==========================================================================
  191.  * For lseek() which isn't implemented yet
  192.  * __pipe_enosys_o()
  193.  */
  194. static off_t __pipe_enosys_o()
  195. {
  196. SET_ERRNO(ENOSYS);
  197. return(NOTOK);
  198. }
  199. /*
  200.  * File descriptor operations
  201.  */
  202. struct fd_ops fd_ops[] = {
  203. { __pipe_write, __pipe_read, __pipe_close, __pipe_enosys,
  204. __pipe_enosys_v, __pipe_enosys_v, __pipe_enosys_o, 0 },
  205. };
  206. /* ==========================================================================
  207.  * open()
  208.  */
  209. /* int __pipe_open(const char *path, int flags, ...) */
  210. int newpipe(int fd[2])
  211. {
  212. struct __pipe *fd_data;
  213. if ((!((fd[0] = fd_allocate()) < OK)) && (!((fd[1] = fd_allocate()) < OK))) {
  214. fd_data = malloc(sizeof(struct __pipe));
  215. fd_data->buf = malloc(4096);
  216. fd_data->size = 4096;
  217. fd_data->count = 0;
  218. fd_data->offset = 0;
  219. fd_data->wait = NULL;
  220. fd_data->flags = 0;
  221. fd_table[fd[0]]->fd.ptr = fd_data;
  222. fd_table[fd[0]]->flags = O_RDONLY;
  223. fd_table[fd[1]]->fd.ptr = fd_data;
  224. fd_table[fd[1]]->flags = O_WRONLY;
  225. return(OK);
  226. }
  227. return(NOTOK);
  228. }