Skip to content
This repository was archived by the owner on Dec 26, 2023. It is now read-only.

Commit 6bec544

Browse files
enjiamainashif
authored andcommitted
samples: fix the metairq_dispatch sample failed on acrn_ehl_crb
The samples/kernel/metairq_dispatch runs failed on acrn_ehl_crb due to the potential race condition in accessing the global stat data. Add a spinlock to prevent this. Fixes zephyrproject-rtos#40889 Signed-off-by: Enjia Mai <[email protected]>
1 parent 48333e6 commit 6bec544

File tree

1 file changed

+11
-0
lines changed
  • samples/kernel/metairq_dispatch/src

1 file changed

+11
-0
lines changed

samples/kernel/metairq_dispatch/src/main.c

+11
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ LOG_MODULE_REGISTER(main, LOG_LEVEL_INF);
1414
/* How many messages can be queued for a single thread */
1515
#define QUEUE_DEPTH 16
1616

17+
static struct k_spinlock lock;
18+
1719
/* Array of worker threads, and their stacks */
1820
static struct thread_rec {
1921
struct k_thread thread;
@@ -118,6 +120,13 @@ static void record_latencies(struct msg *m, uint32_t latency)
118120
return;
119121
}
120122

123+
k_spinlock_key_t key = k_spin_lock(&lock);
124+
125+
if (stats.num_mirq >= MAX_EVENTS) {
126+
k_spin_unlock(&lock, key);
127+
return;
128+
}
129+
121130
int t = m->target;
122131
int lidx = stats.threads[t].nevt++;
123132

@@ -127,6 +136,8 @@ static void record_latencies(struct msg *m, uint32_t latency)
127136

128137
stats.mirq_latencies[atomic_inc(&stats.num_mirq)] = m->metairq_latency;
129138

139+
k_spin_unlock(&lock, key);
140+
130141
/* Once we've logged our final event, print a report. We use
131142
* a semaphore with an initial count of 1 to ensure that only
132143
* one thread gets to do this. Also events can be processed

0 commit comments

Comments
 (0)