unix/SysActivity.cpp
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.ibm.com/developerworks/oss/CPLv1.0.htm */
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 /******************************************************************************/
39 /* REXX Kernel SysActivity.hpp */
40 /* */
41 /* System support for Thread operations */
42 /* */
43 /******************************************************************************/
44 
45 #include "RexxCore.h"
46 #include "ActivityManager.hpp"
47 #include <errno.h>
48 
49 #define THREAD_PRIORITY 100
50 
51 #include "RexxCore.h"
52 #include "SysActivity.hpp"
53 
54 
55 /**
56  * Top launcher function for spinning off a new activity.
57  *
58  * @param args The thread arguments (just the pointer to the activity).
59  */
60 void *threadFnc(void *args)
61 {
62  ((RexxActivity *)args)->runThread();
63  return NULL;
64 }
65 
66 /**
67  * Close out any resources required by this thread descriptor.
68  */
70 {
71  threadId = 0;
72 }
73 
74 /**
75  * Create a new thread for an activity.
76  *
77  * @param activity The activity that will run on this thread.
78  * @param stackSize The desired stack size.
79  */
80 void SysActivity::create(RexxActivity *activity, size_t stackSize)
81 {
82  int rc;
83  pthread_attr_t newThreadAttr;
84  int schedpolicy;
85  struct sched_param schedparam;
86 
87  // Create an attr block for Thread.
88  rc = pthread_attr_init(&newThreadAttr);
89  // Set the stack size.
90  #if defined(LINUX) || defined(OPSYS_SUN) || defined(AIX)
91 
92  /* scheduling on two threads controlled by the result method of the message object */
93  /* do not work proper without an enhanced priority */
94 
95  pthread_getschedparam(pthread_self(), &schedpolicy, &schedparam);
96  schedparam.sched_priority = 100;
97 
98  #if defined(OPSYS_SUN)
99  /* PTHREAD_EXPLICIT_SCHED ==> use scheduling attributes of the new object */
100 
101  rc = pthread_attr_setinheritsched(&newThreadAttr, PTHREAD_EXPLICIT_SCHED);
102 
103  /* Performance measurements show massive performance improvements > 50 % */
104  /* using Round Robin scheduling instead of FIFO scheduling */
105  rc = pthread_attr_setschedpolicy(&newThreadAttr, SCHED_RR);
106  #endif
107  rc = pthread_attr_setschedparam(&newThreadAttr, &schedparam);
108 
109  #endif
110  rc = pthread_attr_setstacksize(&newThreadAttr, stackSize);
111  // Now create the thread
112  rc = pthread_create(&threadId, &newThreadAttr, threadFnc, (void *)activity);
113  // Bumop thread count by one. Threadid
114  if (rc != 0)
115  {
116  reportException(Error_System_service_service, "ERROR CREATING THREAD");
117  }
118  rc = pthread_attr_destroy(&newThreadAttr);
119 }
120 
121 
122 /**
123  * Get the ID of the current thread.
124  *
125  * @return The thread identifer for the current thread.
126  */
128 {
129  return (thread_id_t)pthread_self(); /* just call the correct function */
130 }
131 
132 
133 /**
134  * Check if this activity is getting used on the correct
135  * thread.
136  *
137  * @return true if the current thread is the same as the one
138  * the activity was created for.
139  */
141 {
142  return threadId == pthread_self();
143 }
144 
145 
146 /**
147  * Initialize the descriptor for manipulating the current
148  * active thread.
149  */
151 {
152  // we need both an identifier and a handle
153  threadId = pthread_self();
154 }
155 
156 
157 /**
158  * Return the pointer to the base of the current stack.
159  * This is used for checking recursion overflows.
160  *
161  * @return The character pointer for the stack base.
162  */
163 char *SysActivity::getStackBase(size_t stackSize)
164 {
165  size_t temp;
166  return (char *)&temp - stackSize;
167 }
168 
169 
170 wholenumber_t yieldCount = 0; // Monitoring
171 
173 {
174  yieldCount += 1;
175  sched_yield();
176 }
177 
178 
180 {
181  return yieldCount;
182 }
void reportException(wholenumber_t error)
#define Error_System_service_service
static wholenumber_t yieldCounter()
pthread_t threadId
static thread_id_t queryThreadID()
static void yield()
void create(RexxActivity *activity, size_t stackSize)
char * getStackBase(size_t stackSize)
ssize_t wholenumber_t
Definition: rexx.h:230
void * threadFnc(void *args)
wholenumber_t yieldCount
pthread_t thread_id_t