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

Author: Philippe Gerum <r...@xenomai.org>
Date:   Mon Sep  1 20:48:56 2014 +0200

cobalt/posix/sem, lib/cobalt: make cobalt_sem 32/64bit-neutral

---

 include/cobalt/uapi/sem.h |    8 +--
 kernel/cobalt/posix/sem.c |   24 +++----
 kernel/cobalt/posix/sem.h |    2 +-
 lib/cobalt/semaphore.c    |  175 +++++++++++++++++++++------------------------
 4 files changed, 100 insertions(+), 109 deletions(-)

diff --git a/include/cobalt/uapi/sem.h b/include/cobalt/uapi/sem.h
index 8becd88..c1700b1 100644
--- a/include/cobalt/uapi/sem.h
+++ b/include/cobalt/uapi/sem.h
@@ -26,15 +26,15 @@
 struct cobalt_sem;
 
 struct sem_dat {
-       atomic_long_t value;
-       unsigned long flags;
+       atomic_t value;
+       __u32 flags;
 };
 
 union cobalt_sem_union {
        sem_t native_sem;
        struct cobalt_sem_shadow {
-               unsigned int magic;
-               int datp_offset;
+               __u32 magic;
+               __s32 datp_offset;
                xnhandle_t handle;
        } shadow_sem;
 };
