Changeset 8ddd92d in rtems


Ignore:
Timestamp:
Dec 14, 2017, 5:12:59 AM (22 months ago)
Author:
Sebastian Huber <sebastian.huber@…>
Branches:
master
Children:
03e5a780
Parents:
0940648f
git-author:
Sebastian Huber <sebastian.huber@…> (12/14/17 05:12:59)
git-committer:
Sebastian Huber <sebastian.huber@…> (02/02/18 14:01:23)
Message:

pipe: Use self-contained mutex

Update #2843.

Files:
4 edited

Legend:

Unmodified
Added
Removed
  • cpukit/include/rtems/confdefs.h

    r0940648f r8ddd92d  
    228228#endif
    229229
    230 /*
    231  * This specifies the number of semaphores required for the configured
    232  * number of FIFOs and named pipes.
    233  */
    234 #if CONFIGURE_MAXIMUM_FIFOS > 0 || CONFIGURE_MAXIMUM_PIPES > 0
    235   #define _CONFIGURE_SEMAPHORES_FOR_FIFOS \
    236     (1 + (CONFIGURE_MAXIMUM_FIFOS + CONFIGURE_MAXIMUM_PIPES))
    237 #else
    238   #define _CONFIGURE_SEMAPHORES_FOR_FIFOS 0
    239 #endif
    240 
    241230/**
    242231 *  @defgroup ConfigFilesystems Filesystems and Mount Table Configuration
     
    446435 * file systems including the FIFO plugin to the IMFS.
    447436 */
    448 #define _CONFIGURE_SEMAPHORES_FOR_FILE_SYSTEMS \
    449     (_CONFIGURE_SEMAPHORES_FOR_FIFOS + \
    450      _CONFIGURE_SEMAPHORES_FOR_NFS)
     437#define _CONFIGURE_SEMAPHORES_FOR_FILE_SYSTEMS _CONFIGURE_SEMAPHORES_FOR_NFS
    451438
    452439#ifdef CONFIGURE_INIT
  • cpukit/include/rtems/pipe.h

    r0940648f r8ddd92d  
    2020
    2121#include <rtems/libio.h>
     22#include <rtems/thread.h>
    2223
    2324/**
     
    4647  unsigned int readerCounter;     /* incremental counters */
    4748  unsigned int writerCounter;     /* for differentiation of successive opens */
    48   rtems_id Semaphore;
     49  rtems_mutex Mutex;
    4950  rtems_id readBarrier;   /* wait queues */
    5051  rtems_id writeBarrier;
  • cpukit/libfs/src/pipe/fifo.c

    r0940648f r8ddd92d  
    3434#define LIBIO_NODELAY(_iop) rtems_libio_iop_is_no_delay(_iop)
    3535
    36 static rtems_id pipe_semaphore = RTEMS_ID_NONE;
     36static rtems_mutex pipe_mutex = RTEMS_MUTEX_INITIALIZER("Pipes");
    3737
    3838
     
    4242#define PIPE_WSTART(_pipe) ((_pipe->Start + _pipe->Length) % _pipe->Size)
    4343
    44 #define PIPE_LOCK(_pipe)  \
    45   ( rtems_semaphore_obtain(_pipe->Semaphore, RTEMS_WAIT, RTEMS_NO_TIMEOUT)  \
    46    == RTEMS_SUCCESSFUL )
    47 
    48 #define PIPE_UNLOCK(_pipe)  rtems_semaphore_release(_pipe->Semaphore)
     44#define PIPE_LOCK(_pipe) rtems_mutex_lock(&(_pipe)->Mutex)
     45
     46#define PIPE_UNLOCK(_pipe) rtems_mutex_unlock(&(_pipe)->Mutex)
    4947
    5048#define PIPE_READWAIT(_pipe)  \
     
    9694        &pipe->writeBarrier) != RTEMS_SUCCESSFUL)
    9795    goto err_wbar;
    98   if (rtems_semaphore_create(
    99         rtems_build_name ('P', 'I', 's', c), 1,
    100         RTEMS_BINARY_SEMAPHORE | RTEMS_PRIORITY | RTEMS_INHERIT_PRIORITY,
    101         0, &pipe->Semaphore) != RTEMS_SUCCESSFUL)
    102     goto err_sem;
     96  rtems_mutex_init(&pipe->Mutex, "Pipe");
    10397
    10498  *pipep = pipe;
     
    107101  return 0;
    108102
    109 err_sem:
    110103  rtems_barrier_delete(pipe->writeBarrier);
    111104err_wbar:
     
    125118  rtems_barrier_delete(pipe->readBarrier);
    126119  rtems_barrier_delete(pipe->writeBarrier);
    127   rtems_semaphore_delete(pipe->Semaphore);
     120  rtems_mutex_destroy(&pipe->Mutex);
    128121  free(pipe->Buffer);
    129122  free(pipe);
    130123}
    131124
    132 static int pipe_lock(void)
    133 {
    134   rtems_status_code sc = RTEMS_SUCCESSFUL;
    135 
    136   if (pipe_semaphore == RTEMS_ID_NONE) {
    137     rtems_libio_lock();
    138 
    139     if (pipe_semaphore == RTEMS_ID_NONE) {
    140       sc = rtems_semaphore_create(
    141         rtems_build_name('P', 'I', 'P', 'E'),
    142         1,
    143         RTEMS_BINARY_SEMAPHORE | RTEMS_INHERIT_PRIORITY | RTEMS_PRIORITY,
    144         RTEMS_NO_PRIORITY,
    145         &pipe_semaphore
    146       );
    147     }
    148 
    149     rtems_libio_unlock();
    150   }
    151 
    152   if (sc == RTEMS_SUCCESSFUL) {
    153     sc = rtems_semaphore_obtain(pipe_semaphore, RTEMS_WAIT, RTEMS_NO_TIMEOUT);
    154   }
    155 
    156   if (sc == RTEMS_SUCCESSFUL) {
    157     return 0;
    158   } else {
    159     return -ENOMEM;
    160   }
     125static void pipe_lock(void)
     126{
     127  rtems_mutex_lock(&pipe_mutex);
    161128}
    162129
    163130static void pipe_unlock(void)
    164131{
    165 #ifdef RTEMS_DEBUG
    166   rtems_status_code sc = RTEMS_SUCCESSFUL;
    167 
    168   sc =
    169 #endif
    170    rtems_semaphore_release(pipe_semaphore);
    171   #ifdef RTEMS_DEBUG
    172     if (sc != RTEMS_SUCCESSFUL) {
    173       rtems_fatal_error_occurred(0xdeadbeef);
    174     }
    175   #endif
     132  rtems_mutex_unlock(&pipe_mutex);
    176133}
    177134
     
    189146
    190147  _Assert( pipep );
    191   err = pipe_lock();
    192   if (err)
    193     return err;
     148  pipe_lock();
    194149
    195150  pipe = *pipep;
     
    200155  }
    201156
    202   if (!PIPE_LOCK(pipe))
    203     err = -EINTR;
     157  PIPE_LOCK(pipe);
    204158
    205159  if (*pipep == NULL) {
     
    223177  uint32_t mode;
    224178
    225   #if defined(RTEMS_DEBUG)
    226     /* WARN pipe not freed and pipep not set to NULL! */
    227     if (pipe_lock())
    228       rtems_fatal_error_occurred(0xdeadbeef);
    229 
    230     /* WARN pipe not released! */
    231     if (!PIPE_LOCK(pipe))
    232       rtems_fatal_error_occurred(0xdeadbeef);
    233   #endif
     179  pipe_lock();
     180  PIPE_LOCK(pipe);
    234181
    235182  mode = LIBIO_ACCMODE(iop);
     
    304251          if (! PIPE_READWAIT(pipe))
    305252            goto out_error;
    306           if (! PIPE_LOCK(pipe))
    307             goto out_error;
     253          PIPE_LOCK(pipe);
    308254        } while (prevCounter == pipe->writerCounter);
    309255      }
     
    329275          if (! PIPE_WRITEWAIT(pipe))
    330276            goto out_error;
    331           if (! PIPE_LOCK(pipe))
    332             goto out_error;
     277          PIPE_LOCK(pipe);
    333278        } while (prevCounter == pipe->readerCounter);
    334279      }
     
    362307  int chunk, chunk1, read = 0, ret = 0;
    363308
    364   if (! PIPE_LOCK(pipe))
    365     return -EINTR;
     309  PIPE_LOCK(pipe);
    366310
    367311  while (PIPE_EMPTY(pipe)) {
     
    380324    if (! PIPE_READWAIT(pipe))
    381325      ret = -EINTR;
    382     if (! PIPE_LOCK(pipe)) {
    383       /* WARN waitingReaders not restored! */
    384       ret = -EINTR;
    385       goto out_nolock;
    386     }
     326    PIPE_LOCK(pipe);
    387327    pipe->waitingReaders --;
    388328    if (ret != 0)
     
    414354  PIPE_UNLOCK(pipe);
    415355
    416 out_nolock:
    417356  if (read > 0)
    418357    return read;
     
    433372    return 0;
    434373
    435   if (! PIPE_LOCK(pipe))
    436     return -EINTR;
     374  PIPE_LOCK(pipe);
    437375
    438376  if (pipe->Readers == 0) {
     
    456394      if (! PIPE_WRITEWAIT(pipe))
    457395        ret = -EINTR;
    458       if (! PIPE_LOCK(pipe)) {
    459         /* WARN waitingWriters not restored! */
    460         ret = -EINTR;
    461         goto out_nolock;
    462       }
     396      PIPE_LOCK(pipe);
    463397      pipe->waitingWriters --;
    464398      if (ret != 0)
     
    491425  PIPE_UNLOCK(pipe);
    492426
    493 out_nolock:
    494427#ifdef RTEMS_POSIX_API
    495428  /* Signal SIGPIPE */
     
    514447      return -EFAULT;
    515448
    516     if (! PIPE_LOCK(pipe))
    517       return -EINTR;
     449    PIPE_LOCK(pipe);
    518450
    519451    /* Return length of pipe */
  • testsuites/sptests/spfifo02/init.c

    r0940648f r8ddd92d  
    2727rtems_task Init(rtems_task_argument argument);
    2828void create_all_barriers(void);
    29 void create_all_semaphores(void);
    3029void delete_barrier(void);
    31 void delete_semaphore(void);
    3230void create_fifo(void);
    3331void open_fifo(int expected, int flags);
     
    6765}
    6866
    69 void create_all_semaphores(void)
    70 {
    71   rtems_status_code status;
    72   int               i;
    73 
    74   SemaphoreCount = 0;
    75 
    76   for ( i=0 ; i<MAXIMUM ; i++ ) {
    77     status = rtems_semaphore_create(
    78       rtems_build_name( 'S', 'E', 'M', 0x30+i ),
    79       0,
    80       RTEMS_DEFAULT_ATTRIBUTES,
    81       0,
    82       &Semaphores[i]
    83     );
    84     if ( status == RTEMS_TOO_MANY ) {
    85       printf( "%d Semaphores created\n", SemaphoreCount+1 );
    86       return;
    87     }
    88 
    89     directive_failed( status, "semaphore create" );
    90     SemaphoreCount++;
    91   }
    92 }
    93 
    9467void delete_barrier(void)
    9568{
     
    10174  status = rtems_barrier_delete( Barriers[BarrierCount] );
    10275  directive_failed( status, "barrier delete" );
    103 }
    104 
    105 void delete_semaphore(void)
    106 {
    107   rtems_status_code status;
    108  
    109   SemaphoreCount--;
    110   printf( "Deleting semaphore id=0x%08x\n",
    111     (unsigned int) Semaphores[SemaphoreCount] );
    112   status = rtems_semaphore_delete( Semaphores[SemaphoreCount] );
    113   directive_failed( status, "semaphore delete" );
    11476}
    11577
     
    150112  create_all_barriers();
    151113
    152   puts( "Creating all semaphores" );
    153   create_all_semaphores();
    154 
    155114  puts( "Creating FIFO" );
    156115  create_fifo();
    157 
    158   puts( "Opening FIFO.. expect ENOMEM (semaphore for pipe could not be created)" );
    159   open_fifo(ENOMEM, O_RDWR);
    160 
    161   delete_semaphore();
    162116
    163117  alloc_ptr = malloc( malloc_free_space() - 4 );
     
    174128
    175129  delete_barrier();
    176   puts( "Opening FIFO.. expect ENOMEM (semaphore-1 for pipe could not be created" );
    177   open_fifo(ENOMEM, O_RDWR);
    178 
    179   delete_semaphore();
    180130  puts( "Opening FIFO in RDWR mode. Expect OK" );
    181131  open_fifo(0, O_RDWR);
Note: See TracChangeset for help on using the changeset viewer.