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_read() handles read request
0005  * for a buffer that is owned by an application.
0006  *
0007  * Test sequence:
0008  * -# Call rtems_bdbuf_read() function in thread #1 and
0009  *    provide successful read complete notification for this operation.
0010  *    As the result rtems_bdbuf_read() returns RTEMS_SUCCESSFUL
0011  *    in thread #1.
0012  * -# In thread #2 call rtems_bdbuf_read() function for the same
0013  *    block number. A buffer for this block is owned by an application
0014  *    and as the result thread #2 block on this function.
0015  * -# Call rtems_bdbuf_release() function in thread #1 in order to give
0016  *    it back under control of bdbuf library.
0017  * -# Buffer now is ready to be returned for another application and
0018  *    as the result rtems_bdbuf_read() unblocks and returns
0019  *    RTEMS_SUCCESSFUL in thread #2.
0020  * -# Call rtems_bdbuf_release() function in thread #2.
0021  *
0022  * Copyright (C) 2010 OKTET Labs, St.-Petersburg, Russia
0023  * Author: Oleg Kravtsov <Oleg.Kravtsov@oktetlabs.ru>
0024  *
0025  * Redistribution and use in source and binary forms, with or without
0026  * modification, are permitted provided that the following conditions
0027  * are met:
0028  * 1. Redistributions of source code must retain the above copyright
0029  *    notice, this list of conditions and the following disclaimer.
0030  * 2. Redistributions in binary form must reproduce the above copyright
0031  *    notice, this list of conditions and the following disclaimer in the
0032  *    documentation and/or other materials provided with the distribution.
0033  *
0034  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
0035  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
0036  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
0037  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
0038  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
0039  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
0040  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
0041  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
0042  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
0043  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
0044  * POSSIBILITY OF SUCH DAMAGE.
0045  */
0046 
0047 #ifdef HAVE_CONFIG_H
0048 #include "config.h"
0049 #endif
0050 
0051 #include "bdbuf_tests.h"
0052 
0053 static rtems_task bdbuf_test1_5_thread1(rtems_task_argument arg);
0054 static rtems_task bdbuf_test1_5_thread2(rtems_task_argument arg);
0055 
0056 #define TEST_BLK_NUM 50
0057 
0058 void
0059 bdbuf_test1_5_main()
0060 {
0061     bdbuf_test_msg msg;
0062 
0063     TEST_START("Test 1.5");
0064 
0065     START_THREAD(1, bdbuf_test1_5_thread1);
0066     START_THREAD(2, bdbuf_test1_5_thread2);
0067 
0068     /*
0069      * Step 1:
0070      * Thread #1 calls rtems_bdbuf_read() and successfully
0071      * get requested buffer.
0072      */
0073     WAIT_DRV_MSG(&msg);
0074     SEND_DRV_MSG(0, 0, RTEMS_SUCCESSFUL, 0);
0075 
0076     WAIT_THREAD_SYNC(1);
0077     TEST_CHECK_RESULT("1");
0078 
0079     /*
0080      * Step 2:
0081      * Thread #2 calls rtems_bdbuf_read() and blocks
0082      * on this call because thread #1 owns a buffer.
0083      */
0084     CONTINUE_THREAD(2);
0085 
0086     /* Make sure thread #2 managed to block on a read request. */
0087     CHECK_THREAD_BLOCKED(2);
0088 
0089     /*
0090      * Step 3:
0091      * Now thread #1 releases a buffer.
0092      */
0093     CONTINUE_THREAD(1);
0094 
0095     /*
0096      * Step 4:
0097      * Thread #2 should unblock now and get the buffer.
0098      */
0099     WAIT_THREAD_SYNC(2);
0100     TEST_CHECK_RESULT("4");
0101 
0102     /*
0103      * Step 5:
0104      * Thread #2 release buffer.
0105      */
0106     CONTINUE_THREAD(2);
0107 
0108     TEST_STOP();
0109 }
0110 
0111 static rtems_task
0112 bdbuf_test1_5_thread1(rtems_task_argument arg)
0113 {
0114     rtems_status_code   rc;
0115     rtems_bdbuf_buffer *bd = NULL;
0116 
0117     /*
0118      * Step 1:
0119      * read blk #N on thread #1
0120      */
0121     rc = rtems_bdbuf_read(test_dd, TEST_BLK_NUM, &bd);
0122     if (rc != RTEMS_SUCCESSFUL)
0123     {
0124         TEST_FAILED();
0125     }
0126     CONTINUE_MAIN(1);
0127 
0128     /*
0129      * Step 3:
0130      * Release buffer returned on step 1.
0131      */
0132     rc = rtems_bdbuf_release(bd);
0133     if (rc != RTEMS_SUCCESSFUL)
0134     {
0135         TEST_FAILED();
0136     }
0137     THREAD_END();
0138 }
0139 
0140 static rtems_task
0141 bdbuf_test1_5_thread2(rtems_task_argument arg)
0142 {
0143     rtems_status_code   rc;
0144     rtems_bdbuf_buffer *bd = NULL;
0145 
0146     WAIT_MAIN_SYNC(2);
0147 
0148     /*
0149      * Step 2:
0150      * Try to read block #N. Right now thread #1 owns
0151      * this buffer, so we will block waiting for buffer.
0152      */
0153     rc = rtems_bdbuf_read(test_dd, TEST_BLK_NUM, &bd);
0154     if (rc != RTEMS_SUCCESSFUL)
0155     {
0156         TEST_FAILED();
0157     }
0158     CONTINUE_MAIN(2);
0159 
0160     rc = rtems_bdbuf_release(bd);
0161     if (rc != RTEMS_SUCCESSFUL)
0162     {
0163         TEST_FAILED();
0164     }
0165     THREAD_END();
0166 }