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 3";
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 512
0064 
0065 #define BLOCK_COUNT 2
0066 
0067 static rtems_disk_device *dd;
0068 
0069 static volatile bool sync_done = false;
0070 
0071 static rtems_id task_id_low;
0072 
0073 static rtems_id task_id_high;
0074 
0075 static void task_low(rtems_task_argument arg)
0076 {
0077   rtems_status_code sc = RTEMS_SUCCESSFUL;
0078   rtems_bdbuf_buffer *bd = NULL;
0079 
0080   rtems_test_assert(!sync_done);
0081 
0082   printk("L: try access: 0\n");
0083 
0084   sc = rtems_bdbuf_get(dd, 0, &bd);
0085   ASSERT_SC(sc);
0086 
0087   rtems_test_assert(sync_done);
0088 
0089   printk("L: access: 0\n");
0090 
0091   rtems_test_assert(bd->block == 0);
0092 
0093   TEST_END();
0094 
0095   exit(0);
0096 }
0097 
0098 static void task_high(rtems_task_argument arg)
0099 {
0100   rtems_status_code sc = RTEMS_SUCCESSFUL;
0101   rtems_bdbuf_buffer *bd = NULL;
0102 
0103   rtems_test_assert(!sync_done);
0104 
0105   printk("H: try access: 0\n");
0106 
0107   sc = rtems_bdbuf_get(dd, 0, &bd);
0108   ASSERT_SC(sc);
0109 
0110   rtems_test_assert(sync_done);
0111 
0112   printk("H: access: 0\n");
0113 
0114   printk("H: release: 0\n");
0115 
0116   sc = rtems_bdbuf_release(bd);
0117   ASSERT_SC(sc);
0118 
0119   printk("H: release done: 0\n");
0120 
0121   printk("H: try access: 1\n");
0122 
0123   sc = rtems_bdbuf_get(dd, 1, &bd);
0124   ASSERT_SC(sc);
0125 
0126   printk("H: access: 1\n");
0127 
0128   /* If we reach this code, we have likely a bug */
0129 
0130   printk("H: release: 1\n");
0131 
0132   sc = rtems_bdbuf_release(bd);
0133   ASSERT_SC(sc);
0134 
0135   printk("H: release done: 1\n");
0136 
0137   rtems_task_exit();
0138 }
0139 
0140 static void do_ramdisk_register(
0141   uint32_t media_block_size,
0142   rtems_blkdev_bnum media_block_count,
0143   const char *disk,
0144   rtems_disk_device **dd
0145 )
0146 {
0147   rtems_status_code sc;
0148   int fd;
0149   int rv;
0150 
0151   sc = ramdisk_register(media_block_size, media_block_count, false, disk);
0152   ASSERT_SC(sc);
0153 
0154   fd = open(disk, O_RDWR);
0155   rtems_test_assert(fd >= 0);
0156 
0157   rv = rtems_disk_fd_get_disk_device(fd, dd);
0158   rtems_test_assert(rv == 0);
0159 
0160   rv = close(fd);
0161   rtems_test_assert(rv == 0);
0162 }
0163 
0164 static rtems_task Init(rtems_task_argument argument)
0165 {
0166   rtems_status_code sc = RTEMS_SUCCESSFUL;
0167   rtems_bdbuf_buffer *bd = NULL;
0168 
0169   TEST_BEGIN();
0170 
0171   do_ramdisk_register(BLOCK_SIZE, BLOCK_COUNT, "/dev/rda", &dd);
0172 
0173   sc = rtems_task_create(
0174     rtems_build_name(' ', 'L', 'O', 'W'),
0175     PRIORITY_LOW,
0176     0,
0177     RTEMS_DEFAULT_MODES,
0178     RTEMS_DEFAULT_ATTRIBUTES,
0179     &task_id_low
0180   );
0181   ASSERT_SC(sc);
0182 
0183   sc = rtems_task_start(task_id_low, task_low, 0);
0184   ASSERT_SC(sc);
0185 
0186   sc = rtems_task_create(
0187     rtems_build_name('H', 'I', 'G', 'H'),
0188     PRIORITY_HIGH,
0189     0,
0190     RTEMS_DEFAULT_MODES,
0191     RTEMS_DEFAULT_ATTRIBUTES,
0192     &task_id_high
0193   );
0194   ASSERT_SC(sc);
0195 
0196   sc = rtems_task_start(task_id_high, task_high, 0);
0197   ASSERT_SC(sc);
0198 
0199   sc = rtems_bdbuf_get(dd, 0, &bd);
0200   ASSERT_SC(sc);
0201 
0202   sc = rtems_bdbuf_sync(bd);
0203   ASSERT_SC(sc);
0204 
0205   printk("I: sync done: 0\n");
0206 
0207   sync_done = true;
0208 
0209   sc = rtems_task_suspend(RTEMS_SELF);
0210   ASSERT_SC(sc);
0211 }
0212 
0213 #define CONFIGURE_INIT
0214 
0215 #define CONFIGURE_APPLICATION_DOES_NOT_NEED_CLOCK_DRIVER
0216 #define CONFIGURE_APPLICATION_NEEDS_SIMPLE_CONSOLE_DRIVER
0217 #define CONFIGURE_APPLICATION_NEEDS_LIBBLOCK
0218 
0219 #define CONFIGURE_MAXIMUM_FILE_DESCRIPTORS 4
0220 
0221 #define CONFIGURE_MAXIMUM_TASKS 3
0222 
0223 #define CONFIGURE_INITIAL_EXTENSIONS RTEMS_TEST_INITIAL_EXTENSION
0224 
0225 #define CONFIGURE_RTEMS_INIT_TASKS_TABLE
0226 
0227 #define CONFIGURE_INIT_TASK_PRIORITY PRIORITY_INIT
0228 #define CONFIGURE_INIT_TASK_ATTRIBUTES RTEMS_DEFAULT_ATTRIBUTES
0229 #define CONFIGURE_INIT_TASK_INITIAL_MODES RTEMS_DEFAULT_MODES
0230 
0231 #define CONFIGURE_SWAPOUT_TASK_PRIORITY PRIORITY_SWAPOUT
0232 
0233 #define CONFIGURE_BDBUF_BUFFER_MIN_SIZE BLOCK_SIZE
0234 #define CONFIGURE_BDBUF_BUFFER_MAX_SIZE BLOCK_SIZE
0235 #define CONFIGURE_BDBUF_CACHE_MEMORY_SIZE BLOCK_SIZE
0236 
0237 #include <rtems/confdefs.h>