Back to home page

LXR

 
 

    


File indexing completed on 2025-05-11 08:24:49

0001 /* SPDX-License-Identifier: BSD-2-Clause */
0002 
0003 /*
0004  *  COPYRIGHT (c) 1989-2011.
0005  *  On-Line Applications Research Corporation (OAR).
0006  *
0007  * Redistribution and use in source and binary forms, with or without
0008  * modification, are permitted provided that the following conditions
0009  * are met:
0010  * 1. Redistributions of source code must retain the above copyright
0011  *    notice, this list of conditions and the following disclaimer.
0012  * 2. Redistributions in binary form must reproduce the above copyright
0013  *    notice, this list of conditions and the following disclaimer in the
0014  *    documentation and/or other materials provided with the distribution.
0015  *
0016  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
0017  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
0018  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
0019  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
0020  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
0021  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
0022  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
0023  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
0024  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
0025  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
0026  * POSSIBILITY OF SUCH DAMAGE.
0027  */
0028 
0029 #ifdef HAVE_CONFIG_H
0030 #include "config.h"
0031 #endif
0032 
0033 #include "test_support.h"
0034 #include "tmacros.h"
0035 
0036 #include <unistd.h>
0037 #include <rtems/bspIo.h>
0038 #include <rtems/counter.h>
0039 
0040 static rtems_id locked_print_semaphore;      /* synchronisation semaphore */
0041 
0042 static int locked_printf_plugin(void *context, const char *fmt, va_list ap)
0043 {
0044   (void) context;
0045   return locked_vprintf(fmt, ap);
0046 }
0047 
0048 void locked_print_initialize(void)
0049 {
0050   rtems_status_code sc;
0051   static            bool initted = false;
0052 
0053   if ( initted )
0054     return;
0055 
0056   initted = true;
0057 
0058   /* Create/verify synchronisation semaphore */
0059   sc = rtems_semaphore_create(
0060     rtems_build_name ('S', 'E', 'M', '1'),
0061     1,
0062     RTEMS_LOCAL                   |
0063     RTEMS_BINARY_SEMAPHORE |
0064     RTEMS_PRIORITY_CEILING |
0065     RTEMS_PRIORITY,
0066     1,
0067     &locked_print_semaphore
0068   );
0069   directive_failed( sc, "rtems_semaphore_create" );
0070 
0071   /*
0072    * Set up the printer to use the locked printf printer.
0073    */
0074   rtems_test_printer.context = NULL;
0075   rtems_test_printer.printer = locked_printf_plugin;
0076 }
0077 
0078 int locked_vprintf(const char *fmt, va_list ap)
0079 {
0080   int rv;
0081   rtems_status_code sc;
0082 
0083   locked_print_initialize();
0084 
0085   /* Lock semaphore without releasing the cpu */
0086   sc = rtems_semaphore_obtain( locked_print_semaphore, RTEMS_NO_WAIT, 0 );
0087 
0088   if ( sc != RTEMS_SUCCESSFUL ) {
0089     uint8_t e;
0090     rtems_counter_ticks w;
0091 
0092     /* Use exponential backoff to avoid a livelock */
0093 
0094     getentropy( &e, sizeof( e ) );
0095     w = e + 1;
0096 
0097     do {
0098       rtems_counter_delay_ticks( w );
0099       w *= 2;
0100       sc = rtems_semaphore_obtain( locked_print_semaphore, RTEMS_NO_WAIT, 0 );
0101     } while (sc != RTEMS_SUCCESSFUL );
0102   }
0103 
0104   rv = vprintk(fmt, ap);
0105 
0106   /* Release the semaphore  */
0107   rtems_semaphore_release( locked_print_semaphore );
0108 
0109   return rv;
0110 }
0111 
0112 int locked_printf(const char *fmt, ...)
0113 {
0114   int               rv;
0115   va_list           ap;       /* points to each unnamed argument in turn */
0116 
0117   va_start(ap, fmt); /* make ap point to 1st unnamed arg */
0118   rv = locked_vprintf(fmt, ap);
0119   va_end(ap);        /* clean up when done */
0120 
0121   return rv;
0122 }