Back to home page

LXR

 
 

    


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

0001 /* SPDX-License-Identifier: BSD-2-Clause */
0002 
0003 /*! @file
0004  * @brief Check how rtems_bdbuf_sync() works.
0005  *
0006  * Test sequence:
0007  * -# Call rtems_bdbuf_read(#N) in thread #1.
0008  * -# Call rtems_bdbuf_read(#N) from thread #2.
0009  * -# Thread #2 blocks on this call.
0010  * -# Call rtems_bdbuf_sync(#N) in thread #1.
0011  * -# After a while disk driver gets write request for block #N.
0012  *    Notify bdbuf library about write complete event.
0013  * -# Check that thread #1 unlocked after this.
0014  * -# Check that thread #2 unblocks and get buffer #N.
0015  * -# Release buffer in thread #2.
0016  * .
0017  *
0018  * Copyright (C) 2010 OKTET Labs, St.-Petersburg, Russia
0019  * Author: Oleg Kravtsov <Oleg.Kravtsov@oktetlabs.ru>
0020  *
0021  * Redistribution and use in source and binary forms, with or without
0022  * modification, are permitted provided that the following conditions
0023  * are met:
0024  * 1. Redistributions of source code must retain the above copyright
0025  *    notice, this list of conditions and the following disclaimer.
0026  * 2. Redistributions in binary form must reproduce the above copyright
0027  *    notice, this list of conditions and the following disclaimer in the
0028  *    documentation and/or other materials provided with the distribution.
0029  *
0030  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
0031  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
0032  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
0033  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
0034  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
0035  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
0036  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
0037  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
0038  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
0039  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
0040  * POSSIBILITY OF SUCH DAMAGE.
0041  */
0042 
0043 #ifdef HAVE_CONFIG_H
0044 #include "config.h"
0045 #endif
0046 
0047 #include "bdbuf_tests.h"
0048 
0049 static rtems_task bdbuf_test4_1_thread1(rtems_task_argument arg);
0050 static rtems_task bdbuf_test4_1_thread2(rtems_task_argument arg);
0051 
0052 /* Block number used in the test */
0053 #define TEST_BLK_NUM_N 112
0054 
0055 void
0056 bdbuf_test4_1_main()
0057 {
0058     bdbuf_test_msg msg;
0059 
0060     TEST_START("Test 4.1");
0061 
0062     START_THREAD(1, bdbuf_test4_1_thread1);
0063     START_THREAD(2, bdbuf_test4_1_thread2);
0064 
0065     /*
0066      * Step 1:
0067      * Call rtems_bdbuf_read(#N) in thread #1.
0068      * Wait for read request in disk driver.
0069      */
0070     WAIT_DRV_MSG(&msg);
0071     SEND_DRV_MSG(0, 0, RTEMS_SUCCESSFUL, 0);
0072 
0073     /* Wait for return from rtems_bdbuf_read(#N) function. */
0074     WAIT_THREAD_SYNC(1);
0075 
0076     /*
0077      * Step 2:
0078      * Call rtems_bdbuf_read(#N) in thread #2.
0079      */
0080     CONTINUE_THREAD(2);
0081 
0082     /* Make sure that thread #2 blocks */
0083     CHECK_THREAD_BLOCKED(2);
0084     TEST_CHECK_RESULT("2");
0085 
0086     /*
0087      * Step 3:
0088      * Call rtems_bdbuf_sync (#N) in thread #1.
0089      */
0090     CONTINUE_THREAD(1);
0091 
0092     /*
0093      * Setp 4:
0094      * Wait for Write request to device driver.
0095      */
0096     WAIT_DRV_MSG_WR(&msg);
0097 
0098     /* Check that both threads are blocked right now */
0099     CHECK_THREAD_BLOCKED(1);
0100     CHECK_THREAD_BLOCKED(2);
0101 
0102     /* Send Write complete notification */
0103     SEND_DRV_MSG(0, 0, RTEMS_SUCCESSFUL, 0);
0104 
0105     /*
0106      * Step 5:
0107      * Check that rtems_bdbuf_sync(#N) call is unlocked.
0108      */
0109     WAIT_THREAD_SYNC(1);
0110     TEST_CHECK_RESULT("5");
0111 
0112     /*
0113      * Step 6:
0114      * Check that rtems_bdbuf_read(#N) call in thread #2 is unlocked.
0115      */
0116     WAIT_THREAD_SYNC(2);
0117 
0118     /*
0119      * Step 7:
0120      * Release buffer in thread #2
0121      */
0122     CONTINUE_THREAD(2);
0123 
0124     /*
0125      * Exit from thread #1.
0126      */
0127     CONTINUE_THREAD(1);
0128 
0129     TEST_STOP();
0130 }
0131 
0132 static rtems_task
0133 bdbuf_test4_1_thread1(rtems_task_argument arg)
0134 {
0135     rtems_status_code   rc;
0136     rtems_bdbuf_buffer *bd = NULL;
0137 
0138     /*
0139      * Step 1:
0140      * Call rtems_bdbuf_read(#N) in thread #1;
0141      */
0142     rc = rtems_bdbuf_read(test_dd, TEST_BLK_NUM_N, &bd);
0143     if (rc != RTEMS_SUCCESSFUL)
0144     {
0145         TEST_FAILED();
0146     }
0147 
0148     CONTINUE_MAIN(1);
0149 
0150     /*
0151      * Step 3:
0152      * Call rtems_bdbuf_sync(#N)
0153      */
0154     rc = rtems_bdbuf_sync(bd);
0155     if (rc != RTEMS_SUCCESSFUL)
0156     {
0157         TEST_FAILED();
0158     }
0159 
0160     CONTINUE_MAIN(1);
0161 
0162     THREAD_END();
0163 }
0164 
0165 static rtems_task
0166 bdbuf_test4_1_thread2(rtems_task_argument arg)
0167 {
0168     rtems_status_code   rc;
0169     rtems_bdbuf_buffer *bd = NULL;
0170 
0171     WAIT_MAIN_SYNC(2);
0172 
0173     /*
0174      * Step 2:
0175      * In thread #2 call rtems_bdbuf_read(#N).
0176      * We will block on this call.
0177      */
0178     rc = rtems_bdbuf_read(test_dd, TEST_BLK_NUM_N, &bd);
0179     if (rc != RTEMS_SUCCESSFUL)
0180     {
0181         TEST_FAILED();
0182     }
0183 
0184     CONTINUE_MAIN(2);
0185 
0186     /*
0187      * Release buffer.
0188      */
0189     rc = rtems_bdbuf_release(bd);
0190     if (rc != RTEMS_SUCCESSFUL)
0191     {
0192         TEST_FAILED();
0193     }
0194     THREAD_END();
0195 }