source: rtems/cpukit/libfs/src/pipe/fifo.c @ d9f0918

4.10
Last change on this file since d9f0918 was d9f0918, checked in by Ralf Corsepius <ralf.corsepius@…>, on 06/14/10 at 05:49:56

2010-06-14 Ralf Corsépius <ralf.corsepius@…>

  • libfs/src/pipe/fifo.c, libfs/src/pipe/pipe.h: Eliminate "uint".
  • Property mode set to 100644
File size: 12.6 KB
Line 
1/*
2 * fifo.c: POSIX FIFO/pipe for RTEMS
3 *
4 * Author: Wei Shen <cquark@gmail.com>
5 *
6 * The license and distribution terms for this file may be
7 * found in the file LICENSE in this distribution or at
8 * http://www.rtems.com/license/LICENSE.
9 *
10 * $Id$
11 */
12
13
14#if HAVE_CONFIG_H
15#include "config.h"
16#endif
17
18#ifdef RTEMS_POSIX_API
19#define __RTEMS_VIOLATE_KERNEL_VISIBILITY__
20#endif
21
22#include <errno.h>
23#include <stdlib.h>
24#include "pipe.h"
25
26
27#define MIN(a, b) ((a) < (b)? (a): (b))
28
29#define LIBIO_ACCMODE(_iop) ((_iop)->flags & LIBIO_FLAGS_READ_WRITE)
30#define LIBIO_NODELAY(_iop) ((_iop)->flags & LIBIO_FLAGS_NO_DELAY)
31
32extern uint16_t rtems_pipe_no;
33static rtems_id rtems_pipe_semaphore = 0;
34extern bool rtems_pipe_configured;
35
36
37#define PIPE_EMPTY(_pipe) (_pipe->Length == 0)
38#define PIPE_FULL(_pipe)  (_pipe->Length == _pipe->Size)
39#define PIPE_SPACE(_pipe) (_pipe->Size - _pipe->Length)
40#define PIPE_WSTART(_pipe) ((_pipe->Start + _pipe->Length) % _pipe->Size)
41
42#define PIPE_LOCK(_pipe)  \
43  ( rtems_semaphore_obtain(_pipe->Semaphore, RTEMS_WAIT, RTEMS_NO_TIMEOUT)  \
44   == RTEMS_SUCCESSFUL )
45
46#define PIPE_UNLOCK(_pipe)  rtems_semaphore_release(_pipe->Semaphore)
47
48#define PIPE_READWAIT(_pipe)  \
49  ( rtems_barrier_wait(_pipe->readBarrier, RTEMS_NO_TIMEOUT)  \
50   == RTEMS_SUCCESSFUL)
51
52#define PIPE_WRITEWAIT(_pipe)  \
53  ( rtems_barrier_wait(_pipe->writeBarrier, RTEMS_NO_TIMEOUT)  \
54   == RTEMS_SUCCESSFUL)
55
56#define PIPE_WAKEUPREADERS(_pipe) \
57  do {uint32_t n; rtems_barrier_release(_pipe->readBarrier, &n); } while(0)
58
59#define PIPE_WAKEUPWRITERS(_pipe) \
60  do {uint32_t n; rtems_barrier_release(_pipe->writeBarrier, &n); } while(0)
61
62
63#ifdef RTEMS_POSIX_API
64#define __RTEMS_VIOLATE_KERNEL_VISIBILITY__
65
66#include <rtems/rtems/barrier.h>
67#include <rtems/score/thread.h>
68
69/* Set barriers to be interruptible by signals. */
70static void pipe_interruptible(pipe_control_t *pipe)
71{
72  Objects_Locations location;
73
74  _Barrier_Get(pipe->readBarrier, &location)->Barrier.Wait_queue.state
75    |= STATES_INTERRUPTIBLE_BY_SIGNAL;
76  _Thread_Enable_dispatch();
77  _Barrier_Get(pipe->writeBarrier, &location)->Barrier.Wait_queue.state
78    |= STATES_INTERRUPTIBLE_BY_SIGNAL;
79  _Thread_Enable_dispatch();
80}
81#endif
82
83/*
84 * Alloc pipe control structure, buffer, and resources.
85 * Called with rtems_pipe_semaphore held.
86 */
87static int pipe_alloc(
88  pipe_control_t **pipep
89)
90{
91  static char c = 'a';
92  pipe_control_t *pipe;
93  int err = -ENOMEM;
94
95  pipe = malloc(sizeof(pipe_control_t));
96  if (pipe == NULL)
97    return err;
98  memset(pipe, 0, sizeof(pipe_control_t));
99
100  pipe->Size = PIPE_BUF;
101  pipe->Buffer = malloc(pipe->Size);
102  if (! pipe->Buffer)
103    goto err_buf;
104
105  err = -EINTR;
106  if (rtems_barrier_create(
107        rtems_build_name ('P', 'I', 'r', c),
108        RTEMS_BARRIER_MANUAL_RELEASE, 0,
109        &pipe->readBarrier) != RTEMS_SUCCESSFUL)
110    goto err_rbar;
111  if (rtems_barrier_create(
112        rtems_build_name ('P', 'I', 'w', c),
113        RTEMS_BARRIER_MANUAL_RELEASE, 0,
114        &pipe->writeBarrier) != RTEMS_SUCCESSFUL)
115    goto err_wbar;
116  if (rtems_semaphore_create(
117        rtems_build_name ('P', 'I', 's', c), 1,
118        RTEMS_BINARY_SEMAPHORE | RTEMS_FIFO,
119        RTEMS_NO_PRIORITY, &pipe->Semaphore) != RTEMS_SUCCESSFUL)
120    goto err_sem;
121
122#ifdef RTEMS_POSIX_API
123  pipe_interruptible(pipe);
124#endif
125
126  *pipep = pipe;
127  if (c ++ == 'z')
128    c = 'a';
129  return 0;
130
131err_sem:
132  rtems_barrier_delete(pipe->writeBarrier);
133err_wbar:
134  rtems_barrier_delete(pipe->readBarrier);
135err_rbar:
136  free(pipe->Buffer);
137err_buf:
138  free(pipe);
139  return err;
140}
141
142/* Called with rtems_pipe_semaphore held. */
143static inline void pipe_free(
144  pipe_control_t *pipe
145)
146{
147  rtems_barrier_delete(pipe->readBarrier);
148  rtems_barrier_delete(pipe->writeBarrier);
149  rtems_semaphore_delete(pipe->Semaphore);
150  free(pipe->Buffer);
151  free(pipe);
152}
153
154/*
155 * If called with *pipep = NULL, pipe_new will call pipe_alloc to allocate a
156 * pipe control structure and set *pipep to its address.
157 * pipe is locked, when pipe_new returns with no error.
158 */
159static int pipe_new(
160  pipe_control_t **pipep
161)
162{
163  pipe_control_t *pipe;
164  int err = 0;
165
166  if (rtems_semaphore_obtain(rtems_pipe_semaphore,
167        RTEMS_WAIT, RTEMS_NO_TIMEOUT) != RTEMS_SUCCESSFUL)
168    return -EINTR;
169
170  pipe = *pipep;
171  if (pipe == NULL) {
172    err = pipe_alloc(&pipe);
173    if (err)
174      goto out;
175  }
176
177  if (! PIPE_LOCK(pipe))
178    err = -EINTR;
179
180  if (*pipep == NULL) {
181    if (err)
182      pipe_free(pipe);
183    else
184      *pipep = pipe;
185  }
186
187out:
188  rtems_semaphore_release(rtems_pipe_semaphore);
189  return err;
190}
191
192/*
193 * Interface to file system close.
194 *
195 * *pipep points to pipe control structure. When the last user releases pipe,
196 * it will be set to NULL.
197 */
198int pipe_release(
199  pipe_control_t **pipep,
200  rtems_libio_t *iop
201)
202{
203  pipe_control_t *pipe = *pipep;
204  uint32_t mode;
205
206  rtems_status_code sc;
207  sc = rtems_semaphore_obtain(pipe->Semaphore,
208                              RTEMS_WAIT, RTEMS_NO_TIMEOUT);
209  /* WARN pipe not released! */
210  if(sc != RTEMS_SUCCESSFUL)
211    rtems_fatal_error_occurred(sc);
212
213  mode = LIBIO_ACCMODE(iop);
214  if (mode & LIBIO_FLAGS_READ)
215     pipe->Readers --;
216  if (mode & LIBIO_FLAGS_WRITE)
217     pipe->Writers --;
218
219  sc = rtems_semaphore_obtain(rtems_pipe_semaphore,
220                              RTEMS_WAIT, RTEMS_NO_TIMEOUT);
221  /* WARN pipe not freed and pipep not set to NULL! */
222  if(sc != RTEMS_SUCCESSFUL)
223    rtems_fatal_error_occurred(sc);
224
225  PIPE_UNLOCK(pipe);
226
227  if (pipe->Readers == 0 && pipe->Writers == 0) {
228#if 0
229    /* To delete an anonymous pipe file when all users closed it */
230    if (pipe->Anonymous)
231      delfile = TRUE;
232#endif
233    pipe_free(pipe);
234    *pipep = NULL;
235  }
236  else if (pipe->Readers == 0 && mode != LIBIO_FLAGS_WRITE)
237    /* Notify waiting Writers that all their partners left */
238    PIPE_WAKEUPWRITERS(pipe);
239  else if (pipe->Writers == 0 && mode != LIBIO_FLAGS_READ)
240    PIPE_WAKEUPREADERS(pipe);
241
242  rtems_semaphore_release(rtems_pipe_semaphore);
243
244#if 0
245  if (! delfile)
246    return 0;
247  if (iop->pathinfo.ops->unlink_h == NULL)
248    return 0;
249
250  /* This is safe for IMFS, but how about other FSes? */
251  iop->flags &= ~LIBIO_FLAGS_OPEN;
252  if(iop->pathinfo.ops->unlink_h(&iop->pathinfo))
253    return -errno;
254#endif
255
256  return 0;
257}
258
259/*
260 * Interface to file system open.
261 *
262 * *pipep points to pipe control structure. If called with *pipep = NULL,
263 * fifo_open will try allocating and initializing a control structure. If the
264 * call succeeds, *pipep will be set to address of new control structure.
265 */
266int fifo_open(
267  pipe_control_t **pipep,
268  rtems_libio_t *iop
269)
270{
271  pipe_control_t *pipe;
272  unsigned int prevCounter;
273  int err;
274
275  err = pipe_new(pipep);
276  if (err)
277    return err;
278  pipe = *pipep;
279
280  switch (LIBIO_ACCMODE(iop)) {
281    case LIBIO_FLAGS_READ:
282      pipe->readerCounter ++;
283      if (pipe->Readers ++ == 0)
284        PIPE_WAKEUPWRITERS(pipe);
285
286      if (pipe->Writers == 0) {
287        /* Not an error */
288        if (LIBIO_NODELAY(iop))
289          break;
290
291        prevCounter = pipe->writerCounter;
292        err = -EINTR;
293        /* Wait until a writer opens the pipe */
294        do {
295          PIPE_UNLOCK(pipe);
296          if (! PIPE_READWAIT(pipe))
297            goto out_error;
298          if (! PIPE_LOCK(pipe))
299            goto out_error;
300        } while (prevCounter == pipe->writerCounter);
301      }
302      break;
303
304    case LIBIO_FLAGS_WRITE:
305      if (pipe->Readers == 0 && LIBIO_NODELAY(iop)) {
306        err = -ENXIO;
307        goto out_error;
308      }
309
310      pipe->writerCounter ++;
311      if (pipe->Writers ++ == 0)
312        PIPE_WAKEUPREADERS(pipe);
313
314      if (pipe->Readers == 0) {
315        prevCounter = pipe->readerCounter;
316        err = -EINTR;
317        do {
318          PIPE_UNLOCK(pipe);
319          if (! PIPE_WRITEWAIT(pipe))
320            goto out_error;
321          if (! PIPE_LOCK(pipe))
322            goto out_error;
323        } while (prevCounter == pipe->readerCounter);
324      }
325      break;
326
327    case LIBIO_FLAGS_READ_WRITE:
328      pipe->readerCounter ++;
329      if (pipe->Readers ++ == 0)
330        PIPE_WAKEUPWRITERS(pipe);
331      pipe->writerCounter ++;
332      if (pipe->Writers ++ == 0)
333        PIPE_WAKEUPREADERS(pipe);
334      break;
335  }
336
337  PIPE_UNLOCK(pipe);
338  return 0;
339
340out_error:
341  pipe_release(pipep, iop);
342  return err;
343}
344
345/*
346 * Interface to file system read.
347 */
348ssize_t pipe_read(
349  pipe_control_t *pipe,
350  void           *buffer,
351  size_t          count,
352  rtems_libio_t  *iop
353)
354{
355  int chunk, chunk1, read = 0, ret = 0;
356
357  if (! PIPE_LOCK(pipe))
358    return -EINTR;
359
360  while (read < count) {
361    while (PIPE_EMPTY(pipe)) {
362      /* Not an error */
363      if (pipe->Writers == 0)
364        goto out_locked;
365
366      if (LIBIO_NODELAY(iop)) {
367        ret = -EAGAIN;
368        goto out_locked;
369      }
370
371      /* Wait until pipe is no more empty or no writer exists */
372      pipe->waitingReaders ++;
373      PIPE_UNLOCK(pipe);
374      if (! PIPE_READWAIT(pipe))
375        ret = -EINTR;
376      if (! PIPE_LOCK(pipe)) {
377        /* WARN waitingReaders not restored! */
378        ret = -EINTR;
379        goto out_nolock;
380      }
381      pipe->waitingReaders --;
382      if (ret != 0)
383        goto out_locked;
384    }
385
386    /* Read chunk bytes */
387    chunk = MIN(count - read,  pipe->Length);
388    chunk1 = pipe->Size - pipe->Start;
389    if (chunk > chunk1) {
390      memcpy(buffer + read, pipe->Buffer + pipe->Start, chunk1);
391      memcpy(buffer + read + chunk1, pipe->Buffer, chunk - chunk1);
392    }
393    else
394      memcpy(buffer + read, pipe->Buffer + pipe->Start, chunk);
395
396    pipe->Start += chunk;
397    pipe->Start %= pipe->Size;
398    pipe->Length -= chunk;
399    /* For buffering optimization */
400    if (PIPE_EMPTY(pipe))
401      pipe->Start = 0;
402
403    if (pipe->waitingWriters > 0)
404      PIPE_WAKEUPWRITERS(pipe);
405    read += chunk;
406  }
407
408out_locked:
409  PIPE_UNLOCK(pipe);
410
411out_nolock:
412  if (read > 0)
413    return read;
414  return ret;
415}
416
417/*
418 * Interface to file system write.
419 */
420ssize_t pipe_write(
421  pipe_control_t *pipe,
422  const void     *buffer,
423  size_t          count,
424  rtems_libio_t  *iop
425)
426{
427  int chunk, chunk1, written = 0, ret = 0;
428
429  /* Write nothing */
430  if (count == 0)
431    return 0;
432
433  if (! PIPE_LOCK(pipe))
434    return -EINTR;
435
436  if (pipe->Readers == 0) {
437    ret = -EPIPE;
438    goto out_locked;
439  }
440
441  /* Write of PIPE_BUF bytes or less shall not be interleaved */
442  chunk = count <= pipe->Size ? count : 1;
443
444  while (written < count) {
445    while (PIPE_SPACE(pipe) < chunk) {
446      if (LIBIO_NODELAY(iop)) {
447        ret = -EAGAIN;
448        goto out_locked;
449      }
450
451      /* Wait until there is chunk bytes space or no reader exists */
452      pipe->waitingWriters ++;
453      PIPE_UNLOCK(pipe);
454      if (! PIPE_WRITEWAIT(pipe))
455        ret = -EINTR;
456      if (! PIPE_LOCK(pipe)) {
457        /* WARN waitingWriters not restored! */
458        ret = -EINTR;
459        goto out_nolock;
460      }
461      pipe->waitingWriters --;
462      if (ret != 0)
463        goto out_locked;
464
465      if (pipe->Readers == 0) {
466        ret = -EPIPE;
467        goto out_locked;
468      }
469    }
470
471    chunk = MIN(count - written, PIPE_SPACE(pipe));
472    chunk1 = pipe->Size - PIPE_WSTART(pipe);
473    if (chunk > chunk1) {
474      memcpy(pipe->Buffer + PIPE_WSTART(pipe), buffer + written, chunk1);
475      memcpy(pipe->Buffer, buffer + written + chunk1, chunk - chunk1);
476    }
477    else
478      memcpy(pipe->Buffer + PIPE_WSTART(pipe), buffer + written, chunk);
479
480    pipe->Length += chunk;
481    if (pipe->waitingReaders > 0)
482      PIPE_WAKEUPREADERS(pipe);
483    written += chunk;
484    /* Write of more than PIPE_BUF bytes can be interleaved */
485    chunk = 1;
486  }
487
488out_locked:
489  PIPE_UNLOCK(pipe);
490
491out_nolock:
492#ifdef RTEMS_POSIX_API
493  /* Signal SIGPIPE */
494  if (ret == -EPIPE)
495    kill(getpid(), SIGPIPE);
496#endif
497
498  if (written > 0)
499    return written;
500  return ret;
501}
502
503/*
504 * Interface to file system ioctl.
505 */
506int pipe_ioctl(
507  pipe_control_t *pipe,
508  uint32_t        cmd,
509  void           *buffer,
510  rtems_libio_t  *iop
511)
512{
513  if (cmd == FIONREAD) {
514    if (buffer == NULL)
515      return -EFAULT;
516
517    if (! PIPE_LOCK(pipe))
518      return -EINTR;
519
520    /* Return length of pipe */
521    *(unsigned int *)buffer = pipe->Length;
522    PIPE_UNLOCK(pipe);
523    return 0;
524  }
525
526  return -EINVAL;
527}
528
529/*
530 * Interface to file system lseek.
531 */
532int pipe_lseek(
533  pipe_control_t *pipe,
534  off_t           offset,
535  int             whence,
536  rtems_libio_t  *iop
537)
538{
539  /* Seek on pipe is not supported */
540  return -ESPIPE;
541}
542
543/*
544 * Initialization of FIFO/pipe module.
545 */
546void rtems_pipe_initialize (void)
547{
548  if (!rtems_pipe_configured)
549    return;
550
551  if (rtems_pipe_semaphore)
552    return;
553
554  rtems_status_code sc;
555  sc = rtems_semaphore_create(
556        rtems_build_name ('P', 'I', 'P', 'E'), 1,
557        RTEMS_BINARY_SEMAPHORE | RTEMS_INHERIT_PRIORITY | RTEMS_PRIORITY,
558        RTEMS_NO_PRIORITY, &rtems_pipe_semaphore);
559  if (sc != RTEMS_SUCCESSFUL)
560    rtems_fatal_error_occurred (sc);
561
562  rtems_interval now;
563  now = rtems_clock_get_ticks_since_boot();
564  rtems_pipe_no = now;
565}
Note: See TracBrowser for help on using the repository browser.