From 8ddd92d56ae7c30f086f69c8e2930b44c5a44f1c Mon Sep 17 00:00:00 2001 From: Sebastian Huber Date: Thu, 14 Dec 2017 06:12:59 +0100 Subject: pipe: Use self-contained mutex Update #2843. --- cpukit/libfs/src/pipe/fifo.c | 106 ++++++++----------------------------------- 1 file changed, 19 insertions(+), 87 deletions(-) (limited to 'cpukit/libfs/src') diff --git a/cpukit/libfs/src/pipe/fifo.c b/cpukit/libfs/src/pipe/fifo.c index 4743bd06a1..d664adfba8 100644 --- a/cpukit/libfs/src/pipe/fifo.c +++ b/cpukit/libfs/src/pipe/fifo.c @@ -33,7 +33,7 @@ #define LIBIO_ACCMODE(_iop) (rtems_libio_iop_flags(_iop) & LIBIO_FLAGS_READ_WRITE) #define LIBIO_NODELAY(_iop) rtems_libio_iop_is_no_delay(_iop) -static rtems_id pipe_semaphore = RTEMS_ID_NONE; +static rtems_mutex pipe_mutex = RTEMS_MUTEX_INITIALIZER("Pipes"); #define PIPE_EMPTY(_pipe) (_pipe->Length == 0) @@ -41,11 +41,9 @@ static rtems_id pipe_semaphore = RTEMS_ID_NONE; #define PIPE_SPACE(_pipe) (_pipe->Size - _pipe->Length) #define PIPE_WSTART(_pipe) ((_pipe->Start + _pipe->Length) % _pipe->Size) -#define PIPE_LOCK(_pipe) \ - ( rtems_semaphore_obtain(_pipe->Semaphore, RTEMS_WAIT, RTEMS_NO_TIMEOUT) \ - == RTEMS_SUCCESSFUL ) +#define PIPE_LOCK(_pipe) rtems_mutex_lock(&(_pipe)->Mutex) -#define PIPE_UNLOCK(_pipe) rtems_semaphore_release(_pipe->Semaphore) +#define PIPE_UNLOCK(_pipe) rtems_mutex_unlock(&(_pipe)->Mutex) #define PIPE_READWAIT(_pipe) \ ( rtems_barrier_wait(_pipe->readBarrier, RTEMS_NO_TIMEOUT) \ @@ -95,18 +93,13 @@ static int pipe_alloc( RTEMS_BARRIER_MANUAL_RELEASE, 0, &pipe->writeBarrier) != RTEMS_SUCCESSFUL) goto err_wbar; - if (rtems_semaphore_create( - rtems_build_name ('P', 'I', 's', c), 1, - RTEMS_BINARY_SEMAPHORE | RTEMS_PRIORITY | RTEMS_INHERIT_PRIORITY, - 0, &pipe->Semaphore) != RTEMS_SUCCESSFUL) - goto err_sem; + rtems_mutex_init(&pipe->Mutex, "Pipe"); *pipep = pipe; if (c ++ == 'z') c = 'a'; return 0; -err_sem: rtems_barrier_delete(pipe->writeBarrier); err_wbar: rtems_barrier_delete(pipe->readBarrier); @@ -124,55 +117,19 @@ static inline void pipe_free( { rtems_barrier_delete(pipe->readBarrier); rtems_barrier_delete(pipe->writeBarrier); - rtems_semaphore_delete(pipe->Semaphore); + rtems_mutex_destroy(&pipe->Mutex); free(pipe->Buffer); free(pipe); } -static int pipe_lock(void) +static void pipe_lock(void) { - rtems_status_code sc = RTEMS_SUCCESSFUL; - - if (pipe_semaphore == RTEMS_ID_NONE) { - rtems_libio_lock(); - - if (pipe_semaphore == RTEMS_ID_NONE) { - sc = rtems_semaphore_create( - rtems_build_name('P', 'I', 'P', 'E'), - 1, - RTEMS_BINARY_SEMAPHORE | RTEMS_INHERIT_PRIORITY | RTEMS_PRIORITY, - RTEMS_NO_PRIORITY, - &pipe_semaphore - ); - } - - rtems_libio_unlock(); - } - - if (sc == RTEMS_SUCCESSFUL) { - sc = rtems_semaphore_obtain(pipe_semaphore, RTEMS_WAIT, RTEMS_NO_TIMEOUT); - } - - if (sc == RTEMS_SUCCESSFUL) { - return 0; - } else { - return -ENOMEM; - } + rtems_mutex_lock(&pipe_mutex); } static void pipe_unlock(void) { -#ifdef RTEMS_DEBUG - rtems_status_code sc = RTEMS_SUCCESSFUL; - - sc = -#endif - rtems_semaphore_release(pipe_semaphore); - #ifdef RTEMS_DEBUG - if (sc != RTEMS_SUCCESSFUL) { - rtems_fatal_error_occurred(0xdeadbeef); - } - #endif + rtems_mutex_unlock(&pipe_mutex); } /* @@ -188,9 +145,7 @@ static int pipe_new( int err = 0; _Assert( pipep ); - err = pipe_lock(); - if (err) - return err; + pipe_lock(); pipe = *pipep; if (pipe == NULL) { @@ -199,8 +154,7 @@ static int pipe_new( goto out; } - if (!PIPE_LOCK(pipe)) - err = -EINTR; + PIPE_LOCK(pipe); if (*pipep == NULL) { if (err) @@ -222,15 +176,8 @@ void pipe_release( pipe_control_t *pipe = *pipep; uint32_t mode; - #if defined(RTEMS_DEBUG) - /* WARN pipe not freed and pipep not set to NULL! */ - if (pipe_lock()) - rtems_fatal_error_occurred(0xdeadbeef); - - /* WARN pipe not released! */ - if (!PIPE_LOCK(pipe)) - rtems_fatal_error_occurred(0xdeadbeef); - #endif + pipe_lock(); + PIPE_LOCK(pipe); mode = LIBIO_ACCMODE(iop); if (mode & LIBIO_FLAGS_READ) @@ -303,8 +250,7 @@ int fifo_open( PIPE_UNLOCK(pipe); if (! PIPE_READWAIT(pipe)) goto out_error; - if (! PIPE_LOCK(pipe)) - goto out_error; + PIPE_LOCK(pipe); } while (prevCounter == pipe->writerCounter); } break; @@ -328,8 +274,7 @@ int fifo_open( PIPE_UNLOCK(pipe); if (! PIPE_WRITEWAIT(pipe)) goto out_error; - if (! PIPE_LOCK(pipe)) - goto out_error; + PIPE_LOCK(pipe); } while (prevCounter == pipe->readerCounter); } break; @@ -361,8 +306,7 @@ ssize_t pipe_read( { int chunk, chunk1, read = 0, ret = 0; - if (! PIPE_LOCK(pipe)) - return -EINTR; + PIPE_LOCK(pipe); while (PIPE_EMPTY(pipe)) { /* Not an error */ @@ -379,11 +323,7 @@ ssize_t pipe_read( PIPE_UNLOCK(pipe); if (! PIPE_READWAIT(pipe)) ret = -EINTR; - if (! PIPE_LOCK(pipe)) { - /* WARN waitingReaders not restored! */ - ret = -EINTR; - goto out_nolock; - } + PIPE_LOCK(pipe); pipe->waitingReaders --; if (ret != 0) goto out_locked; @@ -413,7 +353,6 @@ ssize_t pipe_read( out_locked: PIPE_UNLOCK(pipe); -out_nolock: if (read > 0) return read; return ret; @@ -432,8 +371,7 @@ ssize_t pipe_write( if (count == 0) return 0; - if (! PIPE_LOCK(pipe)) - return -EINTR; + PIPE_LOCK(pipe); if (pipe->Readers == 0) { ret = -EPIPE; @@ -455,11 +393,7 @@ ssize_t pipe_write( PIPE_UNLOCK(pipe); if (! PIPE_WRITEWAIT(pipe)) ret = -EINTR; - if (! PIPE_LOCK(pipe)) { - /* WARN waitingWriters not restored! */ - ret = -EINTR; - goto out_nolock; - } + PIPE_LOCK(pipe); pipe->waitingWriters --; if (ret != 0) goto out_locked; @@ -490,7 +424,6 @@ ssize_t pipe_write( out_locked: PIPE_UNLOCK(pipe); -out_nolock: #ifdef RTEMS_POSIX_API /* Signal SIGPIPE */ if (ret == -EPIPE) @@ -513,8 +446,7 @@ int pipe_ioctl( if (buffer == NULL) return -EFAULT; - if (! PIPE_LOCK(pipe)) - return -EINTR; + PIPE_LOCK(pipe); /* Return length of pipe */ *(unsigned int *)buffer = pipe->Length; -- cgit v1.2.3