Module: xenomai-forge
Branch: next
Commit: 2e43d57c6a62d67e4e8905d1341bad8b134ad516
URL:    
http://git.xenomai.org/?p=xenomai-forge.git;a=commit;h=2e43d57c6a62d67e4e8905d1341bad8b134ad516

Author: Philippe Gerum <r...@xenomai.org>
Date:   Wed Jul 31 12:25:20 2013 +0200

cobalt/thread, posix: clearly distinguish exit vs finalization handlers

The exit handler is called locklessly with interrupts enabled, for
dropping all resources allocated by the personality, except the TCB
which may still be referenced for accessing resources maintained by
the Cobalt core.

The finalization handler shall drop the TCB and may therefore assume
that no further reference to this memory may be done afterwards from
the Cobalt core.

---

 include/cobalt/kernel/shadow.h |    4 ++--
 kernel/cobalt/pod.c            |    4 ++--
 kernel/cobalt/posix/syscall.c  |    2 +-
 kernel/cobalt/posix/thread.c   |   35 ++++++++++++++++-------------------
 kernel/cobalt/posix/thread.h   |    2 +-
 kernel/cobalt/shadow.c         |    6 +++---
 6 files changed, 25 insertions(+), 28 deletions(-)

diff --git a/include/cobalt/kernel/shadow.h b/include/cobalt/kernel/shadow.h
index 5afadf8..5f63d55 100644
--- a/include/cobalt/kernel/shadow.h
+++ b/include/cobalt/kernel/shadow.h
@@ -42,7 +42,7 @@ struct xnpersonality {
                void (*detach_process)(struct xnshadow_ppd *ppd);
                struct xnpersonality *(*map_thread)(struct xnthread *thread);
                struct xnpersonality *(*exit_thread)(struct xnthread *thread);
-               struct xnpersonality *(*unmap_thread)(struct xnthread *thread);
+               struct xnpersonality *(*finalize_thread)(struct xnthread 
*thread);
        } ops;
        struct module *module;
 };
@@ -89,7 +89,7 @@ int xnshadow_map_user(struct xnthread *thread,
 int xnshadow_map_kernel(struct xnthread *thread,
                        struct completion *done);
 
-void xnshadow_unmap(struct xnthread *thread);
+void xnshadow_finalize(struct xnthread *thread);
 
 int xnshadow_harden(void);
 
diff --git a/kernel/cobalt/pod.c b/kernel/cobalt/pod.c
index 89ba956..5ff2a9d 100644
--- a/kernel/cobalt/pod.c
+++ b/kernel/cobalt/pod.c
@@ -696,8 +696,8 @@ static void cleanup_thread(struct xnthread *thread) /* 
nklock held, irqs off */
 
        xnsched_forget(thread);
        xnthread_cleanup(thread);
-       /* Unmap last since this incurs releasing the TCB. */
-       xnshadow_unmap(thread);
+       /* Finalize last since this incurs releasing the TCB. */
+       xnshadow_finalize(thread);
 
        if (xnthread_test_state(sched->curr, XNROOT))
                xnfreesync();
diff --git a/kernel/cobalt/posix/syscall.c b/kernel/cobalt/posix/syscall.c
index 84f7c53..cf8c3a9 100644
--- a/kernel/cobalt/posix/syscall.c
+++ b/kernel/cobalt/posix/syscall.c
@@ -365,7 +365,7 @@ struct xnpersonality cobalt_personality = {
                .attach_process = cobalt_process_attach,
                .detach_process = cobalt_process_detach,
                .exit_thread = cobalt_thread_exit,
-               .unmap_thread = cobalt_thread_unmap,
+               .finalize_thread = cobalt_thread_finalize,
        },
 };
 EXPORT_SYMBOL_GPL(cobalt_personality);
diff --git a/kernel/cobalt/posix/thread.c b/kernel/cobalt/posix/thread.c
index bd6f3f1..7caf10f 100644
--- a/kernel/cobalt/posix/thread.c
+++ b/kernel/cobalt/posix/thread.c
@@ -182,14 +182,6 @@ thread_find_local(const struct cobalt_local_hkey *hkey)
        return thread;
 }
 
-static void thread_destroy(struct cobalt_thread *thread)
-{
-       list_del(&thread->link);
-       xnsynch_destroy(&thread->monitor_synch);
-       xnsynch_destroy(&thread->sigwait);
-       xnheap_schedule_free(&kheap, thread, &thread->link);
-}
-
 struct cobalt_thread *cobalt_thread_find(pid_t pid) /* nklocked, IRQs off */
 {
        struct global_thread_hash *gslot;
@@ -218,17 +210,20 @@ struct xnpersonality *cobalt_thread_exit(struct xnthread 
*curr)
        cobalt_mark_deleted(thread);
        cobalt_signal_flush(thread);
        cobalt_timer_flush(thread);
+       xnsynch_destroy(&thread->monitor_synch);
+       xnsynch_destroy(&thread->sigwait);
 
        /* We don't stack over any personality, no chaining. */
        return NULL;
 }
 
