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 read/release work in case of only one buffer in ready list.
0005  *
0006  * Test sequence:
0007  * -# In thread #1 call rtems_bdbuf_get(#N) to get an empty block #N.
0008  * -# In thread #1 call rtems_bdbuf_release_modified() for previously
0009  *    got buffer.
0010  * -# In thread #1 call rtems_bdbuf_read(#N) to get the same buffer
0011  *    (after this call a buffer is in AVL tree with ACCESS_MODIFIED state).
0012  * -# In thread #2 call rtems_bdbuf_read/get(#M).
0013  * -# In thread #1 call rtems_bdbuf_release(#N).
0014  * -# Check that in thread #2 a buffer is obtained.
0015  *
0016  * Copyright (C) 2010 OKTET Labs, St.-Petersburg, Russia
0017  * Author: Oleg Kravtsov <Oleg.Kravtsov@oktetlabs.ru>
0018  *
0019  * Redistribution and use in source and binary forms, with or without
0020  * modification, are permitted provided that the following conditions
0021  * are met:
0022  * 1. Redistributions of source code must retain the above copyright
0023  *    notice, this list of conditions and the following disclaimer.
0024  * 2. Redistributions in binary form must reproduce the above copyright
0025  *    notice, this list of conditions and the following disclaimer in the
0026  *    documentation and/or other materials provided with the distribution.
0027  *
0028  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
0029  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
0030  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
0031  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
0032  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
0033  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
0034  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
0035  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
0036  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
0037  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
0038  * POSSIBILITY OF SUCH DAMAGE.
0039  */
0040 
0041 #ifdef HAVE_CONFIG_H
0042 #include "config.h"
0043 #endif
0044 
0045 #include "bdbuf_tests.h"
0046 
0047 static rtems_task bdbuf_test3_1_thread1(rtems_task_argument arg);
0048 static rtems_task bdbuf_test3_1_thread2(rtems_task_argument arg);
0049 
0050 /* Block number used in the test */
0051 #define TEST_BLK_NUM_N 80
0052 #define TEST_BLK_NUM_M 85
0053 
0054 void
0055 bdbuf_test3_1_main()
0056 {
0057     bdbuf_test_msg msg;
0058 
0059     TEST_START("Test 3.1");
0060 
0061     START_THREAD(1, bdbuf_test3_1_thread1);
0062     START_THREAD(2, bdbuf_test3_1_thread2);
0063 
0064     /*
0065      * Step 1-3:
0066      * Wait for sync from thread #1.
0067      * Thread #1 switch a buffer for #N into ACCESS_MODIFIED state.
0068      */
0069     WAIT_THREAD_SYNC(1);
0070     TEST_CHECK_RESULT("3");
0071 
0072     /*
0073      * Step 4:
0074      * Thread #2 calls get() function and blocks
0075      * (there no buffers available in bdbuf library).
0076      */
0077     CONTINUE_THREAD(2);
0078 
0079     /* Make sure thread #2 managed to block on the buffer. */
0080     CHECK_THREAD_BLOCKED(2);
0081 
0082     /*
0083      * Step 6:
0084      * Thread #1 release buffer.
0085      */
0086     CONTINUE_THREAD(1);
0087 
0088     /*
0089      * Buffer released in thread #1 was in MODIFIED state, so
0090      * there will be a request for disk write
0091      */
0092     WAIT_DRV_MSG_WR(&msg);
0093     SEND_DRV_MSG(0, 0, RTEMS_SUCCESSFUL, 0);
0094 
0095     /*
0096      * Check that thread #2 unblocked after this.
0097      */
0098     WAIT_THREAD_SYNC(2);
0099     TEST_CHECK_RESULT("5");
0100 
0101     /*
0102      * Release buffer in thread #2
0103      */
0104     CONTINUE_THREAD(2);
0105 
0106     /*
0107      * Buffer released in thread #2 switched to MODIFIED state, so
0108      * there will be a request for disk write
0109      */
0110     WAIT_DRV_MSG_WR(&msg);
0111     SEND_DRV_MSG(0, 0, RTEMS_SUCCESSFUL, 0);
0112 
0113     TEST_STOP();
0114 }
0115 
0116 static rtems_task
0117 bdbuf_test3_1_thread1(rtems_task_argument arg)
0118 {
0119     rtems_status_code   rc;
0120     rtems_bdbuf_buffer *bd = NULL;
0121 
0122     /*
0123      * Step 1:
0124      * Call rtems_bdbuf_get(#N) to get an empty block db;
0125      * [this call will remove a buffer from ready list and insert
0126      * it in AVL tree with ACCESS state.
0127      * Step 2:
0128      * Call release_modified(bd);
0129      * [Now we have one entry in modified list and it is still
0130      * in AVL tree with MODIFIED state]
0131      * Step 3:
0132      * Call read(#N) to get the same buffer.
0133      * [An entry is found in AVL tree, removed from modified list and
0134      * returned with state ACCESS_MODIFIED]
0135      */
0136     rc = rtems_bdbuf_get(test_dd, TEST_BLK_NUM_N, &bd);
0137     if (rc != RTEMS_SUCCESSFUL)
0138     {
0139         TEST_FAILED();
0140     }
0141 
0142     rc = rtems_bdbuf_release_modified(bd);
0143     if (rc != RTEMS_SUCCESSFUL)
0144     {
0145         TEST_FAILED();
0146     }
0147 
0148     rc = rtems_bdbuf_read(test_dd, TEST_BLK_NUM_N, &bd);
0149     if (rc != RTEMS_SUCCESSFUL)
0150     {
0151         TEST_FAILED();
0152     }
0153 
0154     CONTINUE_MAIN(1);
0155 
0156     /* Step 5:
0157      * Call rtems_bdbuf_release(#N).
0158      * As the result buffer will be used by bdbuf to get
0159      * buffer #M for thread #2.
0160      */
0161     rc = rtems_bdbuf_release(bd);
0162     if (rc != RTEMS_SUCCESSFUL)
0163     {
0164         TEST_FAILED();
0165     }
0166 
0167     THREAD_END();
0168 }
0169 
0170 static rtems_task
0171 bdbuf_test3_1_thread2(rtems_task_argument arg)
0172 {
0173     rtems_status_code   rc;
0174     rtems_bdbuf_buffer *bd = NULL;
0175 
0176     WAIT_MAIN_SYNC(2);
0177 
0178     /*
0179      * Step 4:
0180      * In thread #2 call read/get(#M)
0181      * [We ask for block number #M - there is no such entry in AVL,
0182      * and all the lists are empty (ready, lru, modified), as a result
0183      * this thread blocks on
0184      * rtems_bdbuf_wait(pool, &pool->waiting, &pool->wait_waiters)]
0185      */
0186     rc = rtems_bdbuf_get(test_dd, TEST_BLK_NUM_M, &bd);
0187     if (rc != RTEMS_SUCCESSFUL)
0188     {
0189         TEST_FAILED();
0190     }
0191 
0192     CONTINUE_MAIN(2);
0193 
0194     /*
0195      * Release buffer.
0196      */
0197     rc = rtems_bdbuf_release_modified(bd);
0198     if (rc != RTEMS_SUCCESSFUL)
0199     {
0200         TEST_FAILED();
0201     }
0202     THREAD_END();
0203 }