Skip to content

Commit 427e5f1

Browse files
hujun260xiaoxiang781216
authored andcommitted
irq: irq with the same priority share the same wqueue
reason: 1 We place interrupt handling functions of the same priority into the work queue corresponding to that priority, allowing high-priority interrupts to preempt low-priority ones, thus ensuring the real-time performance of high-priority interrupts. 2 The sole purpose of the interrupt handler is to wake up the work queue of the corresponding priority and execute the interrupt handling function. 3 Compared to the functionality of isr threads, this approach saves more memory, particularly when the number of interrupts is large. Signed-off-by: hujun5 <[email protected]>
1 parent c6a7816 commit 427e5f1

File tree

6 files changed

+251
-4
lines changed

6 files changed

+251
-4
lines changed

include/nuttx/irq.h

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,9 @@
4747
*/
4848

4949
# define irq_detach(irq) irq_attach(irq, NULL, NULL)
50+
# define irq_detach_wqueue(irq) irq_attach_wqueue(irq, NULL, NULL, NULL, 0)
51+
# define irq_detach_thread(irq) \
52+
irq_attach_thread(irq, NULL, NULL, NULL, 0, 0)
5053

5154
/* Maximum/minimum values of IRQ integer types */
5255

@@ -194,6 +197,31 @@ int irq_attach(int irq, xcpt_t isr, FAR void *arg);
194197
int irq_attach_thread(int irq, xcpt_t isr, xcpt_t isrthread, FAR void *arg,
195198
int priority, int stack_size);
196199

200+
/****************************************************************************
201+
* Name: irq_attach_wqueue
202+
*
203+
* Description:
204+
* Configure the IRQ subsystem so that IRQ number 'irq' is dispatched to
205+
* 'wqueue'
206+
*
207+
* Input Parameters:
208+
* irq - Irq num
209+
* isr - Function to be called when the IRQ occurs, called in interrupt
210+
* context.
211+
* If isr is NULL the default handler is installed(irq_default_handler).
212+
* isrwork - called in thread context, If the isrwork is NULL,
213+
* then the ISR is being detached.
214+
* arg - privdate data
215+
* priority - isrwork pri
216+
*
217+
* Returned Value:
218+
* Zero on success; a negated errno value on failure.
219+
*
220+
****************************************************************************/
221+
222+
int irq_attach_wqueue(int irq, xcpt_t isr, xcpt_t isrwork,
223+
FAR void *arg, int priority);
224+
197225
#ifdef CONFIG_IRQCHAIN
198226
int irqchain_detach(int irq, xcpt_t isr, FAR void *arg);
199227
#else

sched/Kconfig

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -340,6 +340,18 @@ config PREALLOC_IRQCHAIN
340340

341341
endif # IRQCHAIN
342342

343+
config IRQ_NWORKS
344+
int "Max num of active irq wqueue"
345+
default 8
346+
---help---
347+
The max num of active irq wqueue.
348+
349+
config IRQ_WORK_STACKSIZE
350+
int "The default stack size for isr wqueue"
351+
default DEFAULT_TASK_STACKSIZE
352+
---help---
353+
The default stack size for isr wqueue.
354+
343355
config IRQCOUNT
344356
bool
345357
default n

sched/irq/CMakeLists.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@
1818
#
1919
# ##############################################################################
2020

21-
set(SRCS irq_initialize.c irq_attach.c irq_attach_thread.c irq_dispatch.c
22-
irq_unexpectedisr.c)
21+
set(SRCS irq_initialize.c irq_attach.c irq_attach_thread.c irq_attach_wqueue.c
22+
irq_dispatch.c irq_unexpectedisr.c)
2323

2424
if(CONFIG_SPINLOCK)
2525
list(APPEND SRCS irq_spinlock.c)

sched/irq/Make.defs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
############################################################################
2020

2121
CSRCS += irq_initialize.c irq_attach.c irq_dispatch.c irq_unexpectedisr.c
22-
CSRCS += irq_attach_thread.c
22+
CSRCS += irq_attach_thread.c irq_attach_wqueue.c
2323

