unix/SysThread.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 SysThread.cpp */
40 /* */
41 /* Unix implementation of the SysThread class. */
42 /* */
43 /******************************************************************************/
44 
45 #ifdef HAVE_CONFIG_H
46 # include "config.h"
47 #endif
48 
49 #include "SysThread.hpp"
50 #include <stdio.h>
51 #include <unistd.h>
52 
53 // attach an activity to an existing thread
55 {
56  // initialize the thread basics
57  _threadID = pthread_self();
58  attached = true; // we didn't create this one
59 }
60 
61 
62 void SysThread::setPriority(int priority)
63 {
64  int schedpolicy;
65  struct sched_param schedparam;
66 
67  pthread_getschedparam(_threadID, &schedpolicy, &schedparam);
68 
69  /* Medium_priority(=100) is used for every new thread */
70  schedparam.sched_priority = priority;
71  pthread_setschedparam(_threadID, schedpolicy, &schedparam);
72 }
73 
74 
76 {
77  // default dispatch returns immediately
78 }
79 
80 
82 {
83  int32_t temp;
84  return ((char *)(&temp)) - THREAD_STACK_SIZE;
85 }
86 
87 
89 {
90  if (!attached && _threadID != 0)
91  {
92  pthread_detach(_threadID);
93  _threadID = 0;
94  }
95 }
96 
97 
99 {
100  // this is a nop on Unix.
101 }
102 
103 
105 {
106  // this is a nop on Unix.
107 }
108 
109 
111 {
112  sched_yield();
113 }
114 
115 
117 {
118  return pthread_equal(_threadID, other._threadID);
119 }
120 
121 
122 static void * call_thread_function(void *argument)
123 {
124  ((SysThread *)argument)->dispatch();
125  return NULL;
126 }
127 
128 
129 // create a new thread and attach to an activity
131 {
132  pthread_attr_t newThreadAttr;
133  int schedpolicy, maxpri, minpri;
134  struct sched_param schedparam;
135 
136  // Create an attr block for Thread.
137  pthread_attr_init(&newThreadAttr);
138 #if defined(LINUX) || defined(OPSYS_SUN) || defined(AIX)
139  /* scheduling on two threads controlled by the result method of the */
140  /* message object do not work properly without an enhanced priority */
141  pthread_getschedparam(pthread_self(), &schedpolicy, &schedparam);
142 
143 #if defined(AIX)
144  // Starting with AIX 5.3 the priority for a thread created by
145  // a non root user can not be higher then 59. The priority
146  // of a user prog should not be higher then 59 (IBM AIX development).
147  schedparam.sched_priority = 59;
148 #else
149 # ifdef _POSIX_PRIORITY_SCHEDULING
150  maxpri = sched_get_priority_max(schedpolicy);
151  minpri = sched_get_priority_min(schedpolicy);
152  schedparam.sched_priority = (minpri + maxpri) / 2;
153 # endif
154 #endif
155 
156 #if defined(OPSYS_SUN)
157  /* PTHREAD_EXPLICIT_SCHED ==> use scheduling attributes of the new object */
158  pthread_attr_setinheritsched(&newThreadAttr, PTHREAD_EXPLICIT_SCHED);
159 
160  /* Performance measurements show massive performance improvements > 50 % */
161  /* using Round Robin scheduling instead of FIFO scheduling */
162  pthread_attr_setschedpolicy(&newThreadAttr, SCHED_RR);
163 #endif
164 
165 #if defined(AIX)
166  /* PTHREAD_EXPLICIT_SCHED ==> use scheduling attributes of the new object */
167  pthread_attr_setinheritsched(&newThreadAttr, PTHREAD_EXPLICIT_SCHED);
168 
169  /* Each thread has an initial priority that is dynamically modified by */
170  /* the scheduler, according to the thread's activity; thread execution */
171  /* is time-sliced. On other systems, this scheduling policy may be */
172  /* different. */
173  pthread_attr_setschedpolicy(&newThreadAttr, SCHED_OTHER);
174 #endif
175 
176  pthread_attr_setschedparam(&newThreadAttr, &schedparam);
177 #endif
178 
179  // Set the stack size.
180  pthread_attr_setstacksize(&newThreadAttr, THREAD_STACK_SIZE);
181 
182  // Now create the thread
183  int rc = pthread_create(&_threadID, &newThreadAttr, call_thread_function, (void *)this);
184  if (rc != 0)
185  {
186  _threadID = 0;
187  fprintf(stderr," *** ERROR: At SysThread(), createThread - RC = %d !\n", rc);
188  }
189  pthread_attr_destroy(&newThreadAttr);
190  attached = false; // we own this thread
191  return;
192 }
193 
pthread_t _threadID
void startup()
virtual void dispatch()
virtual void attachThread()
void createThread()
void terminate()
void setPriority(int priority)
bool equals(SysThread &other)
char * getStackBase()
static void * call_thread_function(void *argument)
int int32_t