112771Sqtt2@cornell.edu/* 212771Sqtt2@cornell.edu * Copyright (c) 2018, Cornell University 312771Sqtt2@cornell.edu * All rights reserved. 412771Sqtt2@cornell.edu * 512771Sqtt2@cornell.edu * Redistribution and use in source and binary forms, with or 612771Sqtt2@cornell.edu * without modification, are permitted provided that the following 712771Sqtt2@cornell.edu * conditions are met: 812771Sqtt2@cornell.edu * 912771Sqtt2@cornell.edu * Redistributions of source code must retain the above copyright 1012771Sqtt2@cornell.edu * notice, this list of conditions and the following disclaimer. 1112771Sqtt2@cornell.edu * 1212771Sqtt2@cornell.edu * Redistributions in binary form must reproduce the above 1312771Sqtt2@cornell.edu * copyright notice, this list of conditions and the following 1412771Sqtt2@cornell.edu * disclaimer in the documentation and/or other materials provided 1512771Sqtt2@cornell.edu * with the distribution. 1612771Sqtt2@cornell.edu * 1712771Sqtt2@cornell.edu * Neither the name of Cornell University nor the names of its 1812771Sqtt2@cornell.edu * contributors may be used to endorse or promote products derived 1912771Sqtt2@cornell.edu * from this software without specific prior written permission. 2012771Sqtt2@cornell.edu * 2112771Sqtt2@cornell.edu * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND 2212771Sqtt2@cornell.edu * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, 2312771Sqtt2@cornell.edu * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 2412771Sqtt2@cornell.edu * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 2512771Sqtt2@cornell.edu * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR 2612771Sqtt2@cornell.edu * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 2712771Sqtt2@cornell.edu * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 2812771Sqtt2@cornell.edu * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF 2912771Sqtt2@cornell.edu * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 3012771Sqtt2@cornell.edu * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 3112771Sqtt2@cornell.edu * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN 3212771Sqtt2@cornell.edu * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 3312771Sqtt2@cornell.edu * POSSIBILITY OF SUCH DAMAGE. 3412771Sqtt2@cornell.edu * 3512771Sqtt2@cornell.edu * Authors: Tuan Ta, Moyang Wang 3612771Sqtt2@cornell.edu */ 3712771Sqtt2@cornell.edu 3812771Sqtt2@cornell.edu//------------------------------------------------------------------------ 3912771Sqtt2@cornell.edu// sysfutex3_d tests FUTEX_CMP_REQUEUE functionalities of futex system 4012771Sqtt2@cornell.edu// call: 4112771Sqtt2@cornell.edu// - make worker threads waiting on futex 1 4212771Sqtt2@cornell.edu// - wake up 1 thread, requeue the rest of the threads to futex 2 4312771Sqtt2@cornell.edu// - wake all threads waiting on futex 1 and futex 2 4412771Sqtt2@cornell.edu//------------------------------------------------------------------------ 4512771Sqtt2@cornell.edu 4612771Sqtt2@cornell.edu#include "riscv_test.h" 4712771Sqtt2@cornell.edu#include "test_macros.h" 4812771Sqtt2@cornell.edu#include "test_macros_mt_ecall.h" 4912771Sqtt2@cornell.edu 5012771Sqtt2@cornell.edu RVTEST_RV64U 5112771Sqtt2@cornell.edu RVTEST_CODE_BEGIN 5212771Sqtt2@cornell.edu 5312771Sqtt2@cornell.edu#define NUM_THREADS 20 5412771Sqtt2@cornell.edu 5512771Sqtt2@cornell.edu//------------------------------------------------------------------------ 5612771Sqtt2@cornell.edu// Master thread creates new threads, call _master function, waits for all 5712771Sqtt2@cornell.edu// threads to complete, deallocates threads and checks result 5812771Sqtt2@cornell.edu//------------------------------------------------------------------------ 5912771Sqtt2@cornell.edu li a0, NUM_THREADS 6012771Sqtt2@cornell.edu call _create_threads 6112771Sqtt2@cornell.edu 6212771Sqtt2@cornell.edu la t6, n_worker_threads 6312771Sqtt2@cornell.edu ld a0, (t6) 6412771Sqtt2@cornell.edu beqz a0, _fail // exit if there's no worker thread 6512771Sqtt2@cornell.edu 6612771Sqtt2@cornell.edu call _master_work 6712771Sqtt2@cornell.edu 6812771Sqtt2@cornell.edu la t6, n_worker_threads 6912771Sqtt2@cornell.edu ld a0, (t6) 7012771Sqtt2@cornell.edu call _join 7112771Sqtt2@cornell.edu 7212771Sqtt2@cornell.edu la t6, n_worker_threads 7312771Sqtt2@cornell.edu ld a0, (t6) 7412771Sqtt2@cornell.edu call _check 7512771Sqtt2@cornell.edu 7612771Sqtt2@cornell.edu la t6, n_worker_threads 7712771Sqtt2@cornell.edu ld a0, (t6) 7812771Sqtt2@cornell.edu call _delete_threads 7912771Sqtt2@cornell.edu 8012771Sqtt2@cornell.edu li a0, SUCCESS 8112771Sqtt2@cornell.edu 8212771Sqtt2@cornell.edu RVTEST_CODE_END 8312771Sqtt2@cornell.edu 8412771Sqtt2@cornell.edu//------------------------------------------------------------------------ 8512771Sqtt2@cornell.edu// master_work function executed by the parent/master thread 8612771Sqtt2@cornell.edu//------------------------------------------------------------------------ 8712771Sqtt2@cornell.edu_master_work: 8812771Sqtt2@cornell.edu mv s0, ra // save return address 8912771Sqtt2@cornell.edu li t0, 0 // number of threads that have been waken 9012771Sqtt2@cornell.edu la t1, n_worker_threads 9112771Sqtt2@cornell.edu ld t1, (t1) 9212771Sqtt2@cornell.edu 9312771Sqtt2@cornell.edu1: 9412771Sqtt2@cornell.edu // futex(futex_X, FUTEX_CMP_REQUEUE, 1, INT_MAX, futex_Y, *futex_X) 9512771Sqtt2@cornell.edu la a0, futex_X 9612771Sqtt2@cornell.edu li a1, FUTEX_CMP_REQUEUE 9712771Sqtt2@cornell.edu li a2, 1 // wake up at most 1 thread 9812771Sqtt2@cornell.edu li a3, 1000 // practically is INT_MAX 9912771Sqtt2@cornell.edu la a4, futex_Y // move all other waiter to futex_Y 10012771Sqtt2@cornell.edu li a5, 0 10112771Sqtt2@cornell.edu li a7, SYSCALL_FUTEX 10212771Sqtt2@cornell.edu ecall 10312771Sqtt2@cornell.edu 10412771Sqtt2@cornell.edu // loop until wake up one thread, all other waiter are requeued to 10512771Sqtt2@cornell.edu // futex_Y 10612771Sqtt2@cornell.edu beqz a0, 1b 10712771Sqtt2@cornell.edu 10812771Sqtt2@cornell.edu addi t0, t0, 1 10912771Sqtt2@cornell.edu 11012771Sqtt2@cornell.edu2: 11112771Sqtt2@cornell.edu // alternating between futex_X and futex_Y 11212771Sqtt2@cornell.edu // because there could be new threads added to futex_X's queue 11312771Sqtt2@cornell.edu // after our futex_requeue 11412771Sqtt2@cornell.edu 11512771Sqtt2@cornell.edu // futex(futex_Y, FUTEX_WAKE_PRIVATE, 0) 11612771Sqtt2@cornell.edu la a0, futex_Y 11712771Sqtt2@cornell.edu li a1, FUTEX_WAKE 11812771Sqtt2@cornell.edu li a2, 1 // wake up at most 1 thread 11912771Sqtt2@cornell.edu li a7, SYSCALL_FUTEX 12012771Sqtt2@cornell.edu ecall 12112771Sqtt2@cornell.edu 12212771Sqtt2@cornell.edu add t0, t0, a0 // track the number of waken threads so far 12312771Sqtt2@cornell.edu 12412771Sqtt2@cornell.edu // futex(futex_X, FUTEX_WAKE_PRIVATE, 0) 12512771Sqtt2@cornell.edu la a0, futex_X 12612771Sqtt2@cornell.edu li a1, FUTEX_WAKE 12712771Sqtt2@cornell.edu li a2, 1 // wake up at most 1 thread 12812771Sqtt2@cornell.edu li a7, SYSCALL_FUTEX 12912771Sqtt2@cornell.edu ecall 13012771Sqtt2@cornell.edu 13112771Sqtt2@cornell.edu add t0, t0, a0 // track the number of waken threads so far 13212771Sqtt2@cornell.edu 13312771Sqtt2@cornell.edu // keep waking up until all threads are waken up 13412771Sqtt2@cornell.edu blt t0, t1, 2b 13512771Sqtt2@cornell.edu 13612771Sqtt2@cornell.edu // restore return address and return 13712771Sqtt2@cornell.edu mv ra, s0 13812771Sqtt2@cornell.edu ret 13912771Sqtt2@cornell.edu 14012771Sqtt2@cornell.edu//------------------------------------------------------------------------ 14112771Sqtt2@cornell.edu// mt_test function executed by child threads 14212771Sqtt2@cornell.edu// 14312771Sqtt2@cornell.edu// Wait on futex_X 14412771Sqtt2@cornell.edu//------------------------------------------------------------------------ 14512771Sqtt2@cornell.edu_mt_test: 14612771Sqtt2@cornell.edu // futex(futex_X, FUTEX_WAIT_PRIVATE, 1) 14712771Sqtt2@cornell.edu la a0, futex_X 14812771Sqtt2@cornell.edu li a1, FUTEX_WAIT 14912771Sqtt2@cornell.edu li a2, 0 // expected val of futex_X 15012771Sqtt2@cornell.edu li a7, SYSCALL_FUTEX 15112771Sqtt2@cornell.edu ecall 15212771Sqtt2@cornell.edu 15312771Sqtt2@cornell.edu RVTEST_CODE_END 15412771Sqtt2@cornell.edu 15512771Sqtt2@cornell.edu//------------------------------------------------------------------------ 15612771Sqtt2@cornell.edu// _check: 15712771Sqtt2@cornell.edu// counter should equals to number of child threads 15812771Sqtt2@cornell.edu//------------------------------------------------------------------------ 15912771Sqtt2@cornell.edu 16012771Sqtt2@cornell.edu_check: 16112771Sqtt2@cornell.edu ret 16212771Sqtt2@cornell.edu 16312771Sqtt2@cornell.edu_fail: 16412771Sqtt2@cornell.edu li a0, FAILURE 16512771Sqtt2@cornell.edu RVTEST_CODE_END 16612771Sqtt2@cornell.edu 16712771Sqtt2@cornell.edu .data 16812771Sqtt2@cornell.edu 16912771Sqtt2@cornell.edufutex_X: .dword 0 17012771Sqtt2@cornell.edufutex_Y: .dword 0 17112771Sqtt2@cornell.edu 17212771Sqtt2@cornell.eduMT_DATA 173