亚洲欧美第一页_禁久久精品乱码_粉嫩av一区二区三区免费野_久草精品视频

? 歡迎來到蟲蟲下載站! | ?? 資源下載 ?? 資源專輯 ?? 關于我們
? 蟲蟲下載站

?? mailbox.cpp

?? 小型的操作系統開發的原代碼
?? CPP
字號:
//***********************************************************************/
//    Author                    : Garry
//    Original Date             : Jul,24 2005
//    Module Name               : MAILBOX.CPP
//    Module Funciton           : 
//                                This module countains mailbox's implementation code.
//    Last modified Author      :
//    Last modified Date        :
//    Last modified Content     :
//                                1.
//                                2.
//    Lines number              :
//***********************************************************************/

#ifndef __STDAFX_H__
#include "..\INCLUDE\StdAfx.h"
#endif

//#include ".\NetCore\NETBUFF.H"

//
//Pre-declaration of static routines.
//

static DWORD GetMail(__COMMON_OBJECT*,LPVOID*,DWORD);
static DWORD SendMail(__COMMON_OBJECT*,LPVOID,DWORD);
static DWORD MailBoxTimerHandler(LPVOID);

//
//Defines a parameter object,to be used by timer handler.
//

BEGIN_DEFINE_OBJECT(__MAILBOX_TIMERH_PARAM)
    __MAILBOX*               lpMailBox;               //Point to mailbox object.
    __KERNEL_THREAD_OBJECT*  lpKernelThread;          //Point to kernel thread object who
	                                                  //set the timer.
	DWORD                    dwWakeupReason;          //Wake up reason.
	DWORD                    dwSetReason;             //Timer set reason,why set a timer.
END_DEFINE_OBJECT()

#define WAKE_UP_REASON_HAVE_MESSAGE    0x00000001     //The kernel thread is waken up by SendMail routine.
#define WAKE_UP_REASON_TIMEOUT         0x00000002     //The kernel thread is waken up by timer.

#define TIMER_SET_REASON_GETTING       0x00000003     //If a timer is set by a kernel thread wanting
                                                      //to get a mail,but the mailbox is empty,then
													  //it set this value.
#define TIMER_SET_REASON_SENDING       0x00000004     //If a timer is set by a kernel thread wanting
													  //to send a mail,but the mailbox is full,then
													  //it set a timer,and set this value.

#define MAILBOX_TIMEOUT_TIMER_ID       1024

//
//The implementation of Initialize routine.
//This routine does the following:
// 1. Set all member variables to NULL or zero;
// 2. Create two queues,getting queue and sending queue;
// 3. Initializes the queue variables to the two queues.
//

BOOL MailBoxInitialize(__COMMON_OBJECT* lpThis)
{
	__PRIORITY_QUEUE*       lpGettingQueue       = NULL;
	__PRIORITY_QUEUE*       lpSendingQueue       = NULL;
	__MAILBOX*              lpMailBox            = NULL;
	BOOL                    bResult              = FALSE;

	if(NULL == lpThis)    //Parameter check.
		return FALSE;

	lpMailBox = (__MAILBOX*)lpThis;

	lpGettingQueue = (__PRIORITY_QUEUE*)ObjectManager.CreateObject(&ObjectManager,
		NULL,
		OBJECT_TYPE_PRIORITY_QUEUE);    //Create getting queue.
	if(NULL == lpGettingQueue)          //Can not create getting queue,may has not enough memory.
		goto __TERMINAL;
	if(!lpGettingQueue->Initialize((__COMMON_OBJECT*)lpGettingQueue))  //Can not initialize it.
		goto __TERMINAL;

	lpSendingQueue = (__PRIORITY_QUEUE*)ObjectManager.CreateObject(&ObjectManager,
		NULL,
		OBJECT_TYPE_PRIORITY_QUEUE);    //Create sending queue.
	if(NULL == lpSendingQueue)
		goto __TERMINAL;
	if(!lpSendingQueue->Initialize((__COMMON_OBJECT*)lpGettingQueue))  //Initialize it.
		goto __TERMINAL;

	lpMailBox->lpMsg            = NULL;
	lpMailBox->lpGettingQueue   = lpGettingQueue;
	lpMailBox->lpSendingQueue   = lpSendingQueue;
	lpMailBox->GetMail          = GetMail;
	lpMailBox->SendMail         = SendMail;

	bResult = TRUE;


__TERMINAL:
	if(!bResult)    //The initializing process is fail.
	{
		if(NULL == lpGettingQueue)
			ObjectManager.DestroyObject(&ObjectManager,
			(__COMMON_OBJECT*)lpGettingQueue);    //Destroy getting queue.
		if(NULL == lpSendingQueue)
			ObjectManager.DestroyObject(&ObjectManager,
			(__COMMON_OBJECT*)lpSendingQueue);    //Destroy sending queue.
	}
	return bResult;
}

