2 * Copyright (c) 2018, Cornell University
5 * Redistribution and use in source and binary forms, with or
6 * without modification, are permitted provided that the following
9 * Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
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.
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.
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.
36 //------------------------------------------------------------------------
37 // This code tests lr.d and sc.d instructions in multi-threading system.
38 // All threads execute a critical section LOOP_COUNT times. A thread
39 // gets into a critical section by acquiring a lock variable (i.e.,
40 // shared_var) and checking return value.
41 // 0 means the lock is not being locked. Each thread increments
42 // a variable (i.e., var) inside the critical section and releases the
43 // lock by swapping back 0 to the lock variable.
44 // The master thread (i.e., thread 0) waits for all threads to complete
45 // and compare the var's value to the expected result.
46 //------------------------------------------------------------------------
48 #include "riscv_test.h"
49 #include "test_macros.h"
50 #include "test_macros_mt.h"
55 #define LOOP_COUNT 1000
56 #define RESULT NUM_THREADS * LOOP_COUNT
58 //------------------------------------------------------------------------
59 // Master thread creates new threads, waits for all threads to complete,
60 // deallocates threads and checks result
61 //------------------------------------------------------------------------
69 //------------------------------------------------------------------------
70 // mt_test function executed in child threads
71 // A child thread signals its completion by atomicaly adding 1 to barrier
72 //------------------------------------------------------------------------
74 li t0, 1 // initialize the swap value (1-locked)
76 la t2, var // load the var's address
80 lr.d.aq s2, (a0) // load and reserve a0
81 bnez s2, 1b // retry lr if the lock is being held
82 sc.d.rl s2, t0, (a0) // try to lock a0
83 bnez s2, 1b // retry if sc failed
85 lw t3, (t2) // load the var's value
86 addi t3, t3, 1 // add 1 to the value
87 sw t3, (t2) // store the new value to var
89 sd zero, (a0) // release the lock by storing 0 to a0
91 addi t1, t1, -1 // decrement the loop_count
92 bnez t1, 1b // repeat if not done yet
95 amoadd.d zero, t0, (a0) // signal this thread's completion
99 //------------------------------------------------------------------------
100 // Master thread checks result
101 //------------------------------------------------------------------------