rvrw.c

来自「h.248协议源码」· C语言 代码 · 共 97 行

C
97
字号
#if (0)
******************************************************************************
Filename   :
Description:
******************************************************************************
                Copyright (c) 1999 RADVision Inc.
************************************************************************
NOTICE:
This document contains information that is proprietary to RADVision LTD.
No part of this publication may be reproduced in any form whatsoever 
without written prior approval by RADVision LTD..

RADVision LTD. reserves the right to revise this publication and make 
changes without obligation to notify any person of such revisions or 
changes.
******************************************************************************
$Revision:$
$Date:$
$Author: S. Cipolli$
******************************************************************************
#endif

#include "rvlog.h"
#include "rvrw.h"

RvRw* rvRwConstruct(RvRw* rw) {
	rvLogEnter(&rvLog, "rvRwConstruct");

	rw->readerCount = 0;
	rw->busy = 0;
	rvMonConstruct_(&rw->mon);
	rvCondConstruct_(&rw->okToRead, &rw->mon);
	rvCondConstruct_(&rw->okToWrite, &rw->mon);

	rvLogLeave(&rvLog, "rvRwConstruct");

	return rw;
}

void rvRwDestruct(RvRw* rw) {
	rvMonDestruct_(&rw->mon);
	rvCondDestruct_(&rw->okToRead);
	rvCondDestruct_(&rw->okToWrite);
}

void rvRwReadLock(RvRw* rw) {
	rvLogEnter(&rvLog, "rvRwReadLock");

	rvMonLock_(&rw->mon);
	if (rw->busy || rvCondWaiting_(&rw->okToWrite))
		rvCondWait_(&rw->okToRead);
	++(rw->readerCount);
	rvCondNotify_(&rw->okToRead);
	rvMonUnlock_(&rw->mon);

	rvLogLeave(&rvLog, "rvRwReadLock");
}

void rvRwReadUnlock(RvRw* rw) {
	rvLogEnter(&rvLog, "rvRwReadUnlock");

	rvMonLock_(&rw->mon);
	--(rw->readerCount);
	if (rw->readerCount == 0)
		rvCondNotify_(&rw->okToWrite);
	rvMonUnlock_(&rw->mon);

	rvLogLeave(&rvLog, "rvRwReadUnlock");
}

void rvRwWriteLock(RvRw* rw) {
	rvLogEnter(&rvLog, "rvRwWriteLock");

	rvMonLock_(&rw->mon);
	if ((rw->readerCount != 0) || rw->busy)
		rvCondWait_(&rw->okToWrite);
	rw->busy = 1;
	rvMonUnlock_(&rw->mon);

	rvLogLeave(&rvLog, "rvRwWriteLock");
}

void rvRwWriteUnlock(RvRw* rw) {
	rvLogEnter(&rvLog, "rvRwWriteUnlock");

	rvMonLock_(&rw->mon);
	rw->busy = 0;
	if (rvCondWaiting_(&rw->okToRead))
		rvCondNotify_(&rw->okToRead);
	else
		rvCondNotify_(&rw->okToWrite);
	rvMonUnlock_(&rw->mon);

	rvLogLeave(&rvLog, "rvRwWriteUnlock");
}

⌨️ 快捷键说明

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