Back to home page

LXR

 
 

    


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

0001 /* SPDX-License-Identifier: BSD-2-Clause */
0002 
0003 /**
0004  * @file
0005  *
0006  * @ingroup test_bdbuf
0007  *
0008  * @brief Bdbuf test.
0009  */
0010 
0011 /*
0012  * Copyright (C) 2009, 2018 embedded brains GmbH & Co. KG
0013  *
0014  * Redistribution and use in source and binary forms, with or without
0015  * modification, are permitted provided that the following conditions
0016  * are met:
0017  * 1. Redistributions of source code must retain the above copyright
0018  *    notice, this list of conditions and the following disclaimer.
0019  * 2. Redistributions in binary form must reproduce the above copyright
0020  *    notice, this list of conditions and the following disclaimer in the
0021  *    documentation and/or other materials provided with the distribution.
0022  *
0023  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
0024  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
0025  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
0026  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
0027  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
0028  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
0029  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
0030  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
0031  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
0032  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
0033  * POSSIBILITY OF SUCH DAMAGE.
0034  */
0035 
0036 #ifdef HAVE_CONFIG_H
0037 #include "config.h"
0038 #endif
0039 
0040 #include "tmacros.h"
0041 
0042 #include <rtems.h>
0043 #include <rtems/bspIo.h>
0044 #include <rtems/ramdisk.h>
0045 #include <rtems/bdbuf.h>
0046 
0047 #include <sys/stat.h>
0048 #include <fcntl.h>
0049 #include <unistd.h>
0050 
0051 const char rtems_test_name[] = "BLOCK 2";
0052 
0053 #define ASSERT_SC(sc) rtems_test_assert((sc) == RTEMS_SUCCESSFUL)
0054 
0055 #define PRIORITY_INIT 10
0056 
0057 #define PRIORITY_SWAPOUT 50
0058 
0059 #define PRIORITY_HIGH 30
0060 
0061 #define PRIORITY_LOW 40
0062 
0063 #define BLOCK_SIZE_A 512
0064 
0065 #define BLOCK_COUNT_A 2
0066 
0067 #define BLOCK_SIZE_B 1024
0068 
0069 #define BLOCK_COUNT_B 1
0070 
0071 static rtems_disk_device *dd_a;
0072 
0073 static rtems_disk_device *dd_b;
0074 
0075 static volatile bool sync_done = false;
0076 
0077 static rtems_id task_id_low;
0078 
0079 static rtems_id task_id_high;
0080 
0081 static void task_low(rtems_task_argument arg)
0082 {
0083   rtems_status_code sc = RTEMS_SUCCESSFUL;
0084   rtems_bdbuf_buffer *bd = NULL;
0085 
0086   rtems_test_assert(!sync_done);
0087 
0088   printk("L: try access: A0\n");
0089 
0090   sc = rtems_bdbuf_get(dd_a, 0, &bd);
0091   ASSERT_SC(sc);
0092 
0093   rtems_test_assert(sync_done);
0094 
0095   printk("L: access: A0\n");
0096 
0097   rtems_test_assert(bd->dd == dd_a);
0098 
0099   TEST_END();
0100 
0101   exit(0);
0102 }
0103 
0104 static void task_high(rtems_task_argument arg)
0105 {
0106   rtems_status_code sc = RTEMS_SUCCESSFUL;
0107   rtems_bdbuf_buffer *bd = NULL;
0108 
0109   rtems_test_assert(!sync_done);
0110 
0111   printk("H: try access: A0\n");
0112 
0113   sc = rtems_bdbuf_get(dd_a, 0, &bd);
0114   ASSERT_SC(sc);
0115 
0116   rtems_test_assert(sync_done);
0117 
0118   printk("H: access: A0\n");
0119 
0120   printk("H: release: A0\n");
0121 
0122   sc = rtems_bdbuf_release(bd);
0123   ASSERT_SC(sc);
0124 
0125   printk("H: release done: A0\n");
0126 
0127   printk("H: try access: B0\n");
0128 
0129   sc = rtems_bdbuf_get(dd_b, 0, &bd);
0130   ASSERT_SC(sc);
0131 
0132   printk("H: access: B0\n");
0133 
0134   /* If we reach this code, we have likely a bug */
0135 
0136   printk("H: release: B0\n");
0137 
0138   sc = rtems_bdbuf_release(bd);
0139   ASSERT_SC(sc);
0140 
0141   printk("H: release done: B0\n");
0142 
0143   rtems_task_exit();
0144 }
0145 
0146 static void do_ramdisk_register(
0147   uint32_t media_block_size,
0148   rtems_blkdev_bnum media_block_count,
0149   const char *disk,
0150   rtems_disk_device **dd
0151 )
0152 {
0153   rtems_status_code sc;
0154   int fd;
0155   int rv;
0156 
0157   sc = ramdisk_register(media_block_size, media_block_count, false, disk);
0158   ASSERT_SC(sc);
0159 
0160   fd = open(disk, O_RDWR);
0161   rtems_test_assert(fd >= 0);
0162 
0163   rv = rtems_disk_fd_get_disk_device(fd, dd);
0164   rtems_test_assert(rv == 0);
0165 
0166   rv = close(fd);
0167   rtems_test_assert(rv == 0);
0168 }
0169 
0170 static rtems_task Init(rtems_task_argument argument)
0171 {
0172   rtems_status_code sc = RTEMS_SUCCESSFUL;
0173   rtems_bdbuf_buffer *bd = NULL;
0174 
0175   TEST_BEGIN();
0176 
0177   do_ramdisk_register(BLOCK_SIZE_A, BLOCK_COUNT_A, "/dev/rda", &dd_a);
0178   do_ramdisk_register(BLOCK_SIZE_B, BLOCK_COUNT_B, "/dev/rdb", &dd_b);
0179 
0180   sc = rtems_task_create(
0181     rtems_build_name(' ', 'L', 'O', 'W'),
0182     PRIORITY_LOW,
0183     0,
0184     RTEMS_DEFAULT_MODES,
0185     RTEMS_DEFAULT_ATTRIBUTES,
0186     &task_id_low
0187   );
0188   ASSERT_SC(sc);
0189 
0190   sc = rtems_task_start(task_id_low, task_low, 0);
0191   ASSERT_SC(sc);
0192 
0193   sc = rtems_task_create(
0194     rtems_build_name('H', 'I', 'G', 'H'),
0195     PRIORITY_HIGH,
0196     0,
0197     RTEMS_DEFAULT_MODES,
0198     RTEMS_DEFAULT_ATTRIBUTES,
0199     &task_id_high
0200   );
0201   ASSERT_SC(sc);
0202 
0203   sc = rtems_task_start(task_id_high, task_high, 0);
0204   ASSERT_SC(sc);
0205 
0206   sc = rtems_bdbuf_get(dd_a, 0, &bd);
0207   ASSERT_SC(sc);
0208 
0209   sc = rtems_bdbuf_sync(bd);
0210   ASSERT_SC(sc);
0211 
0212   printk("I: sync done: A0\n");
0213 
0214   sync_done = true;
0215 
0216   sc = rtems_task_suspend(RTEMS_SELF);
0217   ASSERT_SC(sc);
0218 }
0219 
0220 #define CONFIGURE_INIT
0221 
0222 #define CONFIGURE_APPLICATION_DOES_NOT_NEED_CLOCK_DRIVER
0223 #define CONFIGURE_APPLICATION_NEEDS_SIMPLE_CONSOLE_DRIVER
0224 #define CONFIGURE_APPLICATION_NEEDS_LIBBLOCK
0225 
0226 #define CONFIGURE_MAXIMUM_FILE_DESCRIPTORS 4
0227 
0228 #define CONFIGURE_MAXIMUM_TASKS 3
0229 
0230 #define CONFIGURE_INITIAL_EXTENSIONS RTEMS_TEST_INITIAL_EXTENSION
0231 
0232 #define CONFIGURE_RTEMS_INIT_TASKS_TABLE
0233 
0234 #define CONFIGURE_INIT_TASK_PRIORITY PRIORITY_INIT
0235 #define CONFIGURE_INIT_TASK_ATTRIBUTES RTEMS_DEFAULT_ATTRIBUTES
0236 #define CONFIGURE_INIT_TASK_INITIAL_MODES RTEMS_DEFAULT_MODES
0237 
0238 #define CONFIGURE_SWAPOUT_TASK_PRIORITY PRIORITY_SWAPOUT
0239 
0240 #define CONFIGURE_BDBUF_BUFFER_MIN_SIZE BLOCK_SIZE_A
0241 #define CONFIGURE_BDBUF_BUFFER_MAX_SIZE BLOCK_SIZE_B
0242 #define CONFIGURE_BDBUF_CACHE_MEMORY_SIZE BLOCK_SIZE_B
0243 
0244 #include <rtems/confdefs.h>