~ubuntu-branches/ubuntu/quantal/zeroc-ice/quantal

« back to all changes in this revision

Viewing changes to cpp/src/IceUtil/Thread.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Cleto Martin Angelina
  • Date: 2011-04-25 18:44:24 UTC
  • mfrom: (6.1.14 sid)
  • Revision ID: james.westby@ubuntu.com-20110425184424-sep9i9euu434vq4c
Tags: 3.4.1-7
* Bug fix: "libdb5.1-java.jar was renamed to db.jar", thanks to Ondřej
  Surý (Closes: #623555).
* Bug fix: "causes noise in php5", thanks to Jayen Ashar (Closes:
  #623533).

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
// **********************************************************************
2
2
//
3
 
// Copyright (c) 2003-2009 ZeroC, Inc. All rights reserved.
 
3
// Copyright (c) 2003-2010 ZeroC, Inc. All rights reserved.
4
4
//
5
5
// This copy of Ice is licensed to you under the terms described in the
6
6
// ICE_LICENSE file included in this distribution.
22
22
#include <climits>
23
23
#include <exception>
24
24
 
 
25
#ifndef _WIN32
 
26
    #include <sys/time.h>
 
27
    #include <sys/resource.h>
 
28
#endif
 
29
 
25
30
using namespace std;
26
31
 
27
32
#ifdef _WIN32
112
117
{
113
118
}
114
119
 
 
120
IceUtil::Thread::Thread(const string& name) :
 
121
    _name(name),
 
122
    _started(false),
 
123
    _running(false),
 
124
    _handle(0),
 
125
    _id(0)
 
126
{
 
127
}
 
128
 
115
129
IceUtil::Thread::~Thread()
116
130
{
117
131
}
151
165
    }
152
166
    catch(...)
153
167
    {
 
168
        if(!thread->name().empty())
 
169
        {
 
170
            cerr << thread->name() << " terminating" << endl;
 
171
        }
154
172
#if defined(_MSC_VER) && (_MSC_VER < 1300)
155
173
        terminate();
156
174
#else
168
186
IceUtil::ThreadControl
169
187
IceUtil::Thread::start(size_t stackSize)
170
188
{
 
189
    return start(stackSize, THREAD_PRIORITY_NORMAL);
 
190
}
 
191
 
 
192
IceUtil::ThreadControl
 
193
IceUtil::Thread::start(size_t stackSize, int priority)
 
194
{
171
195
    //
172
196
    // Keep this alive for the duration of start
173
197
    //
192
216
    __incRef();
193
217
    
194
218
    unsigned int id;
195
 
    
196
219
    _handle = 
197
 
       reinterpret_cast<HANDLE>(
198
 
          _beginthreadex(0, 
199
 
                         static_cast<unsigned int>(stackSize), 
200
 
                         startHook, this, 0, &id));
 
220
        reinterpret_cast<HANDLE>(
 
221
            _beginthreadex(0, 
 
222
                            static_cast<unsigned int>(stackSize), 
 
223
                            startHook, this, CREATE_SUSPENDED, &id));
201
224
    _id = id;
202
225
 
203
226
    if(_handle == 0)
205
228
        __decRef();
206
229
        throw ThreadSyscallException(__FILE__, __LINE__, GetLastError());
207
230
    }
 
231
    if(SetThreadPriority(_handle, priority) == 0)
 
232
    {
 
233
        throw ThreadSyscallException(__FILE__, __LINE__, GetLastError());
 
234
    }
 
235
    if(ResumeThread(_handle) == -1)
 
236
    {
 
237
        __decRef();
 
238
        throw ThreadSyscallException(__FILE__, __LINE__, GetLastError());
 
239
    }
208
240
 
209
241
    _started = true;
210
242
    _running = true;
211
 
                        
 
243
    
212
244
    return ThreadControl(_handle, _id);
213
245
}
214
246
 
255
287
    _running = false;
256
288
}
257
289
 
 
290
const string&
 
291
IceUtil::Thread::name() const
 
292
{
 
293
    return _name;
 
294
}
 
295
 
258
296
#else
259
297
 
260
298
IceUtil::ThreadControl::ThreadControl(pthread_t thread) :
315
353
IceUtil::ThreadControl::ID
316
354
IceUtil::ThreadControl::id() const
317
355
{
318
 
    return _thread;;
 
356
    return _thread;
319
357
}
320
358
 
321
359
void
340
378
{
341
379
}
342
380
 
 
381
IceUtil::Thread::Thread(const string& name) :
 