-struct xnpersonality *cobalt_thread_unmap(struct xnthread *zombie) /* 
nklocked, IRQs off */
+struct xnpersonality *cobalt_thread_finalize(struct xnthread *zombie) /* 
nklocked, IRQs off */
 {
        struct cobalt_thread *thread;
 
        thread = container_of(zombie, struct cobalt_thread, threadbase);
-       thread_destroy(thread);
+       list_del(&thread->link);
+       xnheap_schedule_free(&kheap, thread, &thread->link);
 
        return NULL;
 }
@@ -473,24 +468,26 @@ static inline int pthread_create(struct cobalt_thread 
**thread_p, const pthread_
        if (thread->attr.policy == SCHED_RR)
                xnpod_set_thread_tslice(&thread->threadbase, cobalt_time_slice);
 
-       xnlock_get_irqsave(&nklock, s);
-       thread->container = &cobalt_kqueues(0)->threadq;
-       list_add_tail(&thread->link, thread->container);
-       xnlock_put_irqrestore(&nklock, s);
-
-       thread->hkey.u_pth = 0;
-       thread->hkey.mm = NULL;
-
        /*
         * We need an anonymous registry entry to obtain a handle for
         * fast mutex locking.
         */
        ret = xnthread_register(&thread->threadbase, "");
        if (ret) {
-               thread_destroy(thread);
+               xnsynch_destroy(&thread->monitor_synch);
+               xnsynch_destroy(&thread->sigwait);
+               xnfree(thread);
                return ret;
        }
 
+       xnlock_get_irqsave(&nklock, s);
+       thread->container = &cobalt_kqueues(0)->threadq;
+       list_add_tail(&thread->link, thread->container);
+       xnlock_put_irqrestore(&nklock, s);
+
+       thread->hkey.u_pth = 0;
+       thread->hkey.mm = NULL;
+
        *thread_p = thread; /* Must be done before the thread is started. */
 
        return 0;
diff --git a/kernel/cobalt/posix/thread.h b/kernel/cobalt/posix/thread.h
index 5d868db..1ae2f11 100644
--- a/kernel/cobalt/posix/thread.h
+++ b/kernel/cobalt/posix/thread.h
@@ -192,7 +192,7 @@ int cobalt_sched_setconfig_np(int cpu,
 
 struct xnpersonality *cobalt_thread_exit(struct xnthread *curr);
 
-struct xnpersonality *cobalt_thread_unmap(struct xnthread *zombie);
+struct xnpersonality *cobalt_thread_finalize(struct xnthread *zombie);
 
 #ifdef CONFIG_XENO_OPT_COBALT_EXTENSION
 
diff --git a/kernel/cobalt/shadow.c b/kernel/cobalt/shadow.c
index 06512e6..11776ec 100644
--- a/kernel/cobalt/shadow.c
+++ b/kernel/cobalt/shadow.c
@@ -1021,7 +1021,7 @@ int xnshadow_map_kernel(struct xnthread *thread, struct 
completion *done)
 }
 EXPORT_SYMBOL_GPL(xnshadow_map_kernel);
 
-void xnshadow_unmap(struct xnthread *thread)
+void xnshadow_finalize(struct xnthread *thread)
 {
        struct xnsys_ppd *sys_ppd;
 
@@ -1029,7 +1029,7 @@ void xnshadow_unmap(struct xnthread *thread)
                   "thread %p thread_name %s pid %d",
                   thread, xnthread_name(thread), xnthread_host_pid(thread));
 
-       xnthread_run_handler(thread, unmap_thread);
+       xnthread_run_handler(thread, finalize_thread);
 
        xnthread_clear_state(thread, XNMAPPED);
 
@@ -2191,7 +2191,7 @@ static int handle_taskexit_event(struct task_struct *p) 
/* p == current */
        }
 
        /*
-        * __xnpod_cleanup_thread() -> ... -> xnshadow_unmap(). From
+        * __xnpod_cleanup_thread() -> ... -> xnshadow_finalize(). From
         * that point, the TCB is dropped. Be careful of not treading
         * on stale memory within @thread.
         */


_______________________________________________
Xenomai-git mailing list
Xenomai-git@xenomai.org
http://www.xenomai.org/mailman/listinfo/xenomai-git

Reply via email to