File indexing completed on 2025-05-11 08:24:49
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023
0024
0025
0026
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;
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
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
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
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
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
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;
0116
0117 va_start(ap, fmt);
0118 rv = locked_vprintf(fmt, ap);
0119 va_end(ap);
0120
0121 return rv;
0122 }