382
    _name(name),
 
383
    _started(false),
 
384
    _running(false)
 
385
{
 
386
}
 
387
 
343
388
IceUtil::Thread::~Thread()
344
389
{
345
390
}
369
414
    }
370
415
    catch(...)
371
416
    {
 
417
        if(!thread->name().empty())
 
418
        {
 
419
            cerr << thread->name() << " terminating" << endl;
 
420
        }
372
421
        std::terminate();
373
422
    }
374
423
 
378
427
}
379
428
}
380
429
 
 
430
 
381
431
IceUtil::ThreadControl
382
432
IceUtil::Thread::start(size_t stackSize)
383
433
{
 
434
    return start(stackSize, false, 0);
 
435
}
 
436
 
 
437
IceUtil::ThreadControl
 
438
IceUtil::Thread::start(size_t stackSize, int priority)
 
439
{
 
440
    return start(stackSize, true, priority);
 
441
}
 
442
IceUtil::ThreadControl
 
443
IceUtil::Thread::start(size_t stackSize, bool realtimeScheduling, int priority)
 
444
{
384
445
    //
385
446
    // Keep this alive for the duration of start
386
447
    //
404
465
    //
405
466
    __incRef();
406
467
 
 
468
    pthread_attr_t attr;
 
469
    int rc = pthread_attr_init(&attr);
 
470
    if(rc != 0)
 
471
    {
 
472
        __decRef();
 
473
        pthread_attr_destroy(&attr);
 
474
        throw ThreadSyscallException(__FILE__, __LINE__, rc);
 
475
    }
407
476
    if(stackSize > 0)
408
477
    {
409
 
        pthread_attr_t attr;
410
 
        int rc = pthread_attr_init(&attr);
411
 
        if(rc != 0)
412
 
        {
413
 
            __decRef();
414
 
            throw ThreadSyscallException(__FILE__, __LINE__, rc);
415
 
        }
416
478
        if(stackSize < PTHREAD_STACK_MIN)
417
479
        {
418
480
            stackSize = PTHREAD_STACK_MIN;
427
489
        if(rc != 0)
428
490
        {
429
491
            __decRef();
430
 
            throw ThreadSyscallException(__FILE__, __LINE__, rc);
431
 
        }
432
 
        rc = pthread_create(&_thread, &attr, startHook, this);
433
 
        if(rc != 0)
434
 
        {
435
 
            __decRef();
436
 
            throw ThreadSyscallException(__FILE__, __LINE__, rc);
437
 
        }
438
 
    }
439
 
    else
440
 
    {
441
 
        int rc = pthread_create(&_thread, 0, startHook, this);
442
 
        if(rc != 0)
443
 
        {
444
 
            __decRef();
445
 
            throw ThreadSyscallException(__FILE__, __LINE__, rc);
446
 
        }
 
492
            pthread_attr_destroy(&attr);
 
493
            throw ThreadSyscallException(__FILE__, __LINE__, rc);
 
494
        }
 
495
    }
 
496
 
 
497
    if(realtimeScheduling)
 
498
    {
 
499
        rc = pthread_attr_setschedpolicy(&attr, SCHED_RR);
 
500
        if(rc != 0)
 
501
        {
 
502
            __decRef();
 
503
            throw ThreadSyscallException(__FILE__, __LINE__, rc);
 
504
        }
 
505
        sched_param param;
 
506
        param.sched_priority = priority;
 
507
        rc = pthread_attr_setschedparam(&attr, &param);
 
508
        if(rc != 0)
 
509
        {
 
510
            __decRef();
 
511
            pthread_attr_destroy(&attr);
 
512
            throw ThreadSyscallException(__FILE__, __LINE__, rc);
 
513
        }
 
514
        pthread_attr_setinheritsched(&attr, PTHREAD_EXPLICIT_SCHED);
 
515
    }
 
516
    rc = pthread_create(&_thread, &attr, startHook, this);
 
517
    pthread_attr_destroy(&attr);
 
518
    if(rc != 0)
 
519
    {
 
520
        __decRef();
 
521
        throw ThreadSyscallException(__FILE__, __LINE__, rc);
447
522
    }
448
523
 
449
524
    _started = true;
450
525
    _running = true;
451
 
 
452
526
    return ThreadControl(_thread);
453
527
}
454
528
 
495
569
    _running = false;
496
570
}
497
571
 
 
572
const string&
 
573
IceUtil::Thread::name() const
 
574
{
 
575
    return _name;
 
576
}
 
577
 
498
578
#endif