Skip to content

Commit 653b354

Browse files
committed
Avoid using __sev()
Use lock_internal_spin_unlock_with_notify instead.
1 parent 49fc96b commit 653b354

File tree

1 file changed

+25
-13
lines changed

1 file changed

+25
-13
lines changed

src/common/pico_sync/cond.c

Lines changed: 25 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -34,15 +34,16 @@ bool __time_critical_func(cond_wait_until)(cond_t *cond, mutex_t *mtx, absolute_
3434
spin_lock_unsafe_blocking(cond->core.spin_lock);
3535
}
3636

37-
// Release the mutex but without restoring interrupts and notify.
3837
mtx->owner = LOCK_INVALID_OWNER_ID;
39-
if (!same_spinlock) {
40-
spin_unlock_unsafe(mtx->core.spin_lock);
41-
}
4238

4339
uint64_t current_broadcast = cond->broadcast_count;
4440

4541
if (lock_is_owner_id_valid(cond->waiter)) {
42+
// Release the mutex but without restoring interrupts and notify.
43+
if (!same_spinlock) {
44+
spin_unlock_unsafe(mtx->core.spin_lock);
45+
}
46+
4647
// There is a valid owner of the condition variable: we are not the
4748
// first waiter.
4849
// First iteration: notify
@@ -68,8 +69,11 @@ bool __time_critical_func(cond_wait_until)(cond_t *cond, mutex_t *mtx, absolute_
6869
save = spin_lock_blocking(cond->core.spin_lock);
6970
} while (true);
7071
} else {
71-
// Notify to finish release of mutex
72-
__sev();
72+
// Release the mutex but without restoring interrupts
73+
if (!same_spinlock) {
74+
uint32_t disabled_ints = save_and_disable_interrupts();
75+
lock_internal_spin_unlock_with_notify(&mtx->core, disabled_ints);
76+
}
7377
}
7478

7579
if (success && cond->broadcast_count == current_broadcast) {
@@ -97,13 +101,14 @@ bool __time_critical_func(cond_wait_until)(cond_t *cond, mutex_t *mtx, absolute_
97101
}
98102

99103
// We got the signal (or timed out)
100-
// Acquire the mutex spin lock and release the core spin lock.
101-
if (!same_spinlock) {
102-
spin_lock_unsafe_blocking(mtx->core.spin_lock);
103-
spin_unlock_unsafe(cond->core.spin_lock);
104-
}
105104

106105
if (lock_is_owner_id_valid(mtx->owner)) {
106+
// Acquire the mutex spin lock and release the core spin lock.
107+
if (!same_spinlock) {
108+
spin_lock_unsafe_blocking(mtx->core.spin_lock);
109+
spin_unlock_unsafe(cond->core.spin_lock);
110+
}
111+
107112
// Another core holds the mutex.
108113
// First iteration: notify
109114
lock_internal_spin_unlock_with_notify(&mtx->core, save);
@@ -118,12 +123,19 @@ bool __time_critical_func(cond_wait_until)(cond_t *cond, mutex_t *mtx, absolute_
118123
save = spin_lock_blocking(mtx->core.spin_lock);
119124
} while (true);
120125
} else {
121-
// Notify to finish release of condition variable
122-
__sev();
126+
// Acquire the mutex spin lock and release the core spin lock
127+
// with notify but without restoring interrupts
128+
if (!same_spinlock) {
129+
spin_lock_unsafe_blocking(mtx->core.spin_lock);
130+
uint32_t disabled_ints = save_and_disable_interrupts();
131+
lock_internal_spin_unlock_with_notify(&cond->core, disabled_ints);
132+
}
123133
}
124134

125135
// Eventually hold the mutex.
126136
mtx->owner = caller;
137+
138+
// Restore the interrupts now
127139
spin_unlock(mtx->core.spin_lock, save);
128140

129141
return success;

0 commit comments

Comments
 (0)