⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 spinlock.c

📁 Bycore是一个嵌入式操作系统内核。Bycore包括内存管理、任务管理、中断管理、任务互斥、同步与通信管理等功能。Bycore全部由C语言完成
💻 C
字号:
/**
 *  spinlock.c - Impletment spinlock functions. It depends on hardware.
 *               Atomic operation impletmented with disable interruption
 *               because ARM architecture can not lock bus.
 *
 *  Copyright (C) 2008  ZhangHu
 *  All rights reserved.
 *  E-MAIL: anmnmnly@gmail.com
 *
 *  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 3 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, see <http://www.gnu.org/licenses/>.
 */
#include "cpu.h"
#include "include/spinlock.h"

/**
 * spin_init - Initialize a spinlock.
 * @lock: Pointer to a spinlock.
 * @return:
 *
 * @notes:
 */
void spin_init(spinlock_t *lock) {
    DisableInt();
    lock->slock = 1;
    EnableInt();
}

/**
 * spin_lock - Lock a spinlock.
 * @lock: Pointer to a spinlock.
 * @return:
 *
 * @notes: Don't retry lock the same spinlock after It had been handed.
 */
void spin_lock(spinlock_t *lock) {
    while(1) {
        DisableInt();
        if(lock->slock == 1) {
            break;
        } else {
            EnableInt();
        }
    }

    lock->slock = 0;
    EnableInt();
}

/**
 * spin_unlock - Unlock a spinlock.
 * @lock: Pointer to a spinlock.
 * @return:
 *
 * @notes:
 */
void spin_unlock(spinlock_t *lock) {
    DisableInt();
    lock->slock = 1;
    EnableInt();
}

/**
 * spin_trylock - Try to lock a spinlock.
 * @lock: Pointer to a spinlock.
 * @return: return FALSE if failde, otherwise return TRUE.
 *
 * @notes: It doesn't make task waiting.
 */
bool_t spin_trylock(spinlock_t *lock) {
    DisableInt();
    if(lock->slock == 0) {
        EnableInt();
        return FALSE;
    }
    lock->slock = 0;
    EnableInt();
    return TRUE;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -