summaryrefslogtreecommitdiff
path: root/test/test_spinlock.cc
blob: be794a041e70b4e967a6855abc042c7e3de18285 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
/* SPDX-License-Identifier: GPL-2.0-or-later */
/* Copyright (C) 2026 Richard Qin */

/*
 * Boot Self-Test: Spinlock
 *
 * Tests:
 *   1. Basic acquire/release
 *   2. LockGuard RAII acquire/release
 *   3. Nested locking (two different locks)
 *   4. Interrupt state preservation across lock/unlock
 *   5. is_held_by_current_cpu correctness
 *
 * Note: We cannot test multi-core contention in BSP-only self-test.
 * That requires AP participation (future Phase 2 test).
 */

#include <lume/selftest.h>
#include <kernel/sync/spinlock.h>
#include <kernel/sync/cpu_state.h>
#include <arch/cpu.h>

void selftest_spinlock()
{
    st_begin("spinlock: basic acquire/release");
    {
        Spinlock lock;
        lock.init("test_basic");

        ST_ASSERT(!lock.is_held_by_current_cpu());

        lock.acquire();
        ST_ASSERT(lock.is_held_by_current_cpu());

        lock.release();
        ST_ASSERT(!lock.is_held_by_current_cpu());
    }
    st_pass();

    st_begin("spinlock: LockGuard RAII");
    {
        Spinlock lock;
        lock.init("test_guard");

        {
            LockGuard guard(lock);
            ST_ASSERT(lock.is_held_by_current_cpu());
        }
        // After scope exit, lock should be released
        ST_ASSERT(!lock.is_held_by_current_cpu());
    }
    st_pass();

    st_begin("spinlock: nested locking (two locks)");
    {
        Spinlock lock_a, lock_b;
        lock_a.init("test_nest_a");
        lock_b.init("test_nest_b");

        lock_a.acquire();
        ST_ASSERT(lock_a.is_held_by_current_cpu());

        lock_b.acquire();
        ST_ASSERT(lock_b.is_held_by_current_cpu());

        lock_b.release();
        ST_ASSERT(!lock_b.is_held_by_current_cpu());
        ST_ASSERT(lock_a.is_held_by_current_cpu());  // A still held

        lock_a.release();
        ST_ASSERT(!lock_a.is_held_by_current_cpu());
    }
    st_pass();

    st_begin("spinlock: interrupt state preservation");
    {
        Spinlock lock;
        lock.init("test_intr");

        // Interrupts are currently off (BSP init period), but let's
        // verify the lock doesn't corrupt the state.
        bool before = arch::cpu::intr_enabled();

        lock.acquire();
        // Inside lock, interrupts must be disabled
        ST_ASSERT(!arch::cpu::intr_enabled());
        lock.release();

        // After release, interrupt state should be restored
        ST_ASSERT_EQ(arch::cpu::intr_enabled(), before);
    }
    st_pass();
    st_begin("spinlock: deep nesting tracking (> 2)");
    {
        Spinlock l1, l2, l3;
        l1.init("l1");
        l2.init("l2");
        l3.init("l3");
        
        bool before = arch::cpu::intr_enabled();
        
        l1.acquire();
        l2.acquire();
        l3.acquire();
        
        ST_ASSERT(!arch::cpu::intr_enabled());
        
        l3.release();
        ST_ASSERT(!arch::cpu::intr_enabled());
        
        l2.release();
        ST_ASSERT(!arch::cpu::intr_enabled());
        
        l1.release();
        ST_ASSERT_EQ(arch::cpu::intr_enabled(), before);
    }
    st_pass();

    st_begin("spinlock: interrupt leak detection logic (lock_depth metadata)");
    {
        Spinlock l;
        l.init("l");
        
        auto& state = sync::g_cpu_sync_states[arch::cpu::id()];
        int initial_depth = state.lock_depth;
        
        l.acquire();
        // The lock depth should strictly increase
        ST_ASSERT_EQ(state.lock_depth, initial_depth + 1);
        l.release();
        
        ST_ASSERT_EQ(state.lock_depth, initial_depth);
    }
    st_pass();
}