[go: up one dir, main page]

File: thread.cpp

package info (click to toggle)
libcommoncpp2 1.8.1-10
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 5,300 kB
  • sloc: cpp: 29,221; sh: 10,352; ansic: 1,134; makefile: 238; xml: 5
file content (1775 lines) | stat: -rw-r--r-- 39,378 bytes parent folder | download | duplicates (5)
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
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
// Copyright (C) 1999-2005 Open Source Telecom Corporation.
// Copyright (C) 2006-2010 David Sugar, Tycho Softworks.
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
//
// As a special exception, you may use this file as part of a free software
// library without restriction.  Specifically, if other files instantiate
// templates or use macros or inline functions from this file, or you compile
// this file and link it with other files to produce an executable, this
// file does not by itself cause the resulting executable to be covered by
// the GNU General Public License.  This exception does not however
// invalidate any other reasons why the executable file might be covered by
// the GNU General Public License.
//
// This exception applies only to the code released under the name GNU
// Common C++.  If you copy code from other releases into a copy of GNU
// Common C++, as the General Public License permits, the exception does
// not apply to the code that you add in this way.  To avoid misleading
// anyone as to the status of such modified files, you must delete
// this exception notice from them.
//
// If you write modifications of your own for GNU Common C++, it is your choice
// whether to permit this exception to apply to your modifications.
// If you do not wish that, delete this exception notice.
//

#include <cc++/config.h>
#include <cc++/export.h>
#include <cc++/thread.h>
#include <cc++/exception.h>
#include <cc++/process.h>
#ifdef  __BORLANDC__
#include <stdio.h>
#include <stdlib.h>
#else
#include <cstdio>
#include <cstdlib>
#endif
#include "private.h"

#ifdef CCXX_HAVE_NEW_INIT
#include <new>
#else
inline void* operator new(size_t s,void* p)
{ return p;}
#endif

#ifdef WIN32
#include <process.h>
#endif

