1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027
|
/****************************************************************************
** $Id: qt/src/kernel/qthread_p.h 2.3.1 edited 2001-06-11 $
**
** QThread class for Unix
**
** Created : 20001309
**
** Copyright (C) 1992-2000 Trolltech AS. All rights reserved.
**
** This file is part of the kernel module of the Qt GUI Toolkit.
**
** This file may be distributed under the terms of the Q Public License
** as defined by Trolltech AS of Norway and appearing in the file
** LICENSE.QPL included in the packaging of this file.
**
** This file may be distributed and/or modified under the terms of the
** GNU General Public License version 2 as published by the Free Software
** Foundation and appearing in the file LICENSE.GPL included in the
** packaging of this file.
**
** Licensees holding valid Qt Enterprise Edition or Qt Professional Edition
** licenses may use this file in accordance with the Qt Commercial License
** Agreement provided with the Software.
**
** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
**
** See http://www.trolltech.com/pricing.html or email sales@trolltech.com for
** information about Qt Commercial License Agreements.
** See http://www.trolltech.com/qpl/ for QPL licensing information.
** See http://www.trolltech.com/gpl/ for GPL licensing information.
**
** Contact info@trolltech.com if any conditions of this licensing are
** not clear to you.
**
**********************************************************************/
#ifndef QTHREAD_P_H
#define QTHREAD_P_H
#define gettimeofday __hide_gettimeofday
#include <qglobal.h>
#include <errno.h>
#include <unistd.h>
#ifdef QWS
#include <qptrdict.h>
#else
#include <qintdict.h>
#endif
#include <time.h>
#include <sys/time.h>
#undef gettimeofday
extern "C" int gettimeofday( struct timeval *, struct timezone * );
// Thread definitions for UNIX platforms
#if defined(_OS_LINUX_)
# define _XOPEN_SOURCE 500
// Linux: couldn't we query macros from <unistd.h> instead of library versions?
# if (__GLIBC__ == 2) && (__GLIBC_MINOR__ == 0)
// Linux with glibc 2.0.x - POSIX 1003.4a thread implementation
# define Q_HAS_RECURSIVE_MUTEX
# define Q_USE_PTHREAD_MUTEX_SETKIND
# define Q_NORMAL_MUTEX_TYPE PTHREAD_MUTEX_ERRORCHECK_NP
# define Q_RECURSIVE_MUTEX_TYPE PTHREAD_MUTEX_RECURSIVE_NP
# else
// Linux with glibc 2.1.x - POSIX 1003.1c thread implementation
# define Q_HAS_RECURSIVE_MUTEX
# undef Q_USE_PTHREAD_MUTEX_SETKIND
# define Q_NORMAL_MUTEX_TYPE PTHREAD_MUTEX_DEFAULT
# define Q_RECURSIVE_MUTEX_TYPE PTHREAD_MUTEX_RECURSIVE
# endif
#elif defined(_OS_OSF_)
// Digital UNIX - 4.0 and later has a POSIX 1003.1c implementation
// - should we assume > 4.0?
# define Q_HAS_RECURSIVE_MUTEX
# undef Q_USE_PTHREAD_MUTEX_SETKIND
# define Q_NORMAL_MUTEX_TYPE PTHREAD_MUTEX_ERRORCHECK
# define Q_RECURSIVE_MUTEX_TYPE PTHREAD_MUTEX_RECURSIVE
#elif defined(_OS_AIX_)
// AIX 4.3.x
# define Q_HAS_RECURSIVE_MUTEX
# undef Q_USE_PTHREAD_MUTEX_SETKIND
# define Q_NORMAL_MUTEX_TYPE PTHREAD_MUTEX_ERRORCHECK
# define Q_RECURSIVE_MUTEX_TYPE PTHREAD_MUTEX_RECURSIVE
#elif defined(_OS_HPUX_)
// We only support HP/UX 11.x
# define Q_HAS_RECURSIVE_MUTEX
# undef Q_USE_PTHREAD_MUTEX_SETKIND
# define Q_NORMAL_MUTEX_TYPE PTHREAD_MUTEX_ERRORCHECK
# define Q_RECURSIVE_MUTEX_TYPE PTHREAD_MUTEX_RECURSIVE
#elif defined (_OS_FREEBSD_) || defined(_OS_OPENBSD_)
// FreeBSD and OpenBSD use the same user-space thread implementation
# define Q_HAS_RECURSIVE_MUTEX
# undef Q_USE_PTHREAD_MUTEX_SETKIND
# define Q_NORMAL_MUTEX_TYPE PTHREAD_MUTEX_ERRORCHECK
# define Q_RECURSIVE_MUTEX_TYPE PTHREAD_MUTEX_RECURSIVE
#elif defined(_OS_SOLARIS_)
// Solaris 2.7 and later - we use the native Solaris threads implementation
# undef Q_HAS_RECURSIVE_MUTEX
# undef Q_USE_PTHREAD_MUTEX_SETKIND
# undef Q_NORMAL_MUTEX_TYPE
# undef Q_RECURSIVE_MUTEX_TYPE
#elif defined(_OS_IRIX_)
# define Q_HAS_RECURSIVE_MUTEX
# undef Q_USE_PTHREAD_MUTEX_SETKIND
# define Q_NORMAL_MUTEX_TYPE PTHREAD_MUTEX_ERRORCHECK
# define Q_RECURSIVE_MUTEX_TYPE PTHREAD_MUTEX_RECURSIVE
#else
// Fall through for systems we don't know about
// # warning "Assuming non-POSIX 1003.1c thread implementation. Talk to qt-bugs@trolltech.com."
# undef Q_HAS_RECURSIVE_MUTEX
# undef Q_USE_PTHREAD_MUTEX_SETKIND
# undef Q_NORMAL_MUTEX_TYPE
# undef Q_RECURSIVE_MUTEX_TYPE
#endif
static QMutex *dictMutex = 0;
#ifdef QWS
static QPtrDict<QThread> *thrDict = 0;
#else
static QIntDict<QThread> *thrDict = 0;
#endif
extern "C" { static void * start_thread(void * t); }
#if defined(_OS_SOLARIS_)
#include <thread.h>
// Function usleep() is in C library but not in header files on Solaris 2.5.1.
// Not really a surprise, usleep() is specified by XPG4v2 and XPG4v2 is only
// supported by Solaris 2.6 and better.
// So we are trying to detect Solaris 2.5.1 using macro _XOPEN_UNIX which is
// defined by <unistd.h> when XPG4v2 is supported.
#if !defined(_XOPEN_UNIX)
typedef unsigned int useconds_t;
extern "C" int usleep(useconds_t);
#endif
class QMutexPrivate {
public:
mutex_t mutex;
QMutexPrivate(bool recursive = FALSE)
{
#ifdef CHECK_RANGE
int ret =
#endif
mutex_init( &mutex, NULL, NULL );
#ifdef CHECK_RANGE
if( ret )
qWarning( "QMutex::QMutex: init failure: %s", strerror( ret ) );
#endif
}
virtual ~QMutexPrivate()
{
#ifdef CHECK_RANGE
int ret =
#endif
mutex_destroy( &mutex );
#ifdef CHECK_RANGE
if ( ret )
qWarning( "QMutex::~QMutex: destroy failure: %s", strerror( ret ) );
#endif
}
virtual void lock()
{
#ifdef CHECK_RANGE
int ret =
#endif
mutex_lock(&mutex);
#ifdef CHECK_RANGE
if (ret)
qWarning("QMutex::lock: mutex lock failure: %s", strerror(ret));
#endif
}
virtual void unlock()
{
#ifdef CHECK_RANGE
int ret =
#endif
mutex_unlock(&mutex);
#ifdef CHECK_RANGE
if (ret)
qWarning("QMutex::unlock: mutex unlock failure: %s", strerror(ret));
#endif
}
virtual bool locked()
{
int ret = mutex_trylock(&mutex);
if (ret == EBUSY) {
return TRUE;
} else if (ret) {
#ifdef CHECK_RANGE
qWarning("QMutex::locked: try lock failed: %s", strerror(ret));
#endif
} else {
mutex_unlock(&mutex);
}
return FALSE;
}
#if defined(CHECK_RANGE) || !defined(Q_HAS_RECURSIVE_MUTEX)
virtual int type() const { return Q_MUTEX_NORMAL; }
#endif
};
class QRMutexPrivate : public QMutexPrivate
{
public:
int count;
HANDLE owner;
mutex_t mutex2;
QRMutexPrivate()
: QMutexPrivate(TRUE)
{
#ifdef CHECK_RANGE
int ret =
#endif
mutex_init( &mutex2, NULL, NULL );
#ifdef CHECK_RANGE
if( ret )
qWarning( "QMutex::QMutex: init failure: %s", strerror( ret ) );
#endif
count = 0;
}
~QRMutexPrivate()
{
#ifdef CHECK_RANGE
int ret =
#endif
mutex_destroy(&mutex2);
#ifdef CHECK_RANGE
if( ret )
qWarning( "QMutex::QMutex: destroy failure: %s", strerror( ret ) );
#endif
}
void lock()
{
mutex_lock(&mutex2);
if(count > 0 && owner == QThread::currentThread()) {
count++;
} else {
mutex_unlock(&mutex2);
QMutexPrivate::lock();
mutex_lock(&mutex2);
count = 1;
owner = QThread::currentThread();
}
mutex_unlock(&mutex2);
}
void unlock()
{
mutex_lock(&mutex2);
if (owner != QThread::currentThread()) {
#ifdef CHECK_RANGE
qWarning("QMutex::unlock: unlock from different thread than locker");
qWarning(" was locked by %d, unlock attempt from %d",
owner, QThread::currentThread());
mutex_unlock(&mutex2);
#endif
return;
}
// do nothing if the count is already 0... to reflect the behaviour described
// in the docs
if (count && (--count) < 1) {
QMutexPrivate::unlock();
count=0;
}
mutex_unlock(&mutex2);
}
bool locked()
{
mutex_lock(&mutex2);
bool ret = QMutexPrivate::locked();
mutex_unlock(&mutex2);
return ret;
}
#if defined(CHECK_RANGE) || !defined(Q_HAS_RECURSIVE_MUTEX)
int type() const { return Q_MUTEX_RECURSIVE; }
#endif
};
class QThreadPrivate {
public:
thread_t thread_id;
QWaitCondition thread_done;
bool finished, running;
QThreadPrivate()
: thread_id(0), finished(FALSE), running(FALSE)
{
if (! dictMutex)
dictMutex = new QMutex;
if (! thrDict)
#ifdef QWS
thrDict = new QPtrDict<QThread>;
#else
thrDict = new QIntDict<QThread>;
#endif
}
~QThreadPrivate()
{
dictMutex->lock();
if (thread_id)
thrDict->remove((HANDLE) thread_id);
dictMutex->unlock();
thread_id = 0;
}
void init(QThread *that)
{
that->d->running = TRUE;
that->d->finished = FALSE;
int ret = thr_create( NULL, NULL, start_thread, that, THR_DETACHED,
&thread_id );
#ifdef CHECK_RANGE
if (ret)
qWarning("QThread::start: thread creation error: %s", strerror(ret));
#endif
}
static void internalRun(QThread *that)
{
dictMutex->lock();
thrDict->insert(QThread::currentThread(), that);
dictMutex->unlock();
that->d->running = TRUE;
that->d->finished = FALSE;
that->run();
dictMutex->lock();
QThread *there = thrDict->find(QThread::currentThread());
if (there) {
there->d->running = FALSE;
there->d->finished = TRUE;
there->d->thread_done.wakeAll();
}
thrDict->remove(QThread::currentThread());
dictMutex->unlock();
}
};
class QWaitConditionPrivate {
public:
cond_t cond;
QMutex mutex;
QWaitConditionPrivate()
{
#ifdef CHECK_RANGE
int ret =
#endif
cond_init(&cond, NULL, NULL );
#ifdef CHECK_RANGE
if( ret )
qWarning( "QWaitCondition::QWaitCondition: event init failure %s", strerror( ret ) );
#endif
}
~QWaitConditionPrivate()
{
int ret = cond_destroy(&cond);
if( ret ) {
#ifdef CHECK_RANGE
qWarning( "QWaitCondition::~QWaitCondition: event destroy failure %s", strerror( ret ) );
#endif
// seems we have threads waiting on us, lets wake them up
cond_broadcast(&cond);
}
}
void wakeOne()
{
mutex.lock();
#ifdef CHECK_RANGE
int ret =
#endif
cond_signal(&(cond));
#ifdef CHECK_RANGE
if ( ret ) qWarning("QWaitCondition::wakeOne: wake error: %s",strerror(ret));
#endif
mutex.unlock();
}
void wakeAll()
{
mutex.lock();
#ifdef CHECK_RANGE
int ret =
#endif
cond_broadcast(& (cond) );
#ifdef CHECK_RANGE
if( ret ) qWarning("QWaitCondition::wakeAll: wake error: %s",strerror(ret));
#endif
mutex.unlock();
}
bool wait(unsigned long time)
{
mutex.lock();
int ret;
if (time != ULONG_MAX) {
struct timeval tv;
gettimeofday(&tv, 0);
timespec ti;
ti.tv_sec = tv.tv_sec + (time / 1000);
ti.tv_nsec = (tv.tv_usec * 1000) + (time % 1000) * 1000000;
ret = cond_timedwait(&(cond), &(mutex.d->mutex), &ti);
} else {
ret = cond_wait ( &(cond), &(mutex.d->mutex) );
}
mutex.unlock();
#ifdef CHECK_RANGE
if( ret ) qWarning("QWaitCondition::wait: wait error:%s",strerror(ret));
#endif
return (ret == 0);
}
bool wait(QMutex *mtx, unsigned long time)
{
if (! mtx) return FALSE;
#ifdef CHECK_RANGE
if (mtx->d->type() == Q_MUTEX_RECURSIVE)
qWarning("QWaitCondition::wait: warning - using recursive mutexes with\n"
" conditions is undefined!");
#endif
#ifndef Q_HAS_RECURSIVE_MUTEX
int c = 0;
HANDLE id = 0;
if (mtx->d->type() == Q_MUTEX_RECURSIVE) {
QRMutexPrivate *rmp = (QRMutexPrivate *) mtx->d;
mutex_lock(&(rmp->mutex2));
if (! rmp->count) {
# ifdef CHECK_RANGE
qWarning("QWaitCondition::unlockAndWait: recursive mutex not locked!");
# endif
return FALSE;
}
c = rmp->count;
id = rmp->owner;
rmp->count = 0;
rmp->owner = 0;
mutex_unlock(&(rmp->mutex2));
}
#endif
int ret;
if (time != ULONG_MAX) {
struct timeval tv;
gettimeofday(&tv, 0);
timespec ti;
ti.tv_sec = tv.tv_sec + (time / 1000);
ti.tv_nsec = (tv.tv_usec * 1000) + (time % 1000) * 1000000;
ret = cond_timedwait(&(cond), &(mtx->d->mutex), &ti);
} else {
ret = cond_wait ( &(cond), &(mtx->d->mutex) );
}
#ifndef Q_HAS_RECURSIVE_MUTEX
if (mtx->d->type() == Q_MUTEX_RECURSIVE) {
QRMutexPrivate *rmp = (QRMutexPrivate *) mtx->d;
mutex_lock(&(rmp->mutex2));
rmp->count = c;
rmp->owner = id;
mutex_unlock(&(rmp->mutex2));
}
#endif
#ifdef CHECK_RANGE
if ( ret ) qWarning("QWaitCondition::wait: wait error:%s",strerror(ret));
#endif
return (ret == 0);
}
};
#else // ! defined(_OS_SOLARIS_)
#include <pthread.h>
class QMutexPrivate {
public:
#if defined (_OS_SOLARIS_)
mutex_t mutex;
#else
pthread_mutex_t mutex;
#endif
QMutexPrivate(bool recursive = FALSE)
{
pthread_mutexattr_t attr;
pthread_mutexattr_init(&attr);
#if defined(Q_HAS_RECURSIVE_MUTEX)
if (recursive) {
# if defined(Q_RECURSIVE_MUTEX_TYPE)
# if defined(Q_USE_PTHREAD_MUTEX_SETKIND)
pthread_mutexattr_setkind_np(&attr, Q_RECURSIVE_MUTEX_TYPE);
# else
pthread_mutexattr_settype(&attr, Q_RECURSIVE_MUTEX_TYPE);
# endif
# endif
} else {
#endif
#if defined(Q_NORMAL_MUTEX_TYPE)
# if defined(Q_USE_PTHREAD_MUTEX_SETKIND)
pthread_mutexattr_setkind_np(&attr, Q_NORMAL_MUTEX_TYPE);
# else
pthread_mutexattr_settype(&attr, Q_NORMAL_MUTEX_TYPE);
# endif
#endif
#if defined(Q_HAS_RECURSIVE_MUTEX)
}
#endif
#ifdef CHECK_RANGE
int ret =
#endif
pthread_mutex_init( &mutex, &attr );
pthread_mutexattr_destroy(&attr);
#ifdef CHECK_RANGE
if( ret )
qWarning( "QMutex::QMutex: init failure: %s", strerror( ret ) );
#endif
}
virtual ~QMutexPrivate()
{
#ifdef CHECK_RANGE
int ret =
#endif
pthread_mutex_destroy( &mutex );
#ifdef CHECK_RANGE
if ( ret )
qWarning( "QMutex::~QMutex: destroy failure: %s", strerror( ret ) );
#endif
}
virtual void lock()
{
#ifdef CHECK_RANGE
int ret =
#endif
pthread_mutex_lock(&mutex);
#ifdef CHECK_RANGE
if (ret)
qWarning("QMutex::lock: mutex lock failure: %s", strerror(ret));
#endif
}
virtual void unlock()
{
#ifdef CHECK_RANGE
int ret =
#endif
pthread_mutex_unlock(&mutex);
#ifdef CHECK_RANGE
if (ret)
qWarning("QMutex::unlock: mutex unlock failure: %s", strerror(ret));
#endif
}
virtual bool locked()
{
int ret = pthread_mutex_trylock(&mutex);
if (ret == EBUSY) {
return TRUE;
} else if (ret) {
#ifdef CHECK_RANGE
qWarning("QMutex::locked: try lock failed: %s", strerror(ret));
#endif
} else {
pthread_mutex_unlock(&mutex);
}
return FALSE;
}
#if defined(CHECK_RANGE) || !defined(Q_HAS_RECURSIVE_MUTEX)
virtual int type() const { return Q_MUTEX_NORMAL; }
#endif
};
class QRMutexPrivate : public QMutexPrivate
{
public:
#ifndef Q_HAS_RECURSIVE_MUTEX
int count;
HANDLE owner;
pthread_mutex_t mutex2;
~QRMutexPrivate()
{
# ifdef CHECK_RANGE
int ret =
# endif
pthread_mutex_destroy(&mutex2);
# ifdef CHECK_RANGE
if( ret )
qWarning( "QMutex::QMutex: destroy failure: %s", strerror( ret ) );
# endif
}
void lock()
{
pthread_mutex_lock(&mutex2);
if(count > 0 && owner == QThread::currentThread()) {
count++;
} else {
pthread_mutex_unlock(&mutex2);
QMutexPrivate::lock();
pthread_mutex_lock(&mutex2);
count = 1;
owner = QThread::currentThread();
}
pthread_mutex_unlock(&mutex2);
}
void unlock()
{
pthread_mutex_lock(&mutex2);
if (owner != QThread::currentThread()) {
#ifdef CHECK_RANGE
qWarning("QMutex::unlock: unlock from different thread than locker");
qWarning(" was locked by %d, unlock attempt from %d",
owner, QThread::currentThread());
pthread_mutex_unlock(&mutex2);
#endif
return;
}
// do nothing if the count is already 0... to reflect the behaviour described
// in the docs
if (count && (--count) < 1) {
QMutexPrivate::unlock();
count=0;
}
pthread_mutex_unlock(&mutex2);
}
bool locked()
{
pthread_mutex_lock(&mutex2);
bool ret = QMutexPrivate::locked();
pthread_mutex_unlock(&mutex2);
return ret;
}
#endif
QRMutexPrivate()
: QMutexPrivate(TRUE)
{
#ifndef Q_HAS_RECURSIVE_MUTEX
pthread_mutexattr_t attr;
pthread_mutexattr_init(&attr);
# ifdef CHECK_RANGE
int ret =
# endif
pthread_mutex_init( &mutex2, &attr );
pthread_mutexattr_destroy(&attr);
# ifdef CHECK_RANGE
if( ret )
qWarning( "QMutex::QMutex: init failure: %s", strerror( ret ) );
# endif
count = 0;
#endif
}
#if defined(CHECK_RANGE) || !defined(Q_HAS_RECURSIVE_MUTEX)
int type() const { return Q_MUTEX_RECURSIVE; }
#endif
};
class QThreadPrivate {
public:
pthread_t thread_id;
QWaitCondition thread_done; // Used for QThread::wait()
bool finished, running;
QThreadPrivate()
: thread_id(0), finished(FALSE), running(FALSE)
{
if (! dictMutex)
dictMutex = new QMutex;
if (! thrDict)
#ifdef QWS
thrDict = new QPtrDict<QThread>;
#else
thrDict = new QIntDict<QThread>;
#endif
}
~QThreadPrivate()
{
dictMutex->lock();
if (thread_id)
thrDict->remove((HANDLE) thread_id);
dictMutex->unlock();
thread_id = 0;
}
void init(QThread *that)
{
that->d->running = TRUE;
that->d->finished = FALSE;
pthread_attr_t attr;
pthread_attr_init(&attr);
pthread_attr_setinheritsched(&attr, PTHREAD_INHERIT_SCHED);
pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
int ret = pthread_create( &thread_id, &attr, start_thread, (void *) that );
pthread_attr_destroy(&attr);
if ( ret ) {
#ifdef CHECK_RANGE
qWarning("QThread::start: thread creation error: %s", strerror(ret));
#endif
return;
}
}
static void internalRun(QThread *that)
{
dictMutex->lock();
thrDict->insert(QThread::currentThread(), that);
dictMutex->unlock();
that->run();
dictMutex->lock();
QThread *there = thrDict->find(QThread::currentThread());
if (there) {
there->d->running = FALSE;
there->d->finished = TRUE;
there->d->thread_done.wakeAll();
}
thrDict->remove(QThread::currentThread());
dictMutex->unlock();
}
};
class QWaitConditionPrivate {
public:
pthread_cond_t cond;
QMutex mutex;
QWaitConditionPrivate()
{
pthread_condattr_t cattr;
pthread_condattr_init(&cattr);
#ifdef CHECK_RANGE
int ret =
#endif
pthread_cond_init(&cond, &cattr);
pthread_condattr_destroy(&cattr);
#ifdef CHECK_RANGE
if( ret )
qWarning( "QWaitCondition::QWaitCondition: event init failure %s", strerror( ret ) );
#endif
}
~QWaitConditionPrivate()
{
int ret = pthread_cond_destroy(&cond);
if( ret ) {
#ifdef CHECK_RANGE
qWarning( "QWaitCondition::~QWaitCondition: event destroy failure %s", strerror( ret ) );
#endif
// seems we have threads waiting on us, lets wake them up
pthread_cond_broadcast(&cond);
}
}
void wakeOne()
{
mutex.lock();
#ifdef CHECK_RANGE
int ret =
#endif
pthread_cond_signal( &(cond) );
#ifdef CHECK_RANGE
if ( ret ) qWarning("QWaitCondition::wakeOne: wake error: %s",strerror(ret));
#endif
mutex.unlock();
}
void wakeAll()
{
mutex.lock();
#ifdef CHECK_RANGE
int ret =
#endif
pthread_cond_broadcast(&(cond));
#ifdef CHECK_RANGE
if( ret ) qWarning("QWaitCondition::wakeAll: wake error: %s",strerror(ret));
#endif
mutex.unlock();
}
bool wait(unsigned long time)
{
mutex.lock();
int ret;
if (time != ULONG_MAX) {
struct timeval tv;
gettimeofday(&tv, 0);
timespec ti;
ti.tv_sec = tv.tv_sec + (time / 1000);
ti.tv_nsec = (tv.tv_usec * 1000) + (time % 1000) * 1000000;
ret = pthread_cond_timedwait(&(cond), &(mutex.d->mutex), &ti);
} else {
ret = pthread_cond_wait ( &(cond), &(mutex.d->mutex) );
}
mutex.unlock();
#ifdef CHECK_RANGE
if( ret ) qWarning("QWaitCondition::wait: wait error:%s",strerror(ret));
#endif
return (ret == 0);
}
bool wait(QMutex *mtx, unsigned long time)
{
if (! mtx) return FALSE;
#ifdef CHECK_RANGE
if (mtx->d->type() == Q_MUTEX_RECURSIVE)
qWarning("QWaitCondition::unlockAndWait: warning - using recursive mutexes"
" with\n conditions is undefined!");
#endif
#ifndef Q_HAS_RECURSIVE_MUTEX
int c = 0;
HANDLE id = 0;
if (mtx->d->type() == Q_MUTEX_RECURSIVE) {
QRMutexPrivate *rmp = (QRMutexPrivate *) mtx->d;
pthread_mutex_lock(&(rmp->mutex2));
if (! rmp->count) {
# ifdef CHECK_RANGE
qWarning("QWaitCondition::unlockAndWait: recursive mutex not locked!");
# endif
return FALSE;
}
c = rmp->count;
id = rmp->owner;
rmp->count = 0;
rmp->owner = 0;
pthread_mutex_unlock(&(rmp->mutex2));
}
#endif
int ret;
if (time != ULONG_MAX) {
struct timeval tv;
gettimeofday(&tv, 0);
timespec ti;
ti.tv_sec = tv.tv_sec + (time / 1000);
ti.tv_nsec = (tv.tv_usec * 1000) + (time % 1000) * 1000000;
ret = pthread_cond_timedwait(&(cond), &(mtx->d->mutex), &ti);
} else {
ret = pthread_cond_wait ( &(cond), &(mtx->d->mutex) );
}
#ifndef Q_HAS_RECURSIVE_MUTEX
if (mtx->d->type() == Q_MUTEX_RECURSIVE) {
QRMutexPrivate *rmp = (QRMutexPrivate *) mtx->d;
pthread_mutex_lock(&(rmp->mutex2));
rmp->count = c;
rmp->owner = id;
pthread_mutex_unlock(&(rmp->mutex2));
}
#endif
#ifdef CHECK_RANGE
if ( ret ) qWarning("QWaitCondition::wait: wait error:%s",strerror(ret));
#endif
return (ret == 0);
}
};
#endif // defined(_OS_SOLARIS_)
extern "C" {
static void *start_thread(void *t)
{
QThreadPrivate::internalRun( (QThread *) t );
return 0;
}
}
#endif // QTHREAD_P_H
|