xref: /nrf52832-nimble/rt-thread/components/libc/pthreads/pthread_spin.c (revision 104654410c56c573564690304ae786df310c91fc)
1 /*
2  * Copyright (c) 2006-2018, RT-Thread Development Team
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  *
6  * Change Logs:
7  * Date           Author       Notes
8  * 2010-10-26     Bernard      the first version
9  */
10 
11 #include <pthread.h>
12 
pthread_spin_init(pthread_spinlock_t * lock,int pshared)13 int pthread_spin_init (pthread_spinlock_t *lock, int pshared)
14 {
15     if (!lock)
16         return EINVAL;
17 
18     lock->lock = 0;
19 
20     return 0;
21 }
22 
pthread_spin_destroy(pthread_spinlock_t * lock)23 int pthread_spin_destroy (pthread_spinlock_t *lock)
24 {
25     if (!lock)
26         return EINVAL;
27 
28     return 0;
29 }
30 
pthread_spin_lock(pthread_spinlock_t * lock)31 int pthread_spin_lock (pthread_spinlock_t *lock)
32 {
33     if (!lock)
34         return EINVAL;
35 
36     while (!(lock->lock))
37     {
38         lock->lock = 1;
39     }
40 
41     return 0;
42 }
43 
pthread_spin_trylock(pthread_spinlock_t * lock)44 int pthread_spin_trylock (pthread_spinlock_t *lock)
45 {
46     if (!lock)
47         return EINVAL;
48 
49     if (!(lock->lock))
50     {
51         lock->lock = 1;
52 
53         return 0;
54     }
55 
56     return EBUSY;
57 }
58 
pthread_spin_unlock(pthread_spinlock_t * lock)59 int pthread_spin_unlock (pthread_spinlock_t *lock)
60 {
61     if (!lock)
62         return EINVAL;
63     if (!(lock->lock))
64         return EPERM;
65 
66     lock->lock = 0;
67 
68     return 0;
69 }
70