ODInQue.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443
  1. /* OpenDoors Online Software Programming Toolkit
  2. * (C) Copyright 1991 - 1999 by Brian Pirie.
  3. *
  4. * This library is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU Lesser General Public
  6. * License as published by the Free Software Foundation; either
  7. * version 2 of the License, or (at your option) any later version.
  8. *
  9. * This library is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * Lesser General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU Lesser General Public
  15. * License along with this library; if not, write to the Free Software
  16. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  17. *
  18. *
  19. * File: ODInQue.h
  20. *
  21. * Description: OpenDoors input queue management. This input queue is where
  22. * all input events (e.g. keystrokes) from both local and remote
  23. * systems are combined into a single stream.
  24. *
  25. * Revisions: Date Ver Who Change
  26. * ---------------------------------------------------------------
  27. * Nov 16, 1995 6.00 BP Created.
  28. * Nov 17, 1995 6.00 BP Added multithreading support.
  29. * Jan 04, 1996 6.00 BP tODInQueueEvent -> tODInputEvent.
  30. * Jan 30, 1996 6.00 BP Replaced od_yield() with od_sleep().
  31. * Jan 30, 1996 6.00 BP Add semaphore timeout.
  32. * Jan 30, 1996 6.00 BP Add ODInQueueGetNextEvent() timeout.
  33. * Feb 19, 1996 6.00 BP Changed version number to 6.00.
  34. * Mar 03, 1996 6.10 BP Begin version 6.10.
  35. * Aug 10, 2003 6.23 SH *nix support
  36. */
  37. #define BUILDING_OPENDOORS
  38. #include <stdlib.h>
  39. #include <string.h>
  40. #include "OpenDoor.h"
  41. #include "ODGen.h"
  42. #include "ODInQue.h"
  43. #include "ODPlat.h"
  44. #include "ODKrnl.h"
  45. /* Input queue handle structure. */
  46. typedef struct
  47. {
  48. tODInputEvent *paEvents;
  49. INT nQueueEntries;
  50. INT nInIndex;
  51. INT nOutIndex;
  52. time_t nLastActivityTime;
  53. #ifdef OD_MULTITHREADED
  54. tODSemaphoreHandle hItemCountSemaphore;
  55. tODSemaphoreHandle hAddEventSemaphore;
  56. #endif /* OD_MULTITHREADED */
  57. } tInputQueueInfo;
  58. /* ----------------------------------------------------------------------------
  59. * ODInQueueAlloc()
  60. *
  61. * Allocates a new input queue.
  62. *
  63. * Parameters: phInQueue - Pointer to location where a handle to the
  64. * newly allocated input queue should be
  65. * stored.
  66. *
  67. * nInitialQueueSize - The minimum number of events that the
  68. * input queue should be able to hold.
  69. *
  70. * Return: kODRCSuccess on success, or an error code on failure.
  71. */
  72. tODResult ODInQueueAlloc(tODInQueueHandle *phInQueue, INT nInitialQueueSize)
  73. {
  74. tInputQueueInfo *pInputQueueInfo = NULL;
  75. tODInputEvent *pInputQueue = NULL;
  76. tODResult Result = kODRCNoMemory;
  77. ASSERT(phInQueue != NULL);
  78. if(phInQueue == NULL) return(kODRCInvalidCall);
  79. /* Attempt to allocate a serial port information structure. */
  80. pInputQueueInfo = malloc(sizeof(tInputQueueInfo));
  81. /* If memory allocation failed, return with failure. */
  82. if(pInputQueueInfo == NULL) goto CleanUp;
  83. /* Initialize semaphore handles to NULL. */
  84. #ifdef OD_MULTITHREADED
  85. pInputQueueInfo->hItemCountSemaphore = NULL;
  86. pInputQueueInfo->hAddEventSemaphore = NULL;
  87. #endif /* OD_MULTITHREADED */
  88. /* Attempt to allocate space for the queue itself. */
  89. pInputQueue = calloc(nInitialQueueSize, sizeof(tODInputEvent));
  90. if(pInputQueue == NULL) goto CleanUp;
  91. /* Create semaphores if this is a multithreaded platform. */
  92. #ifdef OD_MULTITHREADED
  93. if(ODSemaphoreAlloc(&pInputQueueInfo->hItemCountSemaphore, 0,
  94. nInitialQueueSize) != kODRCSuccess)
  95. {
  96. goto CleanUp;
  97. }
  98. if(ODSemaphoreAlloc(&pInputQueueInfo->hAddEventSemaphore, 1, 1)
  99. != kODRCSuccess)
  100. {
  101. goto CleanUp;
  102. }
  103. #endif /* OD_MULTITHREADED */
  104. /* Initialize input queue information structure. */
  105. pInputQueueInfo->paEvents = pInputQueue;
  106. pInputQueueInfo->nQueueEntries = nInitialQueueSize;
  107. pInputQueueInfo->nInIndex = 0;
  108. pInputQueueInfo->nOutIndex = 0;
  109. /* Convert intut queue information structure pointer to a handle. */
  110. *phInQueue = ODPTR2HANDLE(pInputQueueInfo, tInputQueueInfo);
  111. /* Reset the time of the last activity. */
  112. ODInQueueResetLastActivity(*phInQueue);
  113. Result = kODRCSuccess;
  114. CleanUp:
  115. if(Result != kODRCSuccess)
  116. {
  117. #ifdef OD_MULTITHREADED
  118. if(pInputQueueInfo != NULL
  119. && pInputQueueInfo->hItemCountSemaphore != NULL)
  120. {
  121. ODSemaphoreFree(pInputQueueInfo->hItemCountSemaphore);
  122. }
  123. if(pInputQueueInfo != NULL
  124. && pInputQueueInfo->hAddEventSemaphore != NULL)
  125. {
  126. ODSemaphoreFree(pInputQueueInfo->hAddEventSemaphore);
  127. }
  128. #endif /* OD_MULTITHREADED */
  129. if(pInputQueue != NULL) free(pInputQueue);
  130. if(pInputQueueInfo != NULL) free(pInputQueueInfo);
  131. *phInQueue = ODPTR2HANDLE(NULL, tInputQueueInfo);
  132. }
  133. /* Return with the appropriate result code. */
  134. return(Result);
  135. }
  136. /* ----------------------------------------------------------------------------
  137. * ODInQueueFree()
  138. *
  139. * Destroys an input queue that was previously created by ODInQueueAlloc().
  140. *
  141. * Parameters: hInQueue - Handle to the input queue to destroy.
  142. *
  143. * Return: void
  144. */
  145. void ODInQueueFree(tODInQueueHandle hInQueue)
  146. {
  147. tInputQueueInfo *pInputQueueInfo = ODHANDLE2PTR(hInQueue, tInputQueueInfo);
  148. ASSERT(pInputQueueInfo != NULL);
  149. /* Deallocate semaphores, if appropriate. */
  150. #ifdef OD_MULTITHREADED
  151. ASSERT(pInputQueueInfo->hItemCountSemaphore != NULL);
  152. ODSemaphoreFree(pInputQueueInfo->hItemCountSemaphore);
  153. #endif /* OD_MULTITHREADED */
  154. /* Deallocate the input queue itself. */
  155. ASSERT(pInputQueueInfo->paEvents != NULL);
  156. free(pInputQueueInfo->paEvents);
  157. /* Deallocate port information structure. */
  158. free(pInputQueueInfo);
  159. }
  160. /* ----------------------------------------------------------------------------
  161. * ODInQueueWaiting()
  162. *
  163. * Determines whether or not an event is currently waiting in the input queue.
  164. *
  165. * Parameters: hInQueue - Handle to the input queue to check.
  166. *
  167. * Return: TRUE if there is one or more waiting events, or FALSE if the
  168. * queue is empty.
  169. */
  170. BOOL ODInQueueWaiting(tODInQueueHandle hInQueue)
  171. {
  172. tInputQueueInfo *pInputQueueInfo = ODHANDLE2PTR(hInQueue, tInputQueueInfo);
  173. BOOL bEventWaiting;
  174. ASSERT(pInputQueueInfo != NULL);
  175. /* There is data waiting in the queue if the in index is not equal to */
  176. /* the out index. */
  177. bEventWaiting = (pInputQueueInfo->nInIndex != pInputQueueInfo->nOutIndex);
  178. return(bEventWaiting);
  179. }
  180. /* ----------------------------------------------------------------------------
  181. * ODInQueueAddEvent()
  182. *
  183. * Adds a new event to the input queue.
  184. *
  185. * Parameters: hInQueue - Handle to the input queue to add an event to.
  186. *
  187. * pEvent - Pointer to the event structure to obtain the
  188. * event information from.
  189. *
  190. * Return: kODRCSuccess on success, or an error code on failure.
  191. */
  192. tODResult ODInQueueAddEvent(tODInQueueHandle hInQueue,
  193. tODInputEvent *pEvent)
  194. {
  195. tInputQueueInfo *pInputQueueInfo = ODHANDLE2PTR(hInQueue, tInputQueueInfo);
  196. INT nNextInPos;
  197. ASSERT(pInputQueueInfo != NULL);
  198. ASSERT(pEvent != NULL);
  199. if(pInputQueueInfo == NULL || pEvent == NULL) return(kODRCInvalidCall);
  200. /* Serialize access to add event function. */
  201. #ifdef OD_MULTITHREADED
  202. ODSemaphoreDown(pInputQueueInfo->hAddEventSemaphore, OD_NO_TIMEOUT);
  203. #endif /* OD_MULTITHREADED */
  204. /* Reset the time of the last activity. */
  205. ODInQueueResetLastActivity(hInQueue);
  206. /* Determine what the next in index would be after this addition to the */
  207. /* queue. */
  208. nNextInPos = (pInputQueueInfo->nInIndex + 1)
  209. % pInputQueueInfo->nQueueEntries;
  210. /* If the queue is full, then return an out of space error. */
  211. if(nNextInPos == pInputQueueInfo->nOutIndex)
  212. {
  213. /* Allow further access to input queue. */
  214. #ifdef OD_MULTITHREADED
  215. ODSemaphoreUp(pInputQueueInfo->hAddEventSemaphore, 1);
  216. #endif /* OD_MULTITHREADED */
  217. return(kODRCNoMemory);
  218. }
  219. /* Otherwise, add the new event to the input queue. */
  220. memcpy(&pInputQueueInfo->paEvents[pInputQueueInfo->nInIndex], pEvent,
  221. sizeof(tODInputEvent));
  222. /* Update queue in index. */
  223. pInputQueueInfo->nInIndex = nNextInPos;
  224. /* Increment queue items count semaphore. */
  225. #ifdef OD_MULTITHREADED
  226. ODSemaphoreUp(pInputQueueInfo->hItemCountSemaphore, 1);
  227. #endif /* OD_MULTITHREADED */
  228. /* Allow further access to add event function. */
  229. #ifdef OD_MULTITHREADED
  230. ODSemaphoreUp(pInputQueueInfo->hAddEventSemaphore, 1);
  231. #endif /* OD_MULTITHREADED */
  232. return(kODRCSuccess);
  233. }
  234. /* ----------------------------------------------------------------------------
  235. * ODInQueueGetNextEvent()
  236. *
  237. * Obtains the next event from the input queue. If no events are currently
  238. * waiting in the input queue, this function blocks until an item is added
  239. * to the queue, or the maximum wait time is reached.
  240. *
  241. * Parameters: hInQueue - Handle to the input queue to obtain the next event
  242. * from.
  243. *
  244. * pEvent - Pointer to structure to store input event information
  245. * in.
  246. *
  247. * Timeout - Maximum time, in milliseconds, to wait for next input
  248. * event. A value of OD_NO_TIMEOUT causes this function
  249. * to only return when an input event is obtained.
  250. *
  251. * Return: kODRCSuccess on succes, or kODRCTimeout if the maximum wait time
  252. * is exceeded.
  253. */
  254. tODResult ODInQueueGetNextEvent(tODInQueueHandle hInQueue,
  255. tODInputEvent *pEvent, tODMilliSec Timeout)
  256. {
  257. tInputQueueInfo *pInputQueueInfo = ODHANDLE2PTR(hInQueue, tInputQueueInfo);
  258. ASSERT(pInputQueueInfo != NULL);
  259. ASSERT(pEvent != NULL);
  260. #ifdef OD_MULTITHREADED
  261. /* In multithreaded implementations, we wait for there to be an item in */
  262. /* the queue by decrementing the queue size semaphore. This will cause */
  263. /* this thread to be blocked until an event is added to the queue, if it */
  264. /* is currently empty. */
  265. if(ODSemaphoreDown(pInputQueueInfo->hItemCountSemaphore, Timeout)==kODRCTimeout)
  266. return(kODRCTimeout);
  267. #else /* !OD_MULTITHREADED */
  268. /* In non-multithreaded implementations, we check queue in and out */
  269. /* indicies to determine whether there are any events waiting in the */
  270. /* queue. If the queue is empty we loop, calling od_kernel() to check */
  271. /* for new events and od_yeild() to give more time to other processors */
  272. /* if there is nothing for us to do, until an event is added to the */
  273. /* queue. */
  274. if(pInputQueueInfo->nInIndex == pInputQueueInfo->nOutIndex)
  275. {
  276. tODTimer Timer;
  277. /* If a timeout has been specified, then start timer to keep track */
  278. /* of how long we have been waiting. */
  279. if(Timeout != 0 && Timeout != OD_NO_TIMEOUT)
  280. {
  281. ODTimerStart(&Timer, Timeout);
  282. }
  283. /* As soon as we see that there is nothing in the queue, we do an */
  284. /* od_kernel() call to check for new input. */
  285. CALL_KERNEL_IF_NEEDED();
  286. /* As long as we don't have new input, we loop, yielding to other */
  287. /* processes, and then giving od_kernel() a chance to run. */
  288. while(pInputQueueInfo->nInIndex == pInputQueueInfo->nOutIndex)
  289. {
  290. /* If a timeout has been specified, then ensure that the maximum */
  291. /* wait time has not elapsed. */
  292. if(Timeout != 0 && Timeout != OD_NO_TIMEOUT
  293. && ODTimerElapsed(&Timer))
  294. {
  295. return(kODRCTimeout);
  296. }
  297. /* Yield the processor to other tasks. */
  298. od_sleep(0);
  299. /* Call od_kernel(). */
  300. CALL_KERNEL_IF_NEEDED();
  301. }
  302. }
  303. #endif /* !OD_MULTITHREADED */
  304. /* Copy next input event from the queue into the caller's structure. */
  305. memcpy(pEvent, &pInputQueueInfo->paEvents[pInputQueueInfo->nOutIndex],
  306. sizeof(tODInputEvent));
  307. /* Move out pointer to the next queue item, wrapping back to the start */
  308. /* of the queue if needed. */
  309. pInputQueueInfo->nOutIndex
  310. = (pInputQueueInfo->nOutIndex + 1) % pInputQueueInfo->nQueueEntries;
  311. /* Now, return with success. */
  312. return(kODRCSuccess);
  313. }
  314. /* ----------------------------------------------------------------------------
  315. * ODInQueueEmpty()
  316. *
  317. * Removes all events from the input queue.
  318. *
  319. * Parameters: hInQueue - Handle to the input queue to be emptied.
  320. *
  321. * Return: void
  322. */
  323. void ODInQueueEmpty(tODInQueueHandle hInQueue)
  324. {
  325. tODInputEvent InputEvent;
  326. ASSERT(hInQueue != NULL);
  327. /* Remove all items from the queue. */
  328. while(ODInQueueWaiting(hInQueue))
  329. {
  330. ODInQueueGetNextEvent(hInQueue, &InputEvent, OD_NO_TIMEOUT);
  331. }
  332. }
  333. /* ----------------------------------------------------------------------------
  334. * ODInQueueGetLastActivity()
  335. *
  336. * Returns the time of the last input activity. This is the latest of the time
  337. * that the queue was created, the time of the last call to
  338. * ODInQueueAddEvent() on this input queue, and the time of the last call to
  339. * ODInQueueResetLastActivity() on this input queue.
  340. *
  341. * Parameters: hInQueue - Handle to the input queue.
  342. *
  343. * Return: void
  344. */
  345. time_t ODInQueueGetLastActivity(tODInQueueHandle hInQueue)
  346. {
  347. tInputQueueInfo *pInputQueueInfo = ODHANDLE2PTR(hInQueue, tInputQueueInfo);
  348. ASSERT(pInputQueueInfo != NULL);
  349. /* Returns the last activity time. */
  350. return(pInputQueueInfo->nLastActivityTime);
  351. }
  352. /* ----------------------------------------------------------------------------
  353. * ODInQueueResetLastActivity()
  354. *
  355. * Resets the time of the last input activity to the current time.
  356. *
  357. * Parameters: hInQueue - Handle to the input queue.
  358. *
  359. * Return: void
  360. */
  361. void ODInQueueResetLastActivity(tODInQueueHandle hInQueue)
  362. {
  363. tInputQueueInfo *pInputQueueInfo = ODHANDLE2PTR(hInQueue, tInputQueueInfo);
  364. ASSERT(pInputQueueInfo != NULL);
  365. /* Resets the last activity time to the current time. */
  366. pInputQueueInfo->nLastActivityTime = time(NULL);
  367. }