Skip to content

Commit f0933d0

Browse files
Andy Rossnashif
Andy Ross
authored andcommitted
kernel/stack: Spinlockify
One lock per stack. Straightforward synchronization. Signed-off-by: Andy Ross <andrew.j.ross@intel.com>
1 parent 9eeb6b8 commit f0933d0

File tree

2 files changed

+12
-11
lines changed

2 files changed

+12
-11
lines changed

Diff for: include/kernel.h

+1
Original file line numberDiff line numberDiff line change
@@ -2370,6 +2370,7 @@ struct k_lifo {
23702370

23712371
struct k_stack {
23722372
_wait_q_t wait_q;
2373+
struct k_spinlock lock;
23732374
u32_t *base, *next, *top;
23742375

23752376
_OBJECT_TRACING_NEXT_PTR(k_stack)

Diff for: kernel/stack.c

+11-11
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,8 @@ void k_stack_init(struct k_stack *stack, u32_t *buffer,
5050
u32_t num_entries)
5151
{
5252
_waitq_init(&stack->wait_q);
53-
stack->base = buffer;
54-
stack->next = buffer;
53+
stack->lock = (struct k_spinlock) {};
54+
stack->next = stack->base = buffer;
5555
stack->top = stack->base + num_entries;
5656

5757
SYS_TRACING_OBJ_INIT(k_stack, stack);
@@ -99,11 +99,11 @@ void k_stack_cleanup(struct k_stack *stack)
9999
void _impl_k_stack_push(struct k_stack *stack, u32_t data)
100100
{
101101
struct k_thread *first_pending_thread;
102-
u32_t key;
102+
k_spinlock_key_t key;
103103

104104
__ASSERT(stack->next != stack->top, "stack is full");
105105

106-
key = irq_lock();
106+
key = k_spin_lock(&stack->lock);
107107

108108
first_pending_thread = _unpend_first_thread(&stack->wait_q);
109109

@@ -112,12 +112,12 @@ void _impl_k_stack_push(struct k_stack *stack, u32_t data)
112112

113113
_set_thread_return_value_with_data(first_pending_thread,
114114
0, (void *)data);
115-
_reschedule_irqlock(key);
115+
_reschedule(&stack->lock, key);
116116
return;
117117
} else {
118118
*(stack->next) = data;
119119
stack->next++;
120-
irq_unlock(key);
120+
k_spin_unlock(&stack->lock, key);
121121
}
122122

123123
}
@@ -138,24 +138,24 @@ Z_SYSCALL_HANDLER(k_stack_push, stack_p, data)
138138

139139
int _impl_k_stack_pop(struct k_stack *stack, u32_t *data, s32_t timeout)
140140
{
141-
u32_t key;
141+
k_spinlock_key_t key;
142142
int result;
143143

144-
key = irq_lock();
144+
key = k_spin_lock(&stack->lock);
145145

146146
if (likely(stack->next > stack->base)) {
147147
stack->next--;
148148
*data = *(stack->next);
149-
irq_unlock(key);
149+
k_spin_unlock(&stack->lock, key);
150150
return 0;
151151
}
152152

153153
if (timeout == K_NO_WAIT) {
154-
irq_unlock(key);
154+
k_spin_unlock(&stack->lock, key);
155155
return -EBUSY;
156156
}
157157

158-
result = _pend_curr_irqlock(key, &stack->wait_q, timeout);
158+
result = _pend_curr(&stack->lock, key, &stack->wait_q, timeout);
159159
if (result == -EAGAIN) {
160160
return -EAGAIN;
161161
}

0 commit comments

Comments
 (0)