Skip to content

Commit a665cd7

Browse files
pangzhen1xiaomixiaoxiang781216
authored andcommitted
sched/irq: functions should have exactly one exit point
According to MISRA C-2004 Rule 14.7, Every function must have exactly one entry point and one exit point. Signed-off-by: pangzhen1 <pangzhen1@xiaomi.com>
1 parent 165f1cc commit a665cd7

3 files changed

Lines changed: 96 additions & 92 deletions

File tree

sched/irq/irq_attach.c

Lines changed: 24 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -113,19 +113,22 @@ int ndx_to_irq(int ndx)
113113

114114
int irq_attach(int irq, xcpt_t isr, FAR void *arg)
115115
{
116+
int ret = OK;
116117
#if NR_IRQS > 0
117-
int ret = -EINVAL;
118+
int ndx = -EINVAL;
119+
irqstate_t flags;
118120

119121
if (irq >= 0 && irq < NR_IRQS)
120122
{
121-
int ndx = IRQ_TO_NDX(irq);
122-
irqstate_t flags;
123-
124-
if (ndx < 0)
125-
{
126-
return ndx;
127-
}
123+
ndx = IRQ_TO_NDX(irq);
124+
}
128125

126+
if (ndx < 0)
127+
{
128+
ret = ndx;
129+
}
130+
else
131+
{
129132
/* If the new ISR is NULL, then the ISR is being detached.
130133
* In this case, disable the ISR and direct any interrupts
131134
* to the unexpected interrupt handler.
@@ -166,26 +169,24 @@ int irq_attach(int irq, xcpt_t isr, FAR void *arg)
166169
{
167170
ret = irqchain_attach(ndx, isr, arg);
168171
spin_unlock_irqrestore(&g_irqlock, flags);
169-
return ret;
170172
}
173+
else
171174
#endif
175+
{
176+
/* Save the new ISR and its argument in the table. */
172177

173-
/* Save the new ISR and its argument in the table. */
174-
175-
g_irqvector[ndx].handler = isr;
176-
g_irqvector[ndx].arg = arg;
177-
#ifdef CONFIG_SCHED_IRQMONITOR
178-
g_irqvector[ndx].start = clock_systime_ticks();
179-
g_irqvector[ndx].time = 0;
180-
g_irqvector[ndx].count = 0;
181-
#endif
178+
g_irqvector[ndx].handler = isr;
179+
g_irqvector[ndx].arg = arg;
180+
#ifdef CONFIG_SCHED_IRQMONITOR
181+
g_irqvector[ndx].start = clock_systime_ticks();
182+
g_irqvector[ndx].time = 0;
183+
g_irqvector[ndx].count = 0;
184+
#endif
182185

183-
spin_unlock_irqrestore(&g_irqlock, flags);
184-
ret = OK;
186+
spin_unlock_irqrestore(&g_irqlock, flags);
187+
}
185188
}
189+
#endif /* NR_IRQS */
186190

187191
return ret;
188-
#else
189-
return OK;
190-
#endif
191192
}

sched/irq/irq_attach_thread.c

Lines changed: 30 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,7 @@ static int isr_thread_main(int argc, FAR char *argv[])
145145
int irq_attach_thread(int irq, xcpt_t isr, xcpt_t isrthread, FAR void *arg,
146146
int priority, int stack_size)
147147
{
148+
int ret = OK;
148149
#if NR_IRQS > 0
149150
static pid_t irq_thread_pid[NR_IRQS];
150151

@@ -154,56 +155,52 @@ int irq_attach_thread(int irq, xcpt_t isr, xcpt_t isrthread, FAR void *arg,
154155
char arg3[32]; /* isrthread */
155156
char arg4[32]; /* arg */
156157
pid_t pid;
157-
int ndx;
158+
int ndx = -EINVAL;
158159

159-
if (irq < 0 || irq >= NR_IRQS)
160+
if (irq >= 0 && irq < NR_IRQS)
160161
{
161-
return -EINVAL;
162+
ndx = IRQ_TO_NDX(irq);
162163
}
163164

164-
ndx = IRQ_TO_NDX(irq);
165165
if (ndx < 0)
166166
{
167-
return ndx;
167+
ret = ndx;
168168
}
169-
170-
/* If the isrthread is NULL, then the ISR is being detached. */
171-
172-
if (isrthread == NULL)
169+
else if(isrthread == NULL)
173170
{
171+
/* If the isrthread is NULL, then the ISR is being detached. */
172+
174173
irq_detach(irq);
175174
DEBUGASSERT(irq_thread_pid[ndx] != 0);
176175
kthread_delete(irq_thread_pid[ndx]);
177176
irq_thread_pid[ndx] = 0;
178-
179-
return OK;
180177
}
181-
182-
if (irq_thread_pid[ndx] != 0)
178+
else if(irq_thread_pid[ndx] != 0)
183179
{
184-
return -EINVAL;
180+
ret = -EINVAL;
185181
}
186-
187-
snprintf(arg1, sizeof(arg1), "%d", irq);
188-
snprintf(arg2, sizeof(arg2), "%p", isr);
189-
snprintf(arg3, sizeof(arg3), "%p", isrthread);
190-
snprintf(arg4, sizeof(arg4), "%p", arg);
191-
argv[0] = arg1;
192-
argv[1] = arg2;
193-
argv[2] = arg3;
194-
argv[3] = arg4;
195-
argv[4] = NULL;
196-
197-
pid = kthread_create("isr_thread", priority, stack_size,
198-
isr_thread_main, argv);
199-
if (pid < 0)
182+
else
200183
{
201-
return pid;
202-
}
203-
204-
irq_thread_pid[ndx] = pid;
184+
snprintf(arg1, sizeof(arg1), "%d", irq);
185+
snprintf(arg2, sizeof(arg2), "%p", isr);
186+
snprintf(arg3, sizeof(arg3), "%p", isrthread);
187+
snprintf(arg4, sizeof(arg4), "%p", arg);
188+
argv[0] = arg1;
189+
argv[1] = arg2;
190+
argv[2] = arg3;
191+
argv[3] = arg4;
192+
argv[4] = NULL;
193+
194+
pid = kthread_create("isr_thread", priority, stack_size,
195+
isr_thread_main, argv);
196+
if (pid < 0)
197+
{
198+
ret = pid;
199+
}
205200

201+
irq_thread_pid[ndx] = pid;
202+
}
206203
#endif /* NR_IRQS */
207204

208-
return OK;
205+
return ret;
209206
}

