CPN
Computational Process Networks
PthreadAttr.cc
Go to the documentation of this file.
1 //=============================================================================
2 // PthreadAttr class
3 //-----------------------------------------------------------------------------
4 // POSIX Pthread class library
5 // Copyright (C) 1997-1999 The University of Texas
6 //
7 // This library is free software; you can redistribute it and/or modify it
8 // under the terms of the GNU Library General Public License as published
9 // by the Free Software Foundation; either version 2 of the License, or
10 // (at your option) any later version.
11 //
12 // This library is distributed in the hope that it will be useful,
13 // but WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 // Library General Public License for more details.
16 //
17 // The GNU Public License is available in the file LICENSE, or you
18 // can write to the Free Software Foundation, Inc., 59 Temple Place -
19 // Suite 330, Boston, MA 02111-1307, USA, or you can find it on the
20 // World Wide Web at http://www.fsf.org.
21 //=============================================================================
22 
23 #ifdef EXTERNAL_TEMPLATES
24 # pragma implementation "PthreadAttr.h"
25 #endif
26 
28 #ifdef _POSIX_THREADS
29 
30 
31 //-----------------------------------------------------------------------------
32 PthreadAttr::PthreadAttr(int systemScope)
33 //-----------------------------------------------------------------------------
34 {
35  TrapError(pthread_attr_init(&attr));
36  if (systemScope) SystemScope();
37  referenceCount = 1;
38 // printf("PthreadAttr::PthreadAttr()\n");
39 }
40 
41 //-----------------------------------------------------------------------------
43 //-----------------------------------------------------------------------------
44 {
45  try {
47  // printf("PthreadAttr::~PthreadAttr: referenceCount = %d\n", referenceCount);
48  if (!referenceCount)
49  TrapError(pthread_attr_destroy(&attr));
50  } catch (...) {
51  std::terminate();
52  }
53 }
54 
55 
56 //-----------------------------------------------------------------------------
58 //-----------------------------------------------------------------------------
59 {
60  attr = cpAttr.attr;
61  referenceCount = cpAttr.referenceCount+1;
62 // printf("PthreadAttr::PthreadAttr(const PthreadAttr&): referenceCount = %d\n", referenceCount);
63 }
64 
65 
66 //-----------------------------------------------------------------------------
68 // Gets the thread's detach state. This is one of:
69 // PTHREAD_CREATE_DETACHED: The thread is detached. It will destroy
70 // itself when it exits or terminates.
71 // PTHREAD_CREATE_JOINABLE: The thread is joinable. It will remain
72 // when it exits or terminates, and wait for its parent to
73 // rejoin with it (see Thread::Join()).
74 //-----------------------------------------------------------------------------
75 {
76  int detState;
77  TrapError(pthread_attr_getdetachstate(&attr, &detState));
78  return detState;
79 }
80 
81 
82 //-----------------------------------------------------------------------------
83 int PthreadAttr::DetachState(int detState)
84 // Sets the thread's detach state. See above for a description of the two
85 // detach states.
86 //-----------------------------------------------------------------------------
87 {
88  TrapError(pthread_attr_setdetachstate(&attr, detState));
89  return detState;
90 }
91 
92 
93 #if defined _XOPEN_REALTIME_THREADS
94 #if defined __USE_XOPEN2K
95 //-----------------------------------------------------------------------------
96 void* PthreadAttr::Stack(void* stackaddr, size_t stacksize)
97 //-----------------------------------------------------------------------------
98 {
99  TrapError(pthread_attr_setstack(&attr, stackaddr, stacksize));
100  return stackaddr;
101 }
102 
103 //-----------------------------------------------------------------------------
104 void* PthreadAttr::Stack(size_t& stackSize)
105 //-----------------------------------------------------------------------------
106 { void* stackaddr;
107  TrapError(pthread_attr_getstack(&attr, &stackaddr, &stackSize));
108  return stackaddr;
109 }
110 #endif
111 #elif defined _POSIX_THREAD_ATTR_STACKADDR
112 //-----------------------------------------------------------------------------
113 void* PthreadAttr::StackAddress(void* stackaddr)
114 //-----------------------------------------------------------------------------
115 {
116  TrapError(pthread_attr_setstackaddr(&attr, stackaddr));
117  return stackaddr;
118 }
119 
120 //-----------------------------------------------------------------------------
121 void* PthreadAttr::StackAddress(void)
122 //-----------------------------------------------------------------------------
123 { void* stackaddr;
124  TrapError(pthread_attr_getstackaddr(&attr, &stackaddr));
125  return stackaddr;
126 }
127 #endif
128 
129 
130 #ifdef _POSIX_THREAD_ATTR_STACKSIZE
131 //-----------------------------------------------------------------------------
132 size_t PthreadAttr::StackSize(size_t stacksize)
133 //-----------------------------------------------------------------------------
134 {
135  TrapError(pthread_attr_setstacksize(&attr, stacksize));
136  return stacksize;
137 }
138 
139 //-----------------------------------------------------------------------------
140 size_t PthreadAttr::StackSize(void)
141 //-----------------------------------------------------------------------------
142 { size_t stacksize;
143  TrapError(pthread_attr_getstacksize(&attr, &stacksize));
144  return stacksize;
145 }
146 #endif
147 
148 
149 #ifdef _POSIX_THREAD_ATTR_PRIORITY_SCHEDULING
150 //-----------------------------------------------------------------------------
151 int PthreadAttr::ScheduleInherit(int inherit)
152 //-----------------------------------------------------------------------------
153 {
154  TrapError(pthread_attr_setinheritsched(&attr, inherit));
155  return inherit;
156 }
157 
158 //-----------------------------------------------------------------------------
159 int PthreadAttr::ScheduleInherit(void)
160 //-----------------------------------------------------------------------------
161 { int inherit;
162  TrapError(pthread_attr_getinheritsched(&attr, &inherit));
163  return inherit;
164 }
165 #endif
166 
167 
168 #ifdef _POSIX_THREAD_ATTR_PRIORITY_SCHEDULING
169 //-----------------------------------------------------------------------------
170 void PthreadAttr::SetScheduleParam(PthreadScheduleParam& sp)
171 //-----------------------------------------------------------------------------
172 {
173  TrapError(pthread_attr_setschedparam(&attr, sp));
174 }
175 
176 //-----------------------------------------------------------------------------
177 void PthreadAttr::GetScheduleParam(PthreadScheduleParam& sp)
178 //-----------------------------------------------------------------------------
179 {
180  TrapError(pthread_attr_getschedparam(&attr, sp));
181 }
182 #endif
183 
184 
185 #ifdef _POSIX_THREAD_ATTR_PRIORITY_SCHEDULING
186 //-----------------------------------------------------------------------------
187 int PthreadAttr::SchedulePolicy(int policy)
188 // SCHED_FIFO: A thread runs until it blocks (then end of priority line).
189 // SCHED_RR: Time-sliced threads (with fixed priorities).
190 // SCHED_OTHER: Implementation dependent (generally Unix-like)
191 //-----------------------------------------------------------------------------
192 {
193  TrapError(pthread_attr_setschedpolicy(&attr, policy));
194  return policy;
195 }
196 
197 //-----------------------------------------------------------------------------
198 int PthreadAttr::SchedulePolicy(void)
199 //-----------------------------------------------------------------------------
200 { int policy;
201  TrapError(pthread_attr_getschedpolicy(&attr, &policy));
202  return policy;
203 }
204 #endif
205 
206 
207 //#ifdef _POSIX_THREAD_ATTR_PRIORITY_SCHEDULING
208 //-----------------------------------------------------------------------------
210 //-----------------------------------------------------------------------------
211 {
212  TrapError(pthread_attr_setscope(&attr, scope));
213  return scope;
214 }
215 
216 //-----------------------------------------------------------------------------
218 //-----------------------------------------------------------------------------
219 { int scope;
220  TrapError(pthread_attr_getscope(&attr, &scope));
221  return scope;
222 }
223 //#endif
224 
225 
226 #if 0
227 //-----------------------------------------------------------------------------
228 int PthreadAttr::Priority(int priority)
229 //-----------------------------------------------------------------------------
230 {
231  PthreadScheduleParam sp.Priority(priority);
232  SetScheduleParam(sp);
233  return sp.Priority();
234 }
235 
236 //-----------------------------------------------------------------------------
237 int PthreadAttr::Priority(void)
238 //-----------------------------------------------------------------------------
239 {
241  GetScheduleParam(sp);
242  return sp.Priority();
243 }
244 #endif
245 
246 
247 #endif
int Priority(int priority)
pthread_attr_t attr
Definition: PthreadAttr.h:100
~PthreadAttr(void)
Definition: PthreadAttr.cc:42
PthreadAttr(int systemScope=0)
Definition: PthreadAttr.cc:32
void SystemScope(void)
Definition: PthreadAttr.h:94
int DetachState(void)
Definition: PthreadAttr.cc:67
void TrapError(int result)
int ScheduleScope(void)
Definition: PthreadAttr.cc:217
int referenceCount
Definition: PthreadAttr.h:101