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  if (!attached && _threadID != 0)
84  {
85  pthread_detach(_threadID);
86  _threadID = 0;
87  }
88 }
89 
90 
92 {
93  // this is a nop on Unix.
94 }
95 
96 
98 {
99  // this is a nop on Unix.
100 }
101 
102 
104 {
105  sched_yield();
106 }
107 
108 
110 {
111  return pthread_equal(_threadID, other._threadID);
112 }
113 
114 
115 static void * call_thread_function(void *argument)
116 {
117  ((SysThread *)argument)->dispatch();
118  return NULL;
119 }
120 
121 
122 // create a new thread and attach to an activity
124 {
125  pthread_attr_t newThreadAttr;
126  int schedpolicy, maxpri, minpri;
127  struct sched_param schedparam;
128 
129  // Create an attr block for Thread.
130  pthread_attr_init(&newThreadAttr);
131 #if defined(LINUX) || defined(OPSYS_SUN) || defined(AIX)
132  /* scheduling on two threads controlled by the result method of the */
133  /* message object do not work properly without an enhanced priority */
134  pthread_getschedparam(pthread_self(), &schedpolicy, &schedparam);
135 
136 #if defined(AIX)
137  // Starting with AIX 5.3 the priority for a thread created by
138  // a non root user can not be higher then 59. The priority
139  // of a user prog should not be higher then 59 (IBM AIX development).
140  schedparam.sched_priority = 59;
141 #else
142 # ifdef _POSIX_PRIORITY_SCHEDULING
143  maxpri = sched_get_priority_max(schedpolicy);
144  minpri = sched_get_priority_min(schedpolicy);
145  schedparam.sched_priority = (minpri + maxpri) / 2;
146 # endif
147 #endif
148 
149 #if defined(OPSYS_SUN)
150  /* PTHREAD_EXPLICIT_SCHED ==> use scheduling attributes of the new object */
151  pthread_attr_setinheritsched(&newThreadAttr, PTHREAD_EXPLICIT_SCHED);
152 
153  /* Performance measurements show massive performance improvements > 50 % */
154  /* using Round Robin scheduling instead of FIFO scheduling */
155  pthread_attr_setschedpolicy(&newThreadAttr, SCHED_RR);
156 #endif
157 
158 #if defined(AIX)
159  /* PTHREAD_EXPLICIT_SCHED ==> use scheduling attributes of the new object */
160  pthread_attr_setinheritsched(&newThreadAttr, PTHREAD_EXPLICIT_SCHED);
161 
162  /* Each thread has an initial priority that is dynamically modified by */
163  /* the scheduler, according to the thread's activity; thread execution */
164  /* is time-sliced. On other systems, this scheduling policy may be */
165  /* different. */
166  pthread_attr_setschedpolicy(&newThreadAttr, SCHED_OTHER);
167 #endif
168 
169  pthread_attr_setschedparam(&newThreadAttr, &schedparam);
170 #endif
171 
172  // Set the stack size.
173  pthread_attr_setstacksize(&newThreadAttr, THREAD_STACK_SIZE);
174 
175  // Now create the thread
176  int rc = pthread_create(&_threadID, &newThreadAttr, call_thread_function, (void *)this);
177  if (rc != 0)
178  {
179  _threadID = 0;
180  fprintf(stderr," *** ERROR: At SysThread(), createThread - RC = %d !\n", rc);
181  }
182  pthread_attr_destroy(&newThreadAttr);
183  attached = false; // we own this thread
184  return;
185 }
186 
pthread_t _threadID
void startup()
virtual void dispatch()
void shutdown()
virtual void attachThread()
void createThread()
void terminate()
void setPriority(int priority)
bool equals(SysThread &other)
static void * call_thread_function(void *argument)