diff --git a/kernel/cobalt/posix/sem.c b/kernel/cobalt/posix/sem.c
index 16fb3ac..f01f843 100644
--- a/kernel/cobalt/posix/sem.c
+++ b/kernel/cobalt/posix/sem.c
@@ -143,7 +143,7 @@ __cobalt_sem_init(const char *name, struct 
cobalt_sem_shadow *sm,
        xnsynch_init(&sem->synchbase, sflags, NULL);
 
        sem->datp = datp;
-       atomic_long_set(&datp->value, value);
+       atomic_set(&datp->value, value);
        datp->flags = flags;
        sem->flags = flags;
        sem->owningq = kq;
@@ -225,7 +225,7 @@ static inline int sem_trywait_inner(struct cobalt_sem *sem)
                return -EPERM;
 #endif
 
-       if (atomic_long_sub_return(1, &sem->datp->value) < 0)
+       if (atomic_sub_return(1, &sem->datp->value) < 0)
                return -EAGAIN;
 
        return 0;
@@ -270,7 +270,7 @@ redo:
                 * applications ported to Linux happy.
                 */
                if (pull_ts) {
-                       atomic_long_inc(&sem->datp->value);
+                       atomic_inc(&sem->datp->value);
                        if (u_ts == NULL)
                                goto efault;
                        xnlock_put_irqrestore(&nklock, s);
@@ -301,7 +301,7 @@ out:
 
        return ret;
 fail:
-       atomic_long_inc(&sem->datp->value);
+       atomic_inc(&sem->datp->value);
        goto out;
 efault:
        ret = -EFAULT;
@@ -332,18 +332,18 @@ int sem_post_inner(struct cobalt_sem *sem, struct 
cobalt_kqueues *ownq, int bcas
                return -EPERM;
 #endif
 
-       if (atomic_long_read(&sem->datp->value) == SEM_VALUE_MAX)
+       if (atomic_read(&sem->datp->value) == SEM_VALUE_MAX)
                return -EINVAL;
 
        if (!bcast) {
-               if (atomic_long_inc_return(&sem->datp->value) <= 0) {
+               if (atomic_inc_return(&sem->datp->value) <= 0) {
                        if (xnsynch_wakeup_one_sleeper(&sem->synchbase))
                                xnsched_run();
                } else if (sem->flags & SEM_PULSE)
-                       atomic_long_set(&sem->datp->value, 0);
+                       atomic_set(&sem->datp->value, 0);
        } else {
-               if (atomic_long_read(&sem->datp->value) < 0) {
-                       atomic_long_set(&sem->datp->value, 0);
+               if (atomic_read(&sem->datp->value) < 0) {
+                       atomic_set(&sem->datp->value, 0);
                        if (xnsynch_flush(&sem->synchbase, 0) ==
                                XNSYNCH_RESCHED)
                                xnsched_run();
@@ -386,7 +386,7 @@ static int sem_getvalue(xnhandle_t handle, int *value)
                return -EPERM;
        }
 
-       *value = atomic_long_read(&sem->datp->value);
+       *value = atomic_read(&sem->datp->value);
        if ((sem->flags & SEM_REPORT) == 0 && *value < 0)
                *value = 0;
 
@@ -539,7 +539,7 @@ int cobalt_sem_inquire(struct cobalt_sem_shadow __user 
*u_sem,
                 * holding any lock, then revalidate the handle.
                 */
                if (t == NULL) {
-                       val = atomic_long_read(&sem->datp->value);
+                       val = atomic_read(&sem->datp->value);
                        if (val >= 0 || u_waitlist == NULL)
                                break;
                        xnlock_put_irqrestore(&nklock, s);
@@ -555,7 +555,7 @@ int cobalt_sem_inquire(struct cobalt_sem_shadow __user 
*u_sem,
                        xnlock_get_irqsave(&nklock, s);
                } else if (pstamp == nstamp)
                        break;
-               else if (val != atomic_long_read(&sem->datp->value)) {
+               else if (val != atomic_read(&sem->datp->value)) {
                        xnlock_put_irqrestore(&nklock, s);
                        if (t != fbuf)
                                xnfree(t);
diff --git a/kernel/cobalt/posix/sem.h b/kernel/cobalt/posix/sem.h
index 1dc3a94..a5bbdaf 100644
--- a/kernel/cobalt/posix/sem.h
+++ b/kernel/cobalt/posix/sem.h
@@ -34,7 +34,7 @@ struct cobalt_sem {
        int flags;
        struct cobalt_kqueues *owningq;
        xnhandle_t handle;
-       unsigned refs;
+       unsigned int refs;
        char name[COBALT_MAXNAME];
 };
 
diff --git a/lib/cobalt/semaphore.c b/lib/cobalt/semaphore.c
index 4197318..c685476 100644
--- a/lib/cobalt/semaphore.c
+++ b/lib/cobalt/semaphore.c
@@ -44,7 +44,7 @@
 
 static inline struct sem_dat *sem_get_datp(struct cobalt_sem_shadow *shadow)
 {
-       unsigned pshared = shadow->datp_offset < 0;
+       unsigned int pshared = shadow->datp_offset < 0;
 
        if (pshared)
                return (struct sem_dat *)
@@ -87,10 +87,10 @@ COBALT_IMPL(int, sem_init, (sem_t *sem, int pshared, 
unsigned int value))
        struct cobalt_sem_shadow *_sem = &((union cobalt_sem_union 
*)sem)->shadow_sem;
        int ret;
 
-       ret = -XENOMAI_SYSCALL3(sc_cobalt_sem_init,
-                               _sem, pshared ? SEM_PSHARED : 0, value);
+       ret = XENOMAI_SYSCALL3(sc_cobalt_sem_init,
+                              _sem, pshared ? SEM_PSHARED : 0, value);
        if (ret) {
-               errno = ret;
+               errno = -ret;
                return -1;
        }
 
@@ -135,7 +135,7 @@ COBALT_IMPL(int, sem_init, (sem_t *sem, int pshared, 
unsigned int value))
 COBALT_IMPL(int, sem_destroy, (sem_t *sem))
 {
        struct cobalt_sem_shadow *_sem = &((union cobalt_sem_union 
*)sem)->shadow_sem;
-       int err;
+       int ret;
 
        if (_sem->magic != COBALT_SEM_MAGIC
            && _sem->magic != COBALT_NAMED_SEM_MAGIC) {
@@ -143,12 +143,13 @@ COBALT_IMPL(int, sem_destroy, (sem_t *sem))
                return -1;
        }
 
-       err = XENOMAI_SYSCALL1(sc_cobalt_sem_destroy, _sem);
-       if (err >= 0)
-               return err;
+       ret = XENOMAI_SYSCALL1(sc_cobalt_sem_destroy, _sem);
+       if (ret < 0) {
+               errno = -ret;
+               return -1;
+       }
 
-       errno = -err;
-       return -1;
+       return ret;
 }
 
 /**
@@ -178,9 +179,8 @@ COBALT_IMPL(int, sem_destroy, (sem_t *sem))
 COBALT_IMPL(int, sem_post, (sem_t *sem))
 {
        struct cobalt_sem_shadow *_sem = &((union cobalt_sem_union 
*)sem)->shadow_sem;
+       int value, ret, old, new;
        struct sem_dat *datp;
-       long value;
-       int err;
 
        if (_sem->magic != COBALT_SEM_MAGIC
            && _sem->magic != COBALT_NAMED_SEM_MAGIC) {
@@ -189,19 +189,13 @@ COBALT_IMPL(int, sem_post, (sem_t *sem))
        }
 
        datp = sem_get_datp(_sem);
-
-       value = atomic_long_read(&datp->value);
-
+       value = atomic_read(&datp->value);
        if (value >= 0) {
-               long old, new;
-
                if (datp->flags & SEM_PULSE)
                        return 0;
-
                do {
                        old = value;
                        new = value + 1;
-
                        value = atomic_cmpxchg(&datp->value, old, new);
                        if (value < 0)
                                goto do_syscall;
@@ -211,12 +205,13 @@ COBALT_IMPL(int, sem_post, (sem_t *sem))
        }
 
   do_syscall:
-       err = -XENOMAI_SYSCALL1(sc_cobalt_sem_post, _sem);
-       if (!err)
-               return 0;
+       ret = XENOMAI_SYSCALL1(sc_cobalt_sem_post, _sem);
+       if (ret) {
+               errno = -ret;
+               return -1;
+       }
 
-       errno = err;
-       return -1;
+       return 0;
 }
 
 /**
@@ -244,7 +239,7 @@ COBALT_IMPL(int, sem_trywait, (sem_t *sem))
 {
        struct cobalt_sem_shadow *_sem = &((union cobalt_sem_union 
*)sem)->shadow_sem;
        struct sem_dat *datp;
-       long value;
+       int value, old, new;
 
        if (_sem->magic != COBALT_SEM_MAGIC
            && _sem->magic != COBALT_NAMED_SEM_MAGIC) {
@@ -253,16 +248,11 @@ COBALT_IMPL(int, sem_trywait, (sem_t *sem))
        }
 
        datp = sem_get_datp(_sem);
-
-       value = atomic_long_read(&datp->value);
-
+       value = atomic_read(&datp->value);
        if (value > 0) {
-               long old, new;
-
                do {
                        old = value;
                        new = value - 1;
-
                        value = atomic_cmpxchg(&datp->value, old, new);
                        if (value <= 0)
                                goto eagain;
@@ -270,9 +260,9 @@ COBALT_IMPL(int, sem_trywait, (sem_t *sem))
 
                return 0;
        }
-
-  eagain:
+eagain:
        errno = EAGAIN;
+
        return -1;
 }
 
@@ -308,24 +298,24 @@ COBALT_IMPL(int, sem_trywait, (sem_t *sem))
 COBALT_IMPL(int, sem_wait, (sem_t *sem))
 {
        struct cobalt_sem_shadow *_sem = &((union cobalt_sem_union 
*)sem)->shadow_sem;
-       int err, oldtype;
-
-       err = __RT(sem_trywait(sem));
+       int ret, oldtype;
 
-       if (err != -1 || errno != EAGAIN)
-               return err;
+       ret = __RT(sem_trywait(sem));
+       if (ret != -1 || errno != EAGAIN)
+               return ret;
 
        pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, &oldtype);
 
-       err = -XENOMAI_SYSCALL1(sc_cobalt_sem_wait, _sem);
+       ret = XENOMAI_SYSCALL1(sc_cobalt_sem_wait, _sem);
 
        pthread_setcanceltype(oldtype, NULL);
 
-       if (err == 0)
-               return 0;
+       if (ret) {
+               errno = -ret;
+               return -1;
+       }
 
-       errno = err;
-       return -1;
+       return 0;
 }
 
 /**
@@ -361,24 +351,24 @@ COBALT_IMPL(int, sem_wait, (sem_t *sem))
 COBALT_IMPL(int, sem_timedwait, (sem_t *sem, const struct timespec 
*abs_timeout))
 {
        struct cobalt_sem_shadow *_sem = &((union cobalt_sem_union 
*)sem)->shadow_sem;
-       int err, oldtype;
+       int ret, oldtype;
 
-       err = __RT(sem_trywait(sem));
-
-       if (err != -1 || errno != EAGAIN)
-               return err;
+       ret = __RT(sem_trywait(sem));
+       if (ret != -1 || errno != EAGAIN)
+               return ret;
 
        pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, &oldtype);
 
-       err = -XENOMAI_SYSCALL2(sc_cobalt_sem_timedwait, _sem, abs_timeout);
+       ret = XENOMAI_SYSCALL2(sc_cobalt_sem_timedwait, _sem, abs_timeout);
 
        pthread_setcanceltype(oldtype, NULL);
 
-       if (!err)
-               return 0;
+       if (ret) {
+               errno = -ret;
+               return -1;
+       }
 
-       errno = err;
-       return -1;
+       return 0;
 }
 
 /**
@@ -413,7 +403,7 @@ COBALT_IMPL(int, sem_getvalue, (sem_t *sem, int *sval))
 {
        struct cobalt_sem_shadow *_sem = &((union cobalt_sem_union 
*)sem)->shadow_sem;
        struct sem_dat *datp;
-       long value;
+       int value;
 
        if (_sem->magic != COBALT_SEM_MAGIC
            && _sem->magic != COBALT_NAMED_SEM_MAGIC) {
@@ -422,12 +412,12 @@ COBALT_IMPL(int, sem_getvalue, (sem_t *sem, int *sval))
        }
 
        datp = sem_get_datp(_sem);
-
-       value = atomic_long_read(&datp->value);
+       value = atomic_read(&datp->value);
        if (value < 0 && (datp->flags & SEM_REPORT) == 0)
                value = 0;
 
        *sval = value;
+
        return 0;
 }
 
@@ -482,31 +472,31 @@ COBALT_IMPL(sem_t *, sem_open, (const char *name, int 
oflags, ...))
        va_list ap;
        int err;
 
-       if ((oflags & O_CREAT)) {
+       if (oflags & O_CREAT) {
                va_start(ap, oflags);
                mode = va_arg(ap, int);
                value = va_arg(ap, unsigned);
                va_end(ap);
        }
 
-       rsem = sem = (union cobalt_sem_union *)malloc(sizeof(*sem));
-
-       if (!rsem) {
-               err = ENOSPC;
+       rsem = sem = malloc(sizeof(*sem));
+       if (rsem == NULL) {
+               err = -ENOSPC;
                goto error;
        }
 
-       err = -XENOMAI_SYSCALL5(sc_cobalt_sem_open,
-                               &rsem, name, oflags, mode, value);
-       if (!err) {
+       err = XENOMAI_SYSCALL5(sc_cobalt_sem_open,
+                              &rsem, name, oflags, mode, value);
+       if (err == 0) {
                if (rsem != sem)
                        free(sem);
                return &rsem->native_sem;
        }
 
        free(sem);
-      error:
-       errno = err;
+error:
+       errno = -err;
+
        return SEM_FAILED;
 }
 
@@ -538,19 +528,19 @@ COBALT_IMPL(sem_t *, sem_open, (const char *name, int 
oflags, ...))
 COBALT_IMPL(int, sem_close, (sem_t *sem))
 {
        struct cobalt_sem_shadow *_sem = &((union cobalt_sem_union 
*)sem)->shadow_sem;
-       int err;
+       int ret;
 
        if (_sem->magic != COBALT_NAMED_SEM_MAGIC) {
                errno = EINVAL;
                return -1;
        }
 
-       err = XENOMAI_SYSCALL1(sc_cobalt_sem_close, _sem);
-       if (err < 0) {
-               errno = -err;
+       ret = XENOMAI_SYSCALL1(sc_cobalt_sem_close, _sem);
+       if (ret < 0) {
+               errno = -ret;
                return -1;
        }
-       if (err)
+       if (ret)
                free(sem);
 
        return 0;
@@ -582,35 +572,36 @@ COBALT_IMPL(int, sem_close, (sem_t *sem))
  */
 COBALT_IMPL(int, sem_unlink, (const char *name))
 {
-       int err;
+       int ret;
 
-       err = -XENOMAI_SYSCALL1(sc_cobalt_sem_unlink, name);
-       if (!err)
-               return 0;
+       ret = XENOMAI_SYSCALL1(sc_cobalt_sem_unlink, name);
+       if (ret) {
+               errno = -ret;
+               return -1;
+       }
 
-       errno = err;
-       return -1;
+       return 0;
 }
 
 int sem_init_np(sem_t *sem, int flags, unsigned int value)
 {
        struct cobalt_sem_shadow *_sem = &((union cobalt_sem_union 
*)sem)->shadow_sem;
-       int err;
+       int ret;
 
-       err = -XENOMAI_SYSCALL3(sc_cobalt_sem_init, _sem, flags, value);
-       if (!err)
-               return 0;
+       ret = XENOMAI_SYSCALL3(sc_cobalt_sem_init, _sem, flags, value);
+       if (ret) {
+               errno = -ret;
+               return -1;
+       }
 
-       errno = err;
-       return -1;
+       return 0;
 }
 
 int sem_broadcast_np(sem_t *sem)
 {
        struct cobalt_sem_shadow *_sem = &((union cobalt_sem_union 
*)sem)->shadow_sem;
        struct sem_dat *datp;
-       long value;
-       int err;
+       int value, ret;
 
        if (_sem->magic != COBALT_SEM_MAGIC
            && _sem->magic != COBALT_NAMED_SEM_MAGIC) {
@@ -619,17 +610,17 @@ int sem_broadcast_np(sem_t *sem)
        }
 
        datp = sem_get_datp(_sem);
-
-       value = atomic_long_read(&datp->value);
+       value = atomic_read(&datp->value);
        if (value >= 0)
                return 0;
 
-       err = -XENOMAI_SYSCALL1(sc_cobalt_sem_broadcast_np, _sem);
-       if (!err)
-               return 0;
+       ret = XENOMAI_SYSCALL1(sc_cobalt_sem_broadcast_np, _sem);
+       if (ret) {
+               errno = -ret;
+               return -1;
+       }
 
-       errno = err;
-       return -1;
+       return 0;
 }
 
 /** @} */


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

Reply via email to