root/juggler/tags/1.0.7/Kernel/vjConfigManager.h

Revision 8789, 9.2 kB (checked in by patrickh, 7 years ago)

Copyright update.

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