| 1 | // **********************************************************************
 | 
|---|
| 2 | //
 | 
|---|
| 3 | // Copyright (c) 2000
 | 
|---|
| 4 | // Object Oriented Concepts, Inc.
 | 
|---|
| 5 | // Billerica, MA, USA
 | 
|---|
| 6 | //
 | 
|---|
| 7 | // All Rights Reserved
 | 
|---|
| 8 | //
 | 
|---|
| 9 | // **********************************************************************
 | 
|---|
| 10 | 
 | 
|---|
| 11 | #ifndef JTC_THREAD_GROUP_H
 | 
|---|
| 12 | #define JTC_THREAD_GROUP_H
 | 
|---|
| 13 | 
 | 
|---|
| 14 | //
 | 
|---|
| 15 | // This class represents a grouping of threads.
 | 
|---|
| 16 | //
 | 
|---|
| 17 | class JTCThreadGroup : public JTCMonitor, public virtual JTCRefCount
 | 
|---|
| 18 | {
 | 
|---|
| 19 |     //
 | 
|---|
| 20 |     // Hide copy constructor and assignment operator.
 | 
|---|
| 21 |     //
 | 
|---|
| 22 |     JTCThreadGroup(const JTCThreadGroup&);
 | 
|---|
| 23 |     void operator=(const JTCThreadGroup&);
 | 
|---|
| 24 | 
 | 
|---|
| 25 |     //
 | 
|---|
| 26 |     // This class represents a collection of ThreadGroupHandles.
 | 
|---|
| 27 |     //
 | 
|---|
| 28 |     class JTCThreadGroupSet
 | 
|---|
| 29 |     {
 | 
|---|
| 30 |         //
 | 
|---|
| 31 |         // Hide copy constructor and assignment operator.
 | 
|---|
| 32 |         //
 | 
|---|
| 33 |         JTCThreadGroupSet(const JTCThreadGroupSet&);
 | 
|---|
| 34 |         JTCThreadGroupSet& operator=(const JTCThreadGroupSet&);
 | 
|---|
| 35 |         
 | 
|---|
| 36 |         JTCThreadGroupHandle* handles_; // The set of handles.
 | 
|---|
| 37 |         int len_; // Length of the set.
 | 
|---|
| 38 | 
 | 
|---|
| 39 |     public:
 | 
|---|
| 40 | 
 | 
|---|
| 41 |         //
 | 
|---|
| 42 |         // Constructor/destructor.
 | 
|---|
| 43 |         //
 | 
|---|
| 44 |         JTCThreadGroupSet() : handles_(0), len_(0) { }
 | 
|---|
| 45 |         ~JTCThreadGroupSet() { delete [] handles_; }
 | 
|---|
| 46 | 
 | 
|---|
| 47 |         //
 | 
|---|
| 48 |         // Allocate n members for the set.
 | 
|---|
| 49 |         //
 | 
|---|
| 50 |         void allocate(int n);
 | 
|---|
| 51 | 
 | 
|---|
| 52 |         //
 | 
|---|
| 53 |         // How many members in the set.
 | 
|---|
| 54 |         //
 | 
|---|
| 55 |         int length() const;
 | 
|---|
| 56 | 
 | 
|---|
| 57 |         //
 | 
|---|
| 58 |         // Return a reference to the ith member of the set.
 | 
|---|
| 59 |         //
 | 
|---|
| 60 |         JTCThreadGroupHandle& operator[](int i);
 | 
|---|
| 61 |     };
 | 
|---|
| 62 |     
 | 
|---|
| 63 |     //
 | 
|---|
| 64 |     // Copy current set of groups.
 | 
|---|
| 65 |     //
 | 
|---|
| 66 |     void createGroupSnapshot(JTCThreadGroupSet& set) const;
 | 
|---|
| 67 | 
 | 
|---|
| 68 |     //
 | 
|---|
| 69 |     // Add and remove ThreadGroup.  Add and remove threads.
 | 
|---|
| 70 |     //
 | 
|---|
| 71 |     void add(JTCThreadGroup* g);
 | 
|---|
| 72 |     void remove(JTCThreadGroup* g);
 | 
|---|
| 73 |     void add(JTCThreadHandle t);
 | 
|---|
| 74 |     void remove(JTCThreadHandle t);
 | 
|---|
| 75 | 
 | 
|---|
| 76 |     //
 | 
|---|
| 77 |     // Copy current set of threads, and ThreadGroups.
 | 
|---|
| 78 |     //
 | 
|---|
| 79 |     int enumerate(JTCThreadHandle* list, int len, int n, bool recurse) const;
 | 
|---|
| 80 |     int enumerate(JTCThreadGroupHandle* list, int len, int n,
 | 
|---|
| 81 |                   bool recurse) const;
 | 
|---|
| 82 | 
 | 
|---|
| 83 |     //
 | 
|---|
| 84 |     // Constructor helper.
 | 
|---|
| 85 |     //
 | 
|---|
| 86 |     void init(JTCThreadGroup* parent, const char* name);
 | 
|---|
| 87 | 
 | 
|---|
| 88 |     JTCThreadGroupHandle parent_; // Parent of this ThreadGroup.
 | 
|---|
| 89 |     char* name_; // Name of ThreadGroup.
 | 
|---|
| 90 |     int maxPriority_; // Maximum priority of this ThreadGroup.
 | 
|---|
| 91 |     bool destroyed_; // Has this ThreadGroup been destroyed?
 | 
|---|
| 92 | 
 | 
|---|
| 93 |     int nthreads_; // Number of threads in this ThreadGroup.
 | 
|---|
| 94 |     JTCThreadHandle* threads_; // Pointer to each thread in this ThreadGroup.
 | 
|---|
| 95 |     int threadsLength_; // Capacity of the above array.
 | 
|---|
| 96 | 
 | 
|---|
| 97 |     int ngroups_; // Number of child ThreadGroups.
 | 
|---|
| 98 |     JTCThreadGroup** groups_; // Pointer to each child ThreadGroup.
 | 
|---|
| 99 |     int groupsLength_; // Capacity of the above array.
 | 
|---|
| 100 | 
 | 
|---|
| 101 |     bool daemon_; // Is this a daemon ThreadGroup?
 | 
|---|
| 102 | 
 | 
|---|
| 103 |     friend class JTCThread;
 | 
|---|
| 104 | 
 | 
|---|
| 105 |     JTCThreadGroup(bool main);
 | 
|---|
| 106 | 
 | 
|---|
| 107 | public:
 | 
|---|
| 108 | 
 | 
|---|
| 109 |     //
 | 
|---|
| 110 |     // Constructor and destructor.
 | 
|---|
| 111 |     //
 | 
|---|
| 112 |     JTCThreadGroup(const char* name);
 | 
|---|
| 113 |     JTCThreadGroup(JTCThreadGroup* parent, const char* name);
 | 
|---|
| 114 | 
 | 
|---|
| 115 |     virtual ~JTCThreadGroup();
 | 
|---|
| 116 | 
 | 
|---|
| 117 |     //
 | 
|---|
| 118 |     // Get the name of this ThreadGroup.
 | 
|---|
| 119 |     //
 | 
|---|
| 120 |     const char* getName() const;
 | 
|---|
| 121 | 
 | 
|---|
| 122 |     //
 | 
|---|
| 123 |     // Return parent of this ThreadGroup.
 | 
|---|
| 124 |     //
 | 
|---|
| 125 |     JTCThreadGroupHandle getParent() const;
 | 
|---|
| 126 | 
 | 
|---|
| 127 |     //
 | 
|---|
| 128 |     // Is this a daemon Threadgroup?
 | 
|---|
| 129 |     //
 | 
|---|
| 130 |     bool isDaemon() const;
 | 
|---|
| 131 | 
 | 
|---|
| 132 |     //
 | 
|---|
| 133 |     // Set the daemon status of a ThreadGroup.
 | 
|---|
| 134 |     //
 | 
|---|
| 135 |     void setDaemon(bool daemon);
 | 
|---|
| 136 | 
 | 
|---|
| 137 |     //
 | 
|---|
| 138 |     // Called on an uncaught exception.
 | 
|---|
| 139 |     //
 | 
|---|
| 140 |     virtual void uncaughtException(JTCThreadHandle t, const JTCException& e);
 | 
|---|
| 141 | 
 | 
|---|
| 142 |     //
 | 
|---|
| 143 |     // Called on an uncaught unknown exception.
 | 
|---|
| 144 |     //
 | 
|---|
| 145 |     virtual void uncaughtException(JTCThreadHandle t);
 | 
|---|
| 146 | 
 | 
|---|
| 147 |     //
 | 
|---|
| 148 |     // Get maximum priority of this ThreadGroup.
 | 
|---|
| 149 |     //
 | 
|---|
| 150 |     int getMaxPriority() const;
 | 
|---|
| 151 | 
 | 
|---|
| 152 | #ifdef HAVE_JTC_STOP
 | 
|---|
| 153 |     //
 | 
|---|
| 154 |     // Stop, suspend and resume all threads in this ThreadGroup, and child
 | 
|---|
| 155 |     // ThreadGroups.
 | 
|---|
| 156 |     //
 | 
|---|
| 157 |     void stop();
 | 
|---|
| 158 |     void resume();
 | 
|---|
| 159 |     void suspend();
 | 
|---|
| 160 | #endif
 | 
|---|
| 161 | 
 | 
|---|
| 162 |     //
 | 
|---|
| 163 |     // Has this ThreadGroup been destroyed?
 | 
|---|
| 164 |     //
 | 
|---|
| 165 |     bool isDestroyed() const;
 | 
|---|
| 166 | 
 | 
|---|
| 167 |     //
 | 
|---|
| 168 |     // Destroy this ThreadGroup.
 | 
|---|
| 169 |     //
 | 
|---|
| 170 |     void destroy();
 | 
|---|
| 171 | 
 | 
|---|
| 172 |     //
 | 
|---|
| 173 |     // Set maximum priority of threads in this ThreadGroup, and our
 | 
|---|
| 174 |     // child ThreadGroups.
 | 
|---|
| 175 |     //
 | 
|---|
| 176 |     void setMaxPriority(int pri);
 | 
|---|
| 177 | 
 | 
|---|
| 178 |     //
 | 
|---|
| 179 |     // Is this ThreadGroup a parent of the argument?
 | 
|---|
| 180 |     //
 | 
|---|
| 181 |     bool parentOf(JTCThreadGroupHandle g);
 | 
|---|
| 182 | 
 | 
|---|
| 183 |     //
 | 
|---|
| 184 |     // Number of active threads in this group, and child ThreadGroups.
 | 
|---|
| 185 |     // Number of active ThreadGroups in this group, and child ThreadGroups.
 | 
|---|
| 186 |     //
 | 
|---|
| 187 |     int activeCount() const;
 | 
|---|
| 188 |     int activeGroupCount() const;
 | 
|---|
| 189 | 
 | 
|---|
| 190 |     //
 | 
|---|
| 191 |     // Enumerate the set of threads in this ThreadGroup, and child
 | 
|---|
| 192 |     // ThreadGroups, if recurse is true.
 | 
|---|
| 193 |     //
 | 
|---|
| 194 |     int enumerate(JTCThreadHandle* list, int len, bool recurse = true) const;
 | 
|---|
| 195 | 
 | 
|---|
| 196 |     //
 | 
|---|
| 197 |     // Enumerate the set of ThreadGroups in this ThreadGroup, and child
 | 
|---|
| 198 |     // ThreadGroups, if recurse is true.
 | 
|---|
| 199 |     //
 | 
|---|
| 200 |     int enumerate(JTCThreadGroupHandle* list, int len,
 | 
|---|
| 201 |                   bool recurse = true) const;
 | 
|---|
| 202 | 
 | 
|---|
| 203 |     //
 | 
|---|
| 204 |     // Display the set of threads and child ThreadGroups
 | 
|---|
| 205 |     // in this ThreadGroup to cout.
 | 
|---|
| 206 |     //
 | 
|---|
| 207 |     void list();
 | 
|---|
| 208 | 
 | 
|---|
| 209 |     //
 | 
|---|
| 210 |     // Display the set of threads and child ThreadGroups
 | 
|---|
| 211 |     // in this ThreadGroup to stream os, with given indent level.
 | 
|---|
| 212 |     //
 | 
|---|
| 213 |     void list(JTC_STD(ostream)& os, int indent);
 | 
|---|
| 214 | };
 | 
|---|
| 215 | 
 | 
|---|
| 216 | //
 | 
|---|
| 217 | // Insert operator for the iostream library.
 | 
|---|
| 218 | //
 | 
|---|
| 219 | JTC_STD(ostream)& operator<<(JTC_STD(ostream)& os, const JTCThreadGroup& g);
 | 
|---|
| 220 | 
 | 
|---|
| 221 | #endif
 | 
|---|