vxwSmLib.h
上传用户:luoyougen
上传日期:2008-05-12
资源大小:23136k
文件大小:13k
源码类别:

VxWorks

开发平台:

C/C++

  1. // VXWSm/vxwSmLib.h - support for shared memory objects
  2. // Copyright 1995-1999 Wind River Systems, Inc.
  3. // modification history
  4. // --------------------
  5. // 01d,09apr99,fle  added it to the doc build
  6. // 01c,21feb99,jdi  added library section, checked in documentation.
  7. // 01b,02oct95,rhp  documented.
  8. // 01a,15jun95,srh  written.
  9. // DESCRIPTION
  10. // This library defines wrapper classes for VxWorks shared-memory
  11. // objects: `VXWSmBSem' (shared-memory binary semaphores), `VXWSmCSem'
  12. // (shared-memory counting semaphores), `VXWSmMsgQ' (shared-memory
  13. // message queues), `VXWSmMemPart' (shared memory partitions), and
  14. // `VXWSmMemBlock' (shared memory blocks).
  15. //
  16. // INHERITANCE
  17. // All of the shared-memory object wrappers inherit the public members
  18. // of `VXWSmNameLib'.
  19. //
  20. // The `VXWSmBSem' and `VXWSmCSem' classes also inherit from `VXWSem';
  21. // `VXWSmMsgQ' also inherits from `VXWMsgQ'; `VXWSmMemPart' also
  22. // inherits from `VXWMemPArt'.
  23. //
  24. // INCLUDE FILES: vxwSmLib.h
  25. //
  26. // SEE ALSO
  27. // vxwSmNameLib, vxwMsgQLib, vxwMemPartLib, vxwSemLib,
  28. // .pG "Shared-Memory Objects"
  29. //
  30. // SECTION: 1C
  31. //
  32. #ifndef vxwSmLib_h
  33. #define vxwSmLib_h
  34. #include "vxWorks.h"
  35. #include "memLib.h"
  36. #include "smMemLib.h"
  37. #include "msgQSmLib.h"
  38. #include "semLib.h"
  39. #include "semSmLib.h"
  40. #include "smNameLib.h"
  41. #include "vxwObject.h"
  42. #include "vxwMemPartLib.h"
  43. #include "vxwMsgQLib.h"
  44. #include "vxwSemLib.h"
  45. #include "vxwSmNameLib.h"
  46. #include "vxwErr.h"
  47. #include "private/semLibP.h"
  48. class VXWSmSem : public VXWSem, public VXWSmName
  49. {
  50. protected:
  51.     VXWSmSem (SEM_ID aSemId)
  52. : VXWSem (aSemId)
  53.     {
  54.     }
  55.     VXWSmSem ()
  56.     {
  57.     }
  58.     VXWSmSem (const VXWSmSem & )
  59.     {
  60.     }
  61.     const VXWSmSem & operator = (const VXWSmSem &)
  62.     {
  63.     return *this;
  64.     }
  65. };
  66. class VXWSmBSem : public VXWSmSem
  67. {
  68. public:
  69. //_ VXWSmBSem Public Constructors
  70. ///////////////////////////////////////////////////////////////////////////////
  71. //
  72. // VXWSmBSem::VXWSmBSem - create and initialize binary shared-memory semaphore (VxMP Option)
  73. //
  74. // This routine allocates and initializes a shared memory binary semaphore.
  75. // The semaphore is initialized to an initial state <istate> of either
  76. // SEM_FULL (available) or SEM_EMPTY (not available).  The shared semaphore
  77. // structure is allocated from the shared semaphore dedicated memory
  78. // partition.  Use the optional <name> argument to identify the new
  79. // semaphore by name.
  80. //
  81. // The queuing style for blocked tasks is set by <opts>; the only
  82. // supported queuing style for shared memory semaphores is first-in-first-out,
  83. // selected by SEM_Q_FIFO.
  84. //
  85. // The maximum number of shared memory semaphores (binary plus counting) that
  86. // can be created is SM_OBJ_MAX_SEM, defined in configAll.h.
  87. //
  88. // AVAILABILITY
  89. // This routine depends on code distributed as a component of the unbundled
  90. // shared memory support option, VxMP.
  91. // 
  92. // RETURNS: N/A.
  93. // 
  94. // ERRNO:
  95. //  S_smMemLib_NOT_ENOUGH_MEMORY
  96. //  S_semLib_INVALID_QUEUE_TYPE
  97. //  S_semLib_INVALID_STATE
  98. //  S_smObjLib_LOCK_TIMEOUT
  99. //
  100. // SEE ALSO: vxwSemLib, vxwSmNameLib
  101.     VXWSmBSem (int opts, SEM_B_STATE istate, char * name = 0)
  102. : VXWSmSem (semBSmCreate (opts, istate))
  103.     {
  104.     if (sem_ == 0)
  105. vxwThrowErrno ();
  106.     if (name != 0 && nameSet (name) != OK)
  107. vxwThrowErrno ();
  108.     }
  109. ///////////////////////////////////////////////////////////////////////////////
  110. //
  111. // VXWSmBSem::VXWSmBSem - build a binary shared-memory semaphore object (VxMP Option)
  112. // 
  113. // This routine builds a shared-memory binary semaphore object around
  114. // an existing named shared-memory semaphore.  The <name> argument
  115. // identifies the existing semaphore; <waitType> specifies whether to
  116. // wait if the desired name is not found in the shared-memory name
  117. // database; see VXWSmName::nameGet().
  118. // 
  119. // Use this routine to take advantage of the VXWSmBSem class while
  120. // working with semaphores created by some other means (for example,
  121. // previously existing C code).
  122. // 
  123. // RETURNS: N/A.
  124. //
  125. // SEE ALSO: VXWSmName::nameGet()
  126.     VXWSmBSem (char * name, int waitType)
  127.     {
  128.     int    type;
  129.     if (smNameFind (name, (void **)(&sem_), &type, waitType) != OK)
  130. vxwThrowErrno ();
  131.     }
  132.     virtual STATUS nameSet (char * name);
  133. protected:
  134.     VXWSmBSem ()
  135.     {
  136.     }
  137.     VXWSmBSem (const VXWSmBSem &)
  138.     {
  139.     }
  140.     VXWSmBSem & operator = (const VXWSmBSem &)
  141.     {
  142.     return *this;
  143.     }
  144. };
  145. class VXWSmCSem : public VXWSmSem
  146. {
  147. public:
  148. //_ VXWSmCSem Public Constructors
  149. ///////////////////////////////////////////////////////////////////////////////
  150. //
  151. // VXWSmCSem::VXWSmCSem - create and initialize a shared memory counting semaphore (VxMP Option)
  152. //
  153. // This routine allocates and initializes a shared memory counting
  154. // semaphore.  The initial count value of the semaphore (the number of
  155. // times the semaphore must be taken before it can be given) is specified
  156. // by <icount>.
  157. //
  158. // The queuing style for blocked tasks is set by <opts>; the only
  159. // supported queuing style for shared memory semaphores is first-in-first-out,
  160. // selected by SEM_Q_FIFO.
  161. //
  162. // The maximum number of shared memory semaphores (binary plus counting) that
  163. // can be created is SM_OBJ_MAX_SEM, defined in configAll.h.
  164. //
  165. // AVAILABILITY
  166. // This routine depends on code distributed as a component of the unbundled
  167. // shared memory support option, VxMP.
  168. // 
  169. // RETURNS: N/A.
  170. //
  171. // ERRNO:
  172. //  S_smMemLib_NOT_ENOUGH_MEMORY
  173. //  S_semLib_INVALID_QUEUE_TYPE
  174. //  S_smObjLib_LOCK_TIMEOUT
  175. //
  176. // SEE ALSO: vxwSemLib, vxwSmNameLib
  177.     VXWSmCSem (int opts, int icount, char * name = 0)
  178. : VXWSmSem (semCSmCreate (opts, icount))
  179.     {
  180.     if (sem_ == 0)
  181. vxwThrowErrno ();
  182.     if (name != 0 && nameSet (name) != OK)
  183. vxwThrowErrno ();
  184.     }
  185. ///////////////////////////////////////////////////////////////////////////////
  186. //
  187. // VXWSmCSem::VXWSmCSem - build a shared-memory counting semaphore object (VxMP Option)
  188. // 
  189. // This routine builds a shared-memory semaphore object around
  190. // an existing named shared-memory counting semaphore.  The <name> argument
  191. // identifies the existing semaphore, and <waitType> specifies whether to
  192. // wait if the desired name is not found in the shared-memory name
  193. // database; see VXWSmName::nameGet().
  194. // 
  195. // Use this routine to take advantage of the VXWSmBSem class while
  196. // working with semaphores created by some other means (for example,
  197. // previously existing C code).
  198. // 
  199. // RETURNS: N/A.
  200. //
  201. // SEE ALSO: VXWSmName::nameGet()
  202.     VXWSmCSem (char * name, int waitType)
  203.     {
  204.     int    type;
  205.     if (smNameFind (name, (void **)(&sem_), &type, waitType) != OK)
  206. vxwThrowErrno ();
  207.     }
  208.     virtual STATUS nameSet (char * name);
  209. protected:
  210.     VXWSmCSem ()
  211.     {
  212.     }
  213.     VXWSmCSem (const VXWSmCSem &)
  214.     {
  215.     }
  216.     VXWSmCSem & operator = (const VXWSmCSem &)
  217.     {
  218.     return *this;
  219.     }
  220. };    
  221. class VXWSmMsgQ : public VXWMsgQ, public  VXWSmName
  222. {
  223. public:
  224. //_ VXWSmMsgQ Public Constructors
  225. ///////////////////////////////////////////////////////////////////////////////
  226. //
  227. // VXWSmMsgQ::VXWSmMsgQ - create and initialize a shared-memory message queue (VxMP Option)
  228. //
  229. // This routine creates a shared memory message queue capable of holding up
  230. // to <maxMsgs> messages, each up to <maxMsgLength> bytes long.  The queue
  231. // can only be created with the option MSG_Q_FIFO (0), thus queuing pended
  232. // tasks in FIFO order.
  233. //
  234. // If there is insufficient memory to store the message queue structure
  235. // in the shared memory message queue partition or if the shared memory system
  236. // pool cannot handle the requested message queue size, shared memory message 
  237. // queue creation fails with `errno' set to S_smMemLib_NOT_ENOUGH_MEMORY.
  238. // This problem can be solved by incrementing the SM_OBJ_MAX_MSG_Q value
  239. // in configAll.h and/or the size of memory dedicated to shared-memory objects
  240. // SM_OBJ_MEM_SIZE in config.h.
  241. //
  242. // AVAILABILITY:
  243. // This routine depends on code distributed as a component of the unbundled
  244. // shared memory objects support option, VxMP.
  245. //
  246. // RETURNS: N/A.
  247. //
  248. // ERRNO:
  249. //  S_smMemLib_NOT_ENOUGH_MEMORY
  250. //  S_intLib_NOT_ISR_CALLABLE
  251. //  S_msgQLib_INVALID_QUEUE_TYPE
  252. //  S_smObjLib_LOCK_TIMEOUT
  253. //
  254. // SEE ALSO: vxwMsgQLib, vxwSmNameLib
  255.     VXWSmMsgQ (int maxMsgs, int maxMsgLength, int options)
  256. : VXWMsgQ (msgQSmCreate (maxMsgs, maxMsgLength, options))
  257.     {
  258.     if (msgq_ == 0)
  259. vxwThrowErrno ();
  260.     }
  261.     virtual STATUS nameSet (char *name);
  262. };
  263. class VXWSmMemPart : public VXWMemPart, public VXWSmName
  264. {
  265. public:
  266. //_ VXWSmMemPart Public Constructors
  267. ///////////////////////////////////////////////////////////////////////////////
  268. //
  269. // VXWSmMemPart::VXWSmMemPart - create a shared memory partition (VxMP Option)
  270. //
  271. // This routine creates a shared memory partition that can be used by tasks
  272. // on all CPUs in the system to manage memory blocks .  Because the
  273. // `VXWSmMemPart' class inherits from `VXWMemPart', you can use the
  274. // general-purpose methods in that class to manage the partition (that
  275. // is, to allocate and free memory blocks in the partition).
  276. //
  277. // <pool> is a pointer to the global address of shared memory dedicated to the
  278. // partition.  The memory area where <pool> points must be in the same
  279. // address space as the shared memory anchor and shared memory pool.
  280. //
  281. // <poolSize> is the size in bytes of shared memory dedicated to the partition.
  282. //
  283. // NOTE
  284. // The descriptor for the new partition is allocated out of an internal
  285. // dedicated shared memory partition.  The maximum number of partitions that can
  286. // be created is SM_OBJ_MAX_MEM_PART, defined in configAll.h.
  287. //
  288. // Memory pool size is rounded down to a 16-byte boundary.
  289. //
  290. // AVAILABILITY
  291. // This routine depends on code distributed as a component of the unbundled
  292. // shared memory objects support option, VxMP.
  293. // 
  294. // RETURNS: N/A.
  295. //
  296. // ERRNO:
  297. //  S_memLib_NOT_ENOUGH_MEMORY
  298. //  S_smObjLib_LOCK_TIMEOUT 
  299. //
  300. // SEE ALSO: vxwMemPartLib, vxwSmNameLib
  301.     VXWSmMemPart (char *pool, unsigned poolSize)
  302. : VXWMemPart (memPartSmCreate (pool, poolSize))
  303.     {
  304.     if (partid_ == 0)
  305. vxwThrowErrno ();
  306.     }
  307.     virtual STATUS nameSet (char *name);
  308. };
  309. class VXWSmMemBlock : public VXWSmName
  310. {
  311. public:
  312. //_ VXWSmMemBlock Public Constructors
  313. ///////////////////////////////////////////////////////////////////////////////
  314. //
  315. // VXWSmMemBlock::VXWSmMemBlock - allocate a block of memory from the shared memory system partition (VxMP Option)
  316. // 
  317. // This routine allocates, from the shared memory system partition, a
  318. // block of memory whose size is equal to or greater than <nBytes>.
  319. // The local address of the allocated shared memory block can be
  320. // obtained from VXWSmMemBlock::baseAddress().
  321. //
  322. // AVAILABILITY
  323. // This routine depends on code distributed as a component of the unbundled
  324. // shared memory objects support option, VxMP.
  325. // 
  326. // RETURNS: N/A.
  327. //
  328. // ERRNO:
  329. //  S_memLib_NOT_ENOUGH_MEMORY
  330. //  S_smObjLib_LOCK_TIMEOUT
  331. //
  332.     VXWSmMemBlock (int nBytes)
  333. : pBlock_ (smMemMalloc (nBytes)), nBytes_ (nBytes)
  334.     {
  335.     if (pBlock_ == 0)
  336. vxwThrowErrno ();
  337.     }
  338. ///////////////////////////////////////////////////////////////////////////////
  339. //
  340. // VXWSmMemBlock::VXWSmMemBlock - allocate memory for an array from the shared memory system partition (VxMP Option)
  341. //
  342. // This routine allocates a block of memory for an array that contains
  343. // <nElems> elements of size <sizeOfElem> from the shared memory system 
  344. // partition.
  345. // The local address of the allocated shared memory block can be
  346. // obtained from VXWSmMemBlock::baseAddress().
  347. //
  348. // AVAILABILITY
  349. // This routine depends on code distributed as a component of the unbundled
  350. // shared memory objects support option, VxMP.
  351. // 
  352. // RETURNS:
  353. // A pointer to the block, or NULL if the memory cannot be allocated.
  354. //
  355. // ERRNO:
  356. //  S_memLib_NOT_ENOUGH_MEMORY
  357. //  S_smObjLib_LOCK_TIMEOUT
  358.     VXWSmMemBlock (int nElems, int sizeOfElem)
  359. : pBlock_ (smMemCalloc (nElems, sizeOfElem)),
  360.   nBytes_ (nElems * sizeOfElem)
  361.     {
  362.     if (pBlock_ == 0)
  363. vxwThrowErrno ();
  364.     }
  365. ///////////////////////////////////////////////////////////////////////////////
  366. //
  367. // VXWSmMemBlock::~VXWSmMemBlock - free a shared memory system partition block of memory (VxMP Option)
  368. //
  369. // This routine returns a `VXWSmMemBlock' shared-memory block to the
  370. // free-memory pool in the shared-memory system partition.
  371. //
  372. // AVAILABILITY
  373. // This routine depends on code distributed as a component of the unbundled shared memory
  374. // objects support option, VxMP.
  375. // 
  376. // RETURNS: N/A.
  377. //
  378. // ERRNO:
  379. //  S_smObjLib_LOCK_TIMEOUT
  380. //
  381. // SEE ALSO: VXWSmMemBlock::VXWSmMemBlock()
  382.     virtual ~VXWSmMemBlock ()
  383.     {
  384.     if (smMemFree (pBlock_) != OK)
  385. vxwThrowErrno ();
  386.     }
  387. //_ VXWSmMemBlock Public Member Functions
  388. ///////////////////////////////////////////////////////////////////////////////
  389. //
  390. // VXWSmMemBlock::baseAddress - address of shared-memory block
  391. //
  392. // This routine reports the local address of a block of shared memory
  393. // managed as a `VXWSmMemBlock' object.
  394. //
  395. // RETURNS: Local address of memory block in shared-memory system partition.
  396.     void * baseAddress () const
  397.     {
  398.     return pBlock_;
  399.     }
  400.     virtual STATUS nameSet (char * name);
  401.     virtual void * myValue ();
  402. protected:
  403.     VXWSmMemBlock ()
  404.     {
  405.     }
  406.     VXWSmMemBlock (const VXWSmMemBlock &)
  407.     {
  408.     }
  409.     VXWSmMemBlock & operator = (const VXWSmMemBlock &)
  410.     {
  411.     return *this;
  412.     }
  413.     void     * pBlock_;
  414.     unsigned   nBytes_;
  415. };
  416. #endif /* ifndef vxwSmLib_h */