| [0b990d] | 1 | // | 
|---|
|  | 2 | // thpthd.cc | 
|---|
|  | 3 | // | 
|---|
|  | 4 | // Copyright (C) 1997 Limit Point Systems, Inc. | 
|---|
|  | 5 | // | 
|---|
|  | 6 | // Author: Edward Seidl <seidl@janed.com> | 
|---|
|  | 7 | // Maintainer: LPS | 
|---|
|  | 8 | // | 
|---|
|  | 9 | // This file is part of the SC Toolkit. | 
|---|
|  | 10 | // | 
|---|
|  | 11 | // The SC Toolkit is free software; you can redistribute it and/or modify | 
|---|
|  | 12 | // it under the terms of the GNU Library General Public License as published by | 
|---|
|  | 13 | // the Free Software Foundation; either version 2, or (at your option) | 
|---|
|  | 14 | // any later version. | 
|---|
|  | 15 | // | 
|---|
|  | 16 | // The SC Toolkit is distributed in the hope that it will be useful, | 
|---|
|  | 17 | // but WITHOUT ANY WARRANTY; without even the implied warranty of | 
|---|
|  | 18 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the | 
|---|
|  | 19 | // GNU Library General Public License for more details. | 
|---|
|  | 20 | // | 
|---|
|  | 21 | // You should have received a copy of the GNU Library General Public License | 
|---|
|  | 22 | // along with the SC Toolkit; see the file COPYING.LIB.  If not, write to | 
|---|
|  | 23 | // the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. | 
|---|
|  | 24 | // | 
|---|
|  | 25 | // The U.S. Government is granted a limited license as per AL 91-7. | 
|---|
|  | 26 | // | 
|---|
|  | 27 |  | 
|---|
|  | 28 | #ifdef __GNUC__ | 
|---|
|  | 29 | #pragma implementation | 
|---|
|  | 30 | #endif | 
|---|
|  | 31 |  | 
|---|
|  | 32 | #ifdef HAVE_CONFIG_H | 
|---|
|  | 33 | #include <scconfig.h> | 
|---|
|  | 34 | #endif | 
|---|
|  | 35 |  | 
|---|
|  | 36 | #if HAVE_PTHREAD_H | 
|---|
|  | 37 | #include <pthread.h> | 
|---|
|  | 38 | #endif | 
|---|
|  | 39 |  | 
|---|
|  | 40 | #include <string.h> | 
|---|
|  | 41 |  | 
|---|
|  | 42 | #include <util/keyval/keyval.h> | 
|---|
|  | 43 | #include <util/group/thpthd.h> | 
|---|
|  | 44 | #include <util/misc/formio.h> | 
|---|
|  | 45 |  | 
|---|
|  | 46 | using namespace std; | 
|---|
|  | 47 | using namespace sc; | 
|---|
|  | 48 |  | 
|---|
|  | 49 | namespace sc { | 
|---|
|  | 50 |  | 
|---|
|  | 51 | ///////////////////////////////////////////////////////////////////////////// | 
|---|
|  | 52 | // PthreadThreadLock class | 
|---|
|  | 53 |  | 
|---|
|  | 54 | class PthreadThreadLock : public ThreadLock { | 
|---|
|  | 55 | private: | 
|---|
|  | 56 | pthread_mutex_t mutex_; | 
|---|
|  | 57 | pthread_mutexattr_t attr_; | 
|---|
|  | 58 |  | 
|---|
|  | 59 | public: | 
|---|
|  | 60 | PthreadThreadLock() { | 
|---|
|  | 61 | pthread_mutexattr_init(&attr_); | 
|---|
|  | 62 | //#if defined(PTHREAD_MUTEX_FAST_NP) | 
|---|
|  | 63 | //      pthread_mutexattr_setkind_np(&attr_, PTHREAD_MUTEX_FAST_NP); | 
|---|
|  | 64 | //#elif defined(MUTEX_FAST_NP) | 
|---|
|  | 65 | //      pthread_mutexattr_setkind_np(&attr_, MUTEX_FAST_NP); | 
|---|
|  | 66 | //#endif | 
|---|
|  | 67 | pthread_mutex_init(&mutex_, &attr_); | 
|---|
|  | 68 | } | 
|---|
|  | 69 |  | 
|---|
|  | 70 | ~PthreadThreadLock() { | 
|---|
|  | 71 | pthread_mutexattr_destroy(&attr_); | 
|---|
|  | 72 | pthread_mutex_destroy(&mutex_); | 
|---|
|  | 73 | } | 
|---|
|  | 74 |  | 
|---|
|  | 75 | void lock() { pthread_mutex_lock(&mutex_); } | 
|---|
|  | 76 | void unlock() { pthread_mutex_unlock(&mutex_); } | 
|---|
|  | 77 | }; | 
|---|
|  | 78 |  | 
|---|
|  | 79 | ///////////////////////////////////////////////////////////////////////////// | 
|---|
|  | 80 | // PthreadThreadGrp members | 
|---|
|  | 81 |  | 
|---|
|  | 82 | static ClassDesc PthreadThreadGrp_cd( | 
|---|
|  | 83 | typeid(PthreadThreadGrp),"PthreadThreadGrp",1,"public ThreadGrp", | 
|---|
|  | 84 | 0, create<PthreadThreadGrp>, 0); | 
|---|
|  | 85 |  | 
|---|
|  | 86 | PthreadThreadGrp::PthreadThreadGrp() | 
|---|
|  | 87 | : ThreadGrp() | 
|---|
|  | 88 | { | 
|---|
|  | 89 | pthreads_ = new pthread_t[nthread_]; | 
|---|
|  | 90 | init_attr(); | 
|---|
|  | 91 | } | 
|---|
|  | 92 |  | 
|---|
|  | 93 |  | 
|---|
|  | 94 | PthreadThreadGrp::PthreadThreadGrp(const PthreadThreadGrp &tg,int nthread): | 
|---|
|  | 95 | ThreadGrp(tg, nthread) | 
|---|
|  | 96 | { | 
|---|
|  | 97 | pthreads_ = new pthread_t[nthread_]; | 
|---|
|  | 98 | init_attr(); | 
|---|
|  | 99 | } | 
|---|
|  | 100 |  | 
|---|
|  | 101 | PthreadThreadGrp::PthreadThreadGrp(const Ref<KeyVal>& keyval) | 
|---|
|  | 102 | : ThreadGrp(keyval) | 
|---|
|  | 103 | { | 
|---|
|  | 104 | pthreads_ = new pthread_t[nthread_]; | 
|---|
|  | 105 | init_attr(); | 
|---|
|  | 106 | } | 
|---|
|  | 107 |  | 
|---|
|  | 108 | PthreadThreadGrp::~PthreadThreadGrp() | 
|---|
|  | 109 | { | 
|---|
|  | 110 | if (pthreads_) { | 
|---|
|  | 111 | delete[] pthreads_; | 
|---|
|  | 112 | pthreads_ = 0; | 
|---|
|  | 113 | delete[] attr_; | 
|---|
|  | 114 | } | 
|---|
|  | 115 | //  delete attr_; | 
|---|
|  | 116 | } | 
|---|
|  | 117 |  | 
|---|
|  | 118 | void | 
|---|
|  | 119 | PthreadThreadGrp::init_attr() | 
|---|
|  | 120 | { | 
|---|
|  | 121 | attr_ = new pthread_attr_t[nthread_]; | 
|---|
|  | 122 |  | 
|---|
|  | 123 | for (int i=0; i<nthread_; i++) { | 
|---|
|  | 124 | pthread_attr_init(&attr_[i]); | 
|---|
|  | 125 | #if defined(PTHREAD_CREATE_UNDETACHED) | 
|---|
|  | 126 | pthread_attr_setdetachstate(&attr_[i], PTHREAD_CREATE_UNDETACHED); | 
|---|
|  | 127 | #elif defined(PTHREAD_CREATE_JOINABLE) | 
|---|
|  | 128 | pthread_attr_setdetachstate(&attr_[i], PTHREAD_CREATE_JOINABLE); | 
|---|
|  | 129 | #endif | 
|---|
|  | 130 | #ifdef HAVE_PTHREAD_ATTR_GETSTACKSIZE | 
|---|
|  | 131 | size_t defstacksize; | 
|---|
|  | 132 | pthread_attr_getstacksize(&attr_[i], &defstacksize); | 
|---|
|  | 133 | #elif HAVE_PTHREAD_ATTR_SETSTACKSIZE | 
|---|
|  | 134 | size_t defstacksize = 1; | 
|---|
|  | 135 | #endif | 
|---|
|  | 136 | #ifdef HAVE_PTHREAD_ATTR_SETSTACKSIZE | 
|---|
|  | 137 | size_t minstacksize = 2097152; | 
|---|
|  | 138 | if (defstacksize < minstacksize) { | 
|---|
| [318d5a] | 139 | pthread_attr_setstacksize(&attr_[i], minstacksize); | 
|---|
| [0b990d] | 140 | } | 
|---|
|  | 141 | #endif | 
|---|
|  | 142 | } | 
|---|
|  | 143 | } | 
|---|
|  | 144 |  | 
|---|
|  | 145 | void PthreadThreadGrp::add_thread(int ithread, Thread* t, int priority) | 
|---|
|  | 146 | { | 
|---|
|  | 147 | if (ithread >= nthread_) { | 
|---|
|  | 148 | ExEnv::err0() << indent | 
|---|
|  | 149 | << "PthreadThreadGrp::add_thread(int, Thread*, int, int): trying to" | 
|---|
|  | 150 | << "add too many threads" << endl; | 
|---|
|  | 151 | } | 
|---|
|  | 152 | else { | 
|---|
|  | 153 | threads_[ithread] = t; | 
|---|
|  | 154 | //init_priority(ithread, priority); | 
|---|
|  | 155 | } | 
|---|
|  | 156 |  | 
|---|
|  | 157 | } | 
|---|
|  | 158 |  | 
|---|
|  | 159 | #if defined(HAVE_SCHED_GET_PRIORITY_MAX) \ | 
|---|
|  | 160 | && defined(HAVE_SCHED_GET_PRIORITY_MAX) \ | 
|---|
|  | 161 | && defined(HAVE_PTHREAD_ATTR_SETSCOPE) \ | 
|---|
|  | 162 | && defined(HAVE_PTHREAD_ATTR_SETSCHEDPARAM) \ | 
|---|
|  | 163 | && defined(HAVE_PTHREAD_ATTR_SETINHERITSCHED) \ | 
|---|
|  | 164 | && defined(HAVE_PTHREAD_ATTR_SETSCHEDPOLICY) | 
|---|
|  | 165 | #define THREAD_PRIORITY_CAN_BE_SET | 
|---|
|  | 166 | #else | 
|---|
|  | 167 | #undef THREAD_PRIORITY_CAN_BE_SET | 
|---|
|  | 168 | #endif | 
|---|
|  | 169 |  | 
|---|
|  | 170 | void PthreadThreadGrp::init_priority(int ithread, int priority) | 
|---|
|  | 171 | { | 
|---|
|  | 172 | #ifdef THREAD_PRIORITY_CAN_BE_SET | 
|---|
|  | 173 | struct sched_param param, low_param, high_param; | 
|---|
|  | 174 | int rc, selected_sched, set_params; | 
|---|
|  | 175 |  | 
|---|
|  | 176 | set_params=0; | 
|---|
|  | 177 |  | 
|---|
|  | 178 | // Check priority settings for various schedulers and select which to use | 
|---|
|  | 179 | selected_sched=-1; | 
|---|
|  | 180 |  | 
|---|
|  | 181 | #ifdef SCHED_OTHER | 
|---|
|  | 182 | low_param.sched_priority = sched_get_priority_min(SCHED_OTHER); | 
|---|
|  | 183 | high_param.sched_priority = sched_get_priority_max(SCHED_OTHER); | 
|---|
|  | 184 | if (high_param.sched_priority > low_param.sched_priority) { | 
|---|
|  | 185 | selected_sched = SCHED_OTHER; | 
|---|
|  | 186 | set_params=1; | 
|---|
|  | 187 | } | 
|---|
|  | 188 | #endif // SCHED_OTHER | 
|---|
|  | 189 | #ifdef SCHED_RR | 
|---|
|  | 190 | if (!set_params) { | 
|---|
|  | 191 | low_param.sched_priority = sched_get_priority_min(SCHED_RR); | 
|---|
|  | 192 | high_param.sched_priority = sched_get_priority_max(SCHED_RR); | 
|---|
|  | 193 | if (high_param.sched_priority > low_param.sched_priority) { | 
|---|
|  | 194 | selected_sched=SCHED_RR; set_params=1; | 
|---|
|  | 195 | } | 
|---|
|  | 196 | } | 
|---|
|  | 197 | #endif // SCHED_RR | 
|---|
|  | 198 | #ifdef SCHED_FIFO | 
|---|
|  | 199 | if (!set_params) { | 
|---|
|  | 200 | low_param.sched_priority = sched_get_priority_min(SCHED_FIFO); | 
|---|
|  | 201 | high_param.sched_priority = sched_get_priority_max(SCHED_FIFO); | 
|---|
|  | 202 | if (high_param.sched_priority > low_param.sched_priority) { | 
|---|
|  | 203 | selected_sched=SCHED_FIFO; set_params=1; | 
|---|
|  | 204 | } | 
|---|
|  | 205 | } | 
|---|
|  | 206 | #endif // SCHED_FIFO | 
|---|
|  | 207 |  | 
|---|
|  | 208 | #ifdef PTHREAD_SCOPE_SYSTEM | 
|---|
|  | 209 | pthread_attr_setscope(&attr_[ithread],PTHREAD_SCOPE_SYSTEM); | 
|---|
|  | 210 | #endif | 
|---|
|  | 211 | if (set_params) { | 
|---|
|  | 212 | pthread_attr_setinheritsched(&attr_[ithread],PTHREAD_EXPLICIT_SCHED); | 
|---|
|  | 213 | pthread_attr_setschedpolicy(&attr_[ithread], selected_sched); | 
|---|
|  | 214 | param.sched_priority = ( sched_get_priority_min(selected_sched) + priority ); | 
|---|
|  | 215 | pthread_attr_setschedparam(&attr_[ithread],¶m); | 
|---|
|  | 216 | } | 
|---|
|  | 217 | #endif // THREAD_PRIORITY_CAN_BE_SET | 
|---|
|  | 218 | } | 
|---|
|  | 219 | int | 
|---|
|  | 220 | PthreadThreadGrp::start_threads() | 
|---|
|  | 221 | { | 
|---|
|  | 222 | for (int i=1; i < nthread_; i++) { | 
|---|
|  | 223 | if (threads_[i]) { | 
|---|
|  | 224 | int res = pthread_create(&pthreads_[i], &attr_[i], | 
|---|
|  | 225 | Thread__run_Thread_run, | 
|---|
|  | 226 | (void*) threads_[i]); | 
|---|
|  | 227 | if (res) { | 
|---|
|  | 228 | ExEnv::errn() << indent << "pthread_create failed" << endl; | 
|---|
|  | 229 | ExEnv::errn() << "error: " << res << ": " << strerror(res) << endl; | 
|---|
|  | 230 | return -1; | 
|---|
|  | 231 | } | 
|---|
|  | 232 | } | 
|---|
|  | 233 | } | 
|---|
|  | 234 | if (threads_[0]) threads_[0]->run(); | 
|---|
|  | 235 |  | 
|---|
|  | 236 | return 0; | 
|---|
|  | 237 | } | 
|---|
|  | 238 |  | 
|---|
|  | 239 | int | 
|---|
|  | 240 | PthreadThreadGrp::wait_threads() | 
|---|
|  | 241 | { | 
|---|
|  | 242 | for (int i=1; i < nthread_; i++) { | 
|---|
|  | 243 | void *tn; | 
|---|
|  | 244 | if (threads_[i]) { | 
|---|
|  | 245 | int rc = pthread_join(pthreads_[i], (void**)&tn); | 
|---|
|  | 246 | if (rc) { | 
|---|
|  | 247 | ExEnv::errn() | 
|---|
|  | 248 | << "PthreadThreadGrp::wait_threads(): error joining thread" | 
|---|
|  | 249 | << endl; | 
|---|
|  | 250 | ExEnv::errn() << "error: " << rc << ": " << strerror(rc) << endl; | 
|---|
|  | 251 | abort(); | 
|---|
|  | 252 | } | 
|---|
|  | 253 | } | 
|---|
|  | 254 | } | 
|---|
|  | 255 |  | 
|---|
|  | 256 | return 0; | 
|---|
|  | 257 | } | 
|---|
|  | 258 |  | 
|---|
|  | 259 | Ref<ThreadLock> | 
|---|
|  | 260 | PthreadThreadGrp::new_lock() | 
|---|
|  | 261 | { | 
|---|
|  | 262 | return new PthreadThreadLock; | 
|---|
|  | 263 | } | 
|---|
|  | 264 |  | 
|---|
|  | 265 | ThreadGrp* | 
|---|
|  | 266 | PthreadThreadGrp::clone(int nthread) | 
|---|
|  | 267 | { | 
|---|
|  | 268 | return new PthreadThreadGrp(*this,nthread); | 
|---|
|  | 269 | } | 
|---|
|  | 270 |  | 
|---|
|  | 271 | ///////////////////////////////////////////////////////////////////////////// | 
|---|
|  | 272 |  | 
|---|
|  | 273 | } | 
|---|
|  | 274 |  | 
|---|
|  | 275 | // Local Variables: | 
|---|
|  | 276 | // mode: c++ | 
|---|
|  | 277 | // c-file-style: "ETS" | 
|---|
|  | 278 | // End: | 
|---|