Module: xenomai-forge
Branch: master
Commit: 057a28c190cc9d94eb9aa3c732d83abe7d0688aa
URL:    
http://git.xenomai.org/?p=xenomai-forge.git;a=commit;h=057a28c190cc9d94eb9aa3c732d83abe7d0688aa

Author: Philippe Gerum <r...@xenomai.org>
Date:   Sun Sep  1 15:53:19 2013 +0200

lib/cobalt: provide primary mode replacement for sleep(3)

---

 include/cobalt/unistd.h    |    2 +
 lib/cobalt/clock.c         |   73 +++++++++++++++++++++++++++++---------------
 lib/cobalt/cobalt.wrappers |    1 +
 lib/cobalt/signal.c        |    2 +-
 lib/cobalt/wrappers.c      |    6 +++
 5 files changed, 58 insertions(+), 26 deletions(-)

diff --git a/include/cobalt/unistd.h b/include/cobalt/unistd.h
index b689ffa..e603ae7 100644
--- a/include/cobalt/unistd.h
+++ b/include/cobalt/unistd.h
@@ -32,6 +32,8 @@ COBALT_DECL(ssize_t, write(int fd, const void *buf, size_t 
nbyte));
 
 COBALT_DECL(int, close(int fildes));
 
+COBALT_DECL(unsigned int, sleep(unsigned int seconds));
+
 #ifdef __cplusplus
 }
 #endif
diff --git a/lib/cobalt/clock.c b/lib/cobalt/clock.c
index 680a622..4eed916 100644
--- a/lib/cobalt/clock.c
+++ b/lib/cobalt/clock.c
@@ -21,7 +21,8 @@
 #include <stdlib.h>
 #include <string.h>
 #include <errno.h>
-#include <pthread.h>           /* For pthread_setcanceltype. */
+#include <pthread.h>
+#include <unistd.h>
 #include <time.h>
 #include <cobalt/uapi/time.h>
 #include <cobalt/ticks.h>
@@ -32,15 +33,17 @@
 
 COBALT_IMPL(int, clock_getres, (clockid_t clock_id, struct timespec *tp))
 {
-       int err = -XENOMAI_SKINCALL2(__cobalt_muxid,
-                                    sc_cobalt_clock_getres,
-                                    clock_id,
-                                    tp);
-       if (!err)
-               return 0;
+       int ret;
+
+       ret = -XENOMAI_SKINCALL2(__cobalt_muxid,
+                                sc_cobalt_clock_getres,
+                                clock_id, tp);
+       if (ret) {
+               errno = ret;
+               return -1;
+       }
 
-       errno = err;
-       return -1;
+       return 0;
 }
 
 static int __do_clock_host_realtime(struct timespec *ts)
@@ -120,42 +123,62 @@ COBALT_IMPL(int, clock_gettime, (clockid_t clock_id, 
struct timespec *tp))
 
 COBALT_IMPL(int, clock_settime, (clockid_t clock_id, const struct timespec 
*tp))
 {
-       int err = -XENOMAI_SKINCALL2(__cobalt_muxid,
-                                    sc_cobalt_clock_settime,
-                                    clock_id,
-                                    tp);
+       int ret;
 
-       if (!err)
-               return 0;
+       ret = -XENOMAI_SKINCALL2(__cobalt_muxid,
+                                sc_cobalt_clock_settime,
+                                clock_id, tp);
+       if (ret) {
+               errno = ret;
+               return -1;
+       }
 
-       errno = err;
-       return -1;
+       return 0;
 }
 
 COBALT_IMPL(int, clock_nanosleep, (clockid_t clock_id,
                                   int flags,
                                   const struct timespec *rqtp, struct timespec 
*rmtp))
 {
-       int err, oldtype;
+       int ret, oldtype;
 
        pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, &oldtype);
 
-       err = -XENOMAI_SKINCALL4(__cobalt_muxid,
+       ret = -XENOMAI_SKINCALL4(__cobalt_muxid,
                                 sc_cobalt_clock_nanosleep,
                                 clock_id, flags, rqtp, rmtp);
 
        pthread_setcanceltype(oldtype, NULL);
 
-       return err;
+       return ret;
 }
 
 COBALT_IMPL(int, nanosleep, (const struct timespec *rqtp, struct timespec 
*rmtp))
 {
-       int err = __wrap_clock_nanosleep(CLOCK_REALTIME, 0, rqtp, rmtp);
+       int ret;
 
-       if (!err)
-               return 0;
+       ret = __wrap_clock_nanosleep(CLOCK_REALTIME, 0, rqtp, rmtp);
+       if (ret) {
+               errno = ret;
+               return -1;
+       }
 
-       errno = err;
-       return -1;
+       return 0;
+}
+
+COBALT_IMPL(unsigned int, sleep, (unsigned int seconds))
+{
+       struct timespec rqt, rem;
+       int ret;
+
+       if (cobalt_get_current_fast() == XN_NO_HANDLE)
+               return __STD(sleep(seconds));
+
+       rqt.tv_sec = seconds;
+       rqt.tv_nsec = 0;
+       ret = __wrap_clock_nanosleep(CLOCK_MONOTONIC, 0, &rqt, &rem);
+       if (ret)
+               return rem.tv_sec;
+
+       return 0;
 }
diff --git a/lib/cobalt/cobalt.wrappers b/lib/cobalt/cobalt.wrappers
index 570d04e..5a16583 100644
--- a/lib/cobalt/cobalt.wrappers
+++ b/lib/cobalt/cobalt.wrappers
@@ -109,3 +109,4 @@
 --wrap sigtimedwait
 --wrap sigpending
 --wrap kill
+--wrap sleep
diff --git a/lib/cobalt/signal.c b/lib/cobalt/signal.c
index 4bc6bce..8ff9090 100644
--- a/lib/cobalt/signal.c
+++ b/lib/cobalt/signal.c
@@ -83,7 +83,7 @@ COBALT_IMPL(int, kill, (pid_t pid, int sig))
         * kernel. We only deal with thread-directed signals.
         */
        if (pid <= 0)
-               return __real_kill(pid, sig);
+               return __STD(kill(pid, sig));
 
        ret = XENOMAI_SKINCALL2(__cobalt_muxid,
                                sc_cobalt_kill, pid, sig);
diff --git a/lib/cobalt/wrappers.c b/lib/cobalt/wrappers.c
index 6620e0b..6247915 100644
--- a/lib/cobalt/wrappers.c
+++ b/lib/cobalt/wrappers.c
@@ -432,6 +432,12 @@ int __real_kill(pid_t pid, int sig)
        return kill(pid, sig);
 }
 
+__attribute__ ((weak))
+unsigned int __real_sleep(unsigned int seconds)
+{
+       return sleep(seconds);
+}
+
 #ifdef __PROVIDE_CPU_COUNT
 
 int __sched_cpucount(size_t setsize, const cpu_set_t *setp)


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

Reply via email to