//
//The implementation of Uninitialize routine.
//This routine only destory the queue objects of mail box.
//

VOID MailBoxUninitialize(__COMMON_OBJECT* lpThis)
{
	__MAILBOX*                lpMailBox        = NULL;

	if(NULL == lpThis)
		return;

	lpMailBox = (__MAILBOX*)lpThis;
	ObjectManager.DestroyObject(&ObjectManager,
		(__COMMON_OBJECT*)lpMailBox->lpGettingQueue);    //Destroy the getting queue.
	ObjectManager.DestroyObject(&ObjectManager,
		(__COMMON_OBJECT*)lpMailBox->lpSendingQueue);    //Destroy the sending queue.

	return;
}

//
//The implementation of GetMail routine.
//This routine does the following:
// 1. Check if the mail box is empty;
// 2. If not empty,then get the mail,and check if there are kernel threads waiting to send mail;
// 3. If there are kernel threads waiting to send mail,then wake up the first one;
// 4. return success;
// 5. If the mail box is empty,then block the current kenrel thread;
// 6. If the dwTimeOut parameter is not zero,then set a timer;
// 7. Reschedule all kernel threads of the system;
// 8. After been waken up,check the wake up reason;
// 9. If waken up by timer,then return MAILBOX_TIMEOUT;
// 10. If waken up by SendMail routine,then get the mail,and cancel timer if
//     set;
// 11. Return appropriate value.
//

