123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443 |
- #define BUILDING_OPENDOORS
- #include <stdlib.h>
- #include <string.h>
- #include "OpenDoor.h"
- #include "ODGen.h"
- #include "ODInQue.h"
- #include "ODPlat.h"
- #include "ODKrnl.h"
- typedef struct
- {
- tODInputEvent *paEvents;
- INT nQueueEntries;
- INT nInIndex;
- INT nOutIndex;
- time_t nLastActivityTime;
- #ifdef OD_MULTITHREADED
- tODSemaphoreHandle hItemCountSemaphore;
- tODSemaphoreHandle hAddEventSemaphore;
- #endif
- } tInputQueueInfo;
- tODResult ODInQueueAlloc(tODInQueueHandle *phInQueue, INT nInitialQueueSize)
- {
- tInputQueueInfo *pInputQueueInfo = NULL;
- tODInputEvent *pInputQueue = NULL;
- tODResult Result = kODRCNoMemory;
- ASSERT(phInQueue != NULL);
- if(phInQueue == NULL) return(kODRCInvalidCall);
-
- pInputQueueInfo = malloc(sizeof(tInputQueueInfo));
-
- if(pInputQueueInfo == NULL) goto CleanUp;
-
- #ifdef OD_MULTITHREADED
- pInputQueueInfo->hItemCountSemaphore = NULL;
- pInputQueueInfo->hAddEventSemaphore = NULL;
- #endif
-
-
- pInputQueue = calloc(nInitialQueueSize, sizeof(tODInputEvent));
- if(pInputQueue == NULL) goto CleanUp;
-
- #ifdef OD_MULTITHREADED
- if(ODSemaphoreAlloc(&pInputQueueInfo->hItemCountSemaphore, 0,
- nInitialQueueSize) != kODRCSuccess)
- {
- goto CleanUp;
- }
- if(ODSemaphoreAlloc(&pInputQueueInfo->hAddEventSemaphore, 1, 1)
- != kODRCSuccess)
- {
- goto CleanUp;
- }
- #endif
-
- pInputQueueInfo->paEvents = pInputQueue;
- pInputQueueInfo->nQueueEntries = nInitialQueueSize;
- pInputQueueInfo->nInIndex = 0;
- pInputQueueInfo->nOutIndex = 0;
-
- *phInQueue = ODPTR2HANDLE(pInputQueueInfo, tInputQueueInfo);
-
- ODInQueueResetLastActivity(*phInQueue);
- Result = kODRCSuccess;
- CleanUp:
- if(Result != kODRCSuccess)
- {
- #ifdef OD_MULTITHREADED
- if(pInputQueueInfo != NULL
- && pInputQueueInfo->hItemCountSemaphore != NULL)
- {
- ODSemaphoreFree(pInputQueueInfo->hItemCountSemaphore);
- }
- if(pInputQueueInfo != NULL
- && pInputQueueInfo->hAddEventSemaphore != NULL)
- {
- ODSemaphoreFree(pInputQueueInfo->hAddEventSemaphore);
- }
- #endif
- if(pInputQueue != NULL) free(pInputQueue);
- if(pInputQueueInfo != NULL) free(pInputQueueInfo);
- *phInQueue = ODPTR2HANDLE(NULL, tInputQueueInfo);
- }
-
- return(Result);
- }
- void ODInQueueFree(tODInQueueHandle hInQueue)
- {
- tInputQueueInfo *pInputQueueInfo = ODHANDLE2PTR(hInQueue, tInputQueueInfo);
- ASSERT(pInputQueueInfo != NULL);
-
- #ifdef OD_MULTITHREADED
- ASSERT(pInputQueueInfo->hItemCountSemaphore != NULL);
- ODSemaphoreFree(pInputQueueInfo->hItemCountSemaphore);
- #endif
-
- ASSERT(pInputQueueInfo->paEvents != NULL);
- free(pInputQueueInfo->paEvents);
-
- free(pInputQueueInfo);
- }
- BOOL ODInQueueWaiting(tODInQueueHandle hInQueue)
- {
- tInputQueueInfo *pInputQueueInfo = ODHANDLE2PTR(hInQueue, tInputQueueInfo);
- BOOL bEventWaiting;
- ASSERT(pInputQueueInfo != NULL);
-
-
- bEventWaiting = (pInputQueueInfo->nInIndex != pInputQueueInfo->nOutIndex);
- return(bEventWaiting);
- }
- tODResult ODInQueueAddEvent(tODInQueueHandle hInQueue,
- tODInputEvent *pEvent)
- {
- tInputQueueInfo *pInputQueueInfo = ODHANDLE2PTR(hInQueue, tInputQueueInfo);
- INT nNextInPos;
- ASSERT(pInputQueueInfo != NULL);
- ASSERT(pEvent != NULL);
- if(pInputQueueInfo == NULL || pEvent == NULL) return(kODRCInvalidCall);
-
- #ifdef OD_MULTITHREADED
- ODSemaphoreDown(pInputQueueInfo->hAddEventSemaphore, OD_NO_TIMEOUT);
- #endif
-
- ODInQueueResetLastActivity(hInQueue);
-
-
- nNextInPos = (pInputQueueInfo->nInIndex + 1)
- % pInputQueueInfo->nQueueEntries;
-
- if(nNextInPos == pInputQueueInfo->nOutIndex)
- {
-
- #ifdef OD_MULTITHREADED
- ODSemaphoreUp(pInputQueueInfo->hAddEventSemaphore, 1);
- #endif
- return(kODRCNoMemory);
- }
-
- memcpy(&pInputQueueInfo->paEvents[pInputQueueInfo->nInIndex], pEvent,
- sizeof(tODInputEvent));
-
- pInputQueueInfo->nInIndex = nNextInPos;
-
- #ifdef OD_MULTITHREADED
- ODSemaphoreUp(pInputQueueInfo->hItemCountSemaphore, 1);
- #endif
-
- #ifdef OD_MULTITHREADED
- ODSemaphoreUp(pInputQueueInfo->hAddEventSemaphore, 1);
- #endif
- return(kODRCSuccess);
- }
- tODResult ODInQueueGetNextEvent(tODInQueueHandle hInQueue,
- tODInputEvent *pEvent, tODMilliSec Timeout)
- {
- tInputQueueInfo *pInputQueueInfo = ODHANDLE2PTR(hInQueue, tInputQueueInfo);
- ASSERT(pInputQueueInfo != NULL);
- ASSERT(pEvent != NULL);
- #ifdef OD_MULTITHREADED
-
-
-
-
- if(ODSemaphoreDown(pInputQueueInfo->hItemCountSemaphore, Timeout)==kODRCTimeout)
- return(kODRCTimeout);
- #else
-
-
-
-
-
-
- if(pInputQueueInfo->nInIndex == pInputQueueInfo->nOutIndex)
- {
- tODTimer Timer;
-
-
- if(Timeout != 0 && Timeout != OD_NO_TIMEOUT)
- {
- ODTimerStart(&Timer, Timeout);
- }
-
-
- CALL_KERNEL_IF_NEEDED();
-
-
- while(pInputQueueInfo->nInIndex == pInputQueueInfo->nOutIndex)
- {
-
-
- if(Timeout != 0 && Timeout != OD_NO_TIMEOUT
- && ODTimerElapsed(&Timer))
- {
- return(kODRCTimeout);
- }
-
- od_sleep(0);
-
- CALL_KERNEL_IF_NEEDED();
- }
- }
- #endif
-
- memcpy(pEvent, &pInputQueueInfo->paEvents[pInputQueueInfo->nOutIndex],
- sizeof(tODInputEvent));
-
-
- pInputQueueInfo->nOutIndex
- = (pInputQueueInfo->nOutIndex + 1) % pInputQueueInfo->nQueueEntries;
-
- return(kODRCSuccess);
- }
- void ODInQueueEmpty(tODInQueueHandle hInQueue)
- {
- tODInputEvent InputEvent;
- ASSERT(hInQueue != NULL);
-
- while(ODInQueueWaiting(hInQueue))
- {
- ODInQueueGetNextEvent(hInQueue, &InputEvent, OD_NO_TIMEOUT);
- }
- }
- time_t ODInQueueGetLastActivity(tODInQueueHandle hInQueue)
- {
- tInputQueueInfo *pInputQueueInfo = ODHANDLE2PTR(hInQueue, tInputQueueInfo);
- ASSERT(pInputQueueInfo != NULL);
-
- return(pInputQueueInfo->nLastActivityTime);
- }
- void ODInQueueResetLastActivity(tODInQueueHandle hInQueue)
- {
- tInputQueueInfo *pInputQueueInfo = ODHANDLE2PTR(hInQueue, tInputQueueInfo);
- ASSERT(pInputQueueInfo != NULL);
-
- pInputQueueInfo->nLastActivityTime = time(NULL);
- }
|