sched/irq/irq_attach_wqueue.c

Lines changed: 42 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ inline_function FAR struct kwork_wqueue_s *irq_get_wqueue(int priority)
7575
static mutex_t irq_wqueue_lock = NXMUTEX_INITIALIZER;
7676
static FAR struct kwork_wqueue_s *irq_wqueue[CONFIG_IRQ_NWORKS];
7777

78-
FAR struct kwork_wqueue_s *queue;
78+
FAR struct kwork_wqueue_s *queue = NULL;
7979
int wqueue_priority;
8080
int i;
8181

@@ -89,17 +89,22 @@ inline_function FAR struct kwork_wqueue_s *irq_get_wqueue(int priority)
8989
if (wqueue_priority == priority)
9090
{
9191
nxmutex_unlock(&irq_wqueue_lock);
92-
return irq_wqueue[i];
92+
queue = irq_wqueue[i];
93+
break;
9394
}
9495
}
9596

9697
DEBUGASSERT(i < CONFIG_IRQ_NWORKS);
9798

98-
queue = work_queue_create("isrwork", priority, irq_work_stack[i],
99-
CONFIG_IRQ_WORK_STACKSIZE, 1);
99+
if (queue == NULL)
100+
{
101+
queue = work_queue_create("isrwork", priority, irq_work_stack[i],
102+
CONFIG_IRQ_WORK_STACKSIZE, 1);
103+
104+
irq_wqueue[i] = queue;
105+
nxmutex_unlock(&irq_wqueue_lock);
106+
}
100107

101-
irq_wqueue[i] = queue;
102-
nxmutex_unlock(&irq_wqueue_lock);
103108
return queue;
104109
}
105110

@@ -170,47 +175,48 @@ int irq_attach_wqueue(int irq, xcpt_t isr, xcpt_t isrwork,
170175
#endif
171176

172177
FAR struct irq_work_info_s *info;
173-
178+
int ret = OK;
174179
#if NR_IRQS > 0
175-
int ndx;
180+
int ndx = -EINVAL;
176181

177-
if (irq < 0 || irq >= NR_IRQS)
182+
if (irq >= 0 && irq < NR_IRQS)
178183
{
179-
return -EINVAL;
184+
ndx = IRQ_TO_NDX(irq);
180185
}
181186

182-
ndx = IRQ_TO_NDX(irq);
183187
if (ndx < 0)
184188
{
185-
return ndx;
189+
ret = ndx;
186190
}
187-
188-
/* If the isrwork is NULL, then the ISR is being detached. */
189-
190-
info = &irq_work_vector[ndx];
191-
192-
if (isrwork == NULL)
191+
else
193192
{
194-
irq_detach(irq);
195-
info->isrwork = NULL;
196-
info->handler = NULL;
197-
info->arg = NULL;
198-
info->wqueue = NULL;
199-
return OK;
200-
}
193+
/* If the isrwork is NULL, then the ISR is being detached. */
201194

202-
info->isrwork = isrwork;
203-
info->handler = isr;
204-
info->arg = arg;
205-
info->irq = irq;
206-
if (info->wqueue == NULL)
207-
{
208-
info->wqueue = irq_get_wqueue(priority);
209-
}
195+
info = &irq_work_vector[ndx];
210196

211-
irq_attach(irq, irq_default_handler, info);
197+
if (isrwork == NULL)
198+
{
199+
irq_detach(irq);
200+
info->isrwork = NULL;
201+
info->handler = NULL;
202+
info->arg = NULL;
203+
info->wqueue = NULL;
204+
}
205+
else
206+
{
207+
info->isrwork = isrwork;
208+
info->handler = isr;
209+
info->arg = arg;
210+
info->irq = irq;
211+
if (info->wqueue == NULL)
212+
{
213+
info->wqueue = irq_get_wqueue(priority);
214+
}
215+
216+
irq_attach(irq, irq_default_handler, info);
217+
}
218+
}
212219
#endif /* NR_IRQS */
213220

214-
return OK;
221+
return ret;
215222
}
216-

0 commit comments

Comments
 (0)