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

📄 sempore.c

📁 minitos是一个实时的嵌入式操作系统
💻 C
字号:
/*
	miniTOS V0.1.3  1998-2004 (c) 林良水 (Lin LS)
	miniTOS是一个开放源码的软件,授权LGPL,但开发人员不保证本软件的可靠性,
以及对您的损失负任何责任。
	www.minitos.com

	本文实现miniTOS信号量。

	create by Lin LS ,1998.10
	Bug Report: mail to(林良水)testmyself@163.net
*/
#include <stdio.h>

#include "minitosdef.h"
#include "extdefine.h"

/**********************************************************
**Function: 建立信号量
**Input: 
***********************************************************/
SEMPORE	* CreateSempore(SEMPORE	* ptrSempore,int Count)
{
	lock_int();
	if(ptrSempore==NULL)
	{	//当为NULL表示要使用系统的信号量 SemSys[]
#ifdef SEM_NO_MALLOC
//不支持malloc 的系统
		SysSem[gSysSemUsed].ptrSuspendProc=NULL;
		SysSem[gSysSemUsed].SemCount=Count;
		gSysSemUsed++;
		unlock_int();
		return &(SysSem[gSysSemUsed-1]);

#else

#endif
	}
	ptrSempore->ptrSuspendProc=NULL;
	ptrSempore->SemCount=Count;
	unlock_int();
	return ptrSempore;
}
int ReleaseSempore(SEMPORE	* ptrSempore)
{
	lock_int();
	if(ptrSempore->ptrSuspendProc==NULL)	//有进程挂起在该信号量吗?
	{  //why ==NULL?
		if(ptrSempore->SemCount<0x7FFFFFFF)
		{
			ptrSempore->SemCount++;
			unlock_int();
			return 6;	
		}else
		{
			unlock_int();
			return 7;//(-1);
		}	
	} else
	{
		ready(ptrSempore->ptrSuspendProc);
		ptrSempore->ptrSuspendProc->status &= 0xFe;	//bit.2 clear to 0,清除挂起标志
		ptrSempore->ptrSuspendProc=NULL;
		sched();
		unlock_int();
		return 5;
	}
		
}
/*********************************************************************************
**Suspend to wait a sempore
**
**********************************************************************************/
int SemporeWaitSuspend(SEMPORE	* ptrSempore)
{
	lock_int();
	if(ptrSempore->SemCount>0)
	{
		ptrSempore->SemCount--;
		unlock_int();
		return 0;
	}
	ptrCurrProc->status|=0x01;  //set status bit mean blocking for waiting semgore
	ptrSempore->ptrSuspendProc=ptrCurrProc;	//目前只支持一个任务挂在该信号量
	ptrCurrProc->ptrNextProc=NULL;	//挂在最后
	unready(ptrCurrProc);
	sched();	
			//调用本子程序的进程继续运行
	lock_int();	//sched()执行了unlock
	ptrCurrProc->status&=0xFe;	//bit.2 clear to 0,清除挂起标志
	ptrSempore->ptrSuspendProc=NULL;	//目前只支持一个任务挂在该信号量
	unlock_int();
	return 0;
	
}

⌨️ 快捷键说明

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