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

📄 lock.c

📁 s3c2440在wince4.2下的SDK,实时操作系统用
💻 C
字号:
//
// Copyright (c) Microsoft Corporation.  All rights reserved.
//
//
// Use of this source code is subject to the terms of the Microsoft end-user
// license agreement (EULA) under which you licensed this SOFTWARE PRODUCT.
// If you did not accept the terms of the EULA, you are not authorized to use
// this source code. For a copy of the EULA, please see the LICENSE.RTF on your
// install media.
//
#include <windows.h>

#include "lock.h"

BOOL InitializeBlock(LPLOCK lpLock)
{
    lpLock->nReaders = 0;
    lpLock->hEventStopRead = CreateEvent(NULL, TRUE, TRUE, NULL);
    lpLock->hMutextStopWrite = CreateMutex(NULL, FALSE, NULL);

    if(!lpLock->hEventStopRead || lpLock->hMutextStopWrite)
    {
        DeleteBlock(lpLock);
        return FALSE;
    }

    return TRUE;
}

BOOL DeleteBlock(LPLOCK lpLock)
{
    if(lpLock->hEventStopRead)
        CloseHandle(lpLock->hEventStopRead);
        
    if(lpLock->hMutexStopWrite)
        CloseHandle(lpLock->hMutexStopWrite);
    
    lpLock->hEventStopRead = NULL;
    lpLock->hMutexStopWrite = NULL;     
        
    return TRUE;
}

BOOL EnterWriteBlock(LPLOCK lpLock)
{
    HANDLE aHandles[2];
    
    aHandles[0] = lpLock->hEventStopRead;
    aHandles[1] = lpLock->hMutexStopWrite;
    
    WaitForMultipleObjects(2, aHandles, TRUE, INFINITE);
    
    return TRUE;
}

BOOL LeaveWriteBlock(LPLOCK lpLock)
{
    ReleaseMutex(lpLock->hMutexStopWrite);
    
    return TRUE;
}

BOOL EnterReadBlock(LPLOCK lpLock)
{
    WaitForSingleObject(lpLock->hMutexStopWrite, INFINITE);
    
    ++lpLock->nReaders;
    if(lpLock->nReaders == 1)
    {
        //we are the first block
        
        ResetEvent(lpLock->hEventStopRead);
    }
    
    ReleaseMutex(lpLock->hMutexStopWrite);
    
    return TRUE;
}

BOOL LeaveReadBlock(LPLOCK lpLock)
{
    WaitForSingleObject(lpLock->hMutexStopWrite, INFINITE);
    
    --lpLock->nReaders;
    if(lpLock->nReaders == 0)
    {
        //we are the first block
        SetEvent(lpLock->hEventStopRead);
    }
    
    ReleaseMutex(lpLock->hMutextStopWrite);
    
    return TRUE;
}

⌨️ 快捷键说明

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