#ifdef  CCXX_NAMESPACES
namespace ost {
#endif

#ifdef  _THR_UNIXWARE
#undef  _POSIX_THREAD_PRIORITY_SCHEDULING
#define sigwait(x, y) _thr_sigwait(x, y)
#endif

#ifdef  __linux__
#define CCXX_SIG_THREAD_ALARM
// NOTE: Comment this line to test Resume/Signal using one signal method
#define CCXX_SIG_THREAD_STOPCONT
#endif

#ifndef WIN32
extern "C"
{
    typedef void    *(*exec_t)(void *);
    typedef RETSIGTYPE (*signalexec_t)(int);

#ifndef CCXX_SIG_THREAD_STOPCONT
#ifndef _THR_SUNOS5
#ifndef HAVE_PTHREAD_SUSPEND
    static RETSIGTYPE ccxx_sigsuspend(int);
#endif
#endif
#endif

    static void ccxx_exec_handler(Thread *th);
    static void ccxx_thread_cleanup(void* arg);
    static void ccxx_thread_destructor(void* arg);
    static void ccxx_sig_handler(int signo);
}

#endif // ndef WIN32

#ifdef  CCXX_SIG_THREAD_CANCEL

extern "C" RETSIGTYPE _th_sigcancel(int sig)
{
    pthread_exit(NULL);
}

#endif

#ifdef WIN32
typedef unsigned (__stdcall *exec_t)(void *);
#ifndef CCXX_NO_DLL
# if defined(_MSC_VER) && !defined(_DLL)
#  error This project cannot be compiled as a static library. Some implementation stuff require DLL
# endif
#endif // CCXX_NO_DLL
#endif // WIN32

/*
 * Start Suspend/Resume stuff
 */

// method to suspend are
// - system suspend/resume recursive
// - system suspend/resume not recursive
// - one signal only, not recursive
#ifndef WIN32
#define CCXX_SUSPEND_MODE_RECURSIVE 1
#define CCXX_SUSPEND_MODE_NOT_RECURSIVE 2
#define CCXX_SUSPEND_MODE_ONE_SIGNAL 3
#define CCXX_SUSPEND_MODE_MACH 4

#if defined(_THR_MACH) && !defined(MACOSX)
#define CCXX_SUSPEND_MODE CCXX_SUSPEND_MODE_MACH
#elif defined(HAVE_PTHREAD_SUSPEND)
#define CCXX_SUSPEND_MODE CCXX_SUSPEND_MODE_NOT_RECURSIVE
static inline void ccxx_resume(cctid_t tid) { pthread_continue(tid); }
static inline void ccxx_suspend(cctid_t tid) { pthread_suspend(tid); }
#else
# if defined(_THR_SUNOS5) || defined(CCXX_SIG_THREAD_STOPCONT)
#  define CCXX_SUSPEND_MODE CCXX_SUSPEND_MODE_NOT_RECURSIVE
#  ifdef _THR_SUNOS5
    static inline void ccxx_resume(cctid_t tid) { thr_continue((thread_t)tid); }
    static inline void ccxx_suspend(cctid_t tid) { thr_suspend((thread_t)tid); }
#  else
#   define CCXX_SIG_THREAD_SUSPEND SIGSTOP
#   define CCXX_SIG_THREAD_RESUME  SIGCONT
    static inline void ccxx_resume(cctid_t tid) {
        pthread_kill(tid, CCXX_SIG_THREAD_RESUME);
}
    static inline void ccxx_suspend(cctid_t tid) {
        pthread_kill(tid, CCXX_SIG_THREAD_SUSPEND);
}
#  endif
# else
#  define CCXX_SUSPEND_MODE CCXX_SUSPEND_MODE_ONE_SIGNAL
#  ifndef SIGUSR3
#  ifdef  SIGWINCH
#  define SIGUSR3 SIGWINCH
#  else
#  define SIGUSR3 SIGINT
#  endif
#  endif
#  define CCXX_SIG_THREAD_SUSPEND SIGUSR3
#  define CCXX_SIG_THREAD_RESUME SIGUSR3
   static inline void ccxx_resume(cctid_t tid) {
       pthread_kill(tid, CCXX_SIG_THREAD_RESUME);
   }
   static inline void ccxx_suspend(cctid_t tid) {
       pthread_kill(tid, CCXX_SIG_THREAD_SUSPEND);
   }
# endif
#endif
#endif // ndef WIN32

Thread::Cancel Thread::enterCancel(void)
{
    Thread *th = getThread();

    if(!th)
        return cancelInitial;

    Cancel old = th->_cancel;
    if(old != cancelDisabled && old != cancelImmediate) {
        th->setCancel(cancelImmediate);
#ifdef  WIN32
        Thread::yield();
#else
        pthread_testcancel();
#endif
    }

    return old;
}

void Thread::exitCancel(Cancel old)
{
    Thread *th = getThread();

    if(!th)
        return;

    if(old != th->_cancel) {
#ifndef WIN32
        pthread_testcancel();
#endif
        th->setCancel(old);
    }
}

void Thread::suspend(void)
{
    if(!priv)
        return;

#ifdef WIN32
    if (!priv->_active || !priv->_suspendEnable) {
#ifdef  CCXX_EXCEPTIONS
        if (Thread::getException() != throwNothing)
            throw this;
#endif
        return;
    }
    SuspendThread(priv->_hThread);

#else

    if (!priv->_suspendEnable) return;
#if CCXX_SUSPEND_MODE == CCXX_SUSPEND_MODE_MACH
    thread_suspend(priv->_mach);
#endif
#if CCXX_SUSPEND_MODE == CCXX_SUSPEND_MODE_RECURSIVE
    ccxx_suspend(priv->_tid);
#endif
#if (CCXX_SUSPEND_MODE == CCXX_SUSPEND_MODE_NOT_RECURSIVE) \
  || (CCXX_SUSPEND_MODE == CCXX_SUSPEND_MODE_ONE_SIGNAL)
    if (++priv->_suspendcount != 1) return;
    ccxx_suspend(priv->_tid);
#endif

#endif // WIN32
}

#if defined(__FreeBSD__)
#define AUTOSTACK   0x10000
#endif

#if defined(MACOSX)
#define AUTOSTACK   0
#endif

#ifndef AUTOSTACK
#define AUTOSTACK   0x100000
#endif

size_t Thread::_autostack = AUTOSTACK;

void Thread::resume(void)
{
    if(!priv)
        return;

#ifdef WIN32
    if (!priv->_active || !priv->_suspendEnable) {
#ifdef  CCXX_EXCEPTIONS
        if (Thread::getException() != throwNothing)
            throw this;
#endif
        return;
    }
    ResumeThread(priv->_hThread);

#else
    if (!priv->_suspendEnable) return;
#if CCXX_SUSPEND_MODE == CCXX_SUSPEND_MODE_MACH
    thread_resume(priv->_mach);
#endif
#if CCXX_SUSPEND_MODE == CCXX_SUSPEND_MODE_RECURSIVE
    ccxx_resume(priv->_tid);
#endif
#if (CCXX_SUSPEND_MODE == CCXX_SUSPEND_MODE_NOT_RECURSIVE) \
  || (CCXX_SUSPEND_MODE == CCXX_SUSPEND_MODE_ONE_SIGNAL)
    int c;
    if ( (c = --priv->_suspendcount) > 0) return;
    if ( c < 0 ) {
        ++priv->_suspendcount;
        return;
    }
    ccxx_resume(priv->_tid);
#endif

#endif // WIN32
}

void Thread::join(void)
{
    bool detached = isDetached();
    joinSem.wait();
    if(detached) {
        joinSem.post();
        return;
    }

#ifdef  WIN32       // wait for real w32 thread to cleanup

    if(priv->_hThread) {
        WaitForSingleObject(priv->_hThread, INFINITE);
        ::CloseHandle(priv->_hThread);
        priv->_hThread = NULL;
    }

#else           // make sure we cleanup exiting thread
    if(priv->_jtid) {
        pthread_join(priv->_jtid, NULL);
    }

    priv->_jtid = 0;
#endif
    joinSem.post(); // enable next waiting thread after cleanup
}

#ifndef WIN32
#if CCXX_SUSPEND_MODE == CCXX_SUSPEND_MODE_ONE_SIGNAL
// NOTE: Do not modify _suspendcount here, one program can call
// Suspend 2 or more time but this function can be called only once
inline RETSIGTYPE ThreadImpl::ThreadSigSuspend(int)
{
    sigset_t sigs;

    sigemptyset(&sigs);
    sigaddset(&sigs, SIGUSR3);
    while ( (getThread()->priv->_suspendcount) > 0) {
#ifdef  HAVE_SIGWAIT2
        int signo;
        sigwait(&sigs, &signo);
#else
        sigwait(&sigs);
#endif
    }
}

static RETSIGTYPE ccxx_sigsuspend(int signo)
{
    return ThreadImpl::ThreadSigSuspend(signo);
}
#endif

void    Thread::setSuspend(Suspend mode)
{
    if(!priv)
        return;

    priv->_suspendEnable = (mode == suspendEnable);
#ifndef HAVE_PTHREAD_SUSPEND
#ifdef  CCXX_SIG_THREAD_SUSPEND
    sigset_t mask;

    sigemptyset(&mask);
    sigaddset(&mask, CCXX_SIG_THREAD_SUSPEND);

    switch(mode) {
    case suspendEnable:
        pthread_sigmask(SIG_UNBLOCK, &mask, NULL);
        return;
    case suspendDisable:
        pthread_sigmask(SIG_BLOCK, &mask, NULL);
    }
#endif
#endif
}

/*
 * End Suspend/Resume stuff
 */

static  sigset_t *blocked_signals(sigset_t *sig)
{
    sigemptyset(sig);
    sigaddset(sig, SIGINT);
    sigaddset(sig, SIGKILL);
    sigaddset(sig, SIGHUP);
    sigaddset(sig, SIGABRT);
    sigaddset(sig, SIGALRM);
    sigaddset(sig, SIGPIPE);
#if CCXX_SUSPEND_MODE == CCXX_SUSPEND_MODE_ONE_SIGNAL
    sigaddset(sig, SIGUSR3);
#endif
    return sig;
}
#endif // ndef WIN32

typedef enum ThreadType {
    threadTypeNormal=0,
    threadTypeMain,
    threadTypePosix,
    threadTypeDummy
} ThreadType;

class MainThread : public Thread
{
protected:
    void run(void) {return;};
#ifndef WIN32
    void onSignal(int signo) { std::exit(signo);};
#endif

public:
    MainThread() : Thread(true) {};
};

// mantain info on thread creation
class DummyThread : public Thread
{
protected:
    void run() {};
public:
    DummyThread() : Thread(false) { priv->_type = threadTypeDummy; }
#ifdef WIN32
    static void CheckDelete();
#endif
};

#ifdef WIN32
static ThreadKey _self;
#else
// NOTE: _self instantiation MUST appear before _mainthread !!
ThreadKey ThreadImpl::_self(ccxx_thread_destructor);
#endif

#ifdef WIN32
void DummyThread::CheckDelete()
{
    Thread *th = (Thread*)_self.getKey();
    if (!th) return;

    // delete if dummy thread
    if (th->priv->_type == threadTypeDummy)
        delete th;
}
#endif

static  MainThread _mainthread;
Thread  *Thread::_main = NULL;

// invalid pointer to thread used to test deleted thread
// point in the middle of mainthread...
#define DUMMY_INVALID_THREAD ((Thread*)(((char*)((Thread*)&_mainthread))+1))

#if !defined(WIN32)
#ifndef CCXX_SIG_THREAD_ALARM
PosixThread *PosixThread::_timer = NULL;
Mutex   PosixThread::_arm;
#endif
#endif

//void PosixThread::sigInstall(int);

Thread::Thread(bool isMain):
_cancel(cancelDefault), _start(NULL), priv(new ThreadImpl(threadTypeDummy))
{
#ifdef WIN32
    priv->_tid = GetCurrentThreadId();

    // FIXME: error handling
    HANDLE process = GetCurrentProcess();
    DuplicateHandle(process,GetCurrentThread(),process,&priv->_hThread,0,FALSE,DUPLICATE_SAME_ACCESS);
    _parent = this;
    priv->_cancellation = CreateEvent(NULL, TRUE, FALSE, NULL);

    if(isMain) {
        setName("main()");
        priv->_type   = threadTypeMain;
        _main = this;
    }
    else
        setName("-dummy-");
    _self.setKey(this);

#else
    priv->_suspendEnable = false;
    priv->_tid = pthread_self();
    _parent = NULL;
    struct sigaction act;

    // NOTE: for race condition (signal handler can use getThread)
    // you should initialize _main and _self before registering signals
    ThreadImpl::_self.setKey(this);
    if(isMain == true) {
        _main = this;
        priv->_type = threadTypeMain;
#if !defined(__CYGWIN32__) && !defined(__MINGW32__)
        PosixThread::sigInstall(SIGHUP);
        PosixThread::sigInstall(SIGALRM);
        PosixThread::sigInstall(SIGPIPE);
        PosixThread::sigInstall(SIGABRT);

        memset(&act, 0, sizeof(act));
        act.sa_handler = (signalexec_t)&ccxx_sig_handler;
        sigemptyset(&act.sa_mask);
# ifdef SA_RESTART
        act.sa_flags = SA_RESTART;
# else
        act.sa_flags = 0;
# endif
# ifdef SA_INTERRUPT
        act.sa_flags |= SA_INTERRUPT;
# endif
# ifdef SIGPOLL
        sigaction(SIGPOLL, &act, NULL);
# else
        sigaction(SIGIO, &act, NULL);
# endif

# if CCXX_SUSPEND_MODE == CCXX_SUSPEND_MODE_ONE_SIGNAL
        act.sa_handler = ccxx_sigsuspend;
        sigemptyset(&act.sa_mask);
#  ifdef    SA_RESTART
        act.sa_flags = SA_RESTART;
#  else
        act.sa_flags = 0;
#  endif
        sigaction(SIGUSR3, &act, NULL);
# endif

# ifdef CCXX_SIG_THREAD_CANCEL
        memset(&act, sizeof(act), 0);
        act.sa_flags = 0;
        act.sa_handler = _th_sigcancel;
        sigemptyset(&act.sa_mask);
        sigaddset(&act.sa_mask, SIGHUP);
        sigaddset(&act.sa_mask, SIGALRM);
        sigaddset(&act.sa_mask, SIGPIPE);

        sigaction(CCXX_SIG_THREAD_CANCEL, &act, NULL);
# endif
#endif
    }
#endif // WIN32
}

Thread::Thread(int pri, size_t stack):
_cancel(cancelDefault), _start(NULL), priv(new ThreadImpl(threadTypeNormal))
{
#ifdef WIN32
    if(!_main) {
        _self.setKey(NULL);
        _main = this;
        setName("main()");
    }
    else
#ifdef  WIN32
        _name[0] = 0;
#else
        snprintf(_name, sizeof(_name), "%d", getId());
#endif

    _parent = Thread::get();
    if(_parent)
        priv->_throw = _parent->priv->_throw;
    else
        _parent = this;

    priv->_cancellation = CreateEvent(NULL, TRUE, FALSE, NULL);
    if(!priv->_cancellation)
        THROW(this);

    if(stack <= _autostack)
        priv->_stack = 0;
    else
        priv->_stack = stack;

    if(pri > 2)
        pri = 2;
    if(pri < -2)
        pri = -2;

    if(Process::isRealtime() && pri < 0)
        pri = 0;

    switch(pri) {
    case 1:
        priv->_priority = THREAD_PRIORITY_ABOVE_NORMAL;
        break;
    case -1:
        priv->_priority = THREAD_PRIORITY_BELOW_NORMAL;
        break;
    case 2:
        priv->_priority = THREAD_PRIORITY_HIGHEST;
        break;
    case -2:
        priv->_priority = THREAD_PRIORITY_LOWEST;
        break;
    default:
        priv->_priority = THREAD_PRIORITY_NORMAL;
    }

#else
    pthread_attr_init(&priv->_attr);
    pthread_attr_setdetachstate(&priv->_attr, PTHREAD_CREATE_JOINABLE);

#ifdef  PTHREAD_STACK_MIN
    if(stack && stack <=  _autostack)
        pthread_attr_setstacksize(&priv->_attr, _autostack);
    else if(stack > _autostack) {
        if(stack < PTHREAD_STACK_MIN)
            stack = PTHREAD_STACK_MIN;
        else {  // align to nearest min boundry
            int salign = stack / PTHREAD_STACK_MIN;
            if(stack % PTHREAD_STACK_MIN)
                ++salign;
            stack = salign * PTHREAD_STACK_MIN;
        }
        if(stack && pthread_attr_setstacksize(&priv->_attr, stack)) {
#ifdef  CCXX_EXCEPTIONS
            switch(Thread::getException()) {
            case throwObject:
                throw(this);
                return;
#ifdef  COMMON_STD_EXCEPTION
            case throwException:
                throw(ThrException("no stack space"));
                return;
#endif
            default:
                return;
            }
#else
            return;
#endif
        }
    }
#endif

#ifndef __FreeBSD__
#ifdef  _POSIX_THREAD_PRIORITY_SCHEDULING
#ifdef HAVE_SCHED_GETSCHEDULER
#define __HAS_PRIORITY_SCHEDULING__
    if(pri < 0 && Process::isRealtime())
        pri = 0;

    if(pri) {
        struct sched_param sched;
        int policy;

        policy = sched_getscheduler(0);
        if(policy < 0) {
#ifdef  CCXX_EXCEPTIONS
            switch(Thread::getException()) {
            case throwObject:
                throw(this);
                return;
#ifdef  COMMON_STD_EXCEPTION
            case throwException:
                throw(ThrException("invalid scheduler"));
                return;
#endif
            default:
                return;
            }
#else
            return;
#endif
        }

        sched_getparam(0, &sched);

        pri = sched.sched_priority - pri;
        if(pri  > sched_get_priority_max(policy))
            pri = sched_get_priority_max(policy);

        if(pri < sched_get_priority_min(policy))
            pri = sched_get_priority_min(policy);

        sched.sched_priority = pri;
        pthread_attr_setschedpolicy(&priv->_attr, policy);
        pthread_attr_setschedparam(&priv->_attr, &sched);
    }
#endif // ifdef HAVE_SCHED_GETSCHEDULER
#endif // ifdef _POSIX_THREAD_PRIORITY_SCHEDULING
#endif // ifndef __FreeBSD__

#ifdef  __HAS_PRIORITY_SCHEDULING__
    if(!pri)
            pthread_attr_setinheritsched(&priv->_attr, PTHREAD_INHERIT_SCHED);
#else
    pthread_attr_setinheritsched(&priv->_attr, PTHREAD_INHERIT_SCHED);
#endif

    _parent = getThread();
    priv->_throw = _parent->priv->_throw;

    _cancel = cancelInitial;

#endif // WIN32
}

#ifndef WIN32
Thread::Thread(const Thread &th)
{
    priv = new ThreadImpl(threadTypeNormal);
    _parent = th._parent;
    priv->_attr = th.priv->_attr;
    _cancel = cancelInitial;
    _start = NULL;
    priv->_throw = th.priv->_throw;
    priv->_suspendEnable = false;

    setName(NULL);

//  sigset_t mask, newmask;
//  int rc;
//
//  pthread_sigmask(SIG_BLOCK, blocked_signals(&newmask), &mask);
//  rc = pthread_create(&_tid, &_attr, exec_t(&ccxx_exec_handler), this);
//  pthread_sigmask(SIG_SETMASK, &mask, NULL);
//  if(rc && Thread::getException() == throwObject)
//      throw(this);
//#ifdef    COMMON_STD_EXCEPTION
//  else if(rc && Thread::getException() == throwException)
//      throw(ThrException("cannot start copy"));
//#endif
}
#endif // ndef WIN32

Thread::~Thread()
{
    if(!priv)
        return;

#ifndef WIN32
    if(this == &_mainthread)
        return;
#endif

    if(priv->_type == threadTypeDummy) {
        delete priv;
        priv = NULL;
        return;
    }

    terminate();
}

void Thread::setName(const char *text)
{
    if(text)
        snprintf(_name, sizeof(_name), "%s", text);
    else
        snprintf(_name, sizeof(_name), "%ld", (long)getId());
}

void Thread::initial(void)
{}

void Thread::final(void)
{}

void *Thread::getExtended(void)
{
    return NULL;
}

void Thread::notify(Thread *)
{}

bool Thread::isThread(void) const
{
    if(!priv)
        return false;

#ifdef WIN32
    return ((priv->_tid == GetCurrentThreadId())) ? true : false;
#else
    return (priv->_tid == pthread_self()) ? true : false;
#endif
}

bool Thread::isDetached(void) const
{
    if(!priv)
        return false;

#ifdef  WIN32
    // win32 doesn't support detached threads directly
    return priv->_detached;
#else
    int state;

    pthread_attr_getdetachstate(&priv->_attr, &state);
    if(state == PTHREAD_CREATE_DETACHED)
        return true;
    return false;
#endif
}

cctid_t Thread::getId(void) const
{
    if(!priv)
        return (cctid_t)-1;

    return priv->_tid;
}

bool Thread::isRunning(void) const
{
    if(!priv)
        return false;
#ifdef WIN32
    return (priv->_tid != 0 && priv->_active) ? true : false;
#else
    return (priv->_tid != 0) ? true : false;
#endif // WIN32
}

int Thread::start(Semaphore *st)
{
    if(!priv)
        return -1;

#ifdef WIN32
    if(priv->_active)
        return -1;

    _start = st;

    priv->_hThread = (HANDLE)_beginthreadex(NULL, (unsigned)priv->_stack, (exec_t)&Execute, (void *)this, CREATE_SUSPENDED, (unsigned *)&priv->_tid);
    if(!priv->_hThread) {
        CloseHandle(priv->_cancellation);
        priv->_cancellation = NULL;
        return -1;
    }

    setCancel(cancelInitial);

    SetThreadPriority(priv->_hThread, priv->_priority);

    ResumeThread(priv->_hThread);

    priv->_active = true;

    return 0;

#else
    if(priv->_tid) {
        if(_start) {
            _start->post();
            return 0;
        }
        else
            return -1;
    }

    _start = st;
    return pthread_create(&priv->_tid, &priv->_attr, exec_t(&ccxx_exec_handler), this);
#endif
}

int Thread::detach(Semaphore *st)
{
    _parent = NULL;
#ifdef WIN32
    // win32 we emulate detach
    if(!priv)
        return -1;
    priv->_detached = true;
    if(!priv->_active)
        return Thread::start(st);
    else if(_start)
        _start->post();
    return 0;
#else
    if(!priv)
        return -1;

    if(priv->_tid) {
        pthread_detach(priv->_tid);
        if(_start) {
            _start->post();
                pthread_attr_setdetachstate(&priv->_attr, PTHREAD_CREATE_DETACHED);
            return 0;
        }
        return -1;
    }

    pthread_attr_setdetachstate(&priv->_attr, PTHREAD_CREATE_DETACHED);

    _start = st;
    if(!pthread_create(&priv->_tid, &priv->_attr, exec_t(&ccxx_exec_handler), this))
        return 0;
    return -1;
#endif
}

void Thread::terminate(void)
{
#ifdef WIN32
    HANDLE hThread;

    if(!priv)
        return;

    hThread = priv->_hThread;

    if (!priv->_tid || isThread()) {
        if( priv->_cancellation)
            ::CloseHandle(priv->_cancellation);
            if(hThread)
                    ::CloseHandle(hThread);
        delete priv;
        priv = NULL;
            return;
    }

    bool terminated = false;
    if(!priv->_active && hThread != NULL) {
        // NOTE: add a test in testthread for multiple
        // suspended Terminate
        ResumeThread(hThread);
        TerminateThread(hThread, 0);
        terminated = true;
    }
    else if(hThread != NULL) {
        switch(_cancel) {
        case cancelImmediate:
            TerminateThread(hThread, 0);
            terminated = true;
            break;
        default:
            SetEvent(priv->_cancellation);
        }
    }
    if(hThread != NULL) {
        WaitForSingleObject(hThread, INFINITE);
        CloseHandle(hThread);
        hThread = NULL;
    }

// what if parent already exited?

//  if(_parent)
//      _parent->notify(this);
    if(priv->_cancellation != NULL)
        CloseHandle(priv->_cancellation);
    priv->_cancellation = NULL;
    priv->_tid = 0;
    if(getThread() == this)
        _self.setKey(DUMMY_INVALID_THREAD);

    if (terminated)
        final();
#else
    if(!priv)
        return;

    cctid_t jtid = priv->_jtid, tid = priv->_tid;

    if(jtid && (pthread_self() != jtid)) {
        pthread_join(jtid, NULL);
        priv->_jtid = 0;
    }
    else if((pthread_self() != tid) && tid) {
        // in suspend thread cannot be cancelled or signaled
        // ??? rigth
        // ccxx_resume(priv->_tid);


        // assure thread has ran before we try to cancel...
        if(_start)
            _start->post();

        pthread_cancel(tid);
        if(!isDetached()) {
            pthread_join(tid,NULL);
            priv->_tid = 0;
        }
    }

    pthread_attr_destroy(&priv->_attr);
#endif
    delete priv;
    priv = NULL;
}

void Thread::sync(void)
{
#if defined(__MACH__) || defined(__GNU__)
    Thread::exit();
#else
    Thread::sleep(TIMEOUT_INF);
#endif
}

void Thread::exit(void)
{
    if (isThread()) {
        setCancel(cancelDisabled);
#ifdef WIN32
        close();
        ExitThread(0);
#else
        pthread_exit(NULL);
#endif // WIN32
    }

}

void Thread::close()
{
    bool detached = isDetached();

#if !defined(CCXX_SIG_THREAD_ALARM) && !defined(__CYGWIN32__) && !defined(__MINGW32__) && !defined(WIN32)
    if(this == PosixThread::_timer)
        PosixThread::_arm.leaveMutex();
#endif
    setCancel(cancelDisabled);
//  if(_parent)
//      _parent->notify(this);

    // final can call destructor (that call Terminate)
    final();

    // test if this class is self-exiting thread

#ifdef WIN32
    if (_self.getKey() == this)
#else
    if (ThreadImpl::_self.getKey() == this)
#endif
    {
        if(priv) {
#ifndef WIN32
            priv->_jtid = priv->_tid;
            priv->_tid = 0;
#else
            priv->_active = false;
#endif
        }
        joinSem.post();
    }

    // see if detached, and hence self deleting

    if(detached)
        delete this;
}

#ifndef WIN32
inline void ThreadImpl::ThreadCleanup(Thread* th)
{
    // close thread
    // (freddy77) Originally I thougth to throw an exception for deferred
    // for capture it and cleanup using C++ destructor
    // this doesn't work out!!
    // Throwing exception here (in cleanup) core dump app
    th->close();
}

extern "C" {
    static void ccxx_thread_cleanup(void* arg)
    {
        ThreadImpl::ThreadCleanup( (Thread*)arg );
    }
}

inline void ThreadImpl::ThreadExecHandler(Thread *th)
{
    ThreadImpl::_self.setKey(th);
    sigset_t mask;

    pthread_sigmask(SIG_BLOCK, blocked_signals(&mask), NULL);
    th->priv->_tid = pthread_self();
#if defined(HAVE_PTHREAD_MACH_THREAD_NP)
    th->priv->_mach = pthread_mach_thread_np(th->priv->_tid);
#elif defined(_THR_MACH)
    th->priv->_mach = mach_thread_self();
#endif
    th->setCancel(Thread::cancelInitial);
    // using SIGUSR3 do not enable suspend by default
    th->setSuspend(Thread::suspendEnable);
    th->yield();
    if(th->_start) {
        th->_start->wait();
        th->_start = NULL;
    }

    pthread_cleanup_push(ccxx_thread_cleanup,th);
    th->initial();
    if(th->getCancel() == Thread::cancelInitial)
        th->setCancel(Thread::cancelDefault);
    th->run();
    th->setCancel(Thread::cancelDisabled);

    pthread_cleanup_pop(0);
    if(th->isDetached())
        ThreadImpl::_self.setKey(NULL);
    th->close();
    pthread_exit(NULL);
}

// delete Thread class created for no CommonC++ thread
inline void ThreadImpl::ThreadDestructor(Thread* th)
{
    if (!th || th == DUMMY_INVALID_THREAD || !th->priv)
        return;
    if(!th->priv)
        return;
    if (th->priv->_type == threadTypeDummy)
        delete th;
}

extern "C" {
    static void ccxx_thread_destructor(void* arg)
    {
        ThreadImpl::ThreadDestructor( (Thread*)arg );
    }

    static void ccxx_exec_handler(Thread *th)
    {
        ThreadImpl::ThreadExecHandler(th);
    }
}
#endif // ndef WIN32

#ifdef  CCXX_SIG_THREAD_CANCEL

void    Thread::setCancel(Cancel mode)
{
    sigset_t    mask;

    sigemptyset(&mask);
    sigaddset(&mask, CCXX_SIG_THREAD_CANCEL);

    switch(mode) {
    case cancelImmediate:
        pthread_sigmask(SIG_UNBLOCK, &mask, NULL);
        break;
    case cancelInitial:
    case cancelDisabled:
    case cancelDeferred:
        pthread_sigmask(SIG_BLOCK, &mask, NULL);
        break;
    }
    _cancel = mode;
}
#else

void    Thread::setCancel(Cancel mode)
{
#ifdef WIN32
    switch(mode) {
    case cancelDeferred:
    case cancelImmediate:
        _cancel = mode;
        yield();
        break;
    case cancelDisabled:
    case cancelInitial:
        _cancel = mode;
    }

#else
    int old;

    switch(mode) {
    case cancelImmediate:
        pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, &old);
        pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, &old);
        break;
    case cancelDeferred:
        pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, &old);
        pthread_setcanceltype(PTHREAD_CANCEL_DEFERRED, &old);
        break;
    case cancelInitial:
    case cancelDisabled:
        pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &old);
        break;
    default:
        return;
    }
    _cancel = mode;
