pthread_spin.c 1.7 KB
Newer Older
Y
yiyue.fang 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
/*
 * File      : pthread_spin.c
 * This file is part of RT-Thread RTOS
 * COPYRIGHT (C) 2006 - 2010, RT-Thread Development Team
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License along
 *  with this program; if not, write to the Free Software Foundation, Inc.,
 *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * Change Logs:
 * Date           Author       Notes
 * 2010-10-26     Bernard      the first version
 */

M
Ming, Bai 已提交
25 26 27 28
#include <pthread.h>

int pthread_spin_init (pthread_spinlock_t *lock, int pshared)
{
Y
yiyue.fang 已提交
29 30 31 32
    if (!lock)
        return EINVAL;

    lock->lock = 0;
M
Ming, Bai 已提交
33

Y
yiyue.fang 已提交
34
    return 0;
M
Ming, Bai 已提交
35 36 37 38
}

int pthread_spin_destroy (pthread_spinlock_t *lock)
{
Y
yiyue.fang 已提交
39 40
    if (!lock)
        return EINVAL;
M
Ming, Bai 已提交
41

Y
yiyue.fang 已提交
42
    return 0;
M
Ming, Bai 已提交
43 44 45 46
}

int pthread_spin_lock (pthread_spinlock_t *lock)
{
Y
yiyue.fang 已提交
47 48
    if (!lock)
        return EINVAL;
M
Ming, Bai 已提交
49

Y
yiyue.fang 已提交
50 51 52 53
    while (!(lock->lock))
    {
        lock->lock = 1;
    }
M
Ming, Bai 已提交
54

Y
yiyue.fang 已提交
55
    return 0;
M
Ming, Bai 已提交
56 57 58 59
}

int pthread_spin_trylock (pthread_spinlock_t *lock)
{
Y
yiyue.fang 已提交
60 61 62 63 64 65
    if (!lock)
        return EINVAL;

    if (!(lock->lock))
    {
        lock->lock = 1;
M
Ming, Bai 已提交
66

Y
yiyue.fang 已提交
67 68
        return 0;
    }
M
Ming, Bai 已提交
69

Y
yiyue.fang 已提交
70
    return EBUSY;
M
Ming, Bai 已提交
71 72 73 74
}

int pthread_spin_unlock (pthread_spinlock_t *lock)
{
Y
yiyue.fang 已提交
75 76 77 78
    if (!lock)
        return EINVAL;
    if (!(lock->lock))
        return EPERM;
M
Ming, Bai 已提交
79

Y
yiyue.fang 已提交
80
    lock->lock = 0;
M
Ming, Bai 已提交
81

Y
yiyue.fang 已提交
82
    return 0;
M
Ming, Bai 已提交
83
}