1
0
mirror of https://github.com/RIOT-OS/RIOT.git synced 2024-12-29 04:50:03 +01:00

sys/posix/pthread: Use C11 atomic instead of atomic.h

This commit is contained in:
Joakim Nohlgård 2016-07-23 17:21:20 +02:00
parent ee0cae69a5
commit 597db0358d
2 changed files with 9 additions and 9 deletions

View File

@ -20,7 +20,7 @@
#ifndef SYS_POSIX_PTHREAD_SPIN_H
#define SYS_POSIX_PTHREAD_SPIN_H
#include <errno.h>
#include <stdatomic.h>
#ifdef __cplusplus
extern "C" {
@ -33,7 +33,7 @@ extern "C" {
* Use irq_disable() and irq_restore() for shortterm locks instead.
*/
typedef struct {
atomic_int_t value;
atomic_flag flag;
} pthread_spinlock_t;
/**

View File

@ -17,8 +17,10 @@
* @}
*/
#include <stdint.h>
#include <stdatomic.h>
#include <errno.h>
#include "pthread.h"
#include "atomic.h"
int pthread_spin_init(pthread_spinlock_t *lock, int pshared)
{
@ -27,7 +29,7 @@ int pthread_spin_init(pthread_spinlock_t *lock, int pshared)
}
(void) pshared;
ATOMIC_VALUE(lock->value) = 0;
atomic_flag_clear(&(lock->flag));
return 0;
}
@ -47,7 +49,7 @@ int pthread_spin_lock(pthread_spinlock_t *lock)
return EINVAL;
}
while (atomic_set_to_one(&(lock->value)) == 0) {
while (atomic_flag_test_and_set(&(lock->flag))) {
/* spin */
}
@ -60,7 +62,7 @@ int pthread_spin_trylock(pthread_spinlock_t *lock)
return EINVAL;
}
if (atomic_set_to_one(&(lock->value)) == 0) {
if (atomic_flag_test_and_set(&(lock->flag))) {
return EBUSY;
}
@ -73,9 +75,7 @@ int pthread_spin_unlock(pthread_spinlock_t *lock)
return EINVAL;
}
if (atomic_set_to_zero(&(lock->value)) == 0) {
return EPERM;
}
atomic_flag_clear(&(lock->flag));
return 0;
}