/
extras.c
1449 lines (1123 loc) · 35.8 KB
/
extras.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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <errno.h>
#include "ficl.h"
#ifdef WIN32
#include <io.h>
#define getcwd _getcwd
#include "dlfcn.h"
#else
#include <dlfcn.h>
#endif
#include "curterm.h"
#include "bswap.h"
#if defined(_MSC_VER)
#define __sync_synchronize _ReadWriteBarrier
#if FICL_ALIGNMENT - 4
#define __sync_bool_compare_and_swap _InterlockedCompareExchange64
#define __sync_val_compare_and_swap _InterlockedCompareExchange64
#define __sync_lock_test_and_set _interlockedbittestandset64
#else
#define __sync_bool_compare_and_swap _InterlockedCompareExchange
#define __sync_val_compare_and_swap _InterlockedCompareExchange
#define __sync_lock_test_and_set _interlockedbittestandset
#endif
#endif
#if !defined(FICL_ANSI) || defined(__MINGW32__)
/*
** Ficl interface to _getcwd (Win32)
** Prints the current working directory using the VM's
** textOut method...
*/
static void ficlPrimitiveGetCwd(ficlVm *vm)
{
char *directory;
directory = getcwd(NULL, 80);
ficlVmTextOut(vm, directory);
ficlVmTextOut(vm, "\n");
free(directory);
return;
}
/*
** Ficl interface to _chdir (Win32)
** Gets a newline (or NULL) delimited string from the input
** and feeds it to the Win32 chdir function...
** Example:
** cd c:\tmp
*/
static void ficlPrimitiveChDir(ficlVm *vm)
{
ficlCountedString *counted = (ficlCountedString *)vm->Pad;
ficlVmGetString(vm, counted, '\n');
if (counted->length > 0)
{
int err = chdir(counted->text);
if (err)
{
ficlVmTextOut(vm, "Error: path not found\n");
ficlVmThrow(vm, FICL_VM_STATUS_QUIT);
}
}
else
{
ficlVmTextOut(vm, "Warning (chdir): nothing happened\n");
}
return;
}
/* : CLOCK ( -- u ) */
static void ficlPrimitiveClock(ficlVm *vm)
{
clock_t now = clock();
ficlStackPushUnsigned(vm->dataStack, (ficlUnsigned)now);
return;
}
/* : GET-MSECS ( -- u ) */
static void ficlPrimitiveGetMSecs(ficlVm *vm)
{
clock_t now = clock();
ficlStackPushUnsigned(
vm->dataStack,
(ficlUnsigned)(1000 * ((double)now / CLOCKS_PER_SEC))
);
return;
}
#endif /* FICL_ANSI */
/*
** Ficl interface to system (ANSI)
** Gets a newline (or NULL) delimited string from the input
** and feeds it to the ANSI system function...
** Example:
** system del *.*
** \ ouch!
*/
static void ficlPrimitiveSystem(ficlVm *vm)
{
ficlCountedString *counted = (ficlCountedString *)vm->Pad;
ficlVmGetString(vm, counted, '\n');
if (FICL_COUNTED_STRING_GET_LENGTH(*counted) > 0)
{
int returnValue = system(FICL_COUNTED_STRING_GET_POINTER(*counted));
if (returnValue)
{
sprintf(vm->Pad, "System call returned %d\n", returnValue);
ficlVmTextOut(vm, vm->Pad);
ficlVmThrow(vm, FICL_VM_STATUS_QUIT);
}
}
else
{
ficlVmTextOut(vm, "Warning (system): nothing happened\n");
}
return;
}
/*
** Ficl add-in to load a text file and execute it...
** Cheesy, but illustrative.
** Line oriented... filename is newline (or NULL) delimited.
** Example:
** load test.f
*/
static void ficlPrimitiveLoad(ficlVm *vm)
{
char buffer[LINE_BUFFER_SIZE];
char filename[LINE_BUFFER_SIZE];
ficlCountedString *counted = (ficlCountedString *)filename;
int line = 0;
FILE *f;
int result = 0;
ficlCell oldSourceId;
ficlString s;
ficlVmGetString(vm, counted, '\n');
if (FICL_COUNTED_STRING_GET_LENGTH(*counted) <= 0)
{
ficlVmTextOut(vm, "Warning (load): nothing happened\n");
return;
}
/*
** get the file's size and make sure it exists
*/
f = fopen(FICL_COUNTED_STRING_GET_POINTER(*counted), "r");
if (!f)
{
ficlVmTextOut(vm, "Unable to open file ");
ficlVmTextOut(vm, FICL_COUNTED_STRING_GET_POINTER(*counted));
ficlVmTextOut(vm, "\n");
ficlVmThrow(vm, FICL_VM_STATUS_QUIT);
}
oldSourceId = vm->sourceId;
vm->sourceId.p = (void *)f;
/* feed each line to ficlExec */
while (fgets(buffer, sizeof(buffer), f))
{
int length = strlen(buffer) - 1;
line++;
if (length <= 0)
continue;
if (buffer[length] == '\n')
buffer[length--] = '\0';
else
ficlVmThrowError(vm, "Error: file <%s> line %d is too long", FICL_COUNTED_STRING_GET_POINTER(*counted), line);
FICL_STRING_SET_POINTER(s, buffer);
FICL_STRING_SET_LENGTH(s, length + 1);
result = ficlVmExecuteString(vm, s);
/* handle "bye" in loaded files. --lch */
switch (result)
{
case FICL_VM_STATUS_OUT_OF_TEXT:
case FICL_VM_STATUS_USER_EXIT:
break;
default:
vm->sourceId = oldSourceId;
fclose(f);
ficlVmThrowError(vm, "Error loading file <%s> line %d (%d)", FICL_COUNTED_STRING_GET_POINTER(*counted), line, result);
break;
}
}
/*
** Pass an empty line with SOURCE-ID == -1 to flush
** any pending REFILLs (as required by FILE wordset)
*/
vm->sourceId.i = -1;
FICL_STRING_SET_FROM_CSTRING(s, "");
ficlVmExecuteString(vm, s);
vm->sourceId = oldSourceId;
fclose(f);
/* handle "bye" in loaded files. --lch */
if (result == FICL_VM_STATUS_USER_EXIT)
ficlVmThrow(vm, FICL_VM_STATUS_USER_EXIT);
return;
}
/*
** Dump a tab delimited file that summarizes the contents of the
** dictionary hash table by hashcode...
*/
static void ficlPrimitiveSpewHash(ficlVm *vm)
{
ficlHash *hash = ficlVmGetDictionary(vm)->forthWordlist;
ficlWord *word;
FILE *f;
unsigned i;
unsigned hashSize = hash->size;
if (!ficlVmGetWordToPad(vm))
ficlVmThrow(vm, FICL_VM_STATUS_OUT_OF_TEXT);
f = fopen(vm->Pad, "w");
if (!f)
{
ficlVmTextOut(vm, "unable to open file\n");
return;
}
for (i = 0; i < hashSize; i++)
{
int n = 0;
word = hash->table[i];
while (word)
{
n++;
word = word->link;
}
fprintf(f, "%d\t%d", i, n);
word = hash->table[i];
while (word)
{
fprintf(f, "\t%s", word->name);
word = word->link;
}
fprintf(f, "\n");
}
fclose(f);
return;
}
/* : BREAK ( -- ) */
static void ficlPrimitiveBreak(ficlVm *vm)
{
vm->state = vm->state;
return;
}
/* : PREPTERM ( 0|1 -- ) */
static void ficlPrimitivePrepterm(ficlVm *vm)
{
int dir = ficlStackPopInteger(vm->dataStack);
prepterm(dir);
cbreak(!dir);
}
/* : KEY ( -- c ) */
static void ficlPrimitiveKey(ficlVm *vm)
{
int ch;
FICL_STACK_CHECK(vm->dataStack, 0, 1);
do {
ch = getkey();
} while (ch > 255);
ficlStackPushInteger(vm->dataStack, ch);
}
/* : KEY? ( -- flag ) */
static void ficlPrimitiveKeyQ(ficlVm *vm)
{
int ret = has_key();
FICL_STACK_CHECK(vm->dataStack, 0, 1);
ficlStackPushInteger(vm->dataStack, FICL_BOOL(ret));
}
/* : EKEY ( -- code ) */
static void ficlPrimitiveEkey(ficlVm *vm)
{
int ch = getkey();
FICL_STACK_CHECK(vm->dataStack, 0, 1);
ficlStackPushInteger(vm->dataStack, ch);
}
/* : UTIME ( -- sec ) */
static void ficlPrimitiveUTime(ficlVm *vm)
{
time_t t = time(NULL);
FICL_STACK_CHECK(vm->dataStack, 0, 1);
ficlStackPushInteger(vm->dataStack, (ficlInteger)t);
}
/* : NOW ( -- sec min hour ) */
static void ficlPrimitiveNow(ficlVm *vm)
{
struct tm *tim;
time_t t;
FICL_STACK_CHECK(vm->dataStack, 0, 3);
t = time(NULL);
tim = localtime(&t);
ficlStackPushInteger(vm->dataStack, tim->tm_sec);
ficlStackPushInteger(vm->dataStack, tim->tm_min);
ficlStackPushInteger(vm->dataStack, tim->tm_hour);
}
/* : TODAY ( -- day mon year ) */
static void ficlPrimitiveToday(ficlVm *vm)
{
struct tm *tim;
time_t t;
FICL_STACK_CHECK(vm->dataStack, 0, 3);
t = time(NULL);
tim = localtime(&t);
ficlStackPushInteger(vm->dataStack, tim->tm_mday);
ficlStackPushInteger(vm->dataStack, tim->tm_mon + 1);
ficlStackPushInteger(vm->dataStack, tim->tm_year + 1900);
}
/* : TIME-ZONE ( -- minutes ) */
static void ficlPrimitiveTimeZone(ficlVm *vm)
{
long gmtoff;
FICL_STACK_CHECK(vm->dataStack, 0, 1);
gmtoff = ficlGetGMTOffset();
ficlStackPushInteger(vm->dataStack, gmtoff / 60);
}
/* : DST? ( -- flag ) */
static void ficlPrimitiveDstQ(ficlVm *vm)
{
struct tm *tim;
time_t t;
FICL_STACK_CHECK(vm->dataStack, 0, 1);
t = time(NULL);
tim = localtime(&t);
ficlStackPushInteger(vm->dataStack, FICL_BOOL(tim->tm_isdst));
}
/* : (DLOPEN) ( ca u -- hnd ) */
static void ficlPrimitiveDlOpen(ficlVm *vm)
{
void *ret, *addr;
int length;
char *path;
FICL_STACK_CHECK(vm->dataStack, 2, 1);
length = ficlStackPopInteger(vm->dataStack);
addr = (void *)ficlStackPopPointer(vm->dataStack);
if (length) {
path = (char*)malloc(length + 1);
memcpy(path, addr, length);
path[length] = 0;
}
else
path = NULL;
ret = dlopen(path, RTLD_NOW);
if (length)
free(path);
ficlStackPushPointer(vm->dataStack, ret);
}
/* : (DLSYM) ( ca u hnd -- addr ) */
static void ficlPrimitiveDlSym(ficlVm *vm)
{
void *ret, *hnd, *addr;
int length;
char *symname;
FICL_STACK_CHECK(vm->dataStack, 3, 1);
hnd = (void *)ficlStackPopPointer(vm->dataStack);
length = ficlStackPopInteger(vm->dataStack);
addr = (void *)ficlStackPopPointer(vm->dataStack);
symname = (char*)malloc(length + 1);
memcpy(symname, addr, length);
symname[length] = 0;
ret = dlsym(hnd, symname);
free(symname);
ficlStackPushPointer(vm->dataStack, ret);
}
/* : (C-CALL) ( argN ... arg1 N fn -- ret ) */
static void ficlPrimitiveCCall(ficlVm *vm)
{
long (*fn)();
long ret, arg[8];
int i, narg;
FICL_STACK_CHECK(vm->dataStack, 2, 0);
fn = (long (*)()) ficlStackPopPointer(vm->dataStack);
narg = ficlStackPopInteger(vm->dataStack);
/* fprintf(stderr,"fn = %p narg = %d\n", fn, narg); */
FICL_STACK_CHECK(vm->dataStack, narg, 1);
for (i = 0; i < narg; i++) {
arg[i] = ficlStackPopInteger(vm->dataStack);
/* fprintf(stderr,"arg[%d] = 0x%lx\n",i,arg[i]); */
}
switch (narg) {
case 0: ret = (*fn)(); break;
case 1: ret = (*fn)(arg[0]); break;
case 2: ret = (*fn)(arg[0], arg[1]); break;
case 3: ret = (*fn)(arg[0], arg[1], arg[2]); break;
case 4: ret = (*fn)(arg[0], arg[1], arg[2], arg[3]); break;
case 5: ret = (*fn)(arg[0], arg[1], arg[2], arg[3], arg[4]); break;
case 6: ret = (*fn)(arg[0], arg[1], arg[2], arg[3], arg[4], arg[5]); break;
case 7: ret = (*fn)(arg[0], arg[1], arg[2], arg[3], arg[4], arg[5], arg[6]); break;
default:
break;
}
ficlStackPushInteger(vm->dataStack, ret);
}
/* callbacks */
static int do_cb(int n, int a1, int a2, int a3, int a4, int a5, int a6, int a7, int a8);
static int cb0(int n, int a1, int a2, int a3, int a4, int a5, int a6, int a7, int a8) { return do_cb(0, a1, a2, a3, a4, a5, a6, a7, a8); }
static int cb1(int n, int a1, int a2, int a3, int a4, int a5, int a6, int a7, int a8) { return do_cb(1, a1, a2, a3, a4, a5, a6, a7, a8); }
static int cb2(int n, int a1, int a2, int a3, int a4, int a5, int a6, int a7, int a8) { return do_cb(2, a1, a2, a3, a4, a5, a6, a7, a8); }
static int cb3(int n, int a1, int a2, int a3, int a4, int a5, int a6, int a7, int a8) { return do_cb(3, a1, a2, a3, a4, a5, a6, a7, a8); }
static int cb4(int n, int a1, int a2, int a3, int a4, int a5, int a6, int a7, int a8) { return do_cb(4, a1, a2, a3, a4, a5, a6, a7, a8); }
static int cb5(int n, int a1, int a2, int a3, int a4, int a5, int a6, int a7, int a8) { return do_cb(5, a1, a2, a3, a4, a5, a6, a7, a8); }
static int cb6(int n, int a1, int a2, int a3, int a4, int a5, int a6, int a7, int a8) { return do_cb(6, a1, a2, a3, a4, a5, a6, a7, a8); }
static int cb7(int n, int a1, int a2, int a3, int a4, int a5, int a6, int a7, int a8) { return do_cb(7, a1, a2, a3, a4, a5, a6, a7, a8); }
typedef struct {
void *xt; /* ficlWord */
int nargs;
void *fn;
} C_CALLBACK;
static C_CALLBACK cb_tbl[] = {
{0, -1, cb0},
{0, -1, cb1},
{0, -1, cb2},
{0, -1, cb3},
{0, -1, cb4},
{0, -1, cb5},
{0, -1, cb6},
{0, -1, cb7}
};
static int do_cb(int n, int a1, int a2, int a3, int a4, int a5, int a6, int a7, int a8)
{
ficlVm *vm;
ficlWord *xt;
int ret;
if (-1 == cb_tbl[n].nargs)
return 0;
vm = ficlVmCreate(NULL, 64, 64);
switch (cb_tbl[n].nargs) {
case 0: goto L0;
case 1: goto L1;
case 2: goto L2;
case 3: goto L3;
case 4: goto L4;
case 5: goto L5;
case 6: goto L6;
case 7: goto L7;
case 8: goto L8;
}
L8: ficlStackPushInteger(vm->dataStack, a8);
L7: ficlStackPushInteger(vm->dataStack, a7);
L6: ficlStackPushInteger(vm->dataStack, a6);
L5: ficlStackPushInteger(vm->dataStack, a5);
L4: ficlStackPushInteger(vm->dataStack, a4);
L3: ficlStackPushInteger(vm->dataStack, a3);
L2: ficlStackPushInteger(vm->dataStack, a2);
L1: ficlStackPushInteger(vm->dataStack, a1);
L0:
xt = (ficlWord*) cb_tbl[n].xt;
ficlVmExecuteWord(vm, xt);
ret = ficlStackPopInteger(vm->dataStack);
ficlVmDestroy(vm);
return ret;
}
/* : (CALLBACK) ( xt nargs cbidx -- ptr ) */
static void ficlPrimitiveCallback(ficlVm *vm)
{
int idx;
FICL_STACK_CHECK(vm->dataStack, 3, 1);
idx = ficlStackPopInteger(vm->dataStack);
cb_tbl[idx].nargs = ficlStackPopInteger(vm->dataStack);
cb_tbl[idx].xt = ficlStackPopPointer(vm->dataStack);
ficlStackPushPointer(vm->dataStack, cb_tbl[idx].fn);
}
#if FICL_WANT_MULTITHREADED
static void* runThread(void *arg)
{
ficlVm *vm = (ficlVm *) arg;
pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL);
ficlVmExecuteXT(vm, vm->runningWord);
return NULL;
}
static void checkReturnCode(ficlVm *vm, const char* where, int rc)
{
char msg[128];
if (rc)
{
sprintf(msg, "%s(): %s", where, strerror(rc));
if (vm)
ficlVmThrowError(vm, msg);
fprintf(stderr, "%s\n", msg);
}
}
void ficlVmSetThreadActive(ficlVm *vm, ficlUnsigned flag)
{
vm->threadActive = flag;
__sync_synchronize();
}
ficlUnsigned ficlVmIsThreadActive(ficlVm *vm)
{
__sync_synchronize();
return vm->threadActive;
}
void ficlVmTerminateThread(ficlVm *vm, ficlUnsigned doCancel)
{
int rc;
void *ptr;
if (FICL_TRUE == doCancel)
{
rc = pthread_cancel(vm->threadID);
/* NB. may return 'no such process' */
checkReturnCode(0, "ficlVmTerminateThread - pthread_cancel", rc);
rc = pthread_join(vm->threadID, &ptr);
checkReturnCode(0, "ficlVmTerminateThread - pthread_join", rc);
}
pthread_cond_destroy(&vm->threadSignal);
pthread_mutex_destroy(&vm->threadStopMutex);
vm->threadWake = FICL_FALSE;
__sync_synchronize();
}
int recursiveMutexInit(pthread_mutex_t *mutex)
{
pthread_mutexattr_t mutexAttr;
int rc;
pthread_mutexattr_init(&mutexAttr);
pthread_mutexattr_settype(&mutexAttr, PTHREAD_MUTEX_RECURSIVE);
rc = pthread_mutex_init(mutex, &mutexAttr);
pthread_mutexattr_destroy(&mutexAttr);
return rc;
}
/* : /TASK ( -- n ) */
static void ficlPrimitiveSlashTask(ficlVm *vm)
{
FICL_STACK_CHECK(vm->dataStack, 0, 1);
ficlStackPushInteger(vm->dataStack, sizeof(ficlVm));
}
/* : CONSTRUCT ( addr -- ) */
static void ficlPrimitiveConstruct(ficlVm *vm)
{
ficlSystem *system = vm->callback.system;
ficlVm *otherVm;
FICL_STACK_CHECK(vm->dataStack, 1, 0);
otherVm = ficlStackPopPointer(vm->dataStack);
if (ficlDictionaryIncludes(system->dictionary, otherVm))
ficlVmThrowError(vm, "Error: task space is not ALLOCATEd.");
memset(otherVm, 0, sizeof(ficlVm));
otherVm = ficlVmCreate(otherVm, system->stackSize, system->stackSize);
ficlSystemInitVm(system, otherVm);
}
/* : ACTIVATE ( xt addr -- ) */
static void ficlPrimitiveActivate(ficlVm *vm)
{
ficlVm *otherVm;
ficlWord *word;
int rc;
const char *where;
FICL_STACK_CHECK(vm->dataStack, 2, 0);
otherVm = ficlStackPopPointer(vm->dataStack);
word = ficlStackPopPointer(vm->dataStack);
ficlVmReset(otherVm);
otherVm->runningWord = word;
// newVm->ip = (ficlIp)(word->param);
where = "ACTIVATE - pthread_mutex_init";
rc = pthread_mutex_init(&otherVm->threadStopMutex, NULL);
if (rc)
goto errout;
where = "ACTIVATE - pthread_cond_init";
rc = pthread_cond_init(&otherVm->threadSignal, NULL);
if (rc)
{
pthread_mutex_destroy(&otherVm->threadStopMutex);
goto errout;
}
otherVm->threadWake = FICL_FALSE;
__sync_synchronize();
where = "ACTIVATE - pthread_create";
ficlVmSetThreadActive(otherVm, FICL_TRUE);
rc = pthread_create(&otherVm->threadID, NULL, runThread, otherVm);
if (rc)
{
pthread_mutex_destroy(&otherVm->threadStopMutex);
pthread_cond_destroy(&otherVm->threadSignal);
ficlVmSetThreadActive(otherVm, FICL_FALSE);
}
errout:
checkReturnCode(vm, where, rc);
}
/* : PAUSE ( -- ) */
static void ficlPrimitivePause(ficlVm *vm)
{
if (FICL_TRUE == ficlVmIsThreadActive(vm))
pthread_testcancel();
sched_yield();
}
#define FICL_CAS(x,y,z) __sync_bool_compare_and_swap(x,y,z)
/* : STOP ( -- ) */
static void ficlPrimitiveStop(ficlVm *vm)
{
int rc;
int i;
if (FICL_FALSE == ficlVmIsThreadActive(vm))
return;
if (FICL_CAS(&vm->threadWake, FICL_TRUE, FICL_FALSE))
return;
rc = pthread_mutex_lock(&vm->threadStopMutex);
checkReturnCode(0, "STOP - pthread_mutex_lock", rc);
while (!FICL_CAS(&vm->threadWake, FICL_TRUE, FICL_FALSE))
{
rc = pthread_cond_wait(&vm->threadSignal, &vm->threadStopMutex);
checkReturnCode(0, "STOP - pthread_cond_wait", rc);
}
rc = pthread_mutex_unlock(&vm->threadStopMutex);
checkReturnCode(0, "STOP - pthread_mutex_unlock", rc);
}
/* : AWAKEN ( addr -- ) */
static void ficlPrimitiveAwaken(ficlVm *vm)
{
ficlVm *otherVm;
int rc;
FICL_STACK_CHECK(vm->dataStack, 1, 0);
otherVm = ficlStackPopPointer(vm->dataStack);
if (FICL_FALSE == ficlVmIsThreadActive(otherVm))
return;
rc = pthread_mutex_lock(&otherVm->threadStopMutex);
checkReturnCode(0, "AWAKEN - pthread_mutex_lock", rc);
otherVm->threadWake = FICL_TRUE;
__sync_synchronize();
rc = pthread_cond_signal(&otherVm->threadSignal);
checkReturnCode(0, "AWAKEN - pthread_cond_signal", rc);
rc = pthread_mutex_unlock(&otherVm->threadStopMutex);
checkReturnCode(0, "AWAKEN - pthread_mutex_unlock", rc);
}
/* : HALT ( -- ) */
static void ficlPrimitiveHalt(ficlVm *vm)
{
if (FICL_FALSE == ficlVmIsThreadActive(vm))
return;
ficlVmSetThreadActive(vm, FICL_FALSE);
ficlVmTerminateThread(vm, FICL_FALSE);
pthread_exit(NULL);
}
/* : TERMINATE ( addr -- ) */
static void ficlPrimitiveTerminate(ficlVm *vm)
{
ficlVm *otherVm;
int rc;
FICL_STACK_CHECK(vm->dataStack, 1, 0);
otherVm = ficlStackPopPointer(vm->dataStack);
if (FICL_FALSE == ficlVmIsThreadActive(otherVm))
return;
ficlVmSetThreadActive(otherVm, FICL_FALSE);
ficlVmTerminateThread(otherVm, FICL_TRUE);
}
/* : HIS ( task addr1 -- addr2 ) */
static void ficlPrimitiveHis(ficlVm *vm)
{
char *addr1, *addr2;
ficlVm *otherVm;
FICL_STACK_CHECK(vm->dataStack, 2, 1);
addr1 = ficlStackPopPointer(vm->dataStack);
otherVm = ficlStackPopPointer(vm->dataStack);
addr2 = (char *)&otherVm->user[0] + (addr1 - (char *)&vm->user[0]);
ficlStackPushPointer(vm->dataStack, addr2);
}
/* : /MUTEX ( -- n ) */
static void ficlPrimitiveSlashMutex(ficlVm *vm)
{
FICL_STACK_CHECK(vm->dataStack, 0, 1);
ficlStackPushInteger(vm->dataStack, sizeof(pthread_mutex_t));
}
/* : MUTEX-INIT ( addr -- ) */
static void ficlPrimitiveMutexInit(ficlVm *vm)
{
pthread_mutex_t *mutex;
pthread_mutexattr_t mutexAttr;
FICL_STACK_CHECK(vm->dataStack, 1, 0);
mutex = ficlStackPopPointer(vm->dataStack);
recursiveMutexInit(mutex);
}
/* : GET ( addr -- ) */
static void ficlPrimitiveMutexGet(ficlVm *vm)
{
pthread_mutex_t *mutex;
int rc;
FICL_STACK_CHECK(vm->dataStack, 1, 0);
mutex = ficlStackPopPointer(vm->dataStack);
rc = pthread_mutex_lock(mutex);
checkReturnCode(vm, "pthread_mutex_lock", rc);
}
/* : RELEASE ( addr -- ) */
static void ficlPrimitiveMutexRelease(ficlVm *vm)
{
pthread_mutex_t *mutex;
int rc;
FICL_STACK_CHECK(vm->dataStack, 1, 0);
mutex = ficlStackPopPointer(vm->dataStack);
rc = pthread_mutex_unlock(mutex);
checkReturnCode(vm, "pthread_mutex_unlock", rc);
}
/* : ATOMIC@ ( addr -- x ) */
static void ficlPrimitiveAtomicFetch(ficlVm *vm)
{
ficlCell *cell;
FICL_STACK_CHECK(vm->dataStack, 1, 1);
cell = (ficlCell *) ficlStackPopPointer(vm->dataStack);
__sync_synchronize();
ficlStackPush(vm->dataStack, cell[0]);
}
/* : ATOMIC! ( x addr -- ) */
static void ficlPrimitiveAtomicStore(ficlVm *vm)
{
ficlCell *cell, x;
FICL_STACK_CHECK(vm->dataStack, 2, 0);
cell = (ficlCell *) ficlStackPopPointer(vm->dataStack);
x = ficlStackPop(vm->dataStack);
cell[0] = x;
__sync_synchronize();
}
/* : ATOMIC-XCHG ( x1 a-addr -- x2 ) */
static void ficlPrimitiveAtomicXchg(ficlVm *vm)
{
ficlUnsigned *addr, x1, x2;
FICL_STACK_CHECK(vm->dataStack, 2, 1);
addr = (ficlUnsigned *) ficlStackPopPointer(vm->dataStack);
x1 = ficlStackPopUnsigned(vm->dataStack);
x2 = __sync_lock_test_and_set(addr, x1);
ficlStackPushUnsigned(vm->dataStack, x2);
}
/* : ATOMIC-CAS ( expected desired a-addr -- prev ) */
static void ficlPrimitiveAtomicCas(ficlVm *vm)
{
ficlUnsigned *addr, desired, expected, prev;
FICL_STACK_CHECK(vm->dataStack, 3, 1);
addr = (ficlUnsigned *) ficlStackPopPointer(vm->dataStack);
desired = ficlStackPopUnsigned(vm->dataStack);
expected = ficlStackPopUnsigned(vm->dataStack);
prev = __sync_val_compare_and_swap(addr, expected, desired);
ficlStackPushUnsigned(vm->dataStack, prev);
}
/* : ATOMIC-OP ( xt a-addr -- ) */
static void ficlPrimitiveAtomicOp(ficlVm *vm)
{
ficlCell *addr, old_value, new_value;
ficlWord *xt;
FICL_STACK_CHECK(vm->dataStack, 2, 0);
addr = ficlStackPopPointer(vm->dataStack);
xt = ficlStackPopPointer(vm->dataStack);
do
{
__sync_synchronize();
old_value = addr[0];
ficlStackPush(vm->dataStack, old_value);
ficlVmExecuteWord(vm, xt);
new_value = ficlStackPop(vm->dataStack);
} while(!FICL_CAS((ficlUnsigned*)addr, old_value.u, new_value.u));
}
#endif /* FICL_WANT_MULTITHREADED */
/* : WFLIP ( uw1 -- uw2 ) */
static void ficlPrimitiveWFlip(ficlVm *vm)
{
ficlUnsigned16 u;
FICL_STACK_CHECK(vm->dataStack, 1, 1);
u = ficlStackPopUnsigned(vm->dataStack);
ficlStackPushUnsigned(vm->dataStack, bswap16(u));
}
/* : QFLIP ( uq1 -- uq2 ) */
static void ficlPrimitiveQFlip(ficlVm *vm)
{
ficlUnsigned32 u;
FICL_STACK_CHECK(vm->dataStack, 1, 1);
u = ficlStackPopUnsigned(vm->dataStack);
ficlStackPushUnsigned(vm->dataStack, bswap32(u));
}
#if defined(__LP64__) || defined(__MINGW64__) || defined(_WIN64)
/* : XFLIP ( ux1 -- ux2 ) */
static void ficlPrimitiveXFlip(ficlVm *vm)
{
ficlUnsigned u;
FICL_STACK_CHECK(vm->dataStack, 1, 1);
u = ficlStackPopUnsigned(vm->dataStack);
ficlStackPushUnsigned(vm->dataStack, bswap64(u));
}
#define ficlPrimitiveFlip ficlPrimitiveXFlip
#else