#endif // WIN32
}

#endif

void    Thread::yield(void)
{
#ifdef WIN32
    Thread::sleep(1);   // note: on Win32, Sleep(0) is "optimized" to NOP.
#else

#ifdef  CCXX_SIG_THREAD_CANCEL
    Thread* th = getThread();
    sigset_t cancel, old;

    sigemptyset(&cancel);
    sigaddset(&cancel, CCXX_SIG_THREAD_CANCEL);

    if(th && th->_cancel != cancelDisabled &&
       th->_cancel != cancelInitial)
        pthread_sigmask(SIG_UNBLOCK, &cancel, &old);
#else
    pthread_testcancel();
#endif
#ifdef  HAVE_PTHREAD_YIELD
    pthread_yield();
#endif

#ifdef  CCXX_SIG_THREAD_CANCEL
    if(th && th->_cancel != cancelDisabled && th->_cancel != cancelInitial)
        pthread_sigmask(SIG_SETMASK, &old, NULL);
#endif

#endif // WIN32
}

void Thread::setException(Thread::Throw mode)
{
    Thread *thread = getThread();
    thread->priv->_throw = mode;
}

Thread::Throw Thread::getException(void)
{
    Thread *thread = getThread();
    return thread->priv->_throw;
}

Cancellation::Cancellation(Thread::Cancel cancel)
{
    Thread *thread = getThread();
    if(!thread)
        return;

    prior = thread->getCancel();
    thread->setCancel(cancel);
}

