sipxportlib  Version 3.3
OsMsgPool.h
Go to the documentation of this file.
1 //
2 // Copyright (C) 2004-2006 SIPfoundry Inc.
3 // Licensed by SIPfoundry under the LGPL license.
4 //
5 // Copyright (C) 2004-2006 Pingtel Corp. All rights reserved.
6 // Licensed to SIPfoundry under a Contributor Agreement.
7 //
8 // $$
10 
11 
12 #ifndef _OsMsgPool_h_
13 #define _OsMsgPool_h_
14 
15 // SYSTEM INCLUDES
16 
17 // APPLICATION INCLUDES
18 #include "os/OsDefs.h"
19 #include "os/OsStatus.h"
20 #include "os/OsMsg.h"
21 #include "os/OsMutex.h"
22 
23 /*****************************************************************************
24  * OS Message Pool
25  * Container for a set of reusable OsMsg objects (actually, message objects
26  * derived from the OsMsg base class). All the OsMsg objects in any
27  * particular pool must be of the same derived type.
28  *
29  * The client of the OsMsgPool is the thread or interrupt service routine
30  * that retrieves available messages from the pool, fills in the payload,
31  * and sends it into an OsMsgQ. A pool with a single client thread is
32  * unshared, while a pool that may be used from more than one thread is
33  * shared.
34  *
35  * Note that an interrupt service routine must use a single client pool of
36  * its very own since interrupt routines cannot Take or Give mutexes. It
37  * must also be completely populated when it is created, as an ISR is not
38  * permitted to allocate memory, as would be necessary to expand the pool.
39  *
40  * Note, also, that the receiver of messages that may be from such a pool
41  * must not delete the messages explicitly, but rather should always invoke
42  * the new OsMsg::releaseMsg(void) method. This method clears the in-use
43  * flag on reusable messages, or deletes one-use messages. It is a safe
44  * and general rule that all OsMsg objects should only be disposed of by
45  * way of releaseMsg() and should never be explicitly deleted. In aid of
46  * this rule the destructor, OsMsg::~OsMsg(), now checks that only objects
47  * without the is-reusable flag set are deleted.
48  *
49  * To create the pool, the caller must supply a "model" message, one of the
50  * type to be contained in the pool. The OsMsgPool will clone that message
51  * by calling its createCopy() method to allocate the messages contained in
52  * the pool. On return from the OsMsgPool constructor, the model message
53  * should be deleted by the caller.
54  *
55  * The pool will initially be populated with the number of messages specified
56  * by the initialCount constructor argument. The pool may grow beyond that
57  * initial size, if so indicated by the other constructor arguments. To
58  * indicate that the pool may be expanded, the caller specifies three more
59  * values:
60  *
61  * softLimit - a count, larger than initialCount, that will generate a
62  * warning if the automatic expansion increases the pool beyond this
63  * count.
64  *
65  * hardLimit - a count, larger than initialCount, that is the absolute
66  * maximum to which the pool can grow. It is a fatal error if this
67  * count is reached and another element is needed to satisfy a request.
68  * This is the size of the array of OsMsg* pointers allocated in the
69  * constructor.
70  *
71  * increment - the number of message to create each time there is no
72  * message available to satisfy a request.
73  */
74 
75 
76 // DEFINES
77 // MACROS
78 // EXTERNAL FUNCTIONS
79 // EXTERNAL VARIABLES
80 // CONSTANTS
81 // STRUCTS
82 // TYPEDEFS
83 // FORWARD DECLARATIONS
84 class UtlString;
85 
86 //:Manager of a collection of OsMsg objects
87 class OsMsgPool
88 {
89 /* //////////////////////////// PUBLIC //////////////////////////////////// */
90 public:
91 
92  // Shared among multiple clients, or used by only a single client
94  {
97  };
98 
99 /* ============================ CREATORS ================================== */
100 
101  OsMsgPool(const char* name, // for identification
102  const OsMsg& model, // message to clone to populate pool
103  int initialCount, // number of messages to create initially
104  int softLimit=0, // number of message without complaining
105  int hardLimit=0, // absolute maximum number of messages
106  int increment=1, // number of messages to allocate when expanding
108  //:Default constructor. model is a message of the single type that
109  //:will be contained in the pool, and its createCopy virtual method
110  //:will be used to populate the pool. The caller disposes of model
111 
112  virtual
113  ~OsMsgPool();
114  //:Destructor
115 
116 /* ============================ MANIPULATORS ============================== */
117 
118  OsMsg* findFreeMsg(void);
119  //:Find and return an available element of the pool, creating more if
120  //:necessary and permitted. Return NULL if failure.
121 
122 /* ============================ ACCESSORS ================================= */
123 
124 /* ============================ INQUIRY =================================== */
125 
126  int getNoInUse(void);
127  //:Return the number of items in use.
128 
129  int getSoftLimit(void);
130  //:Return the current soft limit.
131 
132  int getHardLimit(void);
133  //:Return the current hard limit.
134 
135 /* //////////////////////////// PROTECTED ///////////////////////////////// */
136 protected:
137 
138 /* //////////////////////////// PRIVATE /////////////////////////////////// */
139 private:
140 
141  int mInitialCount; // initial number of items
142  int mCurrentCount; // current number of items
143  int mSoftLimit; // soft limit, warn when expanding above this
144  int mHardLimit; // hard limit AND length of mpElts
145  int mIncrement; // number to create when expanding
146  int mNext; // index to next element to examine
147  OsMutex* mpMutex; // NULL if single client
148  OsMsg* mpModel; // model element to clone
149  OsMsg** mpElts; // array of pointers to contained objects
150  UtlString* mpName; // for ID in error messages
151 
152  OsMsgPool(const OsMsgPool& rOsMsgPool);
153  //:Copy constructor (not implemented for this class)
154 
155  OsMsgPool& operator=(const OsMsgPool& rhs);
156  //:Assignment operator (not implemented for this class)
157 
158 };
159 
160 #endif // _OsMsgPool_h_
161 
OsMsg * findFreeMsg(void)
Definition: OsMsgPool.cpp:116
OsMsgPoolSharing
Definition: OsMsgPool.h:93
int getSoftLimit(void)
Definition: OsMsgPool.cpp:247
Definition: OsMsgPool.h:96
Definition: OsMsgPool.h:95
Definition: UtlString.h:48
OsMsgPool(const char *name, const OsMsg &model, int initialCount, int softLimit=0, int hardLimit=0, int increment=1, OsMsgPoolSharing sharing=MULTIPLE_CLIENTS)
Definition: OsMsgPool.cpp:35
Definition: OsMsg.h:36
int getHardLimit(void)
Definition: OsMsgPool.cpp:253
virtual ~OsMsgPool()
Definition: OsMsgPool.cpp:86
Definition: OsMsgPool.h:87
int getNoInUse(void)
Definition: OsMsgPool.cpp:220