2424
ifeq ($(CONFIG_SPINLOCK),y)
2525
CSRCS += irq_spinlock.c

sched/irq/irq_attach_thread.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ int irq_attach_thread(int irq, xcpt_t isr, xcpt_t isrthread, FAR void *arg,
170170

171171
if (isrthread == NULL)
172172
{
173-
irq_attach(irq, NULL, arg);
173+
irq_detach(irq);
174174
DEBUGASSERT(g_irq_thread_pid[ndx] != 0);
175175
kthread_delete(g_irq_thread_pid[ndx]);
176176
g_irq_thread_pid[ndx] = 0;

sched/irq/irq_attach_wqueue.c

Lines changed: 207 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,207 @@
1+
/****************************************************************************
2+
* sched/irq/irq_attach_wqueue.c
3+
*
4+
* Licensed to the Apache Software Foundation (ASF) under one or more
5+
* contributor license agreements. See the NOTICE file distributed with
6+
* this work for additional information regarding copyright ownership. The
7+
* ASF licenses this file to you under the Apache License, Version 2.0 (the
8+
* "License"); you may not use this file except in compliance with the
9+
* License. You may obtain a copy of the License at
10+
*
11+
* http://www.apache.org/licenses/LICENSE-2.0
12+
*
13+
* Unless required by applicable law or agreed to in writing, software
14+
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15+
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
16+
* License for the specific language governing permissions and limitations
17+
* under the License.
18+
*
19+
****************************************************************************/
20+
21+
/****************************************************************************
22+
* Included Files
23+
****************************************************************************/
24+
25+
#include <nuttx/config.h>
26+
27+
#include <errno.h>
28+
#include <stdio.h>
29+
30+
#include <nuttx/irq.h>
31+
#include <nuttx/wqueue.h>
32+
#include <debug.h>
33+
34+
#include "irq/irq.h"
35+
#include "sched/sched.h"
36+
37+
/****************************************************************************
38+
* Privte Types
39+
****************************************************************************/
40+
41+
/* This is the type of the list of interrupt handlers, one for each IRQ.
42+
* This type provided all of the information necessary to irq_dispatch to
43+
* transfer control to interrupt handlers after the occurrence of an
44+
* interrupt.
45+
*/
46+
47+
struct irq_work_info_s
48+
{
49+
xcpt_t handler; /* Address of the interrupt handler */
50+
xcpt_t isrwork; /* Address of the interrupt worked handler */
51+
FAR void *arg; /* The argument provided to the interrupt handler. */
52+
int irq; /* Irq id */
53+
struct work_s work; /* Interrupt work to the wq */
54+
55+
FAR struct kwork_wqueue_s *wqueue; /* Work queue. */
56+
};
57+
58+
/****************************************************************************
59+
* Private Data
60+
****************************************************************************/
61+
62+
#ifdef CONFIG_ARCH_MINIMAL_VECTORTABLE
63+
static struct irq_work_info_s
64+
g_irq_work_vector[CONFIG_ARCH_NUSER_INTERRUPTS];
65+
#else
66+
static struct irq_work_info_s g_irq_work_vector[NR_IRQS];
67+
#endif
68+
69+
static mutex_t g_irq_wqueue_lock = NXMUTEX_INITIALIZER;
70+
static FAR struct kwork_wqueue_s *g_irq_wqueue[CONFIG_IRQ_NWORKS];
71+
72+
/****************************************************************************
73+
* Private Functions
74+
****************************************************************************/
75+
76+
static
77+
inline_function FAR struct kwork_wqueue_s *irq_get_wqueue(int priority)
78+
{
79+
FAR struct kwork_wqueue_s *queue;
80+
int wqueue_priority;
81+
int i;
82+
83+
nxmutex_lock(&g_irq_wqueue_lock);
84+
for (i = 0; g_irq_wqueue[i] != NULL && i < CONFIG_IRQ_NWORKS; i++)
85+
{
86+
wqueue_priority = work_queue_priority_wq(g_irq_wqueue[i]);
87+
DEBUGASSERT(wqueue_priority >= SCHED_PRIORITY_MIN &&
88+
wqueue_priority <= SCHED_PRIORITY_MAX);
89+
90+
if (wqueue_priority == priority)
91+
{
92+
nxmutex_unlock(&g_irq_wqueue_lock);
93+
return g_irq_wqueue[i];
94+
}
95+
}
96+
97+
DEBUGASSERT(i < CONFIG_IRQ_NWORKS);
98+
99+
queue = work_queue_create("isrwork", priority,
100+
CONFIG_IRQ_WORK_STACKSIZE, 1);
101+
102+
g_irq_wqueue[i] = queue;
103+
nxmutex_unlock(&g_irq_wqueue_lock);
104+
return queue;
105+
}
106+
107+
/* Default interrupt handler for worked interrupts.
108+
* Useful for oneshot interrupts.
109+
*/
110+
111+
static void irq_work_handler(FAR void *arg)
112+
{
113+
FAR struct irq_work_info_s *info = arg;
114+
115+
info->isrwork(info->irq, NULL, info->arg);
116+
}
117+
118+
static int irq_default_handler(int irq, FAR void *regs, FAR void *arg)
119+
{
120+
FAR struct irq_work_info_s *info = arg;
121+
int ret;
122+
123+
ret = info->handler(irq, regs, arg);
124+
125+
if (ret == IRQ_WAKE_THREAD)
126+
{
127+
work_queue_wq(info->wqueue, &info->work, irq_work_handler, info, 0);
128+
ret = OK;
129+
}
130+
131+
return ret;
132+
}
133+
134+
/****************************************************************************
135+
* Public Functions
136+
****************************************************************************/
137+
138+
/****************************************************************************
139+
* Name: irq_attach_wqueue
140+
*
141+
* Description:
142+
* Configure the IRQ subsystem so that IRQ number 'irq' is dispatched to
143+
* 'wqueue'
144+
*
145+
* Input Parameters:
146+
* irq - Irq num
147+
* isr - Function to be called when the IRQ occurs, called in interrupt
148+
* context.
149+
* If isr is NULL the default handler is installed(irq_default_handler).
150+
* isrwork - called in thread context, If the isrwork is NULL,
151+
* then the ISR is being detached.
152+
* arg - privdate data
153+
* priority - isrwork pri
154+
*
155+
* Returned Value:
156+
* Zero on success; a negated errno value on failure.
157+
*
158+
****************************************************************************/
159+
160+
int irq_attach_wqueue(int irq, xcpt_t isr, xcpt_t isrwork,
161+
FAR void *arg, int priority)
162+
{
163+
FAR struct irq_work_info_s *info;
164+
165+
#if NR_IRQS > 0
166+
int ndx;
167+
168+
if ((unsigned)irq >= NR_IRQS)
169+
{
170+
return -EINVAL;
171+
}
172+
173+
ndx = IRQ_TO_NDX(irq);
174+
if (ndx < 0)
175+
{
176+
return ndx;
177+
}
178+
179+
/* If the isrwork is NULL, then the ISR is being detached. */
180+
181+
info = &g_irq_work_vector[ndx];
182+
183+
if (isrwork == NULL)
184+
{
185+
irq_detach(irq);
186+
info->isrwork = NULL;
187+
info->handler = NULL;
188+
info->arg = NULL;
189+
info->wqueue = NULL;
190+
return OK;
191+
}
192+
193+
info->isrwork = isrwork;
194+
info->handler = isr;
195+
info->arg = arg;
196+
info->irq = irq;
197+
if (info->wqueue == NULL)
198+
{
199+
info->wqueue = irq_get_wqueue(priority);
200+
}
201+
202+
irq_attach(irq, irq_default_handler, info);
203+
#endif /* NR_IRQS */
204+
205+
return OK;
206+
}
207+

0 commit comments

Comments
 (0)