Cancellation::~Cancellation()
{
    Thread *thread = getThread();
    if(!thread)
        return;

    thread->setCancel(prior);
}

bool Thread::testCancel(void)
{
#ifdef WIN32
    switch(_cancel) {
    case cancelInitial:
    case cancelDisabled:
        break;
    default:
        if(WaitForSingleObject(priv->_cancellation, 0) == WAIT_OBJECT_0) {
            if (_cancel == cancelManual)
                THROW(InterruptException());
            else
                exit();
        }
    }
    return false;

#else // WIN32

#ifdef  CCXX_SIG_THREAD_CANCEL
    sigset_t cancel, old;

    sigemptyset(&cancel);
    sigaddset(&cancel, CCXX_SIG_THREAD_CANCEL);

    if(_cancel != cancelDisabled && _cancel != cancelInitial)
        pthread_sigmask(SIG_UNBLOCK, &cancel, &old);
#else
    pthread_testcancel();
#endif

#ifdef  CCXX_SIG_THREAD_CANCEL
    if(_cancel != cancelDisabled)
        pthread_sigmask(SIG_SETMASK, &old, NULL);
#endif
    return false;

#endif // WIN32
}

#ifdef WIN32

bool Thread::isCancelled() const
{
    return waitThread(priv->_cancellation, 0) == WAIT_OBJECT_0;
}

