/
hvm.c
12554 lines (10540 loc) · 352 KB
/
hvm.c
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
/*
* The Virtual Machine
*
* Copyright 1999 Antonio Linares <alinares@fivetech.com>
* Copyright 1999-2001 Viktor Szakats (vszakats.net/harbour)
* (hb_vmPushLongConst(), hb_vmPushDoubleConst())
* Copyright 1999 Eddie Runia <eddie@runia.com>
* (__dbgVMVarSGet(), __dbgVMVarSList())
*
* 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, 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; see the file LICENSE.txt. If not, write to
* the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301 USA (or visit https://www.gnu.org/licenses/).
*
* As a special exception, the Harbour Project gives permission for
* additional uses of the text contained in its release of Harbour.
*
* The exception is that, if you link the Harbour libraries with other
* files to produce an executable, this does not by itself cause the
* resulting executable to be covered by the GNU General Public License.
* Your use of that executable is in no way restricted on account of
* linking the Harbour library code into it.
*
* 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 by the Harbour
* Project under the name Harbour. If you copy code from other
* Harbour Project or Free Software Foundation releases into a copy of
* Harbour, 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 Harbour, it is your choice
* whether to permit this exception to apply to your modifications.
* If you do not wish that, delete this exception notice.
*
*/
#define HB_STACK_PRELOAD
#include "hbvmopt.h"
#include "hbapi.h"
#include "hbstack.h"
#include "hbapierr.h"
#include "hbapicls.h"
#include "hbapidbg.h"
#include "hbapiitm.h"
#include "hbapilng.h"
#include "hbapirdd.h"
#include "hbapigt.h"
#include "hbapicdp.h"
#include "hbvm.h"
#include "hbxvm.h"
#include "hbpcode.h"
#include "hbset.h"
#include "hbdate.h"
#include "hbmath.h"
#include "hbdebug.ch"
#if defined( HB_MT_VM )
# include "hbthread.h"
#endif /* HB_MT_VM */
#include "hbmemory.ch"
#ifndef HB_NO_PROFILER
# include <time.h>
#endif
HB_FUNC_EXTERN( SYSINIT );
HB_FUNC_EXTERN( BREAK );
/* PCode functions */
/* Operators (mathematical / character / misc) */
static void hb_vmNegate( void ); /* negates (-) the latest value on the stack */
static void hb_vmInc( PHB_ITEM pItem ); /* increment the latest numeric value on the stack */
static void hb_vmDec( PHB_ITEM pItem ); /* decrements the latest numeric value on the stack */
static void hb_vmFuncPtr( void ); /* pushes a function address pointer. Removes the symbol from the stack */
static void hb_vmAddInt( PHB_ITEM pResult, HB_LONG lAdd ); /* add integer to given item */
static void hb_vmPlus( PHB_ITEM pResult, PHB_ITEM pItem1, PHB_ITEM pItem2 ); /* sums given values */
static void hb_vmMinus( PHB_ITEM pResult, PHB_ITEM pItem1, PHB_ITEM pItem2 ); /* subtracts given values */
static void hb_vmMult( PHB_ITEM pResult, PHB_ITEM pItem1, PHB_ITEM pItem2 ); /* multiplies given values */
static void hb_vmDivide( PHB_ITEM pResult, PHB_ITEM pItem1, PHB_ITEM pItem2 ); /* divides the given values */
static void hb_vmModulus( PHB_ITEM pResult, PHB_ITEM pItem1, PHB_ITEM pItem2 ); /* calculates modulus given values */
static void hb_vmPower( PHB_ITEM pResult, PHB_ITEM pItem1, PHB_ITEM pItem2 ); /* power given values */
/* Operators (relational) */
static void hb_vmEqual( void ); /* checks if the two latest values on the stack are equal, removes both and leaves result */
static void hb_vmExactlyEqual( void ); /* checks if the two latest values on the stack are exactly equal, removes both and leaves result */
static void hb_vmNotEqual( void ); /* checks if the two latest values on the stack are not equal, removes both and leaves result */
static void hb_vmLess( void ); /* checks if the latest - 1 value is less than the latest, removes both and leaves result */
static void hb_vmLessEqual( void ); /* checks if the latest - 1 value is less than or equal the latest, removes both and leaves result */
static void hb_vmGreater( void ); /* checks if the latest - 1 value is greater than the latest, removes both and leaves result */
static void hb_vmGreaterEqual( void ); /* checks if the latest - 1 value is greater than or equal the latest, removes both and leaves result */
static void hb_vmInstring( void ); /* check whether string 1 is contained in string 2 */
static void hb_vmForTest( void ); /* test for end condition of for */
static void hb_vmSeqBlock( void ); /* set begin sequence WITH codeblock */
static void hb_vmWithObjectStart( void ); /* prepare WITH OBJECT block */
static void hb_vmEnumStart( int nVars, int nDescend ); /* prepare FOR EACH loop */
static void hb_vmEnumNext( void ); /* increment FOR EACH loop counter */
static void hb_vmEnumPrev( void ); /* decrement FOR EACH loop counter */
static void hb_vmEnumEnd( void ); /* rewind the stack after FOR EACH loop counter */
static const HB_BYTE * hb_vmSwitch( const HB_BYTE * pCode, HB_USHORT ); /* make a SWITCH statement */
/* Operators (logical) */
static void hb_vmNot( void ); /* changes the latest logical value on the stack */
static void hb_vmAnd( void ); /* performs the logical AND on the latest two values, removes them and leaves result on the stack */
static void hb_vmOr( void ); /* performs the logical OR on the latest two values, removes them and leaves result on the stack */
/* Array */
static void hb_vmArrayPush( void ); /* pushes an array element to the stack, removing the array and the index from the stack */
static void hb_vmArrayPushRef( void ); /* pushes a reference to an array element to the stack, removing the array and the index from the stack */
static void hb_vmArrayPop( void ); /* pops a value from the stack */
static void hb_vmArrayDim( HB_USHORT uiDimensions ); /* generates an uiDimensions Array and initialize those dimensions from the stack values */
static void hb_vmArrayGen( HB_SIZE nElements ); /* generates an nElements Array and fills it from the stack values */
static void hb_vmHashGen( HB_SIZE nElements ); /* generates an nElements Hash and fills it from the stack values */
/* macros */
static void hb_vmMacroDo( HB_USHORT uiArgSets ); /* execute function passing arguments set on HVM stack func( &var ) */
static void hb_vmMacroFunc( HB_USHORT uiArgSets ); /* execute procedure passing arguments set on HVM stack func( &var ) */
static void hb_vmMacroSend( HB_USHORT uiArgSets ); /* execute procedure passing arguments set on HVM stack func( &var ) */
static void hb_vmMacroArrayGen( HB_USHORT uiArgSets ); /* generate array from arguments set on HVM stack { &var } */
static void hb_vmMacroPushIndex( void ); /* push macro array index {...}[ &var ] */
/* Database */
static HB_ERRCODE hb_vmSelectWorkarea( PHB_ITEM, PHB_SYMB ); /* select the workarea using a given item or a substituted value */
static void hb_vmSwapAlias( void ); /* swaps items on the eval stack and pops the workarea number */
/* Execution */
static HARBOUR hb_vmDoBlock( void ); /* executes a codeblock */
static void hb_vmFrame( HB_USHORT usLocals, unsigned char ucParams ); /* increases the stack pointer for the amount of locals and params supplied */
static void hb_vmVFrame( HB_USHORT usLocals, unsigned char ucParams ); /* increases the stack pointer for the amount of locals and variable number of params supplied */
static void hb_vmSFrame( PHB_SYMB pSym ); /* sets the statics frame for a function */
static void hb_vmStatics( PHB_SYMB pSym, HB_USHORT uiStatics ); /* increases the global statics array to hold a PRG statics */
static void hb_vmInitThreadStatics( HB_USHORT uiCount, const HB_BYTE * pCode ); /* mark thread static variables */
static void hb_vmStaticsClear( void ); /* clear complex static variables */
static void hb_vmStaticsRelease( void ); /* release arrays with static variables */
/* Push */
static void hb_vmPushAlias( void ); /* pushes the current workarea number */
static void hb_vmPushAliasedField( PHB_SYMB ); /* pushes an aliased field on the eval stack */
static void hb_vmPushAliasedVar( PHB_SYMB ); /* pushes an aliased variable on the eval stack */
static void hb_vmPushBlock( const HB_BYTE * pCode, PHB_SYMB pSymbols, HB_SIZE nLen ); /* creates a codeblock */
static void hb_vmPushBlockShort( const HB_BYTE * pCode, PHB_SYMB pSymbols, HB_SIZE nLen ); /* creates a codeblock */
static void hb_vmPushMacroBlock( const HB_BYTE * pCode, HB_SIZE nSize, HB_USHORT usParams ); /* creates a macro-compiled codeblock */
static void hb_vmPushDoubleConst( double dNumber, int iWidth, int iDec ); /* Pushes a double constant (pcode) */
static void hb_vmPushLocal( int iLocal ); /* pushes the content of a local onto the stack */
static void hb_vmPushLocalByRef( int iLocal ); /* pushes a local by reference onto the stack */
static void hb_vmPushHBLong( HB_MAXINT nNumber ); /* pushes a HB_MAXINT number onto the stack */
#if ! defined( HB_LONG_LONG_OFF )
static void hb_vmPushLongLongConst( HB_LONGLONG lNumber ); /* Pushes a long long constant (pcode) */
#endif
#if HB_VMINT_MAX >= INT32_MAX
static void hb_vmPushIntegerConst( int iNumber ); /* Pushes a int constant (pcode) */
#else
static void hb_vmPushLongConst( long lNumber ); /* Pushes a long constant (pcode) */
#endif
static void hb_vmPushStatic( HB_USHORT uiStatic ); /* pushes the content of a static onto the stack */
static void hb_vmPushStaticByRef( HB_USHORT uiStatic ); /* pushes a static by reference onto the stack */
static void hb_vmPushVariable( PHB_SYMB pVarSymb ); /* pushes undeclared variable */
static void hb_vmPushObjectVarRef( void ); /* pushes reference to object variable */
static void hb_vmPushVParams( void ); /* pushes variable parameters */
static void hb_vmPushAParams( void ); /* pushes array items */
static void hb_vmPushUnRef( void ); /* push the unreferenced latest value on the stack */
static void hb_vmDuplicate( void ); /* duplicates the latest value on the stack */
static void hb_vmDuplUnRef( void ); /* duplicates the latest value on the stack and unref the source one */
static void hb_vmSwap( int iCount ); /* swap bCount+1 time two items on HVM stack starting from the most top one */
/* Pop */
static HB_BOOL hb_vmPopLogical( void ); /* pops the stack latest value and returns its logical value */
static void hb_vmPopAlias( void ); /* pops the workarea number form the eval stack */
static void hb_vmPopAliasedField( PHB_SYMB ); /* pops an aliased field from the eval stack*/
static void hb_vmPopAliasedVar( PHB_SYMB ); /* pops an aliased variable from the eval stack*/
static void hb_vmPopLocal( int iLocal ); /* pops the stack latest value onto a local */
static void hb_vmPopStatic( HB_USHORT uiStatic ); /* pops the stack latest value onto a static */
/* misc */
static void hb_vmDoInitStatics( void ); /* executes all _INITSTATICS functions */
static void hb_vmDoInitFunctions( HB_BOOL ); /* executes all defined PRGs INIT functions */
static void hb_vmDoExitFunctions( void ); /* executes all defined PRGs EXIT functions */
static void hb_vmReleaseLocalSymbols( void ); /* releases the memory of the local symbols linked list */
static void hb_vmMsgIndexReference( PHB_ITEM pRefer, PHB_ITEM pObject, PHB_ITEM pIndex ); /* create object index reference */
#ifndef HB_NO_DEBUG
static void hb_vmLocalName( HB_USHORT uiLocal, const char * szLocalName ); /* locals and parameters index and name information for the debugger */
static void hb_vmStaticName( HB_BYTE bIsGlobal, HB_USHORT uiStatic, const char * szStaticName ); /* statics vars information for the debugger */
static void hb_vmModuleName( const char * szModuleName ); /* PRG and function name information for the debugger */
static void hb_vmDebugEntry( int nMode, int nLine, const char * szName, int nIndex, PHB_ITEM pFrame );
static void hb_vmDebuggerExit( HB_BOOL fRemove ); /* shuts down the debugger */
static void hb_vmDebuggerShowLine( HB_USHORT uiLine ); /* makes the debugger shows a specific source code line */
static void hb_vmDebuggerEndProc( void ); /* notifies the debugger for an endproc */
static PHB_DYNS s_pDynsDbgEntry = NULL; /* Cached __DBGENTRY symbol */
static HB_DBGENTRY_FUNC s_pFunDbgEntry; /* C level debugger entry */
#endif
static HB_BOOL s_fInternalsEnabled = HB_TRUE;
#if defined( HB_MT_VM )
static int volatile hb_vmThreadRequest = 0;
static void hb_vmRequestTest( void );
static PHB_ITEM s_pSymbolsMtx = NULL;
static HB_CRITICAL_NEW( s_atInitMtx );
# define HB_ATINIT_LOCK() hb_threadEnterCriticalSection( &s_atInitMtx )
# define HB_ATINIT_UNLOCK() hb_threadLeaveCriticalSection( &s_atInitMtx )
# define HB_TASK_SHEDULER() HB_THREAD_SHEDULER()
#else
# define HB_ATINIT_LOCK()
# define HB_ATINIT_UNLOCK()
# define HB_TASK_SHEDULER()
#endif /* HB_MT_VM */
#ifndef HB_NO_PROFILER
static HB_ULONG hb_ulOpcodesCalls[ HB_P_LAST_PCODE ]; /* array to profile opcodes calls */
static HB_ULONG hb_ulOpcodesTime[ HB_P_LAST_PCODE ]; /* array to profile opcodes consumed time */
static HB_BOOL hb_bProfiler = HB_FALSE; /* profiler status is off */
#endif
#if defined( HB_PRG_TRACE )
static HB_BOOL hb_bTracePrgCalls = HB_FALSE; /* prg tracing is off */
# define HB_TRACE_PRG( _TRMSG_ ) if( hb_bTracePrgCalls ) HB_TRACE( HB_TR_ALWAYS, _TRMSG_ )
#else
# define HB_TRACE_PRG( _TRMSG_ )
#endif
static const char * s_vm_pszLinkedMain = NULL; /* name of startup function set by linker */
/* virtual machine state */
HB_SYMB hb_symEval = { "EVAL", { HB_FS_PUBLIC }, { hb_vmDoBlock }, NULL }; /* symbol to evaluate codeblocks */
static HB_SYMB s_symBreak = { "BREAK", { HB_FS_PUBLIC }, { HB_FUNCNAME( BREAK ) }, NULL }; /* symbol to generate break */
static PHB_ITEM s_breakBlock = NULL;
static HB_BOOL s_fHVMActive = HB_FALSE; /* is HVM ready for PCODE executing */
static HB_BOOL s_fDoExitProc = HB_TRUE; /* execute EXIT procedures */
static int s_nErrorLevel = 0; /* application exit status */
static PHB_SYMB s_pSymStart = NULL; /* start symbol of the application. MAIN() is not required */
static PHB_SYMBOLS s_pSymbols = NULL; /* to hold a linked list of all different modules symbol tables */
static HB_ULONG s_ulFreeSymbols = 0;/* number of free module symbols */
static void * s_hDynLibID = NULL; /* unique identifier to mark symbol tables loaded from dynamic libraries */
static HB_BOOL s_fCloneSym = HB_FALSE;/* clone registered symbol tables */
/* main VM thread stack ID */
static void * s_main_thread = NULL;
/* SEQUENCE envelope items position from stack top active
*/
#define HB_RECOVER_STATE -1
#define HB_RECOVER_VALUE -2
#define HB_SEQ_CANRECOVER 64
#define HB_SEQ_DOALWAYS 128
static PHB_FUNC_LIST s_InitFunctions = NULL;
static PHB_FUNC_LIST s_ExitFunctions = NULL;
static PHB_FUNC_LIST s_QuitFunctions = NULL;
static PHB_ITEM hb_breakBlock( void )
{
if( s_breakBlock == NULL )
{
static const HB_BYTE s_pCode[ 8 ] = {
HB_P_PUSHFUNCSYM, 0, 0, /* BREAK */
HB_P_PUSHLOCALNEAR, 1, /* oErr */
HB_P_FUNCTIONSHORT, 1,
HB_P_ENDBLOCK };
s_breakBlock = hb_itemNew( NULL );
s_breakBlock->item.asBlock.value =
hb_codeblockNew( s_pCode, /* pcode buffer */
0, /* number of referenced local variables */
NULL, /* table with referenced local variables */
&s_symBreak,
sizeof( s_pCode ) );
s_breakBlock->type = HB_IT_BLOCK;
s_breakBlock->item.asBlock.paramcnt = 1;
s_breakBlock->item.asBlock.lineno = 0;
s_breakBlock->item.asBlock.hclass = 0;
s_breakBlock->item.asBlock.method = 0;
}
return s_breakBlock;
}
static void hb_breakBlockRelease( void )
{
if( s_breakBlock != NULL )
{
hb_itemRelease( s_breakBlock );
s_breakBlock = NULL;
}
}
static void hb_vmAddModuleFunction( PHB_FUNC_LIST * pLstPtr, HB_INIT_FUNC pFunc, void * cargo )
{
PHB_FUNC_LIST pLst = ( PHB_FUNC_LIST ) hb_xgrab( sizeof( HB_FUNC_LIST ) );
pLst->pFunc = pFunc;
pLst->cargo = cargo;
pLst->hDynLib = s_hDynLibID;
HB_ATINIT_LOCK();
pLst->pNext = *pLstPtr;
*pLstPtr = pLst;
HB_ATINIT_UNLOCK();
}
static void hb_vmDoModuleFunctions( PHB_FUNC_LIST * pLstPtr )
{
while( *pLstPtr )
{
PHB_FUNC_LIST pLst = *pLstPtr;
*pLstPtr = pLst->pNext;
pLst->pFunc( pLst->cargo );
hb_xfree( pLst );
}
}
static void hb_vmDoModuleLibFunctions( PHB_FUNC_LIST * pLstPtr, void * hDynLib )
{
while( *pLstPtr )
{
PHB_FUNC_LIST pLst = *pLstPtr;
if( pLst->hDynLib == hDynLib )
{
*pLstPtr = pLst->pNext;
pLst->pFunc( pLst->cargo );
hb_xfree( pLst );
}
else
pLstPtr = &pLst->pNext;
}
}
static void hb_vmDoModuleSetLibID( PHB_FUNC_LIST pLst, void * hDynLib, void * hNewDynLib )
{
while( pLst )
{
if( pLst->hDynLib == hDynLib )
pLst->hDynLib = hNewDynLib;
pLst = pLst->pNext;
}
}
static void hb_vmCleanModuleFunctions( void )
{
PHB_FUNC_LIST pLst;
while( s_InitFunctions )
{
pLst = s_InitFunctions;
s_InitFunctions = pLst->pNext;
hb_xfree( pLst );
}
while( s_ExitFunctions )
{
pLst = s_ExitFunctions;
s_ExitFunctions = pLst->pNext;
hb_xfree( pLst );
}
while( s_QuitFunctions )
{
pLst = s_QuitFunctions;
s_QuitFunctions = pLst->pNext;
hb_xfree( pLst );
}
}
void hb_vmAtInit( HB_INIT_FUNC pFunc, void * cargo )
{
hb_vmAddModuleFunction( &s_InitFunctions, pFunc, cargo );
}
void hb_vmAtExit( HB_INIT_FUNC pFunc, void * cargo )
{
hb_vmAddModuleFunction( &s_ExitFunctions, pFunc, cargo );
}
void hb_vmAtQuit( HB_INIT_FUNC pFunc, void * cargo )
{
hb_vmAddModuleFunction( &s_QuitFunctions, pFunc, cargo );
}
static void hb_vmDoModuleInitFunctions( void )
{
hb_vmDoModuleFunctions( &s_InitFunctions );
}
static void hb_vmDoModuleExitFunctions( void )
{
hb_vmDoModuleFunctions( &s_ExitFunctions );
}
static void hb_vmDoModuleQuitFunctions( void )
{
hb_vmDoModuleFunctions( &s_QuitFunctions );
}
/* call __HBVMINIT() function to initialize GetList public variable
* and set ErrorBlock() by ErrorSys() function
*/
static void hb_vmDoInitHVM( void )
{
PHB_DYNS pDynSym = hb_dynsymFind( "__HBVMINIT" );
if( pDynSym && pDynSym->pSymbol->value.pFunPtr )
{
hb_vmPushSymbol( pDynSym->pSymbol );
hb_vmPushNil();
hb_vmProc( 0 );
}
}
/* call __SetHelpK() if HELP() function is linked */
static void hb_vmDoInitHelp( void )
{
PHB_DYNS pDynSym = hb_dynsymFind( "HELP" );
if( pDynSym && pDynSym->pSymbol->value.pFunPtr )
{
pDynSym = hb_dynsymFind( "__SETHELPK" );
if( pDynSym && pDynSym->pSymbol->value.pFunPtr )
{
hb_vmPushSymbol( pDynSym->pSymbol );
hb_vmPushNil();
hb_vmProc( 0 );
}
}
}
#if ! defined( HB_MT_VM )
HB_BOOL hb_vmIsMt( void ) { return HB_FALSE; }
HB_BOOL hb_vmThreadIsMain( void * Cargo ) { HB_SYMBOL_UNUSED( Cargo ); return s_fHVMActive; }
void hb_vmLock( void ) {}
void hb_vmUnlock( void ) {}
void hb_vmLockForce( void ) {}
HB_BOOL hb_vmSuspendThreads( HB_BOOL fWait ) { HB_SYMBOL_UNUSED( fWait ); return HB_TRUE; }
void hb_vmResumeThreads( void ) {}
#if 0
HB_BOOL hb_vmThreadRegister( void * Cargo ) { HB_SYMBOL_UNUSED( Cargo ); return HB_FALSE; }
void hb_vmThreadRelease( void * Cargo )
{
PHB_THREADSTATE pState = ( PHB_THREADSTATE ) Cargo;
PHB_ITEM pThItm = pState->pThItm;
pState->pThItm = NULL;
if( pThItm )
hb_itemRelease( pThItm );
}
#endif
#else
static HB_CRITICAL_NEW( s_vmMtx );
static HB_COND_NEW( s_vmCond );
/* number of allocated HVM stacks */
static int volatile s_iStackCount = 0;
/* number of running HVM threads */
static int volatile s_iRunningCount = 0;
/* active HVM stacks list */
static PHB_THREADSTATE s_vmStackLst = NULL;
/* thread number */
static HB_THREAD_NO s_threadNo = 0;
# define HB_THREQUEST_STOP 1
# define HB_THREQUEST_QUIT 2
# define HB_VM_LOCK() hb_threadEnterCriticalSection( &s_vmMtx )
# define HB_VM_UNLOCK() hb_threadLeaveCriticalSection( &s_vmMtx )
HB_BOOL hb_vmIsMt( void ) { return HB_TRUE; }
static void hb_vmRequestTest( void )
{
HB_VM_LOCK();
s_iRunningCount--;
for( ;; )
{
if( hb_vmThreadRequest & HB_THREQUEST_QUIT )
{
HB_STACK_TLS_PRELOAD
if( ! hb_stackQuitState() )
{
hb_stackSetQuitState( HB_TRUE );
hb_stackSetActionRequest( HB_QUIT_REQUESTED );
}
}
if( hb_vmThreadRequest & HB_THREQUEST_STOP )
{
hb_threadCondBroadcast( &s_vmCond );
hb_threadCondWait( &s_vmCond, &s_vmMtx );
}
else
break;
}
s_iRunningCount++;
HB_VM_UNLOCK();
}
/* unlock VM, allow GC and other exclusive single task code execution */
void hb_vmUnlock( void )
{
if( s_fHVMActive )
{
HB_STACK_TLS_PRELOAD
if( hb_stackId() ) /* check if thread has associated HVM stack */
{
if( hb_stackUnlock() == 1 )
{
HB_VM_LOCK();
s_iRunningCount--;
if( hb_vmThreadRequest )
{
if( hb_vmThreadRequest & HB_THREQUEST_QUIT )
{
if( ! hb_stackQuitState() )
{
hb_stackSetQuitState( HB_TRUE );
hb_stackSetActionRequest( HB_QUIT_REQUESTED );
}
}
hb_threadCondBroadcast( &s_vmCond );
}
HB_VM_UNLOCK();
}
}
HB_TASK_SHEDULER();
}
}
/* lock VM blocking GC and other exclusive single task code execution */
void hb_vmLock( void )
{
if( s_fHVMActive )
{
HB_STACK_TLS_PRELOAD
if( hb_stackId() ) /* check if thread has associated HVM stack */
{
if( hb_stackLock() == 0 )
{
HB_VM_LOCK();
for( ;; )
{
if( hb_vmThreadRequest & HB_THREQUEST_QUIT )
{
if( ! hb_stackQuitState() )
{
hb_stackSetQuitState( HB_TRUE );
hb_stackSetActionRequest( HB_QUIT_REQUESTED );
}
}
if( hb_vmThreadRequest & HB_THREQUEST_STOP )
hb_threadCondWait( &s_vmCond, &s_vmMtx );
else
break;
}
s_iRunningCount++;
HB_VM_UNLOCK();
}
}
}
}
void hb_vmLockForce( void )
{
if( s_fHVMActive )
{
HB_STACK_TLS_PRELOAD
if( hb_stackId() ) /* check if thread has associated HVM stack */
{
if( hb_stackLock() == 0 )
{
HB_VM_LOCK();
if( hb_vmThreadRequest & HB_THREQUEST_QUIT )
{
if( ! hb_stackQuitState() )
{
hb_stackSetQuitState( HB_TRUE );
hb_stackSetActionRequest( HB_QUIT_REQUESTED );
}
}
s_iRunningCount++;
HB_VM_UNLOCK();
}
}
}
}
/* (try to) stop all threads except current one */
HB_BOOL hb_vmSuspendThreads( HB_BOOL fWait )
{
HB_VM_LOCK();
if( ( hb_vmThreadRequest & ( HB_THREQUEST_STOP | HB_THREQUEST_QUIT ) ) == 0 )
{
hb_vmThreadRequest |= HB_THREQUEST_STOP;
--s_iRunningCount;
for( ;; )
{
if( s_iRunningCount <= 0 )
{
++s_iRunningCount;
return HB_TRUE;
}
if( ! fWait )
break;
hb_threadCondWait( &s_vmCond, &s_vmMtx );
if( hb_vmThreadRequest & HB_THREQUEST_QUIT )
break;
}
++s_iRunningCount;
hb_vmThreadRequest &= ~HB_THREQUEST_STOP;
hb_threadCondBroadcast( &s_vmCond );
}
HB_VM_UNLOCK();
return HB_FALSE;
}
/* unblock execution of threads stopped by hb_vmSuspendThreads() */
void hb_vmResumeThreads( void )
{
hb_vmThreadRequest &= ~HB_THREQUEST_STOP;
hb_threadCondBroadcast( &s_vmCond );
HB_VM_UNLOCK();
}
/* send QUIT request to all threads except current one
* and wait for their termination,
* should be called only by main HVM thread
*/
void hb_vmTerminateThreads( void )
{
HB_STACK_TLS_PRELOAD
if( s_main_thread == hb_stackId() )
{
HB_VM_LOCK();
hb_vmThreadRequest |= HB_THREQUEST_QUIT;
--s_iRunningCount;
hb_threadMutexUnlockAll();
hb_threadMutexUnsubscribeAll();
hb_threadCondBroadcast( &s_vmCond );
while( s_iStackCount > 1 )
hb_threadCondWait( &s_vmCond, &s_vmMtx );
++s_iRunningCount;
#if 0
hb_vmThreadRequest &= ~HB_THREQUEST_QUIT;
#endif
hb_vmThreadRequest = 0;
HB_VM_UNLOCK();
}
}
/* wait for all threads to terminate
* should be called only by main HVM thread
*/
void hb_vmWaitForThreads( void )
{
HB_STACK_TLS_PRELOAD
if( s_main_thread == hb_stackId() )
{
HB_VM_LOCK();
--s_iRunningCount;
if( hb_vmThreadRequest )
hb_threadCondBroadcast( &s_vmCond );
while( s_iStackCount > 1 )
hb_threadCondWait( &s_vmCond, &s_vmMtx );
++s_iRunningCount;
HB_VM_UNLOCK();
}
}
void * hb_vmThreadState( void )
{
HB_STACK_TLS_PRELOAD
HB_TRACE( HB_TR_DEBUG, ( "hb_vmThreadState()" ) );
return hb_stackId() ? hb_stackList() : NULL;
}
HB_BOOL hb_vmThreadIsMain( void * Cargo )
{
HB_TRACE( HB_TR_DEBUG, ( "hb_vmThreadIsMain(%p)", Cargo ) );
if( ! s_fHVMActive || s_main_thread == NULL )
return HB_FALSE;
else if( Cargo )
return s_main_thread == ( ( PHB_THREADSTATE ) Cargo )->pStackId;
else
{
HB_STACK_TLS_PRELOAD
return s_main_thread == hb_stackId();
}
}
static void hb_vmStackAdd( PHB_THREADSTATE pState )
{
HB_TRACE( HB_TR_DEBUG, ( "hb_vmStackAdd(%p)", ( void * ) pState ) );
if( ! pState->pPrev )
{
if( s_vmStackLst )
{
pState->pNext = s_vmStackLst;
pState->pPrev = s_vmStackLst->pPrev;
pState->pPrev->pNext = pState;
s_vmStackLst->pPrev = pState;
}
else
{
s_vmStackLst = pState->pNext = pState->pPrev = pState;
}
s_iStackCount++;
}
if( pState->th_no == 0 )
pState->th_no = ++s_threadNo;
}
static PHB_ITEM hb_vmStackDel( PHB_THREADSTATE pState, HB_BOOL fCounter )
{
PHB_ITEM pThItm;
HB_TRACE( HB_TR_DEBUG, ( "hb_vmStackDel(%p,%d)", ( void * ) pState, ( int ) fCounter ) );
pState->fActive = HB_FALSE;
pState->pStackId = NULL;
pState->fFinished = HB_TRUE;
if( pState->pPrev )
{
pState->pPrev->pNext = pState->pNext;
pState->pNext->pPrev = pState->pPrev;
if( s_vmStackLst == pState )
{
s_vmStackLst = pState->pNext;
if( s_vmStackLst == pState )
s_vmStackLst = NULL;
}
pState->pPrev = pState->pNext = NULL;
if( fCounter )
s_iStackCount--;
}
/* NOTE: releasing pThItm may force pState freeing if parent
* thread does not keep thread pointer item. So it's
* important to not access it later. [druzus]
*/
pThItm = pState->pThItm;
pState->pThItm = NULL;
return pThItm;
}
static void hb_vmStackInit( PHB_THREADSTATE pState )
{
HB_TRACE( HB_TR_DEBUG, ( "hb_vmStackInit(%p)", ( void * ) pState ) );
hb_stackInit(); /* initialize HVM thread stack */
HB_VM_LOCK();
{
HB_STACK_TLS_PRELOAD
hb_stackUnlock();
pState->pStackId = hb_stackId();
hb_stackListSet( ( void * ) ( pState ) );
pState->fActive = HB_TRUE;
hb_vmStackAdd( pState );
}
HB_VM_UNLOCK();
}
static void hb_vmStackRelease( void )
{
HB_STACK_TLS_PRELOAD
HB_BOOL fLocked;
PHB_ITEM pThItm;
HB_TRACE( HB_TR_DEBUG, ( "hb_vmStackRelease()" ) );
HB_VM_LOCK();
fLocked = hb_stackUnlock() == 1;
pThItm = hb_vmStackDel( ( PHB_THREADSTATE ) hb_stackList(), HB_FALSE );
HB_VM_UNLOCK();
/* NOTE: releasing pThItm may force pState freeing if parent
* thread does not keep thread pointer item. So it's
* important to not access it later. [druzus]
*/
if( pThItm )
hb_itemRelease( pThItm );
hb_setRelease( hb_stackSetStruct() );
hb_stackFree();
hb_threadMutexUnlockAll();
HB_VM_LOCK();
if( fLocked )
s_iRunningCount--;
s_iStackCount--;
hb_threadCondBroadcast( &s_vmCond );
HB_VM_UNLOCK();
}
HB_BOOL hb_vmThreadRegister( void * Cargo )
{
HB_TRACE( HB_TR_DEBUG, ( "hb_vmThreadRegister(%p)", Cargo ) );
HB_VM_LOCK();
hb_vmStackAdd( ( PHB_THREADSTATE ) Cargo );
HB_VM_UNLOCK();
return HB_TRUE;
}
void hb_vmThreadRelease( void * Cargo )
{
PHB_ITEM pThItm;
HB_TRACE( HB_TR_DEBUG, ( "hb_vmThreadRelease(%p)", Cargo ) );
HB_VM_LOCK();
pThItm = hb_vmStackDel( ( PHB_THREADSTATE ) Cargo, HB_TRUE );
hb_threadCondBroadcast( &s_vmCond );
HB_VM_UNLOCK();
if( pThItm )
hb_itemRelease( pThItm );
}
/* thread entry point */
void hb_vmThreadInit( void * Cargo )
{
PHB_THREADSTATE pState;
HB_TRACE( HB_TR_DEBUG, ( "hb_vmThreadInit(%p)", Cargo ) );
pState = ( PHB_THREADSTATE ) Cargo;
if( ! pState )
pState = hb_threadStateNew();
hb_vmStackInit( pState ); /* initialize HVM thread stack */
hb_vmLock();
{
HB_STACK_TLS_PRELOAD
hb_cdpSelectID( pState->pszCDP );
hb_langSelectID( pState->pszLang );
hb_vmSetI18N( pState->pI18N );
pState->pI18N = NULL;
if( pState->pSet )
{
memcpy( hb_stackSetStruct(), pState->pSet, sizeof( HB_SET_STRUCT ) );
hb_xfree( pState->pSet );
pState->pSet = NULL;
}
else
hb_setInitialize( hb_stackSetStruct() );
hb_gtAttach( pState->hGT );
pState->hGT = NULL;
if( pState->pszDefRDD )
hb_stackRDD()->szDefaultRDD = pState->pszDefRDD;
if( s_fHVMActive )
{
/* call __HBVMINIT() function to initialize GetList public variable
* and set ErrorBlock() by ErrorSys() function
*/
hb_vmDoInitHVM();
}
if( pState->pMemvars )
{
hb_memvarRestoreFromArray( pState->pMemvars );
hb_itemRelease( pState->pMemvars );
pState->pMemvars = NULL;
}
}
}
/* thread leave point */
void hb_vmThreadQuit( void )
{
HB_STACK_TLS_PRELOAD
PHB_THREADSTATE pState;
HB_TRACE( HB_TR_DEBUG, ( "hb_vmThreadQuit()" ) );
hb_stackSetQuitState( HB_TRUE );
hb_stackSetActionRequest( 0 );
pState = ( PHB_THREADSTATE ) hb_stackList();
{
PHB_ITEM pReturn = hb_stackReturnItem();
if( HB_IS_BYREF( pReturn ) )
pReturn = hb_itemUnRef( pReturn );
if( ! pState->pResult )
{
pState->pResult = hb_itemNew( pReturn );
hb_gcUnlock( pState->pResult );
}
else
hb_itemCopy( pState->pResult, pReturn );
}
hb_itemClear( hb_stackReturnItem() );
hb_stackSetActionRequest( 0 );
hb_rddCloseAll(); /* close all workareas */
hb_stackRemove( 1 ); /* clear stack items, leave only initial symbol item */
hb_memvarsClear( HB_TRUE ); /* clear all PUBLIC (and PRIVATE if any) variables */
hb_vmSetI18N( NULL ); /* remove i18n translation table */
#ifndef HB_NO_DEBUG
hb_vmDebuggerExit( HB_FALSE ); /* deactivate debugger */
#endif
hb_gtRelease( NULL );
hb_vmStackRelease(); /* release HVM stack and remove it from linked HVM stacks list */
}
/* send QUIT request to given thread */
void hb_vmThreadQuitRequest( void * Cargo )
{
PHB_THREADSTATE pState;
HB_TRACE( HB_TR_DEBUG, ( "hb_vmThreadQuitRequest(%p)", Cargo ) );
pState = ( PHB_THREADSTATE ) Cargo;
HB_VM_LOCK();
if( pState->pStackId && pState->fActive )
hb_stackIdSetActionRequest( pState->pStackId, HB_QUIT_REQUESTED );
HB_VM_UNLOCK();
}
#endif /* HB_MT_VM */
PHB_ITEM hb_vmThreadStart( HB_ULONG ulAttr, PHB_CARGO_FUNC pFunc, void * cargo )
{
HB_TRACE( HB_TR_DEBUG, ( "hb_vmThreadStart(%lu,%p,%p)", ulAttr, ( void * ) pFunc, cargo ) );
#if defined( HB_MT_VM )
return hb_threadStart( ulAttr, pFunc, cargo );
#else
HB_SYMBOL_UNUSED( ulAttr );
HB_SYMBOL_UNUSED( pFunc );
HB_SYMBOL_UNUSED( cargo );
return NULL;
#endif /* HB_MT_VM */