rt-thread/components/libc/posix/pthreads/pthread_spin.c

70 lines
1020 B
C
Raw Normal View History

2013-06-26 23:18:30 +08:00
/*
2021-03-08 18:19:04 +08:00
* Copyright (c) 2006-2021, RT-Thread Development Team
2013-06-26 23:18:30 +08:00
*
2018-10-14 19:28:18 +08:00
* SPDX-License-Identifier: Apache-2.0
2013-06-26 23:18:30 +08:00
*
* Change Logs:
* Date Author Notes
* 2010-10-26 Bernard the first version
*/
2013-01-08 22:40:58 +08:00
#include <pthread.h>
int pthread_spin_init (pthread_spinlock_t *lock, int pshared)
{
2013-06-26 23:18:30 +08:00
if (!lock)
return EINVAL;
lock->lock = 0;
2013-01-08 22:40:58 +08:00
2013-06-26 23:18:30 +08:00
return 0;
2013-01-08 22:40:58 +08:00
}
int pthread_spin_destroy (pthread_spinlock_t *lock)
{
2013-06-26 23:18:30 +08:00
if (!lock)
return EINVAL;
2013-01-08 22:40:58 +08:00
2013-06-26 23:18:30 +08:00
return 0;
2013-01-08 22:40:58 +08:00
}
int pthread_spin_lock (pthread_spinlock_t *lock)
{
2013-06-26 23:18:30 +08:00
if (!lock)
return EINVAL;
2013-01-08 22:40:58 +08:00
2013-06-26 23:18:30 +08:00
while (!(lock->lock))
{
lock->lock = 1;
}
2013-01-08 22:40:58 +08:00
2013-06-26 23:18:30 +08:00
return 0;
2013-01-08 22:40:58 +08:00
}
int pthread_spin_trylock (pthread_spinlock_t *lock)
{
2013-06-26 23:18:30 +08:00
if (!lock)
return EINVAL;
if (!(lock->lock))
{
lock->lock = 1;
2013-01-08 22:40:58 +08:00
2013-06-26 23:18:30 +08:00
return 0;
}
2013-01-08 22:40:58 +08:00
2013-06-26 23:18:30 +08:00
return EBUSY;
2013-01-08 22:40:58 +08:00
}
int pthread_spin_unlock (pthread_spinlock_t *lock)
{
2013-06-26 23:18:30 +08:00
if (!lock)
return EINVAL;
if (!(lock->lock))
return EPERM;
2013-01-08 22:40:58 +08:00
2013-06-26 23:18:30 +08:00
lock->lock = 0;
2013-01-08 22:40:58 +08:00
2013-06-26 23:18:30 +08:00
return 0;
2013-01-08 22:40:58 +08:00
}