static DWORD GetMail(__COMMON_OBJECT* lpThis,LPVOID* llpMsg,DWORD dwTimeOut)
{
	__MAILBOX*                           lpMailBox                = NULL;
	__KERNEL_THREAD_OBJECT*              lpKernelThread           = NULL;
	__TIMER_OBJECT*                      lpTimerObject            = NULL;
	__MAILBOX_TIMERH_PARAM*              lpHandlerParam           = NULL;
	DWORD                                dwFlags                  = 0L;

	if((NULL == lpThis) || (NULL == llpMsg))    //Parameter check.
		return MAILBOX_FAILED;

	lpMailBox = (__MAILBOX*)lpThis;

	//ENTER_CRITICAL_SECTION();
	__LEAVE_CRITICAL_SECTION(NULL,dwFlags);
	if(NULL != lpMailBox->lpMsg)
	{
		*llpMsg = lpMailBox->lpMsg;           //Get the message.
		lpMailBox->lpMsg = NULL;              //Clear the mailbox.
		lpKernelThread = (__KERNEL_THREAD_OBJECT*)
			lpMailBox->lpSendingQueue->GetHeaderElement(
			(__COMMON_OBJECT*)lpMailBox->lpSendingQueue,
			NULL);
		if(NULL == lpKernelThread)    //There is not any kernel thread waiting to send message.
		{
			//LEAVE_CRITICAL_SECTION();
			__LEAVE_CRITICAL_SECTION(NULL,dwFlags);
			return MAILBOX_SUCCESS;
		}
		else    //There at lease one kernel thread waiting to send a mail,so wake up it.
		{
			lpKernelThread->dwThreadStatus = KERNEL_THREAD_STATUS_READY;
			KernelThreadManager.AddReadyKernelThread(
				(__COMMON_OBJECT*)&KernelThreadManager,
				lpKernelThread);  //Add to ready queue.
			__LEAVE_CRITICAL_SECTION(NULL,dwFlags);
			return MAILBOX_SUCCESS;
		}
	}
	else    //There is not any message,so must block the kernel thread.
	{
		lpKernelThread = KernelThreadManager.lpCurrentKernelThread;
		lpKernelThread->dwThreadStatus = KERNEL_THREAD_STATUS_BLOCKED;
		if(dwTimeOut)    //Should set a timeout timer.
		{
			lpHandlerParam = (__MAILBOX_TIMERH_PARAM*)KMemAlloc(sizeof(__MAILBOX_TIMERH_PARAM),
				KMEM_SIZE_TYPE_ANY);
			if(NULL == lpHandlerParam)    //Can not allocate memory.
			{
				lpKernelThread->dwThreadStatus = KERNEL_THREAD_STATUS_RUNNING;
				//LEAVE_CRITICAL_SECTION();
				__LEAVE_CRITICAL_SECTION(NULL,dwFlags);
				return MAILBOX_NO_RESOURCE;
			}
			lpHandlerParam->lpMailBox        = lpMailBox;
			lpHandlerParam->lpKernelThread   = lpKernelThread;
			lpHandlerParam->dwWakeupReason   = WAKE_UP_REASON_HAVE_MESSAGE;
			lpHandlerParam->dwSetReason      = TIMER_SET_REASON_GETTING;

			lpTimerObject = (__TIMER_OBJECT*)System.SetTimer((__COMMON_OBJECT*)&System,
				lpKernelThread,
				MAILBOX_TIMEOUT_TIMER_ID,
				dwTimeOut,
				MailBoxTimerHandler,
				lpHandlerParam,
				TIMER_FLAGS_ONCE);

			if(NULL == lpTimerObject)    //Can not set a timer,maybe has not enough memory.
			{
				lpKernelThread->dwThreadStatus = KERNEL_THREAD_STATUS_RUNNING;
				//LEAVE_CRITICAL_SECTION();
				__LEAVE_CRITICAL_SECTION(NULL,dwFlags);
				KMemFree((LPVOID)lpHandlerParam,KMEM_SIZE_TYPE_ANY,0L);
				return MAILBOX_NO_RESOURCE;
			}
		}
		lpMailBox->lpGettingQueue->InsertIntoQueue((__COMMON_OBJECT*)lpMailBox->lpGettingQueue,
			(__COMMON_OBJECT*)lpKernelThread,
			0L);    //Insert into the getting queue.
		//LEAVE_CRITICAL_SECTION();
		__LEAVE_CRITICAL_SECTION(NULL,dwFlags);
		KernelThreadManager.ScheduleFromProc(&lpKernelThread->KernelThreadContext);

		//
		//The following code is executed after this kernel thread is waken up.
		//
		//ENTER_CRITICAL_SECTION();
		__ENTER_CRITICAL_SECTION(NULL,dwFlags);
		if(!dwTimeOut)    //Have not set a timer.
		{
			*llpMsg = lpMailBox->lpMsg;
			lpMailBox->lpMsg = NULL;
			//LEAVE_CRITICAL_SECTION();
			__LEAVE_CRITICAL_SECTION(NULL,dwFlags);
			return MAILBOX_SUCCESS;
		}
		//
		//The following code deals with the situation that a timer has been set.
		//

		switch (lpHandlerParam->dwWakeupReason)
		{
		case WAKE_UP_REASON_HAVE_MESSAGE:  //In this case,we should cancel timer first.
			*llpMsg = lpMailBox->lpMsg;
			lpMailBox->lpMsg = NULL;
			System.CancelTimer((__COMMON_OBJECT*)&System,
				(__COMMON_OBJECT*)lpTimerObject);
			__LEAVE_CRITICAL_SECTION(NULL,dwFlags);  //NOTE : This is modified later.
			KMemFree((LPVOID)lpHandlerParam,KMEM_SIZE_TYPE_ANY,0L);
			return MAILBOX_SUCCESS;
			break;
		case WAKE_UP_REASON_TIMEOUT:
			//LEAVE_CRITICAL_SECTION();
			__LEAVE_CRITICAL_SECTION(NULL,dwFlags);
			KMemFree((LPVOID)lpHandlerParam,KMEM_SIZE_TYPE_ANY,0L);
			return MAILBOX_TIMEOUT;
		default:
			//LEAVE_CRITICAL_SECTION();
			__LEAVE_CRITICAL_SECTION(NULL,dwFlags);
			KMemFree((LPVOID)lpHandlerParam,KMEM_SIZE_TYPE_ANY,0L);
			return MAILBOX_FAILED;
		}
	}
	return 0L;
}

