amoswap_d.S revision 12771
112839Sgabeblack@google.com/* 212839Sgabeblack@google.com * Copyright (c) 2018, Cornell University 312839Sgabeblack@google.com * All rights reserved. 412839Sgabeblack@google.com * 512839Sgabeblack@google.com * Redistribution and use in source and binary forms, with or 612839Sgabeblack@google.com * without modification, are permitted provided that the following 712839Sgabeblack@google.com * conditions are met: 812839Sgabeblack@google.com * 912839Sgabeblack@google.com * Redistributions of source code must retain the above copyright 1012839Sgabeblack@google.com * notice, this list of conditions and the following disclaimer. 1112839Sgabeblack@google.com * 1212839Sgabeblack@google.com * Redistributions in binary form must reproduce the above 1312839Sgabeblack@google.com * copyright notice, this list of conditions and the following 1412839Sgabeblack@google.com * disclaimer in the documentation and/or other materials provided 1512839Sgabeblack@google.com * with the distribution. 1612839Sgabeblack@google.com * 1712839Sgabeblack@google.com * Neither the name of Cornell University nor the names of its 1812839Sgabeblack@google.com * contributors may be used to endorse or promote products derived 1912839Sgabeblack@google.com * from this software without specific prior written permission. 2012839Sgabeblack@google.com * 2112839Sgabeblack@google.com * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND 2212839Sgabeblack@google.com * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, 2312839Sgabeblack@google.com * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 2412839Sgabeblack@google.com * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 2512839Sgabeblack@google.com * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR 2612839Sgabeblack@google.com * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 2712839Sgabeblack@google.com * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 2812839Sgabeblack@google.com * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF 2912839Sgabeblack@google.com * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 3012839Sgabeblack@google.com * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 3112839Sgabeblack@google.com * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN 3212839Sgabeblack@google.com * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 3312839Sgabeblack@google.com * POSSIBILITY OF SUCH DAMAGE. 3412839Sgabeblack@google.com * 3512839Sgabeblack@google.com * Authors: Tuan Ta 3612839Sgabeblack@google.com */ 3712839Sgabeblack@google.com 3812839Sgabeblack@google.com//------------------------------------------------------------------------ 3912839Sgabeblack@google.com// This code tests amoswap.d instruction in multi-threading system. 4012839Sgabeblack@google.com// All threads execute a critical section LOOP_COUNT times. A thread 4112839Sgabeblack@google.com// gets into a critical section by acquiring a lock variable (i.e., 4212839Sgabeblack@google.com// shared_var) and checking return value. 4312839Sgabeblack@google.com// 0 means the lock is not being locked. Each thread increments 4412839Sgabeblack@google.com// a variable (i.e., var) inside the critical section and releases the 4512839Sgabeblack@google.com// lock by swapping back 0 to the lock variable. 4612839Sgabeblack@google.com// The master thread (i.e., thread 0) waits for all threads to complete 4712839Sgabeblack@google.com// and compare the var's value to the expected result. 4812839Sgabeblack@google.com//------------------------------------------------------------------------ 4912839Sgabeblack@google.com 5012839Sgabeblack@google.com#include "riscv_test.h" 5112839Sgabeblack@google.com#include "test_macros.h" 5212839Sgabeblack@google.com#include "test_macros_mt.h" 5312839Sgabeblack@google.com 5412839Sgabeblack@google.com RVTEST_RV64U 5512839Sgabeblack@google.com RVTEST_CODE_BEGIN 5612839Sgabeblack@google.com 5712839Sgabeblack@google.com#define LOOP_COUNT 1000 5812839Sgabeblack@google.com#define RESULT NUM_THREADS * LOOP_COUNT 5912839Sgabeblack@google.com 6012839Sgabeblack@google.com//------------------------------------------------------------------------ 6112839Sgabeblack@google.com// Master thread creates new threads, waits for all threads to complete, 6212839Sgabeblack@google.com// deallocates threads and checks result 6312839Sgabeblack@google.com//------------------------------------------------------------------------ 6412839Sgabeblack@google.com call _create_threads 6512839Sgabeblack@google.com call _join 6612839Sgabeblack@google.com call _delete_threads 6712839Sgabeblack@google.com call _check 6812839Sgabeblack@google.com 6912839Sgabeblack@google.com RVTEST_CODE_END 7012839Sgabeblack@google.com 7112839Sgabeblack@google.com//------------------------------------------------------------------------ 7212839Sgabeblack@google.com// mt_test function executed in child threads 7312839Sgabeblack@google.com// A child thread signals its completion by atomicaly adding 1 to barrier 7412839Sgabeblack@google.com//------------------------------------------------------------------------ 7512839Sgabeblack@google.com_mt_test: 7612839Sgabeblack@google.com li t0, 1 // initialize the swap value (1-locked) 7712839Sgabeblack@google.com li t1, LOOP_COUNT 7812839Sgabeblack@google.com la t2, var // load the var's address 7912839Sgabeblack@google.com la a0, shared_var 8012839Sgabeblack@google.com 8112839Sgabeblack@google.com1: 8212839Sgabeblack@google.com amoswap.d.aq s2, t0, (a0) // try to swap t0 with the lock 8312839Sgabeblack@google.com bnez s2, 1b // retry if the lock is being held 8412839Sgabeblack@google.com 8512839Sgabeblack@google.com lw t3, (t2) // load the var's value 8612839Sgabeblack@google.com addi t3, t3, 1 // add 1 to the value 8712839Sgabeblack@google.com sw t3, (t2) // store the new value to var 8812839Sgabeblack@google.com 8912839Sgabeblack@google.com amoswap.d.rl zero, zero, (a0)// release the lock by swapping back 0 9012839Sgabeblack@google.com 9112839Sgabeblack@google.com addi t1, t1, -1 // decrement the loop_count 9212839Sgabeblack@google.com bnez t1, 1b // repeat if not done yet 9312839Sgabeblack@google.com 9412839Sgabeblack@google.com la a0, barrier 9512839Sgabeblack@google.com amoadd.d zero, t0, (a0) // signal this thread's completion 9612839Sgabeblack@google.com 9712839Sgabeblack@google.com RVTEST_CODE_END 9812839Sgabeblack@google.com 9912839Sgabeblack@google.com//------------------------------------------------------------------------ 10012839Sgabeblack@google.com// Master thread checks result 10112839Sgabeblack@google.com//------------------------------------------------------------------------ 10212839Sgabeblack@google.com_check: 10312839Sgabeblack@google.com la a0, var 10412839Sgabeblack@google.com li a1, RESULT 10512839Sgabeblack@google.com ld a0, (a0) 10612839Sgabeblack@google.com bne a0, a1, _fail 10712839Sgabeblack@google.com li a0, SUCCESS 10812839Sgabeblack@google.com ret 10912839Sgabeblack@google.com 11012839Sgabeblack@google.com_fail: 11112839Sgabeblack@google.com li a0, FAILURE 11212839Sgabeblack@google.com ret 11312839Sgabeblack@google.com 11412839Sgabeblack@google.com .data 11512839Sgabeblack@google.com 11612839Sgabeblack@google.comMT_DATA 11712839Sgabeblack@google.comvar: .dword 0 11812839Sgabeblack@google.com