📄 semaphore.c
字号:
// This file is part of MANTIS OS, Operating System// See http://mantis.cs.colorado.edu///// Copyright (C) 2003,2004,2005 University of Colorado, Boulder//// This program is free software; you can redistribute it and/or// modify it under the terms of the mos license (see file LICENSE)/* Project Mantis File: semaphore.c Author: Jeff Rose Edited by Hui Dai Date: 1-30-03 Standard counting semaphores...*//** @file semaphore.c * @brief Standard counting semaphores. * @author Jeff Rose * @author Edited by Hui Dai * @date Created: 01/30/2003 */#include "mos.h"#include "sem.h"#include "tlist.h"void mos_sem_init(mos_sem_t *s, int value){ s->val = value; mos_tlist_init(&s->q);}void mos_sem_select_post(mos_sem_t *s, uint16_t thread){ handle_t int_handle; thread_t *selected; int_handle = mos_disable_ints(); // Post a unit and wake-up the next waiting thread s->val++; if((selected = mos_tlist_get_thread(&s->q, thread)) != NULL) { //mos_thread_resume_noints_nodispatch(selected, int_handle); //mos_enable_ints(int_handle); mos_thread_resume_noints(selected, int_handle); } else { mos_enable_ints(int_handle); }}void mos_sem_post(mos_sem_t *s){ handle_t int_handle; thread_t *thread; int_handle = mos_disable_ints(); // Post a unit and wake-up the next waiting thread s->val++; if((thread = mos_tlist_remove(&s->q)) != NULL) { //mos_thread_resume_noints_nodispatch(thread, int_handle); //mos_enable_ints(int_handle); mos_thread_resume_noints(thread, int_handle); } else { mos_enable_ints(int_handle); }}void mos_sem_wait(mos_sem_t *s){ handle_t int_handle; int_handle = mos_disable_ints(); s->val--; // If no resources are available then we wait in the queue if(s->val < 0) { thread_t *id; id = mos_thread_current(); mos_tlist_add(&s->q, id); mos_thread_suspend_noints(int_handle); } else mos_enable_ints(int_handle);}uint8_t mos_sem_try_wait(mos_sem_t *s){ handle_t int_handle; int_handle = mos_disable_ints(); if(s->val > 0) { s->val--; mos_enable_ints(int_handle); return SEM_SUCCESS; } else { mos_enable_ints(int_handle); return SEM_FAIL; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -