Changeset 2fd31117 in rtems


Ignore:
Timestamp:
Jan 9, 2018, 5:13:32 AM (19 months ago)
Author:
Sebastian Huber <sebastian.huber@…>
Branches:
master
Children:
0f7913b
Parents:
2aa5b98
git-author:
Sebastian Huber <sebastian.huber@…> (01/09/18 05:13:32)
git-committer:
Sebastian Huber <sebastian.huber@…> (02/07/18 07:58:31)
Message:

stdio-redirector: Use self-contained mutex

Update #2843.

Location:
cpukit
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • cpukit/include/rtems/stdio-redirect.h

    r2aa5b98 r2fd31117  
    4343#include <stdbool.h>
    4444#include <rtems.h>
     45#include <rtems/thread.h>
    4546
    4647#ifdef __cplusplus
     
    6465  volatile uint32_t            state;       /**< The state. */
    6566  rtems_id                     reader;      /**< The reader thread. */
    66   rtems_id                     lock;        /**< Lock for this struct. */
     67  rtems_mutex                  lock;        /**< Lock for this struct. */
    6768  int                          fd;          /**< The file descriptor to redirect. */
    6869  int                          fd_dup;      /**< Duplicated fd to write to. */
  • cpukit/libmisc/redirector/stdio-redirect.c

    r2aa5b98 r2fd31117  
    2020#include <unistd.h>
    2121
    22 #include <rtems.h>
     22#include <rtems/thread.h>
    2323#include <rtems/error.h>
    2424#include <rtems/stdio-redirect.h>
    2525
    26 #define RTEMS_STDIO_REDIRECT_LOCK_ATTRIBS                             \
    27   (RTEMS_PRIORITY | RTEMS_BINARY_SEMAPHORE |                          \
    28    RTEMS_INHERIT_PRIORITY | RTEMS_NO_PRIORITY_CEILING | RTEMS_LOCAL)
    29 
    3026#define RTEMS_STDIO_REDIRECT_RUNNING  (1 << 0)
    3127#define RTEMS_STDIO_REDIRECT_FINISHED (1 << 1)
    3228
    33 static bool
     29static void
    3430rtems_stdio_redirect_lock(rtems_stdio_redirect* sr)
    3531{
    36   rtems_status_code sc = rtems_semaphore_obtain (sr->lock,
    37                                                  RTEMS_WAIT,
    38                                                  RTEMS_NO_TIMEOUT);
    39   if (sc != RTEMS_SUCCESSFUL)
    40   {
    41     fprintf(stderr, "error: stdio-redirect: lock failed: %s\n", rtems_status_text(sc));
    42     return false;
    43   }
    44   return true;
    45 }
    46 
    47 static bool
     32  rtems_mutex_lock(&sr->lock);
     33}
     34
     35static void
    4836rtems_stdio_redirect_unlock(rtems_stdio_redirect* sr)
    4937{
    50   rtems_status_code sc = rtems_semaphore_release (sr->lock);
    51   if (sc != RTEMS_SUCCESSFUL)
    52   {
    53     fprintf(stderr, "error: stdio-redirect: unlock failed: %s\n", rtems_status_text(sc));
    54     return false;
    55   }
    56   return true;
    57 }
    58 
    59 static bool
     38  rtems_mutex_unlock(&sr->lock);
     39}
     40
     41static void
    6042rtems_stdio_redirect_write (rtems_stdio_redirect* sr, const char* buf, ssize_t len)
    6143{
    62   if (!rtems_stdio_redirect_lock(sr))
    63     return false;
     44  rtems_stdio_redirect_lock(sr);
    6445
    6546  if (sr->buffer)
     
    9475    sr->handler(buf, len);
    9576
    96   return rtems_stdio_redirect_unlock(sr);
     77  rtems_stdio_redirect_unlock(sr);
    9778}
    9879
     
    11293      write (sr->fd_dup, sr->input, r);
    11394
    114     if (!rtems_stdio_redirect_write (sr, sr->input, r))
    115       break;
     95    rtems_stdio_redirect_write (sr, sr->input, r);
    11696  }
    11797
     
    190170  sr->handler = handler;
    191171
    192   sc = rtems_semaphore_create (rtems_build_name ('R', 'S', 'R', 'l'),
    193                                1, RTEMS_STDIO_REDIRECT_LOCK_ATTRIBS, 0,
    194                                &sr->lock);
    195   if (sc != RTEMS_SUCCESSFUL)
    196   {
    197     fprintf(stderr, "error: stdio-redirect: lock create: %s\n", rtems_status_text(sc));
    198     free(sr->buffer);
    199     free(sr->input);
    200     free(sr);
    201     return NULL;
    202   }
     172  rtems_mutex_init(&sr->lock, "stdio-redirect");
    203173
    204174  name = rtems_build_name ('S', 'R', '0' + (fd / 10), '0' + (fd % 10));
     
    212182  {
    213183    fprintf(stderr, "error: stdio-redirect: reader create: %s\n", rtems_status_text(sc));
    214     rtems_semaphore_delete(sr->lock);
     184    rtems_mutex_destroy(&sr->lock);
    215185    free(sr->buffer);
    216186    free(sr->input);
     
    238208    fprintf(stderr, "error: stdio-redirect: reader start: %s\n", rtems_status_text(sc));
    239209    rtems_task_delete(sr->reader);
    240     rtems_semaphore_delete(sr->lock);
     210    rtems_mutex_destroy(&sr->lock);
    241211    free(sr->buffer);
    242212    free(sr->input);
     
    251221rtems_stdio_redirect_close(rtems_stdio_redirect* sr)
    252222{
    253   if (rtems_stdio_redirect_lock(sr))
    254   {
    255     sr->state &= ~RTEMS_STDIO_REDIRECT_RUNNING;
    256     close(sr->pipe[0]);
    257 
    258     rtems_stdio_redirect_unlock(sr);
    259 
    260     while (sr->state & RTEMS_STDIO_REDIRECT_FINISHED)
    261     {
    262       usleep(250UL * 1000000UL);
    263     }
    264 
    265     rtems_stdio_redirect_lock(sr);
    266 
    267     dup2(sr->fd, sr->fd_dup);
    268 
    269     free(sr->buffer);
    270     free(sr->input);
    271 
    272     rtems_stdio_redirect_unlock(sr);
    273 
    274     rtems_semaphore_delete(sr->lock);
    275 
    276     free(sr);
    277   }
     223  rtems_stdio_redirect_lock(sr);
     224
     225  sr->state &= ~RTEMS_STDIO_REDIRECT_RUNNING;
     226  close(sr->pipe[0]);
     227
     228  rtems_stdio_redirect_unlock(sr);
     229
     230  while (sr->state & RTEMS_STDIO_REDIRECT_FINISHED)
     231  {
     232    usleep(250UL * 1000000UL);
     233  }
     234
     235  rtems_stdio_redirect_lock(sr);
     236
     237  dup2(sr->fd, sr->fd_dup);
     238
     239  free(sr->buffer);
     240  free(sr->input);
     241
     242  rtems_stdio_redirect_unlock(sr);
     243
     244  rtems_mutex_destroy(&sr->lock);
     245
     246  free(sr);
    278247}
    279248
     
    285254  ssize_t written = 0;
    286255
    287   if (!rtems_stdio_redirect_lock(sr))
    288     return written;
     256  rtems_stdio_redirect_lock(sr);
    289257
    290258  if (sr->buffer)
Note: See TracChangeset for help on using the changeset viewer.