//
//The implementation of SendMail.
//This routine does the following:
// 1.
// 

static DWORD SendMail(__COMMON_OBJECT* lpThis,LPVOID lpMsg,DWORD dwTimeOut)
{
	__MAILBOX*                           lpMailBox       = NULL;
	__KERNEL_THREAD_OBJECT*              lpKernelThread  = NULL;
	DWORD                                dwFlags         = 0L;

	if((NULL == lpThis) || (NULL == lpMsg))  //Parameters check.
		return MAILBOX_FAILED;

	lpMailBox = (__MAILBOX*)lpThis;
	//ENTER_CRITICAL_SECTION();
	__ENTER_CRITICAL_SECTION(NULL,dwFlags);
	if(NULL == lpMailBox->lpMsg)
	{
		lpMailBox->lpMsg = lpMsg;    //Send the mail.
		lpKernelThread = (__KERNEL_THREAD_OBJECT*)
			lpMailBox->lpGettingQueue->GetHeaderElement(
			(__COMMON_OBJECT*)lpMailBox->lpGettingQueue,
			NULL);
		if(NULL == lpKernelThread)    //There is not any kernel thread waiting for message.
		{
			//LEAVE_CRITICAL_SECTION();
			__LEAVE_CRITICAL_SECTION(NULL,dwFlags);
			return MAILBOX_SUCCESS;
		}
		else               //There is one kernel thread waiting for message at least.
		{
			lpKernelThread->dwThreadStatus = KERNEL_THREAD_STATUS_READY;
			KernelThreadManager.AddReadyKernelThread(
				(__COMMON_OBJECT*)&KernelThreadManager,
				lpKernelThread);  //Add to ready queue.
			__LEAVE_CRITICAL_SECTION(NULL,dwFlags);
			return MAILBOX_SUCCESS;
		}
	}
	else    //The mailbox is full.
	{
		//LEAVE_CRITICAL_SECTION();
		__LEAVE_CRITICAL_SECTION(NULL,dwFlags);
		return MAILBOX_FAILED;
	}
}

//
//The implementation of MailBoxTimerHandler.
//This routine is called by timer object,to indicate the timeout event,and this routine
//runs at the interrupt context.
//It does the following:
// 1. Check the reason that kernel thread set a timer;
// 2. If the reason is to timeout the send event,then try to wake up the sending thread;
// 3. If the reason is to timeout the get event,then try to wake up a getting thread;
// 4. Set the wake up reason correctly,to tell the target kernel thread proper reason
//    that it waken up.
//

