root/juggler/tags/1.0.5/Kernel/vjConfigManager.h
| Revision 7539, 9.2 kB (checked in by anonymous, 7 years ago) | |
|---|---|
| |
| Line | |
|---|---|
| 1 | /*************** <auto-copyright.pl BEGIN do not edit this line> ************** |
| 2 | * |
| 3 | * VR Juggler is (C) Copyright 1998, 1999, 2000 by Iowa State University |
| 4 | * |
| 5 | * Original Authors: |
| 6 | * Allen Bierbaum, Christopher Just, |
| 7 | * Patrick Hartling, Kevin Meinert, |
| 8 | * Carolina Cruz-Neira, Albert Baker |
| 9 | * |
| 10 | * This library is free software; you can redistribute it and/or |
| 11 | * modify it under the terms of the GNU Library General Public |
| 12 | * License as published by the Free Software Foundation; either |
| 13 | * version 2 of the License, or (at your option) any later version. |
| 14 | * |
| 15 | * This library is distributed in the hope that it will be useful, |
| 16 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 18 | * Library General Public License for more details. |
| 19 | * |
| 20 | * You should have received a copy of the GNU Library General Public |
| 21 | * License along with this library; if not, write to the |
| 22 | * Free Software Foundation, Inc., 59 Temple Place - Suite 330, |
| 23 | * Boston, MA 02111-1307, USA. |
| 24 | * |
| 25 | * ----------------------------------------------------------------- |
| 26 | * File: $RCSfile$ |
| 27 | * Date modified: $Date$ |
| 28 | * Version: $Revision$ |
| 29 | * ----------------------------------------------------------------- |
| 30 | * |
| 31 | *************** <auto-copyright.pl END do not edit this line> ***************/ |
| 32 | |
| 33 | |
| 34 | #ifndef _VJ_CONFIG_MANGER_H_ |
| 35 | #define _VJ_CONFIG_MANGER_H_ |
| 36 | |
| 37 | #include <vjConfig.h> |
| 38 | #include <Kernel/vjDebug.h> |
| 39 | //#include <Config/vjConfigChunk.h> |
| 40 | class vjConfigChunk; |
| 41 | |
| 42 | #include <Config/vjConfigChunkDB.h> |
| 43 | #include <Config/vjChunkDescDB.h> |
| 44 | //#include <Config/vjChunkFactory.h> |
| 45 | #include <Sync/vjMutex.h> |
| 46 | #include <Sync/vjGuard.h> |
| 47 | #include <list> |
| 48 | |
| 49 | #include <Utils/vjSingleton.h> |
| 50 | |
| 51 | //: Configuration manager class |
| 52 | // |
| 53 | // |
| 54 | // |
| 55 | //! Created: Jan-13-2000 |
| 56 | //! Author: Allen Bierbaum |
| 57 | // |
| 58 | class vjConfigManager |
| 59 | { |
| 60 | public: |
| 61 | struct vjPendingChunk |
| 62 | { |
| 63 | vjPendingChunk() : mType(0), mChunk(NULL) |
| 64 | {;} |
| 65 | |
| 66 | enum { ADD=0, REMOVE=1}; |
| 67 | unsigned mType; // What type of chunk is it (ADD or REMOVE) |
| 68 | vjConfigChunk* mChunk; |
| 69 | }; |
| 70 | |
| 71 | |
| 72 | public: // -- Query functions --- // |
| 73 | //: Is the chunk in the active configuration?? |
| 74 | //! CONCURRENCY: concurrent |
| 75 | //! NOTE: This locks the active list to do processing |
| 76 | bool isChunkInActiveList(std::string chunk_name) |
| 77 | { |
| 78 | vjGuard<vjMutex> guard(mActiveLock); // Lock the current list |
| 79 | |
| 80 | std::vector<vjConfigChunk*>::iterator i; |
| 81 | for(i=mActiveConfig.begin(); i != mActiveConfig.end();i++) |
| 82 | { |
| 83 | if(std::string((*i)->getProperty("name")) == chunk_name) |
| 84 | return true; |
| 85 | } |
| 86 | |
| 87 | return false; // Not found, so return false |
| 88 | } |
| 89 | |
| 90 | // Add the given chunk db to the pending list as adds |
| 91 | //! PRE: The pending list can NOT be locked |
| 92 | //! POST: pendinglist = old(pendinglist) += db |
| 93 | //! NOTE: The entries are copied |
| 94 | void addChunkDB(vjConfigChunkDB* db); |
| 95 | |
| 96 | //: Add the given chunks to the db as pending removes |
| 97 | //! PRE: The pending list can NOT be locked |
| 98 | //! POST: pendinglist = old(pendinglist) += db |
| 99 | //! NOTE: The entries are copied |
| 100 | void removeChunkDB(vjConfigChunkDB* db); |
| 101 | |
| 102 | |
| 103 | public: // ----- PENDING LIST ----- // |
| 104 | //: Do we need to check the pending list |
| 105 | //! CONCURRENCY: concurrent |
| 106 | // Implements some logic that allows the pending list to become "stale" |
| 107 | // If the pending list has been check a bunch of times and has had no |
| 108 | // changes in size, then we start telling people not to check it because |
| 109 | bool pendingNeedsChecked(); |
| 110 | |
| 111 | //: Get the size of the pending list |
| 112 | //! CONCURRENCY: concurrent |
| 113 | int getNumPending() |
| 114 | { return mPendingConfig.size(); } |
| 115 | |
| 116 | //: Add a pending entry |
| 117 | //! PRE: pending must NOT be locked |
| 118 | //! POST: A copy of the pendingChunk is placed on the pending list |
| 119 | //! concurrency: gaurded |
| 120 | void addPending(vjPendingChunk& pendingChunk) |
| 121 | { |
| 122 | vjASSERT(0 == mPendingLock.test()); |
| 123 | lockPending(); |
| 124 | mPendingConfig.push_back(pendingChunk); |
| 125 | unlockPending(); |
| 126 | |
| 127 | // Reset pending count |
| 128 | mPendingCountMutex.acquire(); |
| 129 | mPendingCheckCount = 0; |
| 130 | mPendingCountMutex.release(); |
| 131 | } |
| 132 | |
| 133 | |
| 134 | //: Lock the pending list |
| 135 | // This function blocks until it can get a lock on the pending list |
| 136 | //! CONCURRENCY: gaurded |
| 137 | void lockPending() |
| 138 | { mPendingLock.acquire(); } |
| 139 | |
| 140 | //: Unlock the pending list |
| 141 | // Unlocks the mutex held on the pending list |
| 142 | //! CONCURRENCY: gaurded |
| 143 | void unlockPending() |
| 144 | { mPendingLock.release(); } |
| 145 | |
| 146 | //: Get the beginning of the pending list |
| 147 | //! PRE: Pending list must be locked |
| 148 | std::list<vjPendingChunk>::iterator getPendingBegin() |
| 149 | { |
| 150 | vjASSERT(1 == mPendingLock.test()); // ASSERT: We must have the lock |
| 151 | return mPendingConfig.begin(); |
| 152 | } |
| 153 | |
| 154 | //: Get the end of the pending list |
| 155 | //! PRE: Pending list must be locked |
| 156 | std::list<vjPendingChunk>::iterator getPendingEnd() |
| 157 | { |
| 158 | vjASSERT(1 == mPendingLock.test()); |
| 159 | return mPendingConfig.end(); |
| 160 | } |
| 161 | |
| 162 | //: Erase an item from the list |
| 163 | //! PRE: Pending list must be locked && item must be in list |
| 164 | //! POST: list = old(list).erase(item) && item is invalid |
| 165 | void removePending(std::list<vjPendingChunk>::iterator item) |
| 166 | { |
| 167 | vjASSERT(1 == mPendingLock.test()); |
| 168 | mPendingConfig.erase(item); |
| 169 | } |
| 170 | |
| 171 | //: Send a copy of the pending list to debug output |
| 172 | //! PRE: Pending must be locked |
| 173 | void debugDumpPending(int debug_level); |
| 174 | |
| 175 | |
| 176 | public: // ----- ACTIVE LIST ----- // |
| 177 | //: Are there items in current //! CONCURRENCY: concurrent |
| 178 | bool isActiveEmpty() |
| 179 | { return mActiveConfig.isEmpty(); } |
| 180 | |
| 181 | //: Lock the current list |
| 182 | // This function blocks until it can get a lock on the current list |
| 183 | //! CONCURRENCY: gaurded |
| 184 | void lockActive() |
| 185 | { mActiveLock.acquire(); } |
| 186 | |
| 187 | //: Unlock the current list |
| 188 | // Unlocks the mutex held on the current list |
| 189 | //! CONCURRENCY: gaurded |
| 190 | void unlockActive() |
| 191 | { mActiveLock.release(); } |
| 192 | |
| 193 | //: Get the beginning of the current list |
| 194 | //! PRE: Pending list must be locked |
| 195 | std::vector<vjConfigChunk*>::iterator getActiveBegin() |
| 196 | { |
| 197 | vjASSERT(1 == mActiveLock.test()); // ASSERT: We must have the lock |
| 198 | return mActiveConfig.begin(); |
| 199 | } |
| 200 | |
| 201 | //: Get the end of the pending list |
| 202 | //! PRE: Active list must be locked |
| 203 | std::vector<vjConfigChunk*>::iterator getActiveEnd() |
| 204 | { |
| 205 | vjASSERT(1 == mActiveLock.test()); |
| 206 | return mActiveConfig.end(); |
| 207 | } |
| 208 | |
| 209 | //: Erase an item from the list |
| 210 | //! PRE: Active list must be locked && item must be in list |
| 211 | //! POST: list = old(list).erase(item) && item is invalid |
| 212 | void removeActive(std::string chunk_name) |
| 213 | { |
| 214 | vjASSERT(0 == mActiveLock.test()); |
| 215 | lockActive(); |
| 216 | mActiveConfig.removeNamed(chunk_name); |
| 217 | unlockActive(); |
| 218 | } |
| 219 | |
| 220 | //: Add an item to the active configuration |
| 221 | //! NOTE: This DOES NOT process the chunk |
| 222 | //+ it just places it into the active configuration list |
| 223 | //! PRE: Current list must NOT be locked |
| 224 | void addActive(vjConfigChunk* chunk) |
| 225 | { |
| 226 | vjASSERT(0 == mActiveLock.test()); |
| 227 | lockActive(); |
| 228 | mActiveConfig.addChunk(chunk); |
| 229 | unlockActive(); |
| 230 | } |
| 231 | |
| 232 | //: Return ptr to the active config dhunk db |
| 233 | //! PRE: active must be locked |
| 234 | //! NOTE: The pointer is only valid until active is unlocked |
| 235 | //! CONCURRENCY: sequential |
| 236 | vjConfigChunkDB* getActiveConfig() |
| 237 | { |
| 238 | vjASSERT(1 == mActiveLock.test()); |
| 239 | return &mActiveConfig; |
| 240 | } |
| 241 | |
| 242 | public: |
| 243 | //: Scan the active list for items that don't have their dependencies filled |
| 244 | //! POST: Any chunks in active with dependencies not filled are added to the |
| 245 | //+ the pending list. (A remove and an add are added to the pending) |
| 246 | //+ The remove item configChunk* == active configChunk* |
| 247 | //+ The add item configChunk* points to a copy of the chunk |
| 248 | //! NOTE: We add an add after the removal to allow for the object |
| 249 | //+ to re-enter the system after its dependencies have been satisfied |
| 250 | //! RETURNS: The number of lost dependencies found |
| 251 | int scanForLostDependencies(); |
| 252 | |
| 253 | private: |
| 254 | vjConfigChunkDB mActiveConfig; //: List of current configuration |
| 255 | std::list<vjPendingChunk> mPendingConfig; //: List of pending configuration changes |
| 256 | vjMutex mPendingLock; //: Lock on pending list |
| 257 | vjMutex mActiveLock; //: Lock for current config list |
| 258 | |
| 259 | // The following variables are used to implment some logic |
| 260 | // that "stales" the pending list. (see pendingNeedsChecked) |
| 261 | vjMutex mPendingCountMutex; |
| 262 | int mPendingCheckCount; //: How many pending checks since last change to pending |
| 263 | int mLastPendingSize; //: The size of pending at last check |
| 264 | protected: |
| 265 | vjConfigManager() |
| 266 | {;} |
| 267 | |
| 268 | |
| 269 | /* |
| 270 | public: |
| 271 | //: Get instance of singleton object |
| 272 | static vjConfigManager* instance() |
| 273 | { |
| 274 | if(_instance == NULL) // First check |
| 275 | { |
| 276 | vjGuard<vjMutex> guard(_inst_lock); // Serial critical section |
| 277 | if (_instance == NULL) // Second check |
| 278 | _instance = new vjConfigManager; |
| 279 | } |
| 280 | vjASSERT(_instance != NULL && "vjConfigManager has NULL _instance"); |
| 281 | return _instance; |
| 282 | } |
| 283 | |
| 284 | |
| 285 | private: |
| 286 | static vjConfigManager* _instance; //: The instance |
| 287 | static vjMutex _inst_lock; |
| 288 | */ |
| 289 | vjSingletonHeader(vjConfigManager); |
| 290 | }; |
| 291 | |
| 292 | |
| 293 | #endif |
| 294 |
Note: See TracBrowser for help on using the browser.
