mirror of
				https://github.com/riscv-software-src/opensbi
				synced 2025-10-30 19:48:23 +00:00 
			
		
		
		
	Implement the test which covers some of the functions from the `riscv_locks.h` file. This test consists of 3 test cases: 1) For lock/unlock functions 2) Unsuccessful trylock (the lock was previously taken) 3) Successful trylock (the lock is free and can be taken) Signed-off-by: Ivan Orlov <ivan.orlov0322@gmail.com> Reviewed-by: Anup Patel <anup@brainfault.org>
		
			
				
	
	
		
			42 lines
		
	
	
		
			1.1 KiB
		
	
	
	
		
			C
		
	
	
	
	
	
			
		
		
	
	
			42 lines
		
	
	
		
			1.1 KiB
		
	
	
	
		
			C
		
	
	
	
	
	
| #include <sbi/sbi_unit_test.h>
 | |
| #include <sbi/riscv_locks.h>
 | |
| 
 | |
| static spinlock_t test_lock = SPIN_LOCK_INITIALIZER;
 | |
| 
 | |
| static void spin_lock_test(struct sbiunit_test_case *test)
 | |
| {
 | |
| 	/* We don't want to accidentally get locked */
 | |
| 	SBIUNIT_ASSERT(test, !spin_lock_check(&test_lock));
 | |
| 
 | |
| 	spin_lock(&test_lock);
 | |
| 	SBIUNIT_EXPECT(test, spin_lock_check(&test_lock));
 | |
| 	spin_unlock(&test_lock);
 | |
| 
 | |
| 	SBIUNIT_ASSERT(test, !spin_lock_check(&test_lock));
 | |
| }
 | |
| 
 | |
| static void spin_trylock_fail(struct sbiunit_test_case *test)
 | |
| {
 | |
| 	/* We don't want to accidentally get locked */
 | |
| 	SBIUNIT_ASSERT(test, !spin_lock_check(&test_lock));
 | |
| 
 | |
| 	spin_lock(&test_lock);
 | |
| 	SBIUNIT_EXPECT(test, !spin_trylock(&test_lock));
 | |
| 	spin_unlock(&test_lock);
 | |
| }
 | |
| 
 | |
| static void spin_trylock_success(struct sbiunit_test_case *test)
 | |
| {
 | |
| 	SBIUNIT_EXPECT(test, spin_trylock(&test_lock));
 | |
| 	spin_unlock(&test_lock);
 | |
| }
 | |
| 
 | |
| static struct sbiunit_test_case locks_test_cases[] = {
 | |
| 	SBIUNIT_TEST_CASE(spin_lock_test),
 | |
| 	SBIUNIT_TEST_CASE(spin_trylock_fail),
 | |
| 	SBIUNIT_TEST_CASE(spin_trylock_success),
 | |
| 	SBIUNIT_END_CASE,
 | |
| };
 | |
| 
 | |
| SBIUNIT_TEST_SUITE(locks_test_suite, locks_test_cases);
 |