DWORD Thread::waitThread(HANDLE hRef, timeout_t timeout)
{
    Thread *th = getThread();

    if(th)
        return th->waitHandle(hRef, timeout);
    else
        return WaitForSingleObject(hRef, timeout);
}

void Thread::sleep(timeout_t timeout)
{
    Thread *th = getThread();
    if(!th) {
        SleepEx(timeout, FALSE);
        return;
    }

    switch(th->_cancel) {
    case cancelInitial:
    case cancelDisabled:
        SleepEx(timeout, FALSE);
        break;
    default:
        if(WaitForSingleObject(th->priv->_cancellation, timeout) == WAIT_OBJECT_0) {
            if (th->_cancel == cancelManual)
                THROW(InterruptException());
            else
                th->exit();
        }
    }
}

DWORD Thread::waitHandle(HANDLE obj, timeout_t timeout)
{
    HANDLE objects[2];
    DWORD stat;

    objects[0] = priv->_cancellation;
    objects[1] = obj;

    // FIXME: what should happen if someone enable cancellation on wait??
    switch(_cancel) {
    case cancelInitial:
    case cancelDisabled:
        return WaitForSingleObject(obj, timeout);
    default:
        switch(stat = WaitForMultipleObjects(2, objects, false, timeout))   {
        case WAIT_OBJECT_0:
            if (_cancel == cancelManual)
                THROW(InterruptException());
            else
                exit();
        case WAIT_OBJECT_0 + 1:
            return WAIT_OBJECT_0;
        default:
            return stat;
        }
    }
}

