亚洲欧美第一页_禁久久精品乱码_粉嫩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一区二区三区免费野_久草精品视频
日韩精品久久理论片| 午夜欧美2019年伦理| 日韩高清一级片| 99精品欧美一区二区三区小说 | 国产伦精品一区二区三区视频青涩 | 精品一区精品二区高清| 色视频一区二区| 国产欧美在线观看一区| 亚洲高清免费观看| 99免费精品在线| 久久免费视频一区| 日韩成人免费在线| 欧美日韩一区二区电影| 国产精品久久久久久福利一牛影视| 日本不卡一区二区三区| 91久久精品日日躁夜夜躁欧美| 精品久久久久久最新网址| 香蕉成人伊视频在线观看| 色综合天天综合色综合av| 国产三级三级三级精品8ⅰ区| 美女一区二区三区| 欧美日韩一区二区三区视频| 国产999精品久久久久久绿帽| 91精品国产色综合久久ai换脸| 亚洲免费电影在线| 成人深夜福利app| 欧美精彩视频一区二区三区| 国产综合色视频| 日韩欧美亚洲一区二区| 午夜激情一区二区三区| 欧美在线播放高清精品| 亚洲欧美日韩国产综合| 成人爱爱电影网址| 国产精品卡一卡二卡三| 国产91精品欧美| 久久精品亚洲乱码伦伦中文| 精品一区二区三区免费视频| 日韩欧美一区中文| 日韩av电影一区| 欧美日韩免费一区二区三区视频| 一区二区三区中文在线观看| 91免费版在线看| 亚洲情趣在线观看| 91免费看片在线观看| 亚洲欧美日韩综合aⅴ视频| av综合在线播放| 成人免费在线播放视频| 91视频免费看| 一区二区在线观看免费| 在线观看亚洲一区| 婷婷丁香久久五月婷婷| 日韩亚洲欧美一区| 韩国精品在线观看| 久久久久久麻豆| 国产精品99久久久久久久女警| 中文字幕不卡在线播放| av色综合久久天堂av综合| 亚洲欧洲av另类| 欧美在线观看一区| 日韩在线一区二区三区| 欧美videossexotv100| 国产精品亚洲第一区在线暖暖韩国| 国产女人水真多18毛片18精品视频| 成人免费看黄yyy456| 亚洲三级电影网站| 欧美日韩免费一区二区三区视频| 日韩高清不卡一区| 久久无码av三级| 成人18精品视频| 亚洲午夜视频在线| 日韩精品一区国产麻豆| 风间由美一区二区av101 | 欧美人狂配大交3d怪物一区| 日韩精品一二三区| 欧美精品一区二区久久久| 成人免费毛片嘿嘿连载视频| 亚洲色图视频网站| 欧美精品三级日韩久久| 麻豆精品一区二区av白丝在线 | 91麻豆成人久久精品二区三区| 亚洲综合丝袜美腿| 日韩三级精品电影久久久 | 五月天激情综合| 精品处破学生在线二十三| aaa国产一区| 日韩不卡在线观看日韩不卡视频| 2欧美一区二区三区在线观看视频 337p粉嫩大胆噜噜噜噜噜91av | 亚洲欧美激情小说另类| 7777精品伊人久久久大香线蕉 | 日一区二区三区| 国产亚洲欧洲一区高清在线观看| 91一区一区三区| 蜜桃一区二区三区四区| 国产精品成人免费在线| 3d成人h动漫网站入口| 高清在线观看日韩| 亚洲国产cao| 国产午夜精品理论片a级大结局| 欧美自拍丝袜亚洲| 日韩欧美自拍偷拍| 不卡高清视频专区| 美女在线视频一区| 最新国产の精品合集bt伙计| 欧美一级日韩一级| 99精品欧美一区二区三区小说| 麻豆国产精品官网| 亚洲精品亚洲人成人网| 26uuu国产日韩综合| 在线视频综合导航| 国产精品一级片在线观看| 亚洲1区2区3区视频| 国产精品伦理在线| 日韩午夜电影av| 色一区在线观看| 国产精品亚洲午夜一区二区三区| 性久久久久久久久| 中文字幕日韩精品一区| 久久蜜桃av一区精品变态类天堂| 欧美日韩精品专区| 波波电影院一区二区三区| 黑人巨大精品欧美一区| 性久久久久久久久| 一区二区三区在线视频观看| 欧美激情综合网| 精品国产伦一区二区三区免费| 欧美亚洲综合久久| 99久久亚洲一区二区三区青草| 国产在线看一区| 日韩二区在线观看| 亚洲夂夂婷婷色拍ww47| 国产精品免费看片| 久久久蜜桃精品| 日韩欧美国产不卡| 制服视频三区第一页精品| 欧美亚洲高清一区| 99久久久精品| 成人毛片视频在线观看| 国产综合色在线视频区| 精油按摩中文字幕久久| 日本在线不卡视频| 亚洲成人av电影| 亚洲一区二区三区精品在线| 亚洲精品欧美专区| 亚洲日本va午夜在线电影| 中文字幕 久热精品 视频在线| 精品国产成人在线影院 | 成人一区二区三区视频在线观看| 久久精品国产99久久6| 日本在线播放一区二区三区| 香蕉影视欧美成人| 性欧美大战久久久久久久久| 亚洲大片免费看| 欧美亚洲一区二区在线观看| 99精品偷自拍| 色美美综合视频| 色综合久久天天| 色一情一乱一乱一91av| 色婷婷综合久久久中文一区二区 | 日本亚洲欧美天堂免费| 日本亚洲三级在线| 蜜臀精品久久久久久蜜臀| 蜜桃av一区二区| 老司机精品视频导航| 狠狠色伊人亚洲综合成人| 国产伦精品一区二区三区免费| 国产在线不卡视频| 大白屁股一区二区视频| 91日韩精品一区| 91黄色在线观看| 精品视频在线视频| 91精品国产欧美日韩| 日韩欧美激情在线| 国产亚洲一区字幕| 中文字幕一区二区三区精华液 | 欧美成人精品1314www| 精品成人一区二区三区| 国产亚洲综合性久久久影院| 国产精品久久一级| 亚洲欧美福利一区二区| 亚洲妇女屁股眼交7| 日韩va欧美va亚洲va久久| 韩日av一区二区| 国产98色在线|日韩| 91原创在线视频| 欧美伦理电影网| 精品免费99久久| 欧美韩国日本综合| 亚洲小说春色综合另类电影| 日韩av一区二区三区四区| 国产伦精品一区二区三区免费迷 | 91国在线观看| 欧美精品久久99久久在免费线 | 久久久精品免费观看| 国产精品久久久久久久久久久免费看| 亚洲欧美成人一区二区三区| 亚洲超碰精品一区二区| 国产精品亚洲а∨天堂免在线| 91啪亚洲精品| 日韩欧美一卡二卡| 国产精品系列在线|