ActivityManager.hpp
Go to the documentation of this file.
1 /*----------------------------------------------------------------------------*/
2 /* */
3 /* Copyright (c) 1995, 2004 IBM Corporation. All rights reserved. */
4 /* Copyright (c) 2005-2009 Rexx Language Association. All rights reserved. */
5 /* */
6 /* This program and the accompanying materials are made available under */
7 /* the terms of the Common Public License v1.0 which accompanies this */
8 /* distribution. A copy is also available at the following address: */
9 /* http://www.oorexx.org/license.html */
10 /* */
11 /* Redistribution and use in source and binary forms, with or */
12 /* without modification, are permitted provided that the following */
13 /* conditions are met: */
14 /* */
15 /* Redistributions of source code must retain the above copyright */
16 /* notice, this list of conditions and the following disclaimer. */
17 /* Redistributions in binary form must reproduce the above copyright */
18 /* notice, this list of conditions and the following disclaimer in */
19 /* the documentation and/or other materials provided with the distribution. */
20 /* */
21 /* Neither the name of Rexx Language Association nor the names */
22 /* of its contributors may be used to endorse or promote products */
23 /* derived from this software without specific prior written permission. */
24 /* */
25 /* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS */
26 /* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT */
27 /* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS */
28 /* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT */
29 /* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, */
30 /* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED */
31 /* TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, */
32 /* OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY */
33 /* OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING */
34 /* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS */
35 /* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */
36 /* */
37 /*----------------------------------------------------------------------------*/
38 #ifndef Included_ActivityManager
39 #define Included_ActivityManager
40 
41 #include <deque>
42 
43 class RexxIdentityTable;
44 class RexxStack;
45 class RexxCode;
46 class RoutineClass;
48 
50 {
51 public:
52  static void live(size_t);
53  static void liveGeneral(int reason);
54 
55  static void addWaitingActivity(RexxActivity *a, bool release);
56  static wholenumber_t addWaitingActivityCounter(); // Monitoring
57  static inline bool hasWaiters() { return !waitingActivities.empty(); }
58  static RexxActivity *findActivity();
60  static RexxActivity *getActivity();
61  static void returnActivity(RexxActivity *);
62  static void activityEnded(RexxActivity *);
63  static void shutdown();
64  static void checkShutdown();
65  static void createInterpreter();
66  static void terminateInterpreter();
67  static void lockKernel();
68  static void unlockKernel();
69  static bool lockKernelImmediate();
70  static void createLocks();
71  static void closeLocks();
72  static void init();
73  static RexxActivation *newActivation(RexxActivity *activity, RexxActivation *parent, RoutineClass *routine, RexxCode *code, RexxString *calltype, RexxString *environment, int context);
74  static RexxActivation *newActivation(RexxActivity *activity, RexxActivation *parent, RexxCode *code, int context);
75  static RexxActivation *newActivation(RexxActivity *activity, RexxActivation *parent, RexxMethod *method, RexxCode *code);
81  static void haltAllActivities(RexxString *);
82  static void traceAllActivities(bool on);
83  static bool setActivityTrace(thread_id_t thread_id, bool on_or_off);
84  static void clearActivityPool();
85  static bool poolActivity(RexxActivity *activity);
86  static bool haltActivity(thread_id_t thread_id, RexxString * description);
87  static void yieldCurrentActivity();
88  static wholenumber_t yieldCounter(); // Monitoring
89  static void exit(int retcode);
90  static void relinquish(RexxActivity *activity);
91  static wholenumber_t relinquishCounter(); // Monitoring
92  static RexxActivity *getRootActivity();
93  static void returnRootActivity(RexxActivity *activity);
94  static RexxActivity *attachThread();
96  static RexxDirectory *getLocal();
97 
98  static RexxActivity * volatile currentActivity; // the currently active thread
99 
100  static inline void postTermination()
101  {
102  terminationSem.post(); /* let anyone who cares know we're done*/
103  }
104 
105  static inline void waitForTermination(const char *ds, int di)
106  {
107  terminationSem.wait(ds, di); // wait until this is posted
108  }
109 
110 protected:
111  enum
112  {
113  MAX_THREAD_POOL_SIZE = 5, // maximum number of activities we'll pool
114  };
115 
116  /* activities in use */
118  /* free activities */
120  /* table of all localact */
122  static RexxIdentityTable *subClasses; /* SubClasses...one per system */
123  static bool processTerminating; // shutdown processing started
124  static size_t interpreterInstances; // number of times an interpreter has been created.
125 
126  static SysMutex kernelSemaphore; // global kernel semaphore lock
127  static SysSemaphore terminationSem; // used to signal that everything has shutdown
128  static volatile bool sentinel; // used to ensure proper ordering of updates
129  static std::deque<RexxActivity *>waitingActivities; // queue of waiting activities
130 };
131 
132 
133  /* various exception/condition */
134  /* reporting routines */
135 inline void reportCondition(RexxString *condition, RexxString *description) { ActivityManager::currentActivity->raiseCondition(condition, OREF_NULL, description, OREF_NULL, OREF_NULL); }
136 inline void reportNovalue(RexxString *description) { reportCondition(OREF_NOVALUE, description); }
137 inline void reportNostring(RexxString *description) { reportCondition(OREF_NOSTRING, description); }
138 
139 inline void reportException(wholenumber_t error)
140 {
142 }
143 
144 inline void reportException(wholenumber_t error, RexxArray *args)
145 {
147 }
148 
149 inline void reportException(wholenumber_t error, RexxObject *a1)
150 {
152 }
153 
155 {
157 }
158 
160 {
162 }
163 
165 {
167 }
168 
170 {
172 }
173 
174 inline void reportException(wholenumber_t error, const char *a1, RexxObject *a2)
175 {
177 }
178 
179 inline void reportException(wholenumber_t error, RexxObject *a1, const char *a2)
180 {
182 }
183 
184 inline void reportException(wholenumber_t error, const char *a1)
185 {
187 }
188 
189 inline void reportException(wholenumber_t error, const char *a1, const char *a2)
190 {
192 }
193 
194 inline void reportException(wholenumber_t error, const char *a1, const char *a2, wholenumber_t a3)
195 {
197 }
198 
199 inline void reportException(wholenumber_t error, const char *a1, wholenumber_t a2)
200 {
202 }
203 
204 inline void reportException(wholenumber_t error, const char *a1, wholenumber_t a2, RexxObject *a3)
205 {
207 }
208 
210 {
212 }
213 
214 inline void reportException(wholenumber_t error, const char *a1, RexxObject *a2, wholenumber_t a3)
215 {
217 }
218 
219 inline void reportException(wholenumber_t error, const char *a1, RexxObject *a2, wholenumber_t a3, RexxObject *a4)
220 {
221  ActivityManager::currentActivity->reportAnException(error, a1, a2, a3, a4);
222 }
223 
224 inline void reportException(wholenumber_t error, RexxObject *a1, const char *a2, wholenumber_t a3)
225 {
227 }
228 
230 {
232 }
233 
235 {
237 }
238 
239 inline void reportException(wholenumber_t error, RexxObject *a1, RexxObject *a2, const char *a3)
240 {
242 }
243 
245 {
247 }
248 
250 {
251  ActivityManager::currentActivity->reportAnException(error, a1, a2, a3, a4);
252 }
253 
255 {
256  ActivityManager::currentActivity->reportAnException(error, a1, a2, a3, a4, a5);
257 }
258 
259 inline void reportException(wholenumber_t error, const char *a1, RexxObject *a2, const char *a3, RexxObject *a4)
260 {
261  ActivityManager::currentActivity->reportAnException(error, a1, a2, a3, a4);
262 }
263 
264 inline void reportException(wholenumber_t error, const char *a1, RexxObject *a2, RexxObject *a3)
265 {
267 }
268 
269 inline void reportException(wholenumber_t error, const char *a1, RexxObject *a2, RexxObject *a3, RexxObject *a4)
270 {
271  ActivityManager::currentActivity->reportAnException(error, a1, a2, a3, a4);
272 }
273 
274 inline void reportException(wholenumber_t error, const char *a1, RexxObject *a2, RexxObject *a3, RexxObject *a4, RexxObject *a5)
275 {
276  ActivityManager::currentActivity->reportAnException(error, a1, a2, a3, a4, a5);
277 }
278 
279 inline void reportException(wholenumber_t error, const char *a1, RexxObject *a2, RexxObject *a3, const char *a4, RexxObject *a5)
280 {
281  ActivityManager::currentActivity->reportAnException(error, a1, a2, a3, a4, a5);
282 }
283 
284 inline void reportException(wholenumber_t error, const char *a1, RexxObject *a2, RexxObject *a3, wholenumber_t a4)
285 {
286  ActivityManager::currentActivity->reportAnException(error, a1, a2, a3, a4);
287 }
288 
289 inline void reportNomethod(RexxString *message, RexxObject *receiver)
290 {
291  if (!ActivityManager::currentActivity->raiseCondition(OREF_NOMETHOD, OREF_NULL, message, receiver, OREF_NULL))
292  {
293  /* raise as a syntax error */
294  reportException(Error_No_method_name, receiver, message);
295  }
296 }
297 
298 
299 inline void missingArgument(RexxString *kind, size_t argumentPosition)
300 {
301  /* just raise the error */
302  reportException(Error_Incorrect_method_noarg, kind, argumentPosition);
303 }
304 
305 
306 inline void missingArgument(RexxString *kind, const char *argumentPosition)
307 {
308  reportException(Error_Invalid_argument_noarg, kind, argumentPosition);
309 }
310 
311 
314 
315 
317 /******************************************************************************/
318 /* Function: Return name of last message sent via messageSend() */
319 /******************************************************************************/
320 {
322 }
323 
325 /******************************************************************************/
326 /* Function: Return last invoked method object (for use by kernel methods */
327 /* only) */
328 /******************************************************************************/
329 {
331 }
332 
333 
334 /**
335  * A class that can be used to release kernel exclusive access inside
336  * a block and have the kernel access automatically reobtained
337  * once the UnsafeBlock object goes out of scope.
338  */
340 {
341 public:
343  {
346  }
347 
349  {
351  }
352 protected:
354 };
355 
356 
357 /**
358  * A class that can be used to release kernel exclusive access inside
359  * a block and have the kernel access automatically reobtained
360  * once the UnsafeBlock object goes out of scope.
361  */
363 {
364 public:
366  {
368  activity->exitKernel();
369  }
370 
372  {
374  }
375 protected:
377 };
378 
379 
380 
382 {
383 public:
387 
388  RexxNativeActivation *self; // the native activation we operate under
389  RexxActivity *activity; // our current activity
390  InterpreterInstance *instance; // potential interpreter instance
391 };
392 
393 #endif
394 
void reportNomethod(RexxString *message, RexxObject *receiver)
RexxActivity * new_activity()
void reportCondition(RexxString *condition, RexxString *description)
void reportNostring(RexxString *description)
void reportNovalue(RexxString *description)
void reportException(wholenumber_t error)
RexxMethod * lastMethod()
void missingArgument(RexxString *kind, size_t argumentPosition)
RexxString * lastMessageName()
#define OREF_NULL
Definition: RexxCore.h:60
#define Error_Invalid_argument_noarg
#define Error_No_method_name
#define Error_Incorrect_method_noarg
static bool processTerminating
static SysSemaphore terminationSem
static void yieldCurrentActivity()
static RexxActivity * createNewActivity()
static RexxList * allActivities
static bool setActivityTrace(thread_id_t thread_id, bool on_or_off)
static RexxNativeActivation * newNativeActivation(RexxActivity *activity, RexxActivation *parent)
static wholenumber_t addWaitingActivityCounter()
static RexxActivation * newActivation(RexxActivity *activity, RexxActivation *parent, RoutineClass *routine, RexxCode *code, RexxString *calltype, RexxString *environment, int context)
static void clearActivityPool()
static bool poolActivity(RexxActivity *activity)
static void createInterpreter()
static void postTermination()
static void relinquish(RexxActivity *activity)
static wholenumber_t yieldCounter()
static size_t interpreterInstances
static bool lockKernelImmediate()
static RexxList * availableActivities
static void init()
static RexxIdentityTable * subClasses
static std::deque< RexxActivity * > waitingActivities
static RexxDirectory * getLocal()
static void returnActivity(RexxActivity *)
static bool hasWaiters()
static void lockKernel()
static RexxList * activeActivities
static void unlockKernel()
static void shutdown()
static void live(size_t)
static void activityEnded(RexxActivity *)
static void addWaitingActivity(RexxActivity *a, bool release)
static void terminateInterpreter()
static void createLocks()
static bool haltActivity(thread_id_t thread_id, RexxString *description)
static RexxActivity * createCurrentActivity()
static RexxObject * getLocalEnvironment(RexxString *name)
static RexxActivity * attachThread()
static void exit(int retcode)
static void liveGeneral(int reason)
static SysMutex kernelSemaphore
static void closeLocks()
static RexxActivity *volatile currentActivity
static void haltAllActivities(RexxString *)
static void returnRootActivity(RexxActivity *activity)
static RexxActivity * getRootActivity()
static RexxActivity * getActivity()
static volatile bool sentinel
static wholenumber_t relinquishCounter()
static void waitForTermination(const char *ds, int di)
static void checkShutdown()
static RexxActivity * findActivity()
static void traceAllActivities(bool on)
RexxActivity * activity
RexxActivity * activity
InterpreterInstance * instance
RexxObject * protect(RexxObject *o)
void requestAccess()
void reportAnException(wholenumber_t, const char *)
void raiseException(wholenumber_t, RexxString *, RexxArray *, RexxObject *)
RexxString * getLastMessageName()
void releaseAccess()
RexxMethod * getLastMethod()
bool raiseCondition(RexxString *, RexxObject *, RexxString *, RexxObject *, RexxObject *)
void wait(const char *ds, int di)
RexxActivity * activity
ssize_t wholenumber_t
Definition: rexx.h:230
pthread_t thread_id_t