// Entry point linked for default disable thread call, not suitable
// for threading library...
BOOL WINAPI DllMain(
    HANDLE  hDllHandle,
    DWORD   dwReason,
    LPVOID  lpreserved
    )
{
    switch(dwReason) {
    case DLL_THREAD_DETACH:
        DummyThread::CheckDelete();
        break;
    }
    return TRUE ;
}

#endif // WIN32

#ifndef WIN32
Thread *Thread::get(void)
{
    Thread *thread;

    // fix strange no-init on Solaris
        if(!Thread::_main) {
        new (&_mainthread) MainThread();
        return &_mainthread;
    }

    thread = (Thread *)ThreadImpl::_self.getKey();

    // class have been deleted, return NULL
    if (thread == DUMMY_INVALID_THREAD)
        return NULL;

    if(!thread) {
        // this Thread will be deleted by ccxx_thread_destruct
        thread = new DummyThread;
        ThreadImpl::_self.setKey(thread);
    }
    return thread;
}

#else // WIN32

Thread *Thread::get(void)
{
    Thread *th = (Thread *)_self.getKey();
    if (th == DUMMY_INVALID_THREAD) return NULL;
    // for no common c++ thread construct a dummy thread
    if (!th)
        th = new DummyThread();
    return th;
}

unsigned __stdcall Thread::Execute(Thread *th)
{
    _self.setKey(th);
    th->yield();

    if(th->_start) {
        th->_start->wait();
        th->_start = NULL;
    }

    try {
        th->priv->_tid = GetCurrentThreadId();
        if(!th->_name[0])
            snprintf(th->_name, sizeof(th->_name), "%d", GetCurrentThreadId());
        th->initial();
        if(th->getCancel() == cancelInitial)
            th->setCancel(cancelDefault);
        th->run();
    }
    // ignore cancellation exception
    catch(const InterruptException&)
    { ; }

    th->close();
    return 0;
}
#endif //WIN32

