1/* 2 * Copyright (c) 2018, Cornell University 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or 6 * without modification, are permitted provided that the following 7 * conditions are met: 8 * 9 * Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer. 11 * 12 * Redistributions in binary form must reproduce the above 13 * copyright notice, this list of conditions and the following 14 * disclaimer in the documentation and/or other materials provided 15 * with the distribution. 16 * 17 * Neither the name of Cornell University nor the names of its 18 * contributors may be used to endorse or promote products derived 19 * from this software without specific prior written permission. 20 * 21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND 22 * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, 23 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 24 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 25 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR 26 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 27 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 28 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF 29 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 30 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN 32 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 33 * POSSIBILITY OF SUCH DAMAGE. 34 * 35 * Authors: Tuan Ta 36 */ 37 38//------------------------------------------------------------------------ 39// This code tests lr.d and sc.d instructions in multi-threading system. 40// All threads execute a critical section LOOP_COUNT times. A thread 41// gets into a critical section by acquiring a lock variable (i.e., 42// shared_var) and checking return value. 43// 0 means the lock is not being locked. Each thread increments 44// a variable (i.e., var) inside the critical section and releases the 45// lock by swapping back 0 to the lock variable. 46// The master thread (i.e., thread 0) waits for all threads to complete 47// and compare the var's value to the expected result. 48//------------------------------------------------------------------------ 49 50#include "riscv_test.h" 51#include "test_macros.h" 52#include "test_macros_mt.h" 53 54 RVTEST_RV64U 55 RVTEST_CODE_BEGIN 56 57#define LOOP_COUNT 1000 58#define RESULT NUM_THREADS * LOOP_COUNT 59 60//------------------------------------------------------------------------ 61// Master thread creates new threads, waits for all threads to complete, 62// deallocates threads and checks result 63//------------------------------------------------------------------------ 64 call _create_threads 65 call _join 66 call _delete_threads 67 call _check 68 69 RVTEST_CODE_END 70 71//------------------------------------------------------------------------ 72// mt_test function executed in child threads 73// A child thread signals its completion by atomicaly adding 1 to barrier 74//------------------------------------------------------------------------ 75_mt_test: 76 li t0, 1 // initialize the swap value (1-locked) 77 li t1, LOOP_COUNT 78 la t2, var // load the var's address 79 la a0, shared_var 80 811: 82 lr.d.aq s2, (a0) // load and reserve a0 83 bnez s2, 1b // retry lr if the lock is being held 84 sc.d.rl s2, t0, (a0) // try to lock a0 85 bnez s2, 1b // retry if sc failed 86 87 lw t3, (t2) // load the var's value 88 addi t3, t3, 1 // add 1 to the value 89 sw t3, (t2) // store the new value to var 90 91 sd zero, (a0) // release the lock by storing 0 to a0 92 93 addi t1, t1, -1 // decrement the loop_count 94 bnez t1, 1b // repeat if not done yet 95 96 la a0, barrier 97 amoadd.d zero, t0, (a0) // signal this thread's completion 98 99 RVTEST_CODE_END 100 101//------------------------------------------------------------------------ 102// Master thread checks result 103//------------------------------------------------------------------------ 104_check: 105 la a0, var 106 li a1, RESULT 107 ld a0, (a0) 108 bne a0, a1, _fail 109 li a0, SUCCESS 110 ret 111 112_fail: 113 li a0, FAILURE 114 ret 115 116 .data 117 118MT_DATA 119var: .dword 0 120