static DWORD MailBoxTimerHandler(LPVOID lpParam)
{
	__MAILBOX_TIMERH_PARAM*             lpHandlerParam      = NULL;
	__PRIORITY_QUEUE*                   lpWaitingQueue      = NULL;
	__KERNEL_THREAD_OBJECT*             lpKernelThread      = NULL;
	
	if(NULL == lpParam)
		return 0L;
	lpHandlerParam = (__MAILBOX_TIMERH_PARAM*)lpParam;
	switch(lpHandlerParam->dwSetReason)
	{
	case TIMER_SET_REASON_GETTING:
		lpWaitingQueue = lpHandlerParam->lpMailBox->lpGettingQueue;
		lpKernelThread = lpHandlerParam->lpKernelThread;
		if(!lpWaitingQueue->DeleteFromQueue((__COMMON_OBJECT*)lpWaitingQueue,
			(__COMMON_OBJECT*)lpKernelThread))    //The kernel thread maybe waken up.
			return 0L;
		lpKernelThread->dwThreadStatus = KERNEL_THREAD_STATUS_READY;  //Wake up the thread.
		KernelThreadManager.AddReadyKernelThread(
			(__COMMON_OBJECT*)&KernelThreadManager,
			lpKernelThread);  //Add to ready queue.

		lpHandlerParam->dwWakeupReason = WAKE_UP_REASON_TIMEOUT;
		return 1L;
		break;
	case TIMER_SET_REASON_SENDING:
		break;
	default:
		return 0L;
	}
	return 0L;
}

?? 快捷鍵說明

