home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / nspr30-e.zip / nspr30-e / include / prmwait.h < prev    next >
C/C++ Source or Header  |  1998-11-02  |  17KB  |  394 lines

  1. /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
  2. /*
  3.  * The contents of this file are subject to the Netscape Public License
  4.  * Version 1.0 (the "NPL"); you may not use this file except in
  5.  * compliance with the NPL.  You may obtain a copy of the NPL at
  6.  * http://www.mozilla.org/NPL/
  7.  * 
  8.  * Software distributed under the NPL is distributed on an "AS IS" basis,
  9.  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the NPL
  10.  * for the specific language governing rights and limitations under the
  11.  * NPL.
  12.  * 
  13.  * The Initial Developer of this code under the NPL is Netscape
  14.  * Communications Corporation.  Portions created by Netscape are
  15.  * Copyright (C) 1998 Netscape Communications Corporation.  All Rights
  16.  * Reserved.
  17.  */
  18.  
  19. #if defined(_PRMWAIT_H)
  20. #else
  21. #define _PRMWAIT_H
  22.  
  23. #include "prio.h"
  24. #include "prtypes.h"
  25. #include "prclist.h"
  26.  
  27. PR_BEGIN_EXTERN_C
  28.  
  29. /********************************************************************************/
  30. /********************************************************************************/
  31. /********************************************************************************/
  32. /******************************       WARNING        ****************************/
  33. /********************************************************************************/
  34. /**************************** This is work in progress. *************************/
  35. /************************** Do not make any assumptions *************************/
  36. /************************** about the stability of this *************************/
  37. /************************** API or the underlying imple- ************************/
  38. /************************** mentation.                   ************************/
  39. /********************************************************************************/
  40. /********************************************************************************/
  41.  
  42. /*
  43. ** STRUCTURE:   PRWaitGroup
  44. ** DESCRIPTION:
  45. **      The client may define several wait groups in order to semantically
  46. **      tie a collection of file descriptors for a single purpose. This allows
  47. **      easier dispatching of threads that returned with active file descriptors
  48. **      from the wait function.
  49. */
  50. typedef struct PRWaitGroup PRWaitGroup;
  51.  
  52. /*
  53. ** ENUMERATION: PRMWStatus
  54. ** DESCRIPTION:
  55. **      This enumeration is used to indicate the completion status of
  56. **      a receive wait object. Generally stated, a positive value indicates
  57. **      that the operation is not yet complete. A zero value indicates
  58. **      success (similar to PR_SUCCESS) and any negative value is an
  59. **      indication of failure. The reason for the failure can be retrieved
  60. **      by calling PR_GetError().
  61. **
  62. **  PR_MW_PENDING       The operation is still pending. None of the other
  63. **                      fields of the object are currently valid.
  64. **  PR_MW_SUCCESS       The operation is complete and it was successful.
  65. **  PR_MW_FAILURE       The operation failed. The reason for the failure
  66. **                      can be retrieved by calling PR_GetError().
  67. **  PR_MW_TIMEOUT       The amount of time allowed for by the object's
  68. **                      'timeout' field has expired w/o the operation
  69. **                      otherwise coming to closure.
  70. **  PR_MW_INTERRUPT     The operation was cancelled, either by the client
  71. **                      calling PR_CancelWaitFileDesc() or destroying the
  72. **                      entire wait group (PR_DestroyWaitGroup()).
  73. */
  74. typedef enum PRMWStatus
  75. {
  76.     PR_MW_PENDING = 1,
  77.     PR_MW_SUCCESS = 0,
  78.     PR_MW_FAILURE = -1,
  79.     PR_MW_TIMEOUT = -2,
  80.     PR_MW_INTERRUPT = -3
  81. } PRMWStatus;
  82.  
  83. /*
  84. ** STRUCTURE:   PRMemoryDescriptor
  85. ** DESCRIPTION:
  86. **      THis is a descriptor for an interval of memory. It contains a
  87. **      pointer to the first byte of that memory and the length (in
  88. **      bytes) of the interval.
  89. */
  90. typedef struct PRMemoryDescriptor
  91. {
  92.     void *start;                /* pointer to first byte of memory */
  93.     PRSize length;              /* length (in bytes) of memory interval */
  94. } PRMemoryDescriptor;
  95.  
  96. /*
  97. ** STRUCTURE:   PRMWaitClientData
  98. ** DESCRIPTION:
  99. **      An opague stucture for which a client MAY give provide a concrete
  100. **      definition and associate with a receive descriptor. The NSPR runtime
  101. **      does not manage this field. It is completely up to the client.
  102. */
  103. typedef struct PRMWaitClientData PRMWaitClientData;
  104.  
  105. /*
  106. ** STRUCTURE:   PRRecvWait
  107. ** DESCRIPTION:
  108. **      A receive wait object contains the file descriptor that is subject
  109. **      to the wait and the amount of time (beginning epoch established
  110. **      when the object is presented to the runtime) the the channel should
  111. **      block before abandoning the process.
  112. **
  113. **      The success of the wait operation will be noted in the object's
  114. **      'outcome' field. The fields are not valid when the NSPR runtime
  115. **      is in possession of the object.
  116. **
  117. **      The memory descriptor describes an interval of writable memory
  118. **      in the caller's address space where data from an initial read
  119. **      can be placed. The description may indicate a null interval.
  120. */
  121. typedef struct PRRecvWait 
  122. {
  123.     PRCList internal;           /* internal runtime linkages */
  124.  
  125.     PRFileDesc *fd;             /* file descriptor associated w/ object */
  126.     PRMWStatus outcome;         /* outcome of the current/last operation */
  127.     PRIntervalTime timeout;     /* time allowed for entire operation */
  128.  
  129.     PRInt32 bytesRecv;          /* number of bytes transferred into buffer */
  130.     PRMemoryDescriptor buffer;  /* where to store first segment of input data */
  131.     PRMWaitClientData *client;  /* pointer to arbitrary client defined data */
  132. } PRRecvWait;
  133.  
  134. /*
  135. ** STRUCTURE:   PRMWaitEnumerator
  136. ** DESCRIPTION:
  137. **      An enumeration object is used to store the state of an existing
  138. **      enumeration over a wait group. The opaque object must be allocated
  139. **      by the client and the reference presented on each call to the
  140. **      pseudo-stateless enumerator. The enumeration objects are sharable
  141. **      only in serial fashion.
  142. */
  143. typedef struct PRMWaitEnumerator PRMWaitEnumerator;
  144.  
  145.  
  146. /*
  147. ** FUNCTION:    PR_AddWaitFileDesc
  148. ** DESCRIPTION:
  149. **      This function will effectively add a file descriptor to the
  150. **      list of those waiting for network receive. The new descriptor
  151. **      will be semantically tied to the wait group specified.
  152. **
  153. **      The ownership for the storage pointed to by 'desc' is temporarily
  154. **      passed over the the NSPR runtime. It will be handed back by the
  155. **      function PR_WaitRecvReady().
  156. **
  157. **  INPUTS
  158. **      group       A reference to a PRWaitGroup or NULL. Wait groups are
  159. **                  created by calling PR_CreateWaitGroup() and are used
  160. **                  to semantically group various file descriptors by the
  161. **                  client's application.
  162. **      desc        A reference to a valid PRRecvWait. The object of the
  163. **                  reference must be preserved and not be modified
  164. **                  until its ownership is returned to the client.
  165. **  RETURN
  166. **      PRStatus    An indication of success. If equal to PR_FAILUE details
  167. **                  of the failure are avaiable via PR_GetError().
  168. **
  169. **  ERRORS
  170. **      PR_INVALID_ARGUMENT_ERROR
  171. **                  Invalid 'group' identifier or duplicate 'desc' object.
  172. **      PR_OUT_OF_MEMORY_ERROR
  173. **                  Insuffient memory for internal data structures.
  174. **      PR_INVALID_STATE_ERROR
  175. **                  The group is being destroyed.
  176. */
  177. PR_EXTERN(PRStatus) PR_AddWaitFileDesc(PRWaitGroup *group, PRRecvWait *desc);
  178.  
  179. /*
  180. ** FUNCTION:    PR_WaitRecvReady
  181. ** DESCRIPTION:
  182. **      PR_WaitRecvReady will block the calling thread until one of the
  183. **      file descriptors that have been added via PR_AddWaitFileDesc is
  184. **      available for input I/O.
  185. **  INPUT
  186. **      group       A pointer to a valid PRWaitGroup or NULL (the null
  187. **                  group. The function will block the caller until a
  188. **                  channel from the wait group becomes ready for receive
  189. **                  or there is some sort of error.
  190. **  RETURN
  191. **      PRReciveWait
  192. **                  When the caller is resumed it is either returned a
  193. **                  valid pointer to a previously added receive wait or
  194. **                  a NULL. If the latter, the function has terminated
  195. **                  for a reason that can be determined by calling
  196. **                  PR_GetError().
  197. **                  If a valid pointer is returned, the reference is to the
  198. **                  file descriptor contained in the receive wait object.
  199. **                  The outcome of the wait operation may still fail, and
  200. **                  if it has, that fact will be noted in the object's
  201. **                  outcome field. Details can be retrieved from PR_GetError().
  202. **
  203. **  ERRORS
  204. **      PR_INVALID_ARGUMENT_ERROR
  205. **                  The 'group' is not known by the runtime.
  206. **      PR_PENDING_INTERRUPT_ERROR
  207.                     The thread was interrupted.
  208. **      PR_INVALID_STATE_ERROR
  209. **                  The group is being destroyed.
  210. */
  211. PR_EXTERN(PRRecvWait*) PR_WaitRecvReady(PRWaitGroup *group);
  212.  
  213. /*
  214. ** FUNCTION:    PR_CancelWaitFileDesc
  215. ** DESCRIPTION:
  216. **      PR_CancelWaitFileDesc is provided as a means for cancelling operations
  217. **      on objects previously submitted by use of PR_AddWaitFileDesc(). If
  218. **      the runtime knows of the object, it will be marked as having failed
  219. **      because it was interrupted (similar to PR_Interrupt()). The first
  220. **      available thread waiting on the group will be made to return the
  221. **      PRRecvWait object with the outcome noted.
  222. **
  223. **  INPUTS
  224. **      group       The wait group under which the wait receive object was
  225. **                  added.
  226. **      desc        A pointer to the wait receive object that is to be
  227. **                  cancelled.
  228. **  RETURN
  229. **      PRStatus    If the wait receive object was located and associated
  230. **                  with the specified wait group, the status returned will
  231. **                  be PR_SUCCESS. There is still a race condition that would
  232. **                  permit the offected object to complete normally, but it
  233. **                  is assured that it will complete in the near future.
  234. **                  If the receive object or wait group are invalid, the
  235. **                  function will return with a status of PR_FAILURE.
  236. **
  237. **  ERRORS
  238. **      PR_INVALID_ARGUMENT_ERROR
  239. **                  The 'group' argument is not recognized as a valid group.
  240. **      PR_COLLECTION_EMPTY_ERROR
  241. **                  There are no more receive wait objects in the group's
  242. **                  collection.
  243. **      PR_INVALID_STATE_ERROR
  244. **                  The group is being destroyed.
  245. */
  246. PR_EXTERN(PRStatus) PR_CancelWaitFileDesc(PRWaitGroup *group, PRRecvWait *desc);
  247.  
  248. /*
  249. ** FUNCTION:    PR_CancelWaitGroup
  250. ** DESCRIPTION:
  251. **      PR_CancelWaitGroup is provided as a means for cancelling operations
  252. **      on objects previously submitted by use of PR_AddWaitFileDesc(). Each
  253. **      successive call will return a pointer to a PRRecvWait object that
  254. **      was previously registered via PR_AddWaitFileDesc(). If no wait
  255. **      objects are associated with the wait group, a NULL will be returned.
  256. **      This function should be called in a loop until a NULL is returned
  257. **      to reclaim all the wait objects prior to calling PR_DestroyWaitGroup().
  258. **
  259. **  INPUTS
  260. **      group       The wait group under which the wait receive object was
  261. **                  added.
  262. **  RETURN
  263. **      PRRecvWait* If the wait group is valid and at least one receive wait
  264. **                  object is present in the group, that object will be
  265. **                  marked as PR_MW_INTERRUPT'd and removed from the group's
  266. **                  queues. Otherwise a NULL will be returned and the reason
  267. **                  for the NULL may be retrieved by calling PR_GetError().
  268. **
  269. **  ERRORS
  270. **      PR_INVALID_ARGUMENT_ERROR
  271. **      PR_GROUP_EMPTY_ERROR
  272. */
  273. PR_EXTERN(PRRecvWait*) PR_CancelWaitGroup(PRWaitGroup *group);
  274.  
  275. /*
  276. ** FUNCTION:    PR_CreateWaitGroup
  277. ** DESCRIPTION:
  278. **      A wait group is an opaque object that a client may create in order
  279. **      to semantically group various wait requests. Each wait group is
  280. **      unique, including the default wait group (NULL). A wait request
  281. **      that was added under a wait group will only be serviced by a caller
  282. **      that specified the same wait group.
  283. **
  284. **  INPUT
  285. **      size        The size of the hash table to be used to contain the
  286. **                  receive wait objects. This is just the initial size.
  287. **                  It will grow as it needs to, but to avoid that hassle
  288. **                  one can suggest a suitable size initially. It should
  289. **                  be ~30% larger than the maximum number of receive wait
  290. **                  objects expected.
  291. **  RETURN
  292. **      PRWaitGroup If successful, the function will return a pointer to an
  293. **                  object that was allocated by and owned by the runtime.
  294. **                  The reference remains valid until it is explicitly destroyed
  295. **                  by calling PR_DestroyWaitGroup().
  296. **
  297. **  ERRORS
  298. **      PR_OUT_OF_MEMORY_ERROR
  299. */
  300. PR_EXTERN(PRWaitGroup*) PR_CreateWaitGroup(PRInt32 size);
  301.  
  302. /*
  303. ** FUNCTION:    PR_DestroyWaitGroup
  304. ** DESCRIPTION:
  305. **      Undo the effects of PR_CreateWaitGroup(). Any receive wait operations
  306. **      on the group will be treated as if the each had been the target of a
  307. **      PR_CancelWaitFileDesc().
  308. **
  309. **  INPUT
  310. **      group       Reference to a wait group previously allocated using
  311. **                  PR_CreateWaitGroup().
  312. **  RETURN
  313. **      PRStatus    Will be PR_SUCCESS if the wait group was valid and there
  314. **                  are no receive wait objects in that group. Otherwise
  315. **                  will indicate PR_FAILURE.
  316. **
  317. **  ERRORS
  318. **      PR_INVALID_ARGUMENT_ERROR
  319. **                  The 'group' argument does not reference a known object.
  320. **      PR_INVALID_STATE_ERROR
  321. **                  The group still contains receive wait objects.
  322. */
  323. PR_EXTERN(PRStatus) PR_DestroyWaitGroup(PRWaitGroup *group);
  324.  
  325. /*
  326. ** FUNCTION:    PR_CreateMWaitEnumerator
  327. ** DESCRIPTION:
  328. **      The PR_CreateMWaitEnumerator() function returns a reference to an
  329. **      opaque PRMWaitEnumerator object. The enumerator object is required
  330. **      as an argument for each successive call in the stateless enumeration
  331. **      of the indicated wait group.
  332. **
  333. **      group       The wait group that the enumeration is intended to
  334. **                  process. It may be be the default wait group (NULL).
  335. ** RETURN
  336. **      PRMWaitEnumerator* group
  337. **                  A reference to an object that will be used to store
  338. **                  intermediate state of enumerations.
  339. ** ERRORS
  340. **      Errors are indicated by the function returning a NULL.
  341. **      PR_INVALID_ARGUMENT_ERROR
  342. **                  The 'group' argument does not reference a known object.
  343. **      PR_OUT_OF_MEMORY_ERROR
  344. */
  345. PR_EXTERN(PRMWaitEnumerator*) PR_CreateMWaitEnumerator(PRWaitGroup *group);
  346.  
  347. /*
  348. ** FUNCTION:    PR_DestroyMWaitEnumerator
  349. ** DESCRIPTION:
  350. **      Destroys the object created by PR_CreateMWaitEnumerator(). The reference
  351. **      used as an argument becomes invalid.
  352. **
  353. ** INPUT
  354. **      PRMWaitEnumerator* enumerator
  355. **          The PRMWaitEnumerator object to destroy.
  356. ** RETURN
  357. **      PRStatus
  358. **          PR_SUCCESS if successful, PR_FAILURE otherwise.
  359. ** ERRORS
  360. **      PR_INVALID_ARGUMENT_ERROR
  361. **                  The enumerator is invalid.
  362. */
  363. PR_EXTERN(PRStatus) PR_DestroyMWaitEnumerator(PRMWaitEnumerator* enumerator);
  364.  
  365. /*
  366. ** FUNCTION:    PR_EnumerateWaitGroup
  367. ** DESCRIPTION:
  368. **      PR_EnumerateWaitGroup is a thread safe enumerator over a wait group.
  369. **      Each call to the enumerator must present a valid PRMWaitEnumerator
  370. **      rererence and a pointer to the "previous" element returned from the
  371. **      enumeration process or a NULL.
  372. **
  373. **      An enumeration is started by passing a NULL as the "previous" value.
  374. **      Subsequent calls to the enumerator must pass in the result of the
  375. **      previous call. The enumeration end is signaled by the runtime returning
  376. **      a NULL as the result.
  377. **
  378. **      Modifications to the content of the wait group are allowed during
  379. **      an enumeration. The effect is that the enumeration may have to be
  380. **      "reset" and that may result in duplicates being returned from the
  381. **      enumeration.
  382. **
  383. **      An enumeration may be abandoned at any time. The runtime is not
  384. **      keeping any state, so there are no issues in that regard.
  385. */
  386. PR_EXTERN(PRRecvWait*) PR_EnumerateWaitGroup(
  387.     PRMWaitEnumerator *enumerator, const PRRecvWait *previous);
  388.    
  389. PR_END_EXTERN_C
  390.  
  391. #endif /* defined(_PRMWAIT_H) */
  392.  
  393. /* prmwait.h */
  394.