1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228
|
/**************************************************************************
*** COPYRIGHT (c) 2002 by TransNexus, Inc. ***
*** ***
*** This software is property of TransNexus, Inc. ***
*** This software is freely available under license from TransNexus. ***
*** The license terms and conditions for free use of this software by ***
*** third parties are defined in the OSP Toolkit Software License ***
*** Agreement (LICENSE.txt). Any use of this software by third ***
*** parties, which does not comply with the terms and conditions of the ***
*** OSP Toolkit Software License Agreement is prohibited without ***
*** the prior, express, written consent of TransNexus, Inc. ***
*** ***
*** Thank you for using the OSP ToolKit(TM). Please report any bugs, ***
*** suggestions or feedback to support@transnexus.com ***
*** ***
**************************************************************************/
/*
* syncque.cpp - SyncQueue object funcions.
*/
#include "syncque.h"
static int SyncQueueInitSync(SYNCQUEUE *);
void SyncQueueIncrementNumberOfTransactions(SYNCQUEUE *);
void SyncQueueDecrementNumberOfTransactions(SYNCQUEUE *);
/*
* Private API
*/
static int SyncQueueInitSync(SYNCQUEUE *syncQueue)
{
int errorcode = OSPC_ERR_NO_ERROR, tmperror = OSPC_ERR_NO_ERROR;
OSPM_MUTEX_INIT(syncQueue->Mutex, OSPC_OSNULL, errorcode);
if (errorcode == OSPC_ERR_NO_ERROR) {
OSPM_CONDVAR_INIT(syncQueue->CondVarNotEmpty, OSPC_OSNULL, errorcode);
if (errorcode == OSPC_ERR_NO_ERROR) {
OSPM_CONDVAR_INIT(syncQueue->CondVarEmpty, OSPC_OSNULL, errorcode);
}
if (errorcode != OSPC_ERR_NO_ERROR) {
OSPM_MUTEX_DESTROY(syncQueue->Mutex, tmperror);
}
}
return errorcode;
}
void SyncQueueIncrementNumberOfTransactions(SYNCQUEUE *syncQueue)
{
syncQueue->NumberOfTransactions++;
}
void SyncQueueDecrementNumberOfTransactions(SYNCQUEUE *syncQueue)
{
syncQueue->NumberOfTransactions--;
}
/*
* Public API
*/
int SyncQueueNew(SYNCQUEUE **syncQueue)
{
int errorcode = OSPC_ERR_NO_ERROR;
OSPM_MALLOC(*syncQueue, SYNCQUEUE, sizeof(SYNCQUEUE));
if (*syncQueue != OSPC_OSNULL) {
OSPM_MEMSET(*syncQueue, 0, sizeof(SYNCQUEUE));
(*syncQueue)->NumberOfTransactions = 0;
/*
* initialize the mutex and conditional variable for the message queue
*/
errorcode = SyncQueueInitSync(*syncQueue);
/*
* initialize the list used to hold message info structures (items)
*/
OSPPListNew(&((*syncQueue)->PayloadList));
if (errorcode != OSPC_ERR_NO_ERROR)
OSPM_FREE(*syncQueue);
} else {
errorcode = OSPC_ERR_MSGQ_NO_MEMORY;
}
return errorcode;
}
void SyncQueueDelete(SYNCQUEUE **syncQueue)
{
if (*syncQueue != OSPC_OSNULL) {
OSPM_FREE(*syncQueue);
*syncQueue = OSPC_OSNULL;
}
}
unsigned SyncQueueGetNumberOfTransactions(SYNCQUEUE *syncQueue)
{
unsigned RetVal = 0;
int errorcode = OSPC_ERR_NO_ERROR;
/* acquire mutex lock */
OSPM_MUTEX_LOCK(syncQueue->Mutex, errorcode);
if (errorcode == OSPC_ERR_NO_ERROR) {
RetVal = syncQueue->NumberOfTransactions;
/* release mutex lock */
OSPM_MUTEX_UNLOCK(syncQueue->Mutex, errorcode);
assert(errorcode == OSPC_ERR_NO_ERROR);
}
return RetVal;
}
int SyncQueueAddTransaction(SYNCQUEUE *syncQueue, void *transaction)
{
int errorcode = OSPC_ERR_NO_ERROR;
/*
* acquire message queue mutex
*/
if (errorcode == OSPC_ERR_NO_ERROR) {
OSPM_MUTEX_LOCK(syncQueue->Mutex, errorcode);
}
if (errorcode == OSPC_ERR_NO_ERROR) {
/*
* add the item to the message queue list
*/
OSPPListAppend(&(syncQueue->PayloadList), transaction);
/*
* increment number of transactions in the queue
*/
SyncQueueIncrementNumberOfTransactions(syncQueue);
/*
* signal the communication manager that a new
* transaction is now in the queue
*/
OSPM_CONDVAR_SIGNAL(syncQueue->CondVarNotEmpty, errorcode);
assert(errorcode == OSPC_ERR_NO_ERROR);
/* release msg queue mutex lock */
OSPM_MUTEX_UNLOCK(syncQueue->Mutex, errorcode);
assert(errorcode == OSPC_ERR_NO_ERROR);
}
return errorcode;
}
int SyncQueueRemoveTransaction(SYNCQUEUE *syncQueue, void **transaction)
{
int errorcode = OSPC_ERR_NO_ERROR;
/*
* acquire message queue mutex
*/
OSPM_MUTEX_LOCK(syncQueue->Mutex, errorcode);
if (errorcode == OSPC_ERR_NO_ERROR) {
/*
* wait for conditional variable on the transaction count
*/
while (0 == syncQueue->NumberOfTransactions) {
OSPM_CONDVAR_WAIT(syncQueue->CondVarNotEmpty, syncQueue->Mutex, errorcode);
}
if (errorcode == OSPC_ERR_NO_ERROR) {
/*
* thereis at least one transaction on the queue
*/
*transaction = OSPPListRemove(&(syncQueue->PayloadList));
SyncQueueDecrementNumberOfTransactions(syncQueue);
/*
* Are there any more transactions on the queue ?
*/
if (0 == syncQueue->NumberOfTransactions) {
OSPM_CONDVAR_SIGNAL(syncQueue->CondVarEmpty, errorcode);
}
}
/*
* release the mutex lock
*/
OSPM_MUTEX_UNLOCK(syncQueue->Mutex, errorcode);
}
return errorcode;
}
int SyncQueueBlockWhileNotEmpty(SYNCQUEUE *syncQueue)
{
int errorcode = OSPC_ERR_NO_ERROR;
/*
* acquire message queue mutex
*/
OSPM_MUTEX_LOCK(syncQueue->Mutex, errorcode);
if (errorcode == OSPC_ERR_NO_ERROR) {
/*
* wait for conditional variable on the transaction count
*/
while (syncQueue->NumberOfTransactions > 0) {
OSPM_CONDVAR_WAIT(syncQueue->CondVarEmpty, syncQueue->Mutex, errorcode);
}
/*
* release the mutex lock
*/
OSPM_MUTEX_UNLOCK(syncQueue->Mutex, errorcode);
}
return errorcode;
}
|