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
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
|
/*
* LANES.C Copyright (c) 2007-08, Asko Kauppi
*
* Multithreading in Lua.
*
* History:
* 3-Jan-11 (2.0.10): linda_send bugfix, was waiting on the wrong signal
* 3-Dec-10 (2.0.9): Added support to generate a lane from a string
* 2-Dec-10 (2.0.8): Fix LuaJIT2 incompatibility (no 'tostring' hijack anymore)
* ???????? (2.0.7): Fixed 'memory leak' in some situations where a free running
* lane is collected before application shutdown
* 24-Aug-10 (2.0.6): Mem fixes, argument checking (lua_toLinda result), thread name
* 24-Jun-09 (2.0.4): Made friendly to already multithreaded host apps.
* 20-Oct-08 (2.0.2): Added closing of free-running threads, but it does
* not seem to eliminate the occasional segfaults at process
* exit.
* ...
* 24-Jun-08 .. 14-Aug-08 AKa: Major revise, Lanes 2008 version (2.0 rc1)
* ...
* 18-Sep-06 AKa: Started the module.
*
* Platforms (tested internally):
* OS X (10.5.7 PowerPC/Intel)
* Linux x86 (Ubuntu 8.04)
* Win32 (Windows XP Home SP2, Visual C++ 2005/2008 Express)
*
* Platforms (tested externally):
* Win32 (MSYS) by Ross Berteig.
*
* Platforms (testers appreciated):
* Win64 - should work???
* Linux x64 - should work
* FreeBSD - should work
* QNX - porting shouldn't be hard
* Sun Solaris - porting shouldn't be hard
*
* References:
* "Porting multithreaded applications from Win32 to Mac OS X":
* <http://developer.apple.com/macosx/multithreadedprogramming.html>
*
* Pthreads:
* <http://vergil.chemistry.gatech.edu/resources/programming/threads.html>
*
* MSDN: <http://msdn2.microsoft.com/en-us/library/ms686679.aspx>
*
* <http://ridiculousfish.com/blog/archives/2007/02/17/barrier>
*
* Defines:
* -DLINUX_SCHED_RR: all threads are lifted to SCHED_RR category, to
* allow negative priorities (-2,-1) be used. Even without this,
* using priorities will require 'sudo' privileges on Linux.
*
* -DUSE_PTHREAD_TIMEDJOIN: use 'pthread_timedjoin_np()' for waiting
* for threads with a timeout. This changes the thread cleanup
* mechanism slightly (cleans up at the join, not once the thread
* has finished). May or may not be a good idea to use it.
* Available only in selected operating systems (Linux).
*
* Bugs:
*
* To-do:
*
* Make waiting threads cancelable.
* ...
*/
const char *VERSION= "2.0.10";
/*
===============================================================================
Copyright (C) 2007-10 Asko Kauppi <akauppi@gmail.com>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
===============================================================================
*/
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include "lua.h"
#include "lauxlib.h"
#include "threading.h"
#include "tools.h"
#if !((defined PLATFORM_WIN32) || (defined PLATFORM_POCKETPC))
# include <sys/time.h>
#endif
/* geteuid() */
#ifdef PLATFORM_LINUX
# include <unistd.h>
# include <sys/types.h>
#endif
/* The selected number is not optimal; needs to be tested. Even using just
* one keeper state may be good enough (depends on the number of Lindas used
* in the applications).
*/
#define KEEPER_STATES_N 1 // 6
/* Do you want full call stacks, or just the line where the error happened?
*
* TBD: The full stack feature does not seem to work (try 'make error').
*/
#define ERROR_FULL_STACK
#ifdef ERROR_FULL_STACK
# define STACK_TRACE_KEY ((void*)lane_error) // used as registry key
#endif
/*
* Lua code for the keeper states (baked in)
*/
static char keeper_chunk[]=
#include "keeper.lch"
// NOTE: values to be changed by either thread, during execution, without
// locking, are marked "volatile"
//
struct s_lane {
THREAD_T thread;
//
// M: sub-thread OS thread
// S: not used
char threadName[64]; //Optional, for debugging and such. owerflowable by a strcpy.
lua_State *L;
//
// M: prepares the state, and reads results
// S: while S is running, M must keep out of modifying the state
volatile enum e_status status;
//
// M: sets to PENDING (before launching)
// S: updates -> RUNNING/WAITING -> DONE/ERROR_ST/CANCELLED
volatile bool_t cancel_request;
//
// M: sets to FALSE, flags TRUE for cancel request
// S: reads to see if cancel is requested
#if !( (defined PLATFORM_WIN32) || (defined PLATFORM_POCKETPC) || (defined PTHREAD_TIMEDJOIN) )
SIGNAL_T done_signal_;
//
// M: Waited upon at lane ending (if Posix with no PTHREAD_TIMEDJOIN)
// S: sets the signal once cancellation is noticed (avoids a kill)
MUTEX_T done_lock_;
//
// Lock required by 'done_signal' condition variable, protecting
// lane status changes to DONE/ERROR_ST/CANCELLED.
#endif
volatile enum {
NORMAL, // normal master side state
KILLED // issued an OS kill
} mstatus;
//
// M: sets to NORMAL, if issued a kill changes to KILLED
// S: not used
struct s_lane * volatile selfdestruct_next;
//
// M: sets to non-NULL if facing lane handle '__gc' cycle but the lane
// is still running
// S: cleans up after itself if non-NULL at lane exit
};
static bool_t cancel_test( lua_State *L );
static void cancel_error( lua_State *L );
#define CANCEL_TEST_KEY ((void*)cancel_test) // used as registry key
#define CANCEL_ERROR ((void*)cancel_error) // 'cancel_error' sentinel
/*
* registry[FINALIZER_REG_KEY] is either nil (no finalizers) or a table
* of functions that Lanes will call after the executing 'pcall' has ended.
*
* We're NOT using the GC system for finalizer mainly because providing the
* error (and maybe stack trace) parameters to the finalizer functions would
* anyways complicate that approach.
*/
#define FINALIZER_REG_KEY ((void*)LG_set_finalizer)
struct s_Linda;
#if 1
# define DEBUG_SIGNAL( msg, signal_ref ) /* */
#else
# define DEBUG_SIGNAL( msg, signal_ref ) \
{ int i; unsigned char *ptr; char buf[999]; \
sprintf( buf, ">>> " msg ": %p\t", (signal_ref) ); \
ptr= (unsigned char *)signal_ref; \
for( i=0; i<sizeof(*signal_ref); i++ ) { \
sprintf( strchr(buf,'\0'), "%02x %c ", ptr[i], ptr[i] ); \
} \
fprintf( stderr, "%s\n", buf ); \
}
#endif
static bool_t thread_cancel( struct s_lane *s, double secs, bool_t force );
/*
* Push a table stored in registry onto Lua stack.
*
* If there is no existing table, create one if 'create' is TRUE.
*
* Returns: TRUE if a table was pushed
* FALSE if no table found, not created, and nothing pushed
*/
static bool_t push_registry_table( lua_State *L, void *key, bool_t create ) {
STACK_GROW(L,3);
lua_pushlightuserdata( L, key );
lua_gettable( L, LUA_REGISTRYINDEX );
if (lua_isnil(L,-1)) {
lua_pop(L,1);
if (!create) return FALSE; // nothing pushed
lua_newtable(L);
lua_pushlightuserdata( L, key );
lua_pushvalue(L,-2); // duplicate of the table
lua_settable( L, LUA_REGISTRYINDEX );
// [-1]: table that's also bound in registry
}
return TRUE; // table pushed
}
/*---=== Serialize require ===---
*/
static MUTEX_T require_cs;
//---
// [val]= new_require( ... )
//
// Call 'old_require' but only one lane at a time.
//
// Upvalues: [1]: original 'require' function
//
static int new_require( lua_State *L ) {
int rc;
int args= lua_gettop(L);
STACK_GROW(L,1);
STACK_CHECK(L)
// Using 'lua_pcall()' to catch errors; otherwise a failing 'require' would
// leave us locked, blocking any future 'require' calls from other lanes.
//
MUTEX_LOCK( &require_cs );
{
lua_pushvalue( L, lua_upvalueindex(1) );
lua_insert( L, 1 );
rc= lua_pcall( L, args, 1 /*retvals*/, 0 /*errfunc*/ );
//
// LUA_ERRRUN / LUA_ERRMEM
}
MUTEX_UNLOCK( &require_cs );
if (rc) lua_error(L); // error message already at [-1]
STACK_END(L,0)
return 1;
}
/*
* Serialize calls to 'require', if it exists
*/
static
void serialize_require( lua_State *L ) {
STACK_GROW(L,1);
STACK_CHECK(L)
// Check 'require' is there; if not, do nothing
//
lua_getglobal( L, "require" );
if (lua_isfunction( L, -1 )) {
// [-1]: original 'require' function
lua_pushcclosure( L, new_require, 1 /*upvalues*/ );
lua_setglobal( L, "require" );
} else {
// [-1]: nil
lua_pop(L,1);
}
STACK_END(L,0)
}
/*---=== Keeper states ===---
*/
/*
* Pool of keeper states
*
* Access to keeper states is locked (only one OS thread at a time) so the
* bigger the pool, the less chances of unnecessary waits. Lindas map to the
* keepers randomly, by a hash.
*/
struct s_Keeper {
MUTEX_T lock_;
lua_State *L;
} keeper[ KEEPER_STATES_N ];
/* We could use an empty table in 'keeper.lua' as the sentinel, but maybe
* checking for a lightuserdata is faster.
*/
static bool_t nil_sentinel;
/*
* Initialize keeper states
*
* If there is a problem, return an error message (NULL for okay).
*
* Note: Any problems would be design flaws; the created Lua state is left
* unclosed, because it does not really matter. In production code, this
* function never fails.
*/
static const char *init_keepers(void) {
unsigned int i;
for( i=0; i<KEEPER_STATES_N; i++ ) {
// Initialize Keeper states with bare minimum of libs (those required
// by 'keeper.lua')
//
lua_State *L= luaL_newstate();
if (!L) return "out of memory";
luaG_openlibs( L, "io,table" ); // 'io' for debugging messages
lua_pushlightuserdata( L, &nil_sentinel );
lua_setglobal( L, "nil_sentinel" );
// Read in the preloaded chunk (and run it)
//
if (luaL_loadbuffer( L, keeper_chunk, sizeof(keeper_chunk), "=lanes_keeper" ))
return "luaL_loadbuffer() failed"; // LUA_ERRMEM
if (lua_pcall( L, 0 /*args*/, 0 /*results*/, 0 /*errfunc*/ )) {
// LUA_ERRRUN / LUA_ERRMEM / LUA_ERRERR
//
const char *err= lua_tostring(L,-1);
assert(err);
return err;
}
MUTEX_INIT( &keeper[i].lock_ );
keeper[i].L= L;
}
return NULL; // ok
}
static
struct s_Keeper *keeper_acquire( const void *ptr ) {
/*
* Any hashing will do that maps pointers to 0..KEEPER_STATES_N-1
* consistently.
*
* Pointers are often aligned by 8 or so - ignore the low order bits
*/
unsigned int i= ((unsigned long)(ptr) >> 3) % KEEPER_STATES_N;
struct s_Keeper *K= &keeper[i];
MUTEX_LOCK( &K->lock_ );
return K;
}
static
void keeper_release( struct s_Keeper *K ) {
MUTEX_UNLOCK( &K->lock_ );
}
/*
* Call a function ('func_name') in the keeper state, and pass on the returned
* values to 'L'.
*
* 'linda': deep Linda pointer (used only as a unique table key, first parameter)
* 'starting_index': first of the rest of parameters (none if 0)
*
* Returns: number of return values (pushed to 'L')
*/
static
int keeper_call( lua_State* K, const char *func_name,
lua_State *L, struct s_Linda *linda, uint_t starting_index ) {
int args= starting_index ? (lua_gettop(L) - starting_index +1) : 0;
int Ktos= lua_gettop(K);
int retvals;
STACK_GROW( K, 2 );
lua_getglobal( K, func_name );
ASSERT_L( lua_isfunction(K,-1) );
lua_pushlightuserdata( K, linda );
luaG_inter_copy( L,K, args ); // L->K
lua_call( K, 1+args, LUA_MULTRET );
retvals= lua_gettop(K) - Ktos;
luaG_inter_move( K,L, retvals ); // K->L
return retvals;
}
/*---=== Linda ===---
*/
/*
* Actual data is kept within a keeper state, which is hashed by the 's_Linda'
* pointer (which is same to all userdatas pointing to it).
*/
struct s_Linda {
SIGNAL_T read_happened;
SIGNAL_T write_happened;
};
static int LG_linda_id( lua_State* );
#define lua_toLinda(L,n) ((struct s_Linda *)luaG_todeep( L, LG_linda_id, n ))
/*
* bool= linda_send( linda_ud, [timeout_secs=-1,] key_num|str|bool|lightuserdata, ... )
*
* Send one or more values to a Linda. If there is a limit, all values must fit.
*
* Returns: 'true' if the value was queued
* 'false' for timeout (only happens when the queue size is limited)
*/
LUAG_FUNC( linda_send ) {
struct s_Linda *linda= lua_toLinda( L, 1 );
bool_t ret;
bool_t cancel= FALSE;
struct s_Keeper *K;
time_d timeout= -1.0;
uint_t key_i= 2; // index of first key, if timeout not there
luaL_argcheck( L, linda, 1, "expected a linda object!");
if (lua_isnumber(L,2)) {
timeout= SIGNAL_TIMEOUT_PREPARE( lua_tonumber(L,2) );
key_i++;
} else if (lua_isnil(L,2))
key_i++;
if (lua_isnil(L,key_i))
luaL_error( L, "nil key" );
STACK_GROW(L,1);
K= keeper_acquire( linda );
{
lua_State *KL= K->L; // need to do this for 'STACK_CHECK'
STACK_CHECK(KL)
while(TRUE) {
int pushed;
STACK_MID(KL,0)
pushed= keeper_call( K->L, "send", L, linda, key_i );
ASSERT_L( pushed==1 );
ret= lua_toboolean(L,-1);
lua_pop(L,1);
if (ret) {
// Wake up ALL waiting threads
//
SIGNAL_ALL( &linda->write_happened );
break;
} else if (timeout==0.0) {
break; /* no wait; instant timeout */
} else {
/* limit faced; push until timeout */
cancel= cancel_test( L ); // testing here causes no delays
if (cancel) break;
// Bugfix by Benoit Germain Dec-2009: change status of lane to "waiting"
//
#if 1
{
struct s_lane *s;
enum e_status prev_status = ERROR_ST; // prevent 'might be used uninitialized' warnings
STACK_GROW(L,1);
STACK_CHECK(L)
lua_pushlightuserdata( L, CANCEL_TEST_KEY );
lua_rawget( L, LUA_REGISTRYINDEX );
s= lua_touserdata( L, -1 ); // lightuserdata (true 's_lane' pointer) / nil
lua_pop(L,1);
STACK_END(L,0)
if (s) {
prev_status = s->status;
s->status = WAITING;
}
if (!SIGNAL_WAIT( &linda->read_happened, &K->lock_, timeout )) {
if (s) { s->status = prev_status; }
break;
}
if (s) s->status = prev_status;
}
#else
// K lock will be released for the duration of wait and re-acquired
//
if (!SIGNAL_WAIT( &linda->read_happened, &K->lock_, timeout ))
break; // timeout
#endif
}
}
STACK_END(KL,0)
}
keeper_release(K);
if (cancel)
cancel_error(L);
lua_pushboolean( L, ret );
return 1;
}
/*
* [val, key]= linda_receive( linda_ud, [timeout_secs_num=-1], key_num|str|bool|lightuserdata [, ...] )
*
* Receive a value from Linda, consuming it.
*
* Returns: value received (which is consumed from the slot)
* key which had it
*/
LUAG_FUNC( linda_receive ) {
struct s_Linda *linda= lua_toLinda( L, 1 );
int pushed;
bool_t cancel= FALSE;
struct s_Keeper *K;
time_d timeout= -1.0;
uint_t key_i= 2;
luaL_argcheck( L, linda, 1, "expected a linda object!");
if (lua_isnumber(L,2)) {
timeout= SIGNAL_TIMEOUT_PREPARE( lua_tonumber(L,2) );
key_i++;
} else if (lua_isnil(L,2))
key_i++;
K= keeper_acquire( linda );
{
while(TRUE) {
pushed= keeper_call( K->L, "receive", L, linda, key_i );
if (pushed) {
ASSERT_L( pushed==2 );
// To be done from within the 'K' locking area
//
SIGNAL_ALL( &linda->read_happened );
break;
} else if (timeout==0.0) {
break; /* instant timeout */
} else { /* nothing received; wait until timeout */
cancel= cancel_test( L ); // testing here causes no delays
if (cancel) break;
// Bugfix by Benoit Germain Dec-2009: change status of lane to "waiting"
//
#if 1
{
struct s_lane *s;
enum e_status prev_status = ERROR_ST; // prevent 'might be used uninitialized' warnings
STACK_GROW(L,1);
STACK_CHECK(L)
lua_pushlightuserdata( L, CANCEL_TEST_KEY );
lua_rawget( L, LUA_REGISTRYINDEX );
s= lua_touserdata( L, -1 ); // lightuserdata (true 's_lane' pointer) / nil
lua_pop(L,1);
STACK_END(L,0)
if (s) {
prev_status = s->status;
s->status = WAITING;
}
if (!SIGNAL_WAIT( &linda->write_happened, &K->lock_, timeout )) {
if (s) { s->status = prev_status; }
break;
}
if (s) s->status = prev_status;
}
#else
// Release the K lock for the duration of wait, and re-acquire
//
if (!SIGNAL_WAIT( &linda->write_happened, &K->lock_, timeout ))
break;
#endif
}
}
}
keeper_release(K);
if (cancel)
cancel_error(L);
return pushed;
}
/*
* = linda_set( linda_ud, key_num|str|bool|lightuserdata [,value] )
*
* Set a value to Linda.
*
* Existing slot value is replaced, and possible queue entries removed.
*/
LUAG_FUNC( linda_set ) {
struct s_Linda *linda= lua_toLinda( L, 1 );
bool_t has_value= !lua_isnil(L,3);
struct s_Keeper *K;
luaL_argcheck( L, linda, 1, "expected a linda object!");
K= keeper_acquire( linda );
{
int pushed= keeper_call( K->L, "set", L, linda, 2 );
ASSERT_L( pushed==0 );
/* Set the signal from within 'K' locking.
*/
if (has_value) {
SIGNAL_ALL( &linda->write_happened );
}
}
keeper_release(K);
return 0;
}
/*
* [val]= linda_get( linda_ud, key_num|str|bool|lightuserdata )
*
* Get a value from Linda.
*/
LUAG_FUNC( linda_get ) {
struct s_Linda *linda= lua_toLinda( L, 1 );
int pushed;
struct s_Keeper *K;
luaL_argcheck( L, linda, 1, "expected a linda object!");
K= keeper_acquire( linda );
{
pushed= keeper_call( K->L, "get", L, linda, 2 );
ASSERT_L( pushed==0 || pushed==1 );
}
keeper_release(K);
return pushed;
}
/*
* = linda_limit( linda_ud, key_num|str|bool|lightuserdata, uint [, ...] )
*
* Set limits to 1 or more Linda keys.
*/
LUAG_FUNC( linda_limit ) {
struct s_Linda *linda= lua_toLinda( L, 1 );
struct s_Keeper *K;
luaL_argcheck( L, linda, 1, "expected a linda object!");
K= keeper_acquire( linda );
{
int pushed= keeper_call( K->L, "limit", L, linda, 2 );
ASSERT_L( pushed==0 );
}
keeper_release(K);
return 0;
}
/*
* lightuserdata= linda_deep( linda_ud )
*
* Return the 'deep' userdata pointer, identifying the Linda.
*
* This is needed for using Lindas as key indices (timer system needs it);
* separately created proxies of the same underlying deep object will have
* different userdata and won't be known to be essentially the same deep one
* without this.
*/
LUAG_FUNC( linda_deep ) {
struct s_Linda *linda= lua_toLinda( L, 1 );
luaL_argcheck( L, linda, 1, "expected a linda object!");
lua_pushlightuserdata( L, linda ); // just the address
return 1;
}
/*
* Identity function of a shared userdata object.
*
* lightuserdata= linda_id( "new" [, ...] )
* = linda_id( "delete", lightuserdata )
*
* Creation and cleanup of actual 'deep' objects. 'luaG_...' will wrap them into
* regular userdata proxies, per each state using the deep data.
*
* tbl= linda_id( "metatable" )
*
* Returns a metatable for the proxy objects ('__gc' method not needed; will
* be added by 'luaG_...')
*
* = linda_id( str, ... )
*
* For any other strings, the ID function must not react at all. This allows
* future extensions of the system.
*/
LUAG_FUNC( linda_id ) {
const char *which= lua_tostring(L,1);
if (strcmp( which, "new" )==0) {
struct s_Linda *s;
/* We don't use any parameters, but one could (they're at [2..TOS])
*/
ASSERT_L( lua_gettop(L)==1 );
/* The deep data is allocated separately of Lua stack; we might no
* longer be around when last reference to it is being released.
* One can use any memory allocation scheme.
*/
s= (struct s_Linda *) malloc( sizeof(struct s_Linda) );
ASSERT_L(s);
SIGNAL_INIT( &s->read_happened );
SIGNAL_INIT( &s->write_happened );
lua_pushlightuserdata( L, s );
return 1;
} else if (strcmp( which, "delete" )==0) {
struct s_Keeper *K;
struct s_Linda *s= lua_touserdata(L,2);
ASSERT_L(s);
/* Clean associated structures in the keeper state.
*/
K= keeper_acquire(s);
{
keeper_call( K->L, "clear", L, s, 0 );
}
keeper_release(K);
/* There aren't any lanes waiting on these lindas, since all proxies
* have been gc'ed. Right?
*/
SIGNAL_FREE( &s->read_happened );
SIGNAL_FREE( &s->write_happened );
free(s);
return 0;
} else if (strcmp( which, "metatable" )==0) {
STACK_CHECK(L)
lua_newtable(L);
lua_newtable(L);
//
// [-2]: linda metatable
// [-1]: metatable's to-be .__index table
lua_pushcfunction( L, LG_linda_send );
lua_setfield( L, -2, "send" );
lua_pushcfunction( L, LG_linda_receive );
lua_setfield( L, -2, "receive" );
lua_pushcfunction( L, LG_linda_limit );
lua_setfield( L, -2, "limit" );
lua_pushcfunction( L, LG_linda_set );
lua_setfield( L, -2, "set" );
lua_pushcfunction( L, LG_linda_get );
lua_setfield( L, -2, "get" );
lua_pushcfunction( L, LG_linda_deep );
lua_setfield( L, -2, "deep" );
lua_setfield( L, -2, "__index" );
STACK_END(L,1)
return 1;
}
return 0; // unknown request, be quiet
}
/*---=== Finalizer ===---
*/
//---
// void= finalizer( finalizer_func )
//
// finalizer_func( [err, stack_tbl] )
//
// Add a function that will be called when exiting the lane, either via
// normal return or an error.
//
LUAG_FUNC( set_finalizer )
{
STACK_GROW(L,3);
// Get the current finalizer table (if any)
//
push_registry_table( L, FINALIZER_REG_KEY, TRUE /*do create if none*/ );
lua_pushinteger( L, lua_objlen(L,-1)+1 );
lua_pushvalue( L, 1 ); // copy of the function
lua_settable( L, -3 );
lua_pop(L,1);
return 0;
}
//---
// Run finalizers - if any - with the given parameters
//
// If 'rc' is nonzero, error message and stack index are available as:
// [-1]: stack trace (table)
// [-2]: error message (any type)
//
// Returns:
// 0 if finalizers were run without error (or there were none)
// LUA_ERRxxx return code if any of the finalizers failed
//
// TBD: should we add stack trace on failing finalizer, wouldn't be hard..
//
static int run_finalizers( lua_State *L, int lua_rc )
{
unsigned error_index, tbl_index;
unsigned n;
int rc= 0;
if (!push_registry_table(L, FINALIZER_REG_KEY, FALSE /*don't create one*/))
return 0; // no finalizers
tbl_index= lua_gettop(L);
error_index= (lua_rc!=0) ? tbl_index-2 : 0; // absolute indices
STACK_GROW(L,4);
// [-1]: { func [, ...] }
//
for( n= (unsigned int)lua_objlen(L,-1); n>0; n-- ) {
unsigned args= 0;
lua_pushinteger( L,n );
lua_gettable( L, -2 );
// [-1]: function
// [-2]: finalizers table
if (error_index) {
lua_pushvalue( L, error_index );
lua_pushvalue( L, error_index+1 ); // stack trace
args= 2;
}
rc= lua_pcall( L, args, 0 /*retvals*/, 0 /*no errfunc*/ );
//
// LUA_ERRRUN / LUA_ERRMEM
if (rc!=0) {
// [-1]: error message
//
// If one finalizer fails, don't run the others. Return this
// as the 'real' error, preceding that we could have had (or not)
// from the actual code.
//
break;
}
}
lua_remove(L,tbl_index); // take finalizer table out of stack
return rc;
}
/*---=== Threads ===---
*/
static MUTEX_T selfdestruct_cs;
//
// Protects modifying the selfdestruct chain
#define SELFDESTRUCT_END ((struct s_lane *)(-1))
//
// The chain is ended by '(struct s_lane*)(-1)', not NULL:
// 'selfdestruct_first -> ... -> ... -> (-1)'
struct s_lane * volatile selfdestruct_first= SELFDESTRUCT_END;
/*
* Add the lane to selfdestruct chain; the ones still running at the end of the
* whole process will be cancelled.
*/
static void selfdestruct_add( struct s_lane *s ) {
MUTEX_LOCK( &selfdestruct_cs );
{
assert( s->selfdestruct_next == NULL );
s->selfdestruct_next= selfdestruct_first;
selfdestruct_first= s;
}
MUTEX_UNLOCK( &selfdestruct_cs );
}
/*
* A free-running lane has ended; remove it from selfdestruct chain
*/
static void selfdestruct_remove( struct s_lane *s ) {
MUTEX_LOCK( &selfdestruct_cs );
{
// Make sure (within the MUTEX) that we actually are in the chain
// still (at process exit they will remove us from chain and then
// cancel/kill).
//
if (s->selfdestruct_next != NULL) {
struct s_lane **ref= (struct s_lane **) &selfdestruct_first;
bool_t found= FALSE;
while( *ref != SELFDESTRUCT_END ) {
if (*ref == s) {
*ref= s->selfdestruct_next;
s->selfdestruct_next= NULL;
found= TRUE;
break;
}
ref= (struct s_lane **) &((*ref)->selfdestruct_next);
}
assert( found );
}
}
MUTEX_UNLOCK( &selfdestruct_cs );
}
/*
* Process end; cancel any still free-running threads
*/
static void selfdestruct_atexit( void ) {
if (selfdestruct_first == SELFDESTRUCT_END) return; // no free-running threads
// Signal _all_ still running threads to exit
//
MUTEX_LOCK( &selfdestruct_cs );
{
struct s_lane *s= selfdestruct_first;
while( s != SELFDESTRUCT_END ) {
s->cancel_request= TRUE;
s= s->selfdestruct_next;
}
}
MUTEX_UNLOCK( &selfdestruct_cs );
// When noticing their cancel, the lanes will remove themselves from
// the selfdestruct chain.
// TBD: Not sure if Windows (multi core) will require the timed approach,
// or single Yield. I don't have machine to test that (so leaving
// for timed approach). -- AKa 25-Oct-2008
#ifdef PLATFORM_LINUX
// It seems enough for Linux to have a single yield here, which allows
// other threads (timer lane) to proceed. Without the yield, there is
// segfault.
//
YIELD();
#else
// OS X 10.5 (Intel) needs more to avoid segfaults.
//
// "make test" is okay. 100's of "make require" are okay.
//
// Tested on MacBook Core Duo 2GHz and 10.5.5:
// -- AKa 25-Oct-2008
//
#ifndef ATEXIT_WAIT_SECS
# define ATEXIT_WAIT_SECS (0.1)
#endif
{
double t_until= now_secs() + ATEXIT_WAIT_SECS;
while( selfdestruct_first != SELFDESTRUCT_END ) {
YIELD(); // give threads time to act on their cancel
if (now_secs() >= t_until) break;
}
}
#endif
//---
// Kill the still free running threads
//
if ( selfdestruct_first != SELFDESTRUCT_END ) {
unsigned n=0;
MUTEX_LOCK( &selfdestruct_cs );
{
struct s_lane *s= selfdestruct_first;
while( s != SELFDESTRUCT_END ) {
n++;
s= s->selfdestruct_next;
}
}
MUTEX_UNLOCK( &selfdestruct_cs );
// Linux (at least 64-bit): CAUSES A SEGFAULT IF THIS BLOCK IS ENABLED
// and works without the block (so let's leave those lanes running)
//
//we want to free memory and such when we exit.
#if 0
// 2.0.2: at least timer lane is still here
//
DEBUGEXEC(fprintf( stderr, "Left %d lane(s) with cancel request at process end.\n", n ));
#else
n=0;
MUTEX_LOCK( &selfdestruct_cs );
{
struct s_lane *s= selfdestruct_first;
while( s != SELFDESTRUCT_END ) {
struct s_lane *next_s= s->selfdestruct_next;
s->selfdestruct_next= NULL; // detach from selfdestruct chain
THREAD_KILL( &s->thread );
lua_close(s->L);
free(s);
s= next_s;
n++;
}
selfdestruct_first= SELFDESTRUCT_END;
}
MUTEX_UNLOCK( &selfdestruct_cs );
DEBUGEXEC(fprintf( stderr, "Killed %d lane(s) at process end.\n", n ));
#endif
}
{
int i;
for(i=0;i<KEEPER_STATES_N;i++){
lua_close(keeper[i].L);
keeper[i].L = 0;
}
}
}
// To allow free-running threads (longer lifespan than the handle's)
// 'struct s_lane' are malloc/free'd and the handle only carries a pointer.
// This is not deep userdata since the handle's not portable among lanes.
//
#define lua_toLane(L,i) (* ((struct s_lane**) lua_touserdata(L,i)))
/*
* Check if the thread in question ('L') has been signalled for cancel.
*
* Called by cancellation hooks and/or pending Linda operations (because then
* the check won't affect performance).
*
* Returns TRUE if any locks are to be exited, and 'cancel_error()' called,
* to make execution of the lane end.
*/
static bool_t cancel_test( lua_State *L ) {
struct s_lane *s;
STACK_GROW(L,1);
STACK_CHECK(L)
lua_pushlightuserdata( L, CANCEL_TEST_KEY );
lua_rawget( L, LUA_REGISTRYINDEX );
s= lua_touserdata( L, -1 ); // lightuserdata (true 's_lane' pointer) / nil
lua_pop(L,1);
STACK_END(L,0)
// 's' is NULL for the original main state (no-one can cancel that)
//
return s && s->cancel_request;
}
static void cancel_error( lua_State *L ) {
STACK_GROW(L,1);
lua_pushlightuserdata( L, CANCEL_ERROR ); // special error value
lua_error(L); // no return
}
static void cancel_hook( lua_State *L, lua_Debug *ar ) {
(void)ar;
if (cancel_test(L)) cancel_error(L);
}
//---
// = _single( [cores_uint=1] )
//
// Limits the process to use only 'cores' CPU cores. To be used for performance
// testing on multicore devices. DEBUGGING ONLY!
//
LUAG_FUNC( _single ) {
uint_t cores= luaG_optunsigned(L,1,1);
#ifdef PLATFORM_OSX
#ifdef _UTILBINDTHREADTOCPU
if (cores > 1) luaL_error( L, "Limiting to N>1 cores not possible." );
// requires 'chudInitialize()'
utilBindThreadToCPU(0); // # of CPU to run on (we cannot limit to 2..N CPUs?)
#else
luaL_error( L, "Not available: compile with _UTILBINDTHREADTOCPU" );
#endif
#else
luaL_error( L, "not implemented!" );
#endif
(void)cores;
return 0;
}
/*
* str= lane_error( error_val|str )
*
* Called if there's an error in some lane; add call stack to error message
* just like 'lua.c' normally does.
*
* ".. will be called with the error message and its return value will be the
* message returned on the stack by lua_pcall."
*
* Note: Rather than modifying the error message itself, it would be better
* to provide the call stack (as string) completely separated. This would
* work great with non-string error values as well (current system does not).
* (This is NOT possible with the Lua 5.1 'lua_pcall()'; we could of course
* implement a Lanes-specific 'pcall' of our own that does this). TBD!!! :)
* --AKa 22-Jan-2009
*/
#ifdef ERROR_FULL_STACK
static int lane_error( lua_State *L ) {
lua_Debug ar;
unsigned lev,n;
// [1]: error message (any type)
assert( lua_gettop(L)==1 );
// Don't do stack survey for cancelled lanes.
//
#if 1
if (lua_touserdata(L,1) == CANCEL_ERROR)
return 1; // just pass on
#endif
// Place stack trace at 'registry[lane_error]' for the 'luc_pcall()'
// caller to fetch. This bypasses the Lua 5.1 limitation of only one
// return value from error handler to 'lua_pcall()' caller.
// It's adequate to push stack trace as a table. This gives the receiver
// of the stack best means to format it to their liking. Also, it allows
// us to add more stack info later, if needed.
//
// table of { "sourcefile.lua:<line>", ... }
//
STACK_GROW(L,3);
lua_newtable(L);
// Best to start from level 1, but in some cases it might be a C function
// and we don't get '.currentline' for that. It's okay - just keep level
// and table index growing separate. --AKa 22-Jan-2009
//
lev= 0;
n=1;
while( lua_getstack(L, ++lev, &ar ) ) {
lua_getinfo(L, "Sl", &ar);
if (ar.currentline > 0) {
lua_pushinteger( L, n++ );
lua_pushfstring( L, "%s:%d", ar.short_src, ar.currentline );
lua_settable( L, -3 );
}
}
lua_pushlightuserdata( L, STACK_TRACE_KEY );
lua_insert(L,-2);
lua_settable( L, LUA_REGISTRYINDEX );
assert( lua_gettop(L)== 1 );
return 1; // the untouched error value
}
#endif
#if defined PLATFORM_WIN32 && !defined __GNUC__
//see http://msdn.microsoft.com/en-us/library/xcb2z8hs.aspx
#define MS_VC_EXCEPTION 0x406D1388
#pragma pack(push,8)
typedef struct tagTHREADNAME_INFO
{
DWORD dwType; // Must be 0x1000.
LPCSTR szName; // Pointer to name (in user addr space).
DWORD dwThreadID; // Thread ID (-1=caller thread).
DWORD dwFlags; // Reserved for future use, must be zero.
} THREADNAME_INFO;
#pragma pack(pop)
void SetThreadName( DWORD dwThreadID, char* threadName)
{
THREADNAME_INFO info;
Sleep(10);
info.dwType = 0x1000;
info.szName = threadName;
info.dwThreadID = dwThreadID;
info.dwFlags = 0;
__try
{
RaiseException( MS_VC_EXCEPTION, 0, sizeof(info)/sizeof(ULONG_PTR), (ULONG_PTR*)&info );
}
__except(EXCEPTION_EXECUTE_HANDLER)
{
}
}
#endif
//---
#if (defined PLATFORM_WIN32) || (defined PLATFORM_POCKETPC)
static THREAD_RETURN_T __stdcall lane_main( void *vs )
#else
static THREAD_RETURN_T lane_main( void *vs )
#endif
{
struct s_lane *s= (struct s_lane *)vs;
int rc, rc2;
lua_State *L= s->L;
#if defined PLATFORM_WIN32 && !defined __GNUC__
SetThreadName(-1, s->threadName);
#endif
s->status= RUNNING; // PENDING -> RUNNING
// Tie "set_finalizer()" to the state
//
lua_pushcfunction( L, LG_set_finalizer );
lua_setglobal( L, "set_finalizer" );
#ifdef ERROR_FULL_STACK
STACK_GROW( L, 1 );
lua_pushcfunction( L, lane_error );
lua_insert( L, 1 );
// [1]: error handler
// [2]: function to run
// [3..top]: parameters
//
rc= lua_pcall( L, lua_gettop(L)-2, LUA_MULTRET, 1 /*error handler*/ );
// 0: no error
// LUA_ERRRUN: a runtime error (error pushed on stack)
// LUA_ERRMEM: memory allocation error
// LUA_ERRERR: error while running the error handler (if any)
assert( rc!=LUA_ERRERR ); // since we've authored it
lua_remove(L,1); // remove error handler
// Lua 5.1 error handler is limited to one return value; taking stack trace
// via registry
//
if (rc!=0) {
STACK_GROW(L,1);
lua_pushlightuserdata( L, STACK_TRACE_KEY );
lua_gettable(L, LUA_REGISTRYINDEX);
// For cancellation, a stack trace isn't placed
//
assert( lua_istable(L,2) || (lua_touserdata(L,1)==CANCEL_ERROR) );
// Just leaving the stack trace table on the stack is enough to get
// it through to the master.
}
#else
// This code does not use 'lane_error'
//
// [1]: function to run
// [2..top]: parameters
//
rc= lua_pcall( L, lua_gettop(L)-1, LUA_MULTRET, 0 /*no error handler*/ );
// 0: no error
// LUA_ERRRUN: a runtime error (error pushed on stack)
// LUA_ERRMEM: memory allocation error
#endif
//STACK_DUMP(L);
// Call finalizers, if the script has set them up.
//
rc2= run_finalizers(L,rc);
if (rc2!=0) {
// Error within a finalizer!
//
// [-1]: error message
rc= rc2; // we're overruling the earlier script error or normal return
lua_insert( L,1 ); // make error message [1]
lua_settop( L,1 ); // remove all rest
// Place an empty stack table just to keep the API simple (always when
// there's an error, there's also stack table - though it may be empty).
//
lua_newtable(L);
}
if (s->selfdestruct_next != NULL) {
// We're a free-running thread and no-one's there to clean us up.
//
lua_close( s->L );
s->L = L = 0;
#if !( (defined PLATFORM_WIN32) || (defined PLATFORM_POCKETPC) || (defined PTHREAD_TIMEDJOIN) )
SIGNAL_FREE( &s->done_signal_ );
MUTEX_FREE( &s->done_lock_ );
#endif
selfdestruct_remove(s); // away from selfdestruct chain
free(s);
} else {
// leave results (1..top) or error message + stack trace (1..2) on the stack - master will copy them
enum e_status st=
(rc==0) ? DONE
: (lua_touserdata(L,1)==CANCEL_ERROR) ? CANCELLED
: ERROR_ST;
// Posix no PTHREAD_TIMEDJOIN:
// 'done_lock' protects the -> DONE|ERROR_ST|CANCELLED state change
//
#if (defined PLATFORM_WIN32) || (defined PLATFORM_POCKETPC) || (defined PTHREAD_TIMEDJOIN)
s->status= st;
#else
MUTEX_LOCK( &s->done_lock_ );
{
s->status= st;
SIGNAL_ONE( &s->done_signal_ ); // wake up master (while 's->done_lock' is on)
}
MUTEX_UNLOCK( &s->done_lock_ );
#endif
}
return 0; // ignored
}
//---
// lane_ud= thread_new( function, [libs_str],
// [cancelstep_uint=0],
// [prio_int=0],
// [globals_tbl],
// [... args ...] )
//
// Upvalues: metatable to use for 'lane_ud'
//
LUAG_FUNC( thread_new )
{
lua_State *L2;
struct s_lane *s;
struct s_lane **ud;
const char *threadName = 0;
const char *libs= lua_tostring( L, 2 );
uint_t cs= luaG_optunsigned( L, 3,0);
int prio= (int)luaL_optinteger( L, 4,0);
uint_t glob= luaG_isany(L,5) ? 5:0;
#define FIXED_ARGS (5)
uint_t args= lua_gettop(L) - FIXED_ARGS;
if (prio < THREAD_PRIO_MIN || prio > THREAD_PRIO_MAX)
{
luaL_error( L, "Priority out of range: %d..+%d (%d)",
THREAD_PRIO_MIN, THREAD_PRIO_MAX, prio );
}
/* --- Create and prepare the sub state --- */
L2 = luaL_newstate(); // uses standard 'realloc()'-based allocator,
// sets the panic callback
if (!L2) luaL_error( L, "'luaL_newstate()' failed; out of memory" );
STACK_GROW( L,2 );
// Setting the globals table (needs to be done before loading stdlibs,
// and the lane function)
//
if (glob!=0)
{
STACK_CHECK(L)
if (!lua_istable(L,glob))
luaL_error( L, "Expected table, got %s", luaG_typename(L,glob) );
lua_pushvalue( L, glob );
lua_pushstring( L, "threadName");
lua_gettable( L, -2);
threadName = lua_tostring( L, -1);
lua_pop( L, 1);
luaG_inter_move( L,L2, 1 ); // moves the table to L2
// L2 [-1]: table of globals
// "You can change the global environment of a Lua thread using lua_replace"
// (refman-5.0.pdf p. 30)
//
lua_replace( L2, LUA_GLOBALSINDEX );
STACK_END(L,0)
}
// Selected libraries
//
if (libs)
{
const char *err= luaG_openlibs( L2, libs );
ASSERT_L( !err ); // bad libs should have been noticed by 'lanes.lua'
serialize_require( L2 );
}
// Lane main function
//
STACK_CHECK(L)
if( lua_type(L, 1) == LUA_TFUNCTION)
{
lua_pushvalue( L, 1 );
luaG_inter_move( L,L2, 1 ); // L->L2
STACK_MID(L,0)
}
else if( lua_type(L, 1) == LUA_TSTRING)
{
// compile the string
if( luaL_loadstring( L2, lua_tostring( L, 1)) != 0)
{
luaL_error( L, "error when parsing lane function code");
}
}
ASSERT_L( lua_gettop(L2) == 1 );
ASSERT_L( lua_isfunction(L2,1) );
// revive arguments
//
if (args) luaG_inter_copy( L,L2, args ); // L->L2
STACK_MID(L,0)
ASSERT_L( (uint_t)lua_gettop(L2) == 1+args );
ASSERT_L( lua_isfunction(L2,1) );
// 's' is allocated from heap, not Lua, since its life span may surpass
// the handle's (if free running thread)
//
ud= lua_newuserdata( L, sizeof(struct s_lane*) );
ASSERT_L(ud);
s= *ud= malloc( sizeof(struct s_lane) );
ASSERT_L(s);
//memset( s, 0, sizeof(struct s_lane) );
s->L= L2;
s->status= PENDING;
s->cancel_request= FALSE;
threadName = threadName ? threadName : "<unnamed thread>";
strcpy(s->threadName, threadName);
#if !( (defined PLATFORM_WIN32) || (defined PLATFORM_POCKETPC) || (defined PTHREAD_TIMEDJOIN) )
MUTEX_INIT( &s->done_lock_ );
SIGNAL_INIT( &s->done_signal_ );
#endif
s->mstatus= NORMAL;
s->selfdestruct_next= NULL;
// Set metatable for the userdata
//
lua_pushvalue( L, lua_upvalueindex(1) );
lua_setmetatable( L, -2 );
STACK_MID(L,1)
// Place 's' to registry, for 'cancel_test()' (even if 'cs'==0 we still
// do cancel tests at pending send/receive).
//
lua_pushlightuserdata( L2, CANCEL_TEST_KEY );
lua_pushlightuserdata( L2, s );
lua_rawset( L2, LUA_REGISTRYINDEX );
if (cs)
{
lua_sethook( L2, cancel_hook, LUA_MASKCOUNT, cs );
}
THREAD_CREATE( &s->thread, lane_main, s, prio );
STACK_END(L,1)
return 1;
}
//---
// = thread_gc( lane_ud )
//
// Cleanup for a thread userdata. If the thread is still executing, leave it
// alive as a free-running thread (will clean up itself).
//
// * Why NOT cancel/kill a loose thread:
//
// At least timer system uses a free-running thread, they should be handy
// and the issue of cancelling/killing threads at gc is not very nice, either
// (would easily cause waits at gc cycle, which we don't want).
//
// * Why YES kill a loose thread:
//
// Current way causes segfaults at program exit, if free-running threads are
// in certain stages. Details are not clear, but this is the core reason.
// If gc would kill threads then at process exit only one thread would remain.
//
// Todo: Maybe we should have a clear #define for selecting either behaviour.
//
LUAG_FUNC( thread_gc )
{
struct s_lane *s= lua_toLane(L,1);
// We can read 's->status' without locks, but not wait for it
//
if (s->status < DONE)
{
//
selfdestruct_add(s);
assert( s->selfdestruct_next );
return 0;
}
else if (s->mstatus==KILLED)
{
// Make sure a kill has proceeded, before cleaning up the data structure.
//
// If not doing 'THREAD_WAIT()' we should close the Lua state here
// (can it be out of order, since we killed the lane abruptly?)
//
#if 0
lua_close( s->L );
s->L = 0;
#else
DEBUGEXEC(fprintf( stderr, "** Joining with a killed thread (needs testing) **" ));
#if (defined PLATFORM_WIN32) || (defined PLATFORM_POCKETPC) || (defined PTHREAD_TIMEDJOIN)
THREAD_WAIT( &s->thread, -1 );
#else
THREAD_WAIT( &s->thread, &s->done_signal_, &s->done_lock_, &s->status, -1 );
#endif
DEBUGEXEC(fprintf( stderr, "** Joined ok **" ));
#endif
}
else if( s->L)
{
lua_close( s->L);
s->L = 0;
}
// Clean up after a (finished) thread
//
#if (! ((defined PLATFORM_WIN32) || (defined PLATFORM_POCKETPC) || (defined PTHREAD_TIMEDJOIN)))
SIGNAL_FREE( &s->done_signal_ );
MUTEX_FREE( &s->done_lock_ );
#endif
free(s);
return 0;
}
//---
// = thread_cancel( lane_ud [,timeout_secs=0.0] [,force_kill_bool=false] )
//
// The originator thread asking us specifically to cancel the other thread.
//
// 'timeout': <0: wait forever, until the lane is finished
// 0.0: just signal it to cancel, no time waited
// >0: time to wait for the lane to detect cancellation
//
// 'force_kill': if true, and lane does not detect cancellation within timeout,
// it is forcefully killed. Using this with 0.0 timeout means just kill
// (unless the lane is already finished).
//
// Returns: true if the lane was already finished (DONE/ERROR_ST/CANCELLED) or if we
// managed to cancel it.
// false if the cancellation timed out, or a kill was needed.
//
LUAG_FUNC( thread_cancel )
{
struct s_lane *s= lua_toLane(L,1);
double secs= 0.0;
uint_t force_i=2;
bool_t force, done= TRUE;
if (lua_isnumber(L,2)) {
secs= lua_tonumber(L,2);
force_i++;
} else if (lua_isnil(L,2))
force_i++;
force= lua_toboolean(L,force_i); // FALSE if nothing there
// We can read 's->status' without locks, but not wait for it (if Posix no PTHREAD_TIMEDJOIN)
//
if (s->status < DONE) {
s->cancel_request= TRUE; // it's now signalled to stop
done= thread_cancel( s, secs, force );
}
lua_pushboolean( L, done );
return 1;
}
static bool_t thread_cancel( struct s_lane *s, double secs, bool_t force )
{
bool_t done=
#if (defined PLATFORM_WIN32) || (defined PLATFORM_POCKETPC) || (defined PTHREAD_TIMEDJOIN)
THREAD_WAIT( &s->thread, secs );
#else
THREAD_WAIT( &s->thread, &s->done_signal_, &s->done_lock_, &s->status, secs );
#endif
if ((!done) && force) {
// Killing is asynchronous; we _will_ wait for it to be done at
// GC, to make sure the data structure can be released (alternative
// would be use of "cancellation cleanup handlers" that at least
// PThread seems to have).
//
THREAD_KILL( &s->thread );
s->mstatus= KILLED; // mark 'gc' to wait for it
}
return done;
}
//---
// str= thread_status( lane_ud )
//
// Returns: "pending" not started yet
// -> "running" started, doing its work..
// <-> "waiting" blocked in a receive()
// -> "done" finished, results are there
// / "error" finished at an error, error value is there
// / "cancelled" execution cancelled by M (state gone)
//
LUAG_FUNC( thread_status )
{
struct s_lane *s= lua_toLane(L,1);
enum e_status st= s->status; // read just once (volatile)
const char *str;
if (s->mstatus == KILLED)
st= CANCELLED;
str= (st==PENDING) ? "pending" :
(st==RUNNING) ? "running" : // like in 'co.status()'
(st==WAITING) ? "waiting" :
(st==DONE) ? "done" :
(st==ERROR_ST) ? "error" :
(st==CANCELLED) ? "cancelled" : NULL;
ASSERT_L(str);
lua_pushstring( L, str );
return 1;
}
//---
// [...] | [nil, err_any, stack_tbl]= thread_join( lane_ud [, wait_secs=-1] )
//
// timeout: returns nil
// done: returns return values (0..N)
// error: returns nil + error value + stack table
// cancelled: returns nil
//
LUAG_FUNC( thread_join )
{
struct s_lane *s= lua_toLane(L,1);
double wait_secs= luaL_optnumber(L,2,-1.0);
lua_State *L2= s->L;
int ret;
bool_t done=
#if (defined PLATFORM_WIN32) || (defined PLATFORM_POCKETPC) || (defined PTHREAD_TIMEDJOIN)
THREAD_WAIT( &s->thread, wait_secs );
#else
THREAD_WAIT( &s->thread, &s->done_signal_, &s->done_lock_, &s->status, wait_secs );
#endif
if (!done)
return 0; // timeout: pushes none, leaves 'L2' alive
// Thread is DONE/ERROR_ST/CANCELLED; all ours now
STACK_GROW( L, 1 );
switch( s->status ) {
case DONE: {
uint_t n= lua_gettop(L2); // whole L2 stack
luaG_inter_move( L2,L, n );
ret= n;
} break;
case ERROR_ST:
lua_pushnil(L);
luaG_inter_move( L2,L, 2 ); // error message at [-2], stack trace at [-1]
ret= 3;
break;
case CANCELLED:
ret= 0;
break;
default:
DEBUGEXEC(fprintf( stderr, "Status: %d\n", s->status ));
ASSERT_L( FALSE ); ret= 0;
}
lua_close(L2);
s->L = L2 = 0;
return ret;
}
/*---=== Timer support ===---
*/
/*
* secs= now_secs()
*
* Returns the current time, as seconds (millisecond resolution).
*/
LUAG_FUNC( now_secs )
{
lua_pushnumber( L, now_secs() );
return 1;
}
/*
* wakeup_at_secs= wakeup_conv( date_tbl )
*/
LUAG_FUNC( wakeup_conv )
{
int year, month, day, hour, min, sec, isdst;
struct tm tm= {0};
//
// .year (four digits)
// .month (1..12)
// .day (1..31)
// .hour (0..23)
// .min (0..59)
// .sec (0..61)
// .yday (day of the year)
// .isdst (daylight saving on/off)
STACK_CHECK(L)
lua_getfield( L, 1, "year" ); year= (int)lua_tointeger(L,-1); lua_pop(L,1);
lua_getfield( L, 1, "month" ); month= (int)lua_tointeger(L,-1); lua_pop(L,1);
lua_getfield( L, 1, "day" ); day= (int)lua_tointeger(L,-1); lua_pop(L,1);
lua_getfield( L, 1, "hour" ); hour= (int)lua_tointeger(L,-1); lua_pop(L,1);
lua_getfield( L, 1, "min" ); min= (int)lua_tointeger(L,-1); lua_pop(L,1);
lua_getfield( L, 1, "sec" ); sec= (int)lua_tointeger(L,-1); lua_pop(L,1);
// If Lua table has '.isdst' we trust that. If it does not, we'll let
// 'mktime' decide on whether the time is within DST or not (value -1).
//
lua_getfield( L, 1, "isdst" );
isdst= lua_isboolean(L,-1) ? lua_toboolean(L,-1) : -1;
lua_pop(L,1);
STACK_END(L,0)
tm.tm_year= year-1900;
tm.tm_mon= month-1; // 0..11
tm.tm_mday= day; // 1..31
tm.tm_hour= hour; // 0..23
tm.tm_min= min; // 0..59
tm.tm_sec= sec; // 0..60
tm.tm_isdst= isdst; // 0/1/negative
lua_pushnumber( L, (double) mktime( &tm ) ); // ms=0
return 1;
}
/*---=== Module linkage ===---
*/
#define REG_FUNC( name ) \
lua_pushcfunction( L, LG_##name ); \
lua_setglobal( L, #name )
#define REG_FUNC2( name, val ) \
lua_pushcfunction( L, val ); \
lua_setglobal( L, #name )
#define REG_STR2( name, val ) \
lua_pushstring( L, val ); \
lua_setglobal( L, #name )
#define REG_INT2( name, val ) \
lua_pushinteger( L, val ); \
lua_setglobal( L, #name )
/*
* One-time initializations
*/
static void init_once_LOCKED( lua_State *L, volatile DEEP_PRELUDE ** timer_deep_ref ) {
const char *err;
#if (defined PLATFORM_WIN32) || (defined PLATFORM_POCKETPC)
now_secs(); // initialize 'now_secs()' internal offset
#endif
#if (defined PLATFORM_OSX) && (defined _UTILBINDTHREADTOCPU)
chudInitialize();
#endif
// Locks for 'tools.c' inc/dec counters
//
MUTEX_INIT( &deep_lock );
MUTEX_INIT( &mtid_lock );
// Serialize calls to 'require' from now on, also in the primary state
//
MUTEX_RECURSIVE_INIT( &require_cs );
serialize_require( L );
// Selfdestruct chain handling
//
MUTEX_INIT( &selfdestruct_cs );
atexit( selfdestruct_atexit );
//---
// Linux needs SCHED_RR to change thread priorities, and that is only
// allowed for sudo'ers. SCHED_OTHER (default) has no priorities.
// SCHED_OTHER threads are always lower priority than SCHED_RR.
//
// ^-- those apply to 2.6 kernel. IF **wishful thinking** these
// constraints will change in the future, non-sudo priorities can
// be enabled also for Linux.
//
#ifdef PLATFORM_LINUX
sudo= geteuid()==0; // we are root?
// If lower priorities (-2..-1) are wanted, we need to lift the main
// thread to SCHED_RR and 50 (medium) level. Otherwise, we're always below
// the launched threads (even -2).
//
#ifdef LINUX_SCHED_RR
if (sudo) {
struct sched_param sp= {0}; sp.sched_priority= _PRIO_0;
PT_CALL( pthread_setschedparam( pthread_self(), SCHED_RR, &sp) );
}
#endif
#endif
err= init_keepers();
if (err) {
luaL_error( L, "Unable to initialize: %s", err );
}
// Initialize 'timer_deep'; a common Linda object shared by all states
//
ASSERT_L( timer_deep_ref && (!(*timer_deep_ref)) );
STACK_CHECK(L)
{
// proxy_ud= deep_userdata( idfunc )
//
lua_pushcfunction( L, luaG_deep_userdata );
lua_pushcfunction( L, LG_linda_id );
lua_call( L, 1 /*args*/, 1 /*retvals*/ );
ASSERT_L( lua_isuserdata(L,-1) );
// Proxy userdata contents is only a 'DEEP_PRELUDE*' pointer
//
*timer_deep_ref= * (DEEP_PRELUDE**) lua_touserdata( L, -1 );
ASSERT_L( (*timer_deep_ref) && (*timer_deep_ref)->refcount==1 && (*timer_deep_ref)->deep );
lua_pop(L,1); // we don't need the proxy
}
STACK_END(L,0)
}
int
#if (defined PLATFORM_WIN32) || (defined PLATFORM_POCKETPC)
__declspec(dllexport)
#endif
luaopen_lanes( lua_State *L ) {
// Initialized by 'init_once_LOCKED()': the deep userdata Linda object
// used for timers (each lane will get a proxy to this)
//
static volatile DEEP_PRELUDE *timer_deep; // = NULL
/*
* Making one-time initializations.
*
* When the host application is single-threaded (and all threading happens via Lanes)
* there is no problem. But if the host is multithreaded, we need to lock around the
* initializations.
*/
static volatile int /*bool*/ go_ahead; // = 0
#ifdef PLATFORM_WIN32
{
// TBD: Someone please replace this with reliable Win32 API code. Problem is,
// there's no autoinitializing locks (s.a. PTHREAD_MUTEX_INITIALIZER) in
// Windows so 'InterlockedIncrement' or something needs to be used.
// This is 99.9999% safe, though (and always safe if host is single-threaded)
// -- AKa 24-Jun-2009
//
static volatile unsigned my_number; // = 0
if (my_number++ == 0) { // almost atomic
init_once_LOCKED(L, &timer_deep);
go_ahead= 1; // let others pass
} else {
while( !go_ahead ) { Sleep(1); } // changes threads
}
}
#else
if (!go_ahead) {
static pthread_mutex_t my_lock= PTHREAD_MUTEX_INITIALIZER;
pthread_mutex_lock(&my_lock);
{
// Recheck now that we're within the lock
//
if (!go_ahead) {
init_once_LOCKED(L, &timer_deep);
go_ahead= 1;
}
}
pthread_mutex_unlock(&my_lock);
}
#endif
assert( timer_deep != 0 );
// Linda identity function
//
REG_FUNC( linda_id );
// metatable for threads
//
lua_newtable( L );
lua_pushcfunction( L, LG_thread_gc );
lua_setfield( L, -2, "__gc" );
lua_pushcclosure( L, LG_thread_new, 1 ); // metatable as closure param
lua_setglobal( L, "thread_new" );
REG_FUNC( thread_status );
REG_FUNC( thread_join );
REG_FUNC( thread_cancel );
REG_STR2( _version, VERSION );
REG_FUNC( _single );
REG_FUNC2( _deep_userdata, luaG_deep_userdata );
REG_FUNC( now_secs );
REG_FUNC( wakeup_conv );
luaG_push_proxy( L, LG_linda_id, (DEEP_PRELUDE *) timer_deep );
lua_setglobal( L, "timer_gateway" );
REG_INT2( max_prio, THREAD_PRIO_MAX );
lua_pushlightuserdata( L, CANCEL_ERROR );
lua_setglobal( L, "cancel_error" );
return 0;
}
|