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 // sysfutex_d tests FUTEX_WAKE_OP functionalities of futex system call:
38 // - make a thread wait on a variable
39 // - atomically wake up a thread waiting on a variable and perform
40 // a operation on a value
41 //------------------------------------------------------------------------
43 #include "riscv_test.h"
44 #include "test_macros.h"
45 #include "test_macros_mt_ecall.h"
51 #define LOOP_COUNT 1000
53 //------------------------------------------------------------------------
54 // Master thread creates new threads, call _master function, waits for all
55 // threads to complete, deallocates threads and checks result
56 //------------------------------------------------------------------------
60 la t6, n_worker_threads
62 beqz a0, _fail // exit if there's no worker thread
66 la t6, n_worker_threads
70 la t6, n_worker_threads
74 la t6, n_worker_threads
82 //------------------------------------------------------------------------
83 // master_work function executed by the parent/master thread
85 // Wake up thread(s) waiting on futex_X and then wait on futex_Y in a
86 // loop. Also atomically modify futex_Z during the wake-up.
87 //------------------------------------------------------------------------
89 mv s0, ra // save return address
95 // futex(futex_X, FUTEX_WAKE_OP, 1, val2, futex_Z, val3 )
98 li a2, 1 // wake up at most 1 thread
99 li a3, 0 // should not perform the second wake up
100 la a4, futex_Z // add 1 to futex_Z each time
101 li a5, FUTEX_OP(FUTEX_OP_ADD, 1, FUTEX_OP_CMP_LT, 0)
105 // increment count_Z (should equals to futex_Z)
110 // keep waking up until at least one thread is waken up
113 // increment count_master
118 // futex(futex_Y, FUTEX_WAIT_PRIVATE, 0)
120 li a1, FUTEX_WAIT_PRIVATE
121 li a2, 0 // expected val of futex_Y
129 // restore return address and return
133 //------------------------------------------------------------------------
134 // mt_test function executed by child threads
136 // Wait on futex_X and then wake up threads waiting on futex_Y in a loop
137 //------------------------------------------------------------------------
143 // futex(futex_X, FUTEX_WAIT_PRIVATE, 1)
145 li a1, FUTEX_WAIT_PRIVATE
146 li a2, 0 // expected val of futex_X
150 // increment count_child
156 // futex(futex_Y, FUTEX_WAKE_PRIVATE, 0)
158 li a1, FUTEX_WAKE_PRIVATE
159 li a2, 1 // wake up at most 1 thread
163 // keep waking up until at least one thread is waken up
172 //------------------------------------------------------------------------
174 // Each thread should do LOOP_COUNT iterations
175 //------------------------------------------------------------------------
206 count_master: .dword 0
207 count_child: .dword 0