復制代碼 Ctrl + C
搜索代碼 Ctrl + F
全屏模式 F11
切換主題 Ctrl + Shift + D
顯示快捷鍵 ?
增大字號 Ctrl + =
減小字號 Ctrl + -
亚洲欧美第一页_禁久久精品乱码_粉嫩av一区二区三区免费野_久草精品视频
成人一级片网址| 精品国产露脸精彩对白| 日韩视频免费直播| 亚洲欧美电影院| 激情深爱一区二区| 欧美性生活久久| 国产午夜精品在线观看| 日本伊人精品一区二区三区观看方式 | 亚洲一区二区三区四区的| 久久爱另类一区二区小说| 欧美中文字幕一区二区三区亚洲| 久久精品网站免费观看| 精品中文av资源站在线观看| 欧洲在线/亚洲| 中文字幕在线观看不卡| 国产乱码精品一品二品| 欧美一二三区在线观看| 亚洲一区二区综合| 色哟哟欧美精品| 中文字幕日韩一区二区| 国产福利一区在线| 精品欧美久久久| 麻豆91在线播放| 日韩免费观看高清完整版在线观看| 一区二区三区在线视频观看58| 成人av中文字幕| 国产精品久久777777| proumb性欧美在线观看| 国产丝袜美腿一区二区三区| 国产在线不卡视频| 久久久久久久一区| 国产乱对白刺激视频不卡| 亚洲精品一区二区三区在线观看| 奇米影视一区二区三区小说| 91精品国产乱码| 美女免费视频一区二区| 欧美不卡一区二区三区| 国产成人在线网站| 国产精品女主播av| 99久久婷婷国产综合精品电影| 国产精品久久久久永久免费观看 | 国产91在线看| ...av二区三区久久精品| 91啪九色porn原创视频在线观看| 亚洲色大成网站www久久九九| 99re66热这里只有精品3直播 | 26uuu精品一区二区三区四区在线| 蜜臀a∨国产成人精品| 久久综合色一综合色88| 成人av资源站| 亚洲小说春色综合另类电影| 91超碰这里只有精品国产| 日韩av电影免费观看高清完整版| 日韩欧美一级二级三级久久久| 精品一区二区久久| 中文字幕日韩av资源站| 欧美日韩国产一区| 国产麻豆午夜三级精品| 国产精品伦一区二区三级视频| 91丨九色丨尤物| 日本欧美一区二区在线观看| 国产欧美视频一区二区三区| 99国产精品视频免费观看| 午夜电影网一区| 久久网这里都是精品| 色欧美88888久久久久久影院| 五月激情综合色| 欧美国产精品专区| 欧美日韩一本到| 懂色av一区二区三区免费看| 亚洲一区二区免费视频| 国产日韩欧美制服另类| 欧美在线观看视频一区二区| 激情综合亚洲精品| 一区二区在线免费观看| 精品国产乱码久久久久久老虎| 99re66热这里只有精品3直播 | 亚洲欧美日韩一区二区| 欧美岛国在线观看| 色哟哟精品一区| 国产成人在线看| 日日欢夜夜爽一区| 亚洲欧美在线视频观看| 日韩一级黄色大片| 欧美主播一区二区三区美女| 国产精品69毛片高清亚洲| 日日夜夜一区二区| 亚洲精品国产第一综合99久久| 日韩欧美一区二区视频| 欧美色综合天天久久综合精品| 国产美女视频91| 美女一区二区久久| 午夜激情综合网| 亚洲黄一区二区三区| 欧美激情中文字幕| 精品久久久久99| 日韩一区二区三区高清免费看看| 色婷婷精品久久二区二区蜜臂av | 高清日韩电视剧大全免费| 日本欧美久久久久免费播放网| 一区二区三区中文在线| 欧美国产丝袜视频| 久久精品人人做人人爽人人| 日韩三级精品电影久久久| 欧美日韩的一区二区| 欧美日韩国产系列| 欧美制服丝袜第一页| 色噜噜狠狠成人中文综合| 99精品在线观看视频| 成人av小说网| 99在线精品观看| 成人av影院在线| 91在线免费看| 色素色在线综合| 欧美视频一区二区三区四区| 在线观看视频91| 欧美日韩激情在线| 69av一区二区三区| 日韩女优电影在线观看| 欧美成人精品二区三区99精品| 日韩精品一区二区三区在线| 精品久久久久久久久久久久久久久久久 | 欧美成人a∨高清免费观看| 日韩女优av电影在线观看| 日韩精品一区二区三区老鸭窝| 精品人在线二区三区| 久久婷婷色综合| 国产精品无圣光一区二区| 国产精品女上位| 亚洲一区二区三区精品在线| 天天色 色综合| 国产麻豆午夜三级精品| 99精品偷自拍| 欧美日韩一本到| 精品剧情v国产在线观看在线| 久久色视频免费观看| 国产精品美女久久久久久久网站| 综合在线观看色| 日韩va欧美va亚洲va久久| 老司机午夜精品| 99久久99久久精品免费看蜜桃| 欧美日韩午夜精品| 久久久噜噜噜久久中文字幕色伊伊 | 国产乱码精品一区二区三区忘忧草| 国产成人av一区二区| 色老综合老女人久久久| 日韩精品一区二区三区在线 | 欧美精品777| 久久女同互慰一区二区三区| 中文字幕中文字幕一区| 视频一区视频二区中文| 国产伦精品一区二区三区免费| 91视频一区二区三区| 日韩欧美一区二区三区在线| 中文字幕一区二区在线观看 | 一区二区三区四区精品在线视频| 日本伊人精品一区二区三区观看方式 | 成人禁用看黄a在线| 欧美日韩精品一区二区天天拍小说 | 亚洲欧洲精品天堂一级 | 亚洲网友自拍偷拍| 国产酒店精品激情| 欧美福利电影网| 国产精品乱子久久久久| 秋霞av亚洲一区二区三| 91尤物视频在线观看| 26uuu久久综合| 五月综合激情网| 91小视频在线观看| 久久久亚洲欧洲日产国码αv| 一区二区成人在线| 成人h动漫精品| 精品国产乱码久久久久久老虎 | 国产精品久久久久久久久快鸭 | 国产成人亚洲综合a∨猫咪| 欧美日韩和欧美的一区二区| 亚洲欧美综合色| 福利一区在线观看| 欧美大白屁股肥臀xxxxxx| 亚洲第一福利一区| 在线观看网站黄不卡| 中文字幕制服丝袜成人av | 成人av午夜影院| 久久久久97国产精华液好用吗| 日韩一区欧美二区| 欧美猛男男办公室激情| 一二三四区精品视频| 91猫先生在线| 国产精品久久一级| 成a人片亚洲日本久久| 久久久久久免费网| 国产精品自拍在线| 久久精品亚洲麻豆av一区二区| 久久99久久精品欧美| 欧美一区二区三区人| 三级一区在线视频先锋| 欧美精品电影在线播放| 免费久久99精品国产| 欧美xxx久久| 国精产品一区一区三区mba桃花|