Skip to content

Commit a174881

Browse files
authored
Merge pull request #4209 from jesven/cleanup
cleanup操作改由当前线程退出前执行
2 parents 674ae1f + 5957e15 commit a174881

File tree

2 files changed

+50
-66
lines changed

2 files changed

+50
-66
lines changed

src/idle.c

Lines changed: 17 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -150,82 +150,37 @@ void rt_thread_idle_excute(void)
150150
{
151151
/* Loop until there is no dead thread. So one call to rt_thread_idle_excute
152152
* will do all the cleanups. */
153-
while (_has_defunct_thread())
153+
/* disable interrupt */
154+
155+
RT_DEBUG_NOT_IN_INTERRUPT;
156+
157+
#ifdef RT_USING_HEAP
158+
while (1)
154159
{
155160
rt_base_t lock;
156161
rt_thread_t thread;
157-
#ifdef RT_USING_MODULE
158-
struct rt_dlmodule *module = RT_NULL;
159-
#endif
160-
RT_DEBUG_NOT_IN_INTERRUPT;
161162

162-
/* disable interrupt */
163163
lock = rt_hw_interrupt_disable();
164164

165-
/* re-check whether list is empty */
166-
if (_has_defunct_thread())
167-
{
168-
/* get defunct thread */
169-
thread = rt_list_entry(rt_thread_defunct.next,
170-
struct rt_thread,
171-
tlist);
172-
#ifdef RT_USING_MODULE
173-
module = (struct rt_dlmodule*)thread->module_id;
174-
if (module)
175-
{
176-
dlmodule_destroy(module);
177-
}
178-
#endif
179-
/* remove defunct thread */
180-
rt_list_remove(&(thread->tlist));
181-
182-
/* lock scheduler to prevent scheduling in cleanup function. */
183-
rt_enter_critical();
184-
185-
/* invoke thread cleanup */
186-
if (thread->cleanup != RT_NULL)
187-
thread->cleanup(thread);
188-
189-
#ifdef RT_USING_SIGNALS
190-
rt_thread_free_sig(thread);
191-
#endif
192-
193-
/* if it's a system object, not delete it */
194-
if (rt_object_is_systemobject((rt_object_t)thread) == RT_TRUE)
195-
{
196-
/* detach this object */
197-
rt_object_detach((rt_object_t)thread);
198-
/* unlock scheduler */
199-
rt_exit_critical();
200-
201-
/* enable interrupt */
202-
rt_hw_interrupt_enable(lock);
203-
204-
return;
205-
}
206-
207-
/* unlock scheduler */
208-
rt_exit_critical();
209-
}
210-
else
165+
/* check whether list is empty */
166+
if (!_has_defunct_thread())
211167
{
212-
/* enable interrupt */
213168
rt_hw_interrupt_enable(lock);
214-
215-
/* may the defunct thread list is removed by others, just return */
216-
return;
169+
break;
217170
}
218-
219-
/* enable interrupt */
220-
rt_hw_interrupt_enable(lock);
221-
222-
#ifdef RT_USING_HEAP
171+
/* get defunct thread */
172+
thread = rt_list_entry(rt_thread_defunct.next,
173+
struct rt_thread,
174+
tlist);
175+
/* remove defunct thread */
176+
rt_list_remove(&(thread->tlist));
223177
/* release thread's stack */
224178
RT_KERNEL_FREE(thread->stack_addr);
225179
/* delete thread object */
226180
rt_object_delete((rt_object_t)thread);
227-
#endif
181+
rt_hw_interrupt_enable(lock);
228182
}
183+
#endif
229184
}
230185

231186
extern void rt_system_power_manager(void);

src/thread.c

Lines changed: 33 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,31 @@ void rt_thread_inited_sethook(void (*hook)(rt_thread_t thread))
7777

7878
#endif
7979

80+
/* must be invoke witch rt_hw_interrupt_disable */
81+
static void _thread_cleanup_execute(rt_thread_t thread)
82+
{
83+
register rt_base_t level;
84+
#ifdef RT_USING_MODULE
85+
struct rt_dlmodule *module = RT_NULL;
86+
#endif
87+
level = rt_hw_interrupt_disable();
88+
#ifdef RT_USING_MODULE
89+
module = (struct rt_dlmodule*)thread->module_id;
90+
if (module)
91+
{
92+
dlmodule_destroy(module);
93+
}
94+
#endif
95+
/* invoke thread cleanup */
96+
if (thread->cleanup != RT_NULL)
97+
thread->cleanup(thread);
98+
99+
#ifdef RT_USING_SIGNALS
100+
rt_thread_free_sig(thread);
101+
#endif
102+
rt_hw_interrupt_enable(level);
103+
}
104+
80105
void rt_thread_exit(void)
81106
{
82107
struct rt_thread *thread;
@@ -88,6 +113,8 @@ void rt_thread_exit(void)
88113
/* disable interrupt */
89114
level = rt_hw_interrupt_disable();
90115

116+
_thread_cleanup_execute(thread);
117+
91118
/* remove from schedule */
92119
rt_schedule_remove_thread(thread);
93120
/* change stat */
@@ -96,8 +123,7 @@ void rt_thread_exit(void)
96123
/* remove it from timer list */
97124
rt_timer_detach(&thread->thread_timer);
98125

99-
if ((rt_object_is_systemobject((rt_object_t)thread) == RT_TRUE) &&
100-
thread->cleanup == RT_NULL)
126+
if (rt_object_is_systemobject((rt_object_t)thread) == RT_TRUE)
101127
{
102128
rt_object_detach((rt_object_t)thread);
103129
}
@@ -347,14 +373,15 @@ rt_err_t rt_thread_detach(rt_thread_t thread)
347373
rt_schedule_remove_thread(thread);
348374
}
349375

376+
_thread_cleanup_execute(thread);
377+
350378
/* release thread timer */
351379
rt_timer_detach(&(thread->thread_timer));
352380

353381
/* change stat */
354382
thread->stat = RT_THREAD_CLOSE;
355383

356-
if ((rt_object_is_systemobject((rt_object_t)thread) == RT_TRUE) &&
357-
thread->cleanup == RT_NULL)
384+
if (rt_object_is_systemobject((rt_object_t)thread) == RT_TRUE)
358385
{
359386
rt_object_detach((rt_object_t)thread);
360387
}
@@ -449,6 +476,8 @@ rt_err_t rt_thread_delete(rt_thread_t thread)
449476
rt_schedule_remove_thread(thread);
450477
}
451478

479+
_thread_cleanup_execute(thread);
480+
452481
/* release thread timer */
453482
rt_timer_detach(&(thread->thread_timer));
454483

0 commit comments

Comments
 (0)