queues.c

/*
    TCP Client/Server Queuing Example
    Steve Falkenburg, MacDTS, Apple Computer
    3/11/92
    
    this client/server sample uses MacTCP to implement a simple "greeting" server.  the server
    opens up several listeners on kGreetingPort (1235).  when a client connects, the data entered
    in the greeting dialog is sent to the remote connection, and the connection is closed.
    
    connection management is done through the use of Operating System queues to simplify tracking
    and usage.
*/
 
 
#include "const.h"
#include "globals.h"
#include "utils.h"
#include "queues.h"
 
static  QHdr        gFreeQueue,gCompletedQueue;     // our queue header structures
static  MyQElem     gBufferPool[kNumBuffers];       // queue storage area is here
 
 
/* initializes our queue handling scheme */
 
void InitQueues(void)
{
    short i;
    
    gFreeQueue.qFlags = 0;
    gFreeQueue.qHead = 0;
    gFreeQueue.qTail = 0;
    
    gCompletedQueue.qFlags = 0;
    gCompletedQueue.qHead = 0;
    gCompletedQueue.qTail = 0;
    
    gFree = kNumBuffers;
    gRunning = gCompleted = 0;
    gServiced = 0;
    
    for (i=0; i<kNumBuffers; i++)
        Enqueue(&gBufferPool[i],&gFreeQueue);
}
 
 
/* pulls a parameter block off of the unused queue to be used in a device manager call
*/
 
MyQElemPtr GetUnusedPBlock(void)
{
    MyQElemPtr retValue;
    
    retValue = (MyQElemPtr) gFreeQueue.qHead;
    if (retValue) {
        Dequeue(retValue,&gFreeQueue);
        gFree--;
    }
    
    return retValue;
}
 
 
/*  puts a parameter block back onto the unused queue to be picked up and used by someone else
*/
 
void RecycleFreePBlock(MyQElemPtr pBlock)
{
    Enqueue(pBlock,&gFreeQueue);
    gFree++;
}
 
 
/*  pulls a parameter block off of the completed queue for processing by the caller
*/
 
MyQElemPtr GetCompletedPBlock(void)
{
    MyQElemPtr retValue;
    
    retValue = (MyQElemPtr) gCompletedQueue.qHead;
    if (retValue) {
        Dequeue(retValue,&gCompletedQueue);
        gCompleted--;
    }
    
    return retValue;
}
 
 
/*  queues a parameter block onto the completed queue.  this is normally called from completion
    routines at interrupt time.
*/
 
void StoreCompletedPBlock(MyQElemPtr pBlock)
{
    Enqueue(pBlock,&gCompletedQueue);
    gCompleted++;
}