Module: xenomai-2.6
Branch: master
Commit: 62b960b9b297e463dec6c2b8aa8cc7936ffe5035
URL:    
http://git.xenomai.org/?p=xenomai-2.6.git;a=commit;h=62b960b9b297e463dec6c2b8aa8cc7936ffe5035

Author: Gilles Chanteperdrix <gilles.chanteperd...@xenomai.org>
Date:   Fri Jun 17 11:57:59 2016 +0200

posix/once: cosmetic fixes

---

 include/posix/pthread.h |    2 +-
 ksrc/skins/posix/once.c |   28 +++++++++++++++++++---------
 2 files changed, 20 insertions(+), 10 deletions(-)

diff --git a/include/posix/pthread.h b/include/posix/pthread.h
index e90e94e..cbba26c 100644
--- a/include/posix/pthread.h
+++ b/include/posix/pthread.h
@@ -98,7 +98,7 @@ typedef struct pse51_key *pthread_key_t;
 
 typedef struct pse51_once {
        unsigned magic;
-       int routine_called;
+       int init_step;
 } pthread_once_t;
 
 #ifdef __KERNEL__
diff --git a/ksrc/skins/posix/once.c b/ksrc/skins/posix/once.c
index ca8c2dd..30de828 100644
--- a/ksrc/skins/posix/once.c
+++ b/ksrc/skins/posix/once.c
@@ -50,12 +50,18 @@ static pthread_cond_t cond;
  *
  */
 
-static void once_cleanup(void *cookie)
+enum init_step {
+       UNINIT = 0,
+       INIT_STARTED,
+       INIT_DONE,
+};
+
+static void once_rollback(void *cookie)
 {
        pthread_once_t *once = cookie;
 
        pthread_mutex_lock(&mutex);
-       once->routine_called = 0;
+       once->init_step = UNINIT;
        pthread_cond_broadcast(&cond);
        pthread_mutex_unlock(&mutex);
 }
@@ -64,6 +70,10 @@ int pthread_once(pthread_once_t *once, void 
(*init_routine)(void))
 {
        int err;
 
+       if (pse51_obj_active(once, PSE51_ONCE_MAGIC, pthread_once_t) &&
+               once->init_step == INIT_DONE)
+               return 0;
+
        err = pthread_mutex_lock(&mutex);
        if (err)
                return err;
@@ -73,22 +83,22 @@ int pthread_once(pthread_once_t *once, void 
(*init_routine)(void))
                goto out;
        }
 
-       while (once->routine_called != 2)
-               switch (once->routine_called) {
-               case 0:
-                       once->routine_called = 1;
+       while (once->init_step != INIT_DONE)
+               switch (once->init_step) {
+               case UNINIT:
+                       once->init_step = INIT_STARTED;
                        pthread_mutex_unlock(&mutex);
 
-                       pthread_cleanup_push(once_cleanup, once);
+                       pthread_cleanup_push(once_rollback, once);
                        init_routine();
                        pthread_cleanup_pop(0);
 
                        pthread_mutex_lock(&mutex);
-                       once->routine_called = 2;
+                       once->init_step = INIT_DONE;
                        pthread_cond_broadcast(&cond);
                        break;
 
-               case 1:
+               case INIT_STARTED:
                        err = pthread_cond_wait(&cond, &mutex);
                        if (err)
                                goto out;


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

Reply via email to