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

📄 rvqueue.c

📁 h.248协议源码
💻 C
字号:
#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 <assert.h>
#include <stdio.h>				/* Must include to make assert.h happy */
#include "rvlog.h"
#include "rvmem.h"
#include "rvqueue.h"

/* private */
#define rvQueueInc_(x, n)		(((x) == (n)) ? 0 : (x) + 1)
#define rvQueueDec_(x, n)		(((x) == 0) ? (n) : (x) - 1)
#define rvQueueFull_(q)			(rvQueueSize_(q) >= q->n)
#define rvQueueEmpty_(q)		(rvQueueSize_(q) == 0)

static size_t rvQueueSize_(RvQueue* q) {
	if (q->first < q->last)
		return (q->last - q->first) - 1;
	else
		return (q->last + q->n) - q->first;
}

static void rvQueuePush_(RvQueue* q, RvQueueType m) {
	assert(!rvQueueFull_(q));
	q->data[rvQueueDec_(q->last, q->n)] = m;
	q->last = rvQueueInc_(q->last, q->n);
}

static RvQueueType rvQueuePop_(RvQueue* q) {
	RvQueueType m;
	assert(!rvQueueEmpty_(q));
	m = q->data[q->first];
	q->first = rvQueueInc_(q->first, q->n);
	return m;
}

/* public */
RvQueue* rvQueueConstruct(RvQueue* q, size_t n) {
	rvLogEnter(&rvLog, "rvQueueConstruct");

	assert(n > 0);
	q->data = (void**)rvMemAlloc((n + 1) * sizeof(RvQueueType));
	q->done = 0;
	q->first = 0;
	q->last = 1;
	q->emptyCount = q->fullCount = 0;
	q->n = n;
	rvMonConstruct_(&q->mon);
	rvCondConstruct_(&q->cond, &q->mon);

	rvLogLeave(&rvLog, "rvQueueConstruct");

	return q;
}

void rvQueueDestruct(RvQueue* q) {
	rvLogEnter(&rvLog, "rvQueueDestruct");

	rvQueueStop(q);
	rvCondDestruct_(&q->cond);
	rvMonDestruct_(&q->mon);
	rvMemFree(q->data);

	rvLogLeave(&rvLog, "rvQueueDestruct");
}

size_t rvQueueSize(RvQueue* q) {
	size_t r;
	
	rvLogEnter(&rvLog, "rvQueueSize");

	rvMonLock_(&q->mon);
	r = rvQueueSize_(q);
	rvMonUnlock_(&q->mon);
	
	rvLogLeave(&rvLog, "rvQueueSize");

	return r;
}

int rvQueuePush(RvQueue* q, RvQueueType m) {
	int r = 0;

	rvLogEnter(&rvLog, "rvQueuePush");

	rvMonLock_(&q->mon);

	/* If queue is full ... pend */
	if (!q->done && rvQueueFull_(q)) {
		++(q->fullCount);
		rvCondWait_(&q->cond);
		--(q->fullCount);
	}

	/* Note: state of "done" could have changed while waiting above */
	if (!q->done) {

		rvQueuePush_(q, m);

		/* If Pop's are pending ... release one */
		if (q->emptyCount > 0)
			rvCondNotify_(&q->cond);
	} else
		r = (-1);					/* Indicate queue has been stopped */
	
	rvMonUnlock_(&q->mon);

	rvLogLeave(&rvLog, "rvQueuePush");

	return r;
}

int rvQueuePop(RvQueue* q, OUT RvQueueType* m) {
	int r = 0;

	rvLogEnter(&rvLog, "rvQueuePop");

	rvMonLock_(&q->mon);

    /* Check to see if the queue is not stopped and empty */
   	if(!(q->done && rvQueueEmpty_(q))) 
    {

	    /* If queue is empty and not stopped ... pend */
	    if (!q->done && rvQueueEmpty_(q)) {
		    ++(q->emptyCount);
		    rvCondWait_(&q->cond);
		    --(q->emptyCount);
	    }

	    /* Note: state of "done" could have changed while waiting above */
	    if (!(q->done && rvQueueEmpty_(q))) {

		    *m = rvQueuePop_(q);
		    
		    /* If Push's are pending ... release one */
		    if (q->fullCount > 0)
			    rvCondNotify_(&q->cond);

            if(q->done)
                r = (-1);   /* Indicate queue has been stopped */
	    } 
        else
        {
		    r = (-2);		/* Indicate queue is stopped and empty */
        }
    }
    else
    {
		r = (-2);		/* Indicate queue is stopped and empty */
    }

    
    rvMonUnlock_(&q->mon);

	rvLogLeave(&rvLog, "rvQueuePop");

	return r;
}

void rvQueueStop(RvQueue* q) {
	rvLogEnter(&rvLog, "rvQueueStop");

	rvMonLock_(&q->mon);

	q->done = 1;
	while (q->emptyCount + q->fullCount)
		rvCondNotify_(&q->cond);

	rvMonUnlock_(&q->mon);

	rvLogLeave(&rvLog, "rvQueueStop");
}

⌨️ 快捷键说明

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