home *** CD-ROM | disk | FTP | other *** search
/ Tools / WinSN5.0Ver.iso / NETSCAP.50 / WIN1998.ZIP / ns / nsprpub / pr / include / prmwait.h < prev    next >
Encoding:
C/C++ Source or Header  |  1998-04-08  |  13.3 KB  |  305 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. /********************************************************************************/
  28. /********************************************************************************/
  29. /********************************************************************************/
  30. /******************************       WARNING        ****************************/
  31. /********************************************************************************/
  32. /**************************** This is work in progress. *************************/
  33. /************************** Do not make any assumptions *************************/
  34. /************************** about the stability of this *************************/
  35. /************************** API or the underlying imple- ************************/
  36. /************************** mentation.                   ************************/
  37. /********************************************************************************/
  38. /********************************************************************************/
  39.  
  40. /*
  41. ** STRUCTURE:   PRWaitGroup
  42. ** DESCRIPTION:
  43. **      The client may define several wait groups in order to semantically
  44. **      tie a collection of file descriptors for a single purpose. This allows
  45. **      easier dispatching of threads that returned with active file descriptors
  46. **      from the wait function.
  47. */
  48. typedef struct PRWaitGroup PRWaitGroup;
  49.  
  50. /*
  51. ** ENUMERATION: PRMWStatus
  52. ** DESCRIPTION:
  53. **      This enumeration is used to indicate the completion status of
  54. **      a recieve wait object. Generally stated, a positive value indicates
  55. **      that the operation is not yet complete. A zero value indicates
  56. **      success (similar to PR_SUCCESS) and any negative value is an
  57. **      indication of failure. The reason for the failure can be retrieved
  58. **      by calling PR_GetError().
  59. **
  60. **  PR_MW_PENDING       The operation is still pending. None of the other
  61. **                      fields of the object are currently valid.
  62. **  PR_MW_SUCCESS       The operation is complete and it was successful.
  63. **  PR_MW_FAILURE       The operation failed. The reason for the failure
  64. **                      can be retrieved by calling PR_GetError().
  65. **  PR_MW_TIMEOUT       The amount of time allowed for by the object's
  66. **                      'timeout' field has expired w/o the operation
  67. **                      otherwise coming to closure.
  68. **  PR_MW_INTERRUPT     The operation was cancelled, either by the client
  69. **                      calling PR_CancelWaitFileDesc() or destroying the
  70. **                      entire wait group (PR_DestroyWaitGroup()).
  71. */
  72. typedef enum PRMWStatus
  73. {
  74.     PR_MW_PENDING = 1,
  75.     PR_MW_SUCCESS = 0,
  76.     PR_MW_FAILURE = -1,
  77.     PR_MW_TIMEOUT = -2,
  78.     PR_MW_INTERRUPT = -3
  79. } PRMWStatus;
  80.  
  81. /*
  82. ** STRUCTURE:   PRMemoryDescriptor
  83. ** DESCRIPTION:
  84. **      THis is a descriptor for an interval of memory. It contains a
  85. **      pointer to the first byte of that memory and the length (in
  86. **      bytes) of the interval.
  87. */
  88. typedef struct PRMemoryDescriptor
  89. {
  90.     void *start;                /* pointer to first byte of memory */
  91.     PRSize length;              /* length (in bytes) of memory interval */
  92. } PRMemoryDescriptor;
  93.  
  94. /*
  95. ** STRUCTURE:   PRRecvWait
  96. ** DESCRIPTION:
  97. **      A receive wait object contains the file descriptor that is subject
  98. **      to the wait and the amount of time (beginning epoch established
  99. **      when the object is presented to the runtime) the the channel should
  100. **      block before abandoning the process.
  101. **
  102. **      The success of the wait operation will be noted in the object's
  103. **      'outcome' field. The fields are not valid when the NSPR runtime
  104. **      is in possession of the object.
  105. **
  106. **      The memory descriptor describes an interval of writable memory
  107. **      in the caller's address space where data from an initial read
  108. **      can be placed. The description may indicate a null interval.
  109. */
  110. typedef struct PRRecvWait 
  111. {
  112.     PRCList internal;           /* internal runtime linkages */
  113.  
  114.     PRFileDesc *fd;             /* file descriptor associated w/ object */
  115.     PRMWStatus outcome;         /* outcome of the current/last operation */
  116.     PRIntervalTime timeout;     /* time allowed for entire operation */
  117.  
  118.     PRInt32 bytesRecv;         /* number of bytes transferred into buffer */
  119.     PRMemoryDescriptor buffer;  /* where to store first segment of input data */
  120. } PRRecvWait;
  121.  
  122.  
  123. /*
  124. ** FUNCTION:    PR_AddWaitFileDesc
  125. ** DESCRIPTION:
  126. **      This function will effectively add a file descriptor to the
  127. **      list of those waiting for network receive. The new descriptor
  128. **      will be semantically tied to the wait group specified.
  129. **
  130. **      The ownership for the storage pointed to by 'desc' is temporarily
  131. **      passed over the the NSPR runtime. It will be handed back by the
  132. **      function PR_WaitRecvReady().
  133. **
  134. **  INPUTS
  135. **      group       A reference to a PRWaitGroup or NULL. Wait groups are
  136. **                  created by calling PR_CreateWaitGroup() and are used
  137. **                  to semantically group various file descriptors by the
  138. **                  client's application.
  139. **      desc        A reference to a valid PRRecvWait. The object of the
  140. **                  reference must be preserved and treated as read-only
  141. **                  until its ownership is returned to the client.
  142. **  RETURN
  143. **      PRStatus    An indication of success. If equal to PR_FAILUE details
  144. **                  of the failure are avaiable via PR_GetError().
  145. **
  146. **  ERRORS
  147. **      PR_INVALID_ARGUMENT_ERROR
  148. **                  Invalid 'group' identifier or duplicate 'desc' object.
  149. **      PR_OUT_OF_MEMORY_ERROR
  150. **                  Insuffient memory for internal data structures.
  151. **      PR_INVALID_STATE_ERROR
  152. **                  The group is being destroyed.
  153. */
  154. PR_EXTERN(PRStatus) PR_AddWaitFileDesc(PRWaitGroup *group, PRRecvWait *desc);
  155.  
  156. /*
  157. ** FUNCTION:    PR_WaitRecvReady
  158. ** DESCRIPTION:
  159. **      PR_WaitRecvReady will block the calling thread until one of the
  160. **      file descriptors that have been added via PR_AddWaitFileDesc is
  161. **      available for input I/O.
  162. **  INPUT
  163. **      group       A pointer to a valid PRWaitGroup or NULL (the null
  164. **                  group. The function will block the caller until a
  165. **                  channel from the wait group becomes ready for receive
  166. **                  or there is some sort of error.
  167. **  RETURN
  168. **      PRReciveWait
  169. **                  When the caller is resumed it is either returned a
  170. **                  valid pointer to a previously added receive wait or
  171. **                  a NULL. If the latter, the function has terminated
  172. **                  for a reason that can be determined by calling
  173. **                  PR_GetError().
  174. **                  If a valid pointer is returned, the reference is to the
  175. **                  file descriptor contained in the receive wait object.
  176. **                  The outcome of the wait operation may still fail, and
  177. **                  if it has, that fact will be noted in the object's
  178. **                  outcome field. Details can be retrieved from PR_GetError().
  179. **
  180. **  ERRORS
  181. **      PR_INVALID_ARGUMENT_ERROR
  182. **                  The 'group' is not known by the runtime.
  183. **      PR_PENDING_INTERRUPT_ERROR
  184.                     The thread was interrupted.
  185. **      PR_INVALID_STATE_ERROR
  186. **                  The group is being destroyed.
  187. */
  188. PR_EXTERN(PRRecvWait*) PR_WaitRecvReady(PRWaitGroup *group);
  189.  
  190. /*
  191. ** FUNCTION:    PR_CancelWaitFileDesc
  192. ** DESCRIPTION:
  193. **      PR_CancelWaitFileDesc is provided as a means for cancelling operations
  194. **      on objects previously submitted by use of PR_AddWaitFileDesc(). If
  195. **      the runtime knows of the object, it will be marked as having failed
  196. **      because it was interrupted (similar to PR_Interrupt()). The first
  197. **      available thread waiting on the group will be made to return the
  198. **      PRRecvWait object with the outcome noted.
  199. **
  200. **  INPUTS
  201. **      group       The wait group under which the wait receive object was
  202. **                  added.
  203. **      desc        A pointer to the wait receive object that is to be
  204. **                  cancelled.
  205. **  RETURN
  206. **      PRStatus    If the wait receive object was located and associated
  207. **                  with the specified wait group, the status returned will
  208. **                  be PR_SUCCESS. There is still a race condition that would
  209. **                  permit the offected object to complete normally, but it
  210. **                  is assured that it will complete in the near future.
  211. **                  If the receive object or wait group are invalid, the
  212. **                  function will return with a status of PR_FAILURE.
  213. **
  214. **  ERRORS
  215. **      PR_INVALID_ARGUMENT_ERROR
  216. **                  The 'group' argument is not recognized as a valid group.
  217. **      PR_COLLECTION_EMPTY_ERROR
  218. **                  There are no more receive wait objects in the group's
  219. **                  collection.
  220. **      PR_INVALID_STATE_ERROR
  221. **                  The group is being destroyed.
  222. */
  223. PR_EXTERN(PRStatus) PR_CancelWaitFileDesc(PRWaitGroup *group, PRRecvWait *desc);
  224.  
  225. /*
  226. ** FUNCTION:    PR_CancelWaitGroup
  227. ** DESCRIPTION:
  228. **      PR_CancelWaitGroup is provided as a means for cancelling operations
  229. **      on objects previously submitted by use of PR_AddWaitFileDesc(). Each
  230. **      successive call will return a pointer to a PRRecvWait object that
  231. **      was previously registered via PR_AddWaitFileDesc(). If no wait
  232. **      objects are associated with the wait group, a NULL will be returned.
  233. **      This function should be called in a loop until a NULL is returned
  234. **      to reclaim all the wait objects prior to calling PR_DestroyWaitGroup().
  235. **
  236. **  INPUTS
  237. **      group       The wait group under which the wait receive object was
  238. **                  added.
  239. **  RETURN
  240. **      PRRecvWait* If the wait group is valid and at least one receive wait
  241. **                  object is present in the group, that object will be
  242. **                  marked as PR_MW_INTERRUPT'd and removed from the group's
  243. **                  queues. Otherwise a NULL will be returned and the reason
  244. **                  for the NULL may be retrieved by calling PR_GetError().
  245. **
  246. **  ERRORS
  247. **      PR_INVALID_ARGUMENT_ERROR
  248. **      PR_GROUP_EMPTY_ERROR
  249. */
  250. PR_EXTERN(PRRecvWait*) PR_CancelWaitGroup(PRWaitGroup *group);
  251.  
  252. /*
  253. ** FUNCTION:    PR_CreateWaitGroup
  254. ** DESCRIPTION:
  255. **      A wait group is an opaque object that a client may create in order
  256. **      to semantically group various wait requests. Each wait group is
  257. **      unique, including the default wait group (NULL). A wait request
  258. **      that was added under a wait group will only be serviced by a caller
  259. **      that specified the same wait group.
  260. **
  261. **  INPUT
  262. **      size        The size of the hash table to be used to contain the
  263. **                  receive wait objects. This is just the initial size.
  264. **                  It will grow as it needs to, but to avoid that hassle
  265. **                  one can suggest a suitable size initially. It should
  266. **                  be ~30% larger than the maximum number of receive wait
  267. **                  objects expected.
  268. **  RETURN
  269. **      PRWaitGroup If successful, the function will return a pointer to an
  270. **                  object that was allocated by and owned by the runtime.
  271. **                  The reference remains valid until it is explicitly destroyed
  272. **                  by calling PR_DestroyWaitGroup().
  273. **
  274. **  ERRORS
  275. **      PR_OUT_OF_MEMORY_ERROR
  276. */
  277. PR_EXTERN(PRWaitGroup*) PR_CreateWaitGroup(PRInt32 size);
  278.  
  279. /*
  280. ** FUNCTION:    PR_DestroyWaitGroup
  281. ** DESCRIPTION:
  282. **      Undo the effects of PR_CreateWaitGroup(). Any receive wait operations
  283. **      on the group will be treated as if the each had been the target of a
  284. **      PR_CancelWaitFileDesc().
  285. **
  286. **  INPUT
  287. **      group       Reference to a wait group previously allocated using
  288. **                  PR_CreateWaitGroup().
  289. **  RETURN
  290. **      PRStatus    Will be PR_SUCCESS if the wait group was valid and there
  291. **                  are no receive wait objects in that group. Otherwise
  292. **                  will indicate PR_FAILURE.
  293. **
  294. **  ERRORS
  295. **      PR_INVALID_ARGUMENT_ERROR
  296.                     The 'group' argument does not reference a known object.
  297. **      PR_INVALID_STATE_ERROR
  298. **                  The group still contains receive wait objects.
  299. */
  300. PR_EXTERN(PRStatus) PR_DestroyWaitGroup(PRWaitGroup *group);
  301.  
  302. #endif /* defined(_PRMWAIT_H) */
  303.  
  304. /* prmwait.h */
  305.