#if !defined(WIN32)
/*
 * PosixThread implementation
 */
inline void ThreadImpl::PosixThreadSigHandler(int signo)
{
    Thread  *t = getThread();
    PosixThread *th = NULL;

#ifdef  CCXX_EXCEPTIONS
    if (t) th = dynamic_cast<PosixThread*>(t);
#else
        if (t) th = (PosixThread*)(t);
#endif

    if (!th) return;

    switch(signo) {
    case SIGHUP:
        if(th)
            th->onHangup();
        break;
    case SIGABRT:
        if(th)
            th->onException();
        break;
    case SIGPIPE:
        if(th)
            th->onDisconnect();
        break;
    case SIGALRM:
#ifndef CCXX_SIG_THREAD_ALARM
        if(PosixThread::_timer) {
            PosixThread::_timer->_alarm = 0;
            PosixThread::_timer->onTimer();
        }
        else
#endif
            if(th)
            th->onTimer();
        break;
#ifdef  SIGPOLL
    case SIGPOLL:
#else
    case SIGIO:
#endif
        if(th)
            th->onPolling();
        break;
    default:
        if(th)
            th->onSignal(signo);
    }
}

extern "C" {
    static void ccxx_sig_handler(int signo)
    {
        ThreadImpl::PosixThreadSigHandler(signo);
    }
}

PosixThread::PosixThread(int pri, size_t stack):
Thread(pri,stack)
{
    SysTime::getTime(&_alarm);
}

void PosixThread::onTimer(void)
{}

void PosixThread::onHangup(void)
{}

void PosixThread::onException(void)
{}

void PosixThread::onDisconnect(void)
{}

void PosixThread::onPolling(void)
{}

void PosixThread::onSignal(int sig)
{}

void PosixThread::setTimer(timeout_t timer, bool periodic)
{
    sigset_t sigs;

#ifdef  HAVE_SETITIMER
    struct itimerval itimer;

    memset(&itimer, 0, sizeof(itimer));
    itimer.it_value.tv_usec = (timer * 1000) % 1000000;
    itimer.it_value.tv_sec = timer / 1000;
    if (periodic) {
        itimer.it_interval.tv_usec = itimer.it_value.tv_usec;
        itimer.it_interval.tv_sec = itimer.it_value.tv_sec;
    }
#else
    timer /= 1000;
#endif

#ifndef CCXX_SIG_THREAD_ALARM
    _arm.enterMutex();
    _timer = this;
#endif
       SysTime::getTime(&_alarm);
    sigemptyset(&sigs);
    sigaddset(&sigs, SIGALRM);
    pthread_sigmask(SIG_UNBLOCK, &sigs, NULL);
#ifdef  HAVE_SETITIMER
    setitimer(ITIMER_REAL, &itimer, NULL);
#else
    alarm(timer);
#endif
}

timeout_t PosixThread::getTimer(void) const
{
#ifdef  HAVE_SETITIMER
    struct itimerval itimer;
#endif

    if(!_alarm)
        return 0;

#ifdef  HAVE_SETITIMER
    getitimer(ITIMER_REAL, &itimer);
    return (timeout_t)(itimer.it_value.tv_sec * 1000 +
        itimer.it_value.tv_usec / 1000);
#else
    time_t now = SysTime::getTime();
    return (timeout_t)(((now - _alarm) * 1000) + 500);
#endif
}

void PosixThread::endTimer(void)
{
#ifdef  HAVE_SETITIMER
    static const struct itimerval itimer = {{0, 0},{0,0}};
#endif

    sigset_t sigs;
#ifndef CCXX_SIG_THREAD_ALARM
    if(_timer != this)
        return;
#endif

#ifdef  HAVE_SETITIMER
    setitimer(ITIMER_REAL, (struct itimerval *)&itimer, NULL);
#else
    alarm(0);
#endif
    sigemptyset(&sigs);
    sigaddset(&sigs, SIGALRM);
    pthread_sigmask(SIG_BLOCK, &sigs, NULL);
#ifndef CCXX_SIG_THREAD_ALARM
    _arm.leaveMutex();
    _timer = NULL;
#endif
}

#if defined(HAVE_SIGWAIT) || defined(HAVE_SIGWAIT2)
void    PosixThread::waitSignal(signo_t signo)
{
    sigset_t    mask;

    sigemptyset(&mask);
    sigaddset(&mask, signo);
#ifndef HAVE_SIGWAIT2
    signo = sigwait(&mask);
#else
    sigwait(&mask, &signo);
#endif
}
#endif // ifdef HAVE_SIGWAIT

void    PosixThread::setSignal(int signo, bool mode)
{
    sigset_t sigs;

    sigemptyset(&sigs);
    sigaddset(&sigs, signo);

    if(mode)
        pthread_sigmask(SIG_UNBLOCK, &sigs, NULL);
    else
        pthread_sigmask(SIG_BLOCK, &sigs, NULL);
}

void    PosixThread::signalThread(Thread* th,signo_t signo)
{
    pthread_kill(th->priv->_tid, signo);
    }

pthread_attr_t * PosixThread::getPthreadAttrPtr(void)
{
    return &priv->_attr;
}

pthread_t PosixThread::getPthreadId(void)
{
    return priv->_tid;
}

void PosixThread::sigInstall(int signo)
{
    struct sigaction act;

    act.sa_handler = (signalexec_t)&ccxx_sig_handler;
    sigemptyset(&act.sa_mask);

#ifdef  SA_INTERRUPT
    act.sa_flags = SA_INTERRUPT;
#else
    act.sa_flags = 0;
#endif
    sigaction(signo, &act, NULL);
}
#endif

#ifdef  USE_POLL

Poller::Poller()
{
    nufds = 0;
    ufds = NULL;
}

Poller::~Poller()
{
    if(ufds) {
        delete[] ufds;
        ufds = NULL;
    }
}

pollfd *Poller::getList(int cnt)
{
    if(nufds < cnt) {
        if(ufds)
            delete[] ufds;
        ufds = new pollfd[cnt];
        nufds = cnt;
    }
    return ufds;
}

#endif

Mutex SysTime::timeLock;

time_t SysTime::getTime(time_t *tloc)
{
    time_t ret;
    lock();
    time_t temp;
#ifdef  WIN32
    ::time(&temp);
#else
    std::time(&temp);
#endif
    memcpy(&ret, &temp, sizeof(time_t));
    if (tloc != NULL)
        memcpy(tloc, &ret, sizeof(time_t));
    unlock();
    return ret;
}

int SysTime::getTimeOfDay(struct timeval *tp)
{
    struct timeval temp;
    int ret(0);
    lock();

#ifdef  WIN32
    // We could use _ftime(), but it is not available on WinCE.
    // (WinCE also lacks time.h)
    // Note also that the average error of _ftime is around 20 ms :)
    time_t now;
    time(&now);
    temp.tv_sec = (long)now;
    temp.tv_usec = (GetTickCount() % 1000) * 1000;
    memcpy(tp, &temp, sizeof(struct timeval));
#else
    ret = ::gettimeofday(&temp, NULL);
    if(ret == 0)
        memcpy(tp, &temp, sizeof(struct timeval));
#endif

    unlock();
    return ret;
}

struct tm *SysTime::getLocalTime(const time_t *clock, struct tm* result)
{
    lock();
#ifdef  WIN32
    struct tm *temp = ::localtime(clock);
#else
    struct tm *temp = std::localtime(clock);
#endif
    memcpy(result, temp, sizeof(struct tm));
    unlock();
    return result;
}

struct tm *SysTime::getGMTTime(const time_t *clock, struct tm* result)
{
    lock();
#ifdef  WIN32
    struct tm *temp = ::gmtime(clock);
#else
    struct tm *temp = std::gmtime(clock);
#endif
    memcpy(result, temp, sizeof(struct tm));
    unlock();
    return result;
}

// C stuff
// this function must declared as extern "C" for some compiler

#ifdef CCXX_NAMESPACES
}
#endif

/** EMACS **
 * Local variables:
 * mode: c++
 * tab-width: 4
 * c-basic-offset: 4
 * End:
 */