-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPVM.c
More file actions
1411 lines (1410 loc) · 47.1 KB
/
PVM.c
File metadata and controls
1411 lines (1410 loc) · 47.1 KB
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 "PVM.h"
void debugPrintWaitingList()
{
//show the waiting list
int a0;
waitL *p;
for(a0=0;a0<listMutexSize;a0++)
{
//print list of each mutex
p=listMutex[a0].waitList;
while(p)
{
printf("%ld|",(long)p->pid);
p=p->next;
}
printf("||\n");
}
printf("\n");
}
void *mutexHandler()
{
//here is the new one
int a0;
for(;;)
{
//infinite loop
//get the execution permission
pthread_mutex_lock(&rtExecLock);
//do the job
//read arguments global arguments
//find the mutex
//printf("MMMMMMMMMMM:%d\n",listMutexSize);
for(a0=0;a0<listMutexSize;a0++)
{
//check address
//printf("$%lx$\t$%lx$",listMutex[a0].content,mutexHandlerArg.mTarget);
if(listMutex[a0].content == (void*)mutexHandlerArg.mTarget)
{
mutexHandlerArg.mTarget = (mutex*)(listMutex+a0);
break;
}
}
if(a0==listMutexSize)
{
printf("Error : invalid mutex entry.\n");
//error handler
pthread_mutex_unlock(&haltExecLock);
}
//printf("mutex handler : \"I am awake!\"%lx\n",mutexHandlerArg.mTarget);
//printf("%ld",mutexHandlerArg.pid);
switch(mutexHandlerArg.opTyp)
{
case MTX_HDL_TYP_WAIT:
//test the mutex status
if(mutexHandlerArg.mTarget->lock==NULL)
{
//printf("wait mutex%lx\n",mutexHandlerArg.mTarget);
//can take over
//take over
//printf("Got it!\n");
mutexHandlerArg.mTarget->lock=mutexHandlerArg.pid;
//set status to running
mutexHandlerArg.pid->status=PROCESSOR_STATUS_RUNNING;
mutexHandlerArg.pid->performance=INITIAL_PERFORMANCE_VAL;
}
else
{
//need wait, put it on the waiting list
//list is empty?
//printf("Waiting\n");
if(mutexHandlerArg.mTarget->waitList==NULL)
{
//empty, set the first element
mutexHandlerArg.mTarget->waitList = malloc(sizeof(waitL));
mutexHandlerArg.mTarget->waitList->next=NULL;
mutexHandlerArg.mTarget->waitList->pid=mutexHandlerArg.pid;
}
else
{
//find the tail
waitL *p = mutexHandlerArg.mTarget->waitList;
while(p->next) p=p->next;
p->next = malloc(sizeof(waitL));
p->next->next=NULL;
p->next->pid=mutexHandlerArg.pid;
}
mutexHandlerArg.pid->status=PROCESSOR_STATUS_SUSPENDED;
}
break;
case MTX_HDL_TYP_TEST:
//test if it has been take over
if(mutexHandlerArg.mTarget->lock==NULL)
{
//can take over
//take over
mutexHandlerArg.mTarget->lock=mutexHandlerArg.pid;
//set flag to -1
mutexHandlerArg.pid->eflag=-1;
}
else
{
//can not take over
//set flag to 0
mutexHandlerArg.pid->eflag=0;
}
//no matter what happened, set status to running
mutexHandlerArg.pid->status=PROCESSOR_STATUS_RUNNING;
mutexHandlerArg.pid->performance=INITIAL_PERFORMANCE_VAL;
break;
case MTX_HDL_TYP_LEAVE:
//set the master instance's status to running
//printf("Leave.\n");
mutexHandlerArg.pid->status=PROCESSOR_STATUS_RUNNING;
mutexHandlerArg.pid->performance=INITIAL_PERFORMANCE_VAL;
//look up the list(branch)
if(mutexHandlerArg.mTarget->waitList==NULL)
{
//waiting list empty
//just set lock to empty
mutexHandlerArg.mTarget->lock=NULL;
}
else
{
waitL *p=mutexHandlerArg.mTarget->waitList;
//someone are waiting
//put the guy to lock
mutexHandlerArg.mTarget->lock=p->pid;
mutexHandlerArg.mTarget->waitList = p->next;
//set this guy running
p->pid->status=PROCESSOR_STATUS_RUNNING;
p->pid->performance=INITIAL_PERFORMANCE_VAL;
//release memory
free(p);
}
break;
default:;//awake error handler
}
//debugPrintWaitingList();
//release the mutex
pthread_mutex_unlock(&rtLock);
}
//
}
void VMReadFile(char *file)
{
int c0;
//get file
VMpe=parseFile(file);
//checkStructure(VMpe);
//initialize the code list
c0=VMpe->processorTemplateNum;
listCode = malloc(sizeof(void*) * c0);
//getchar();
while(c0--)
{
*(listCode+c0)=(VMpe->processorTemplates + c0)->code;
}//getchar();
//initialaze the mutex list
c0=VMpe->mutexNum;
listMutexSize = c0;
listMutex=malloc(sizeof(mutex) * c0);
while(c0--)
{
(listMutex + c0)->lock = NULL;
(listMutex + c0)->size = *(VMpe->mutexSizeList + c0);
(listMutex + c0)->content = malloc((listMutex + c0)->size);
printf("Mutex %d/%lx/%lx\n",c0,(long)(listMutex+c0),(long)((listMutex + c0)->content));
(listMutex + c0)->waitList = NULL;
}
//initialize the instance list
//allocate the space
listInstanceSize = VMpe->processorInstanceNUM;
listInstance = malloc(sizeof(PBase) * listInstanceSize);
printf("%d\n",listInstanceSize);//getchar();
for(c0=0;c0<listInstanceSize;c0++)
{
//allocate data section.
///listInstance[c0].data = malloc(VMpe->processorTemplates[VMpe->processorInstances[c0].processorReferenceNo].dataSize);
//assign value
listInstance[c0].code = VMpe->processorTemplates[VMpe->processorInstances[c0].processorReferenceNo].code;
listInstance[c0].pc = VMpe->processorTemplates[VMpe->processorInstances[c0].processorReferenceNo].code;//getchar();
listInstance[c0].PID = &listInstance[c0];
listInstance[c0].status = VMpe->processorInstances[c0].initStatus;
listInstance[c0].eflag = 0;
listInstance[c0].triggerVal=0;
listInstance[c0].currentVal=0;
listInstance[c0].performance=INITIAL_PERFORMANCE_VAL;
//allocate data space!
listInstance[c0].data=malloc(
VMpe->processorTemplates[VMpe->processorInstances[c0].processorReferenceNo].stack0Size+
VMpe->processorTemplates[VMpe->processorInstances[c0].processorReferenceNo].stackSize+
VMpe->processorTemplates[VMpe->processorInstances[c0].processorReferenceNo].globalSize+
256*sizeof(void*));
{
void* *ap = listInstance[c0].data;
//PRINTADDR(listInstance[c0].data);
//PRINTADDR(ap);
//PRINTADDR(ap[0]);
//data r
ap[0] = listInstance[c0].data;
//ap+=sizeof(void*);
//PRINTADDR(ap[0]);
//PRINTADDR(*(long*)listInstance[c0].data);
//stack0 base pointer
ap[1] = listInstance[c0].data + 256 * 8;
//ap++;
//stack0 pointer
ap[2] = listInstance[c0].data + 256 * 8;
//ap++;
//stack base pointer
ap[3] = listInstance[c0].data + 256 * 8 + VMpe->processorTemplates[VMpe->processorInstances[c0].processorReferenceNo].stack0Size;
//ap++;
//stack pointer
ap[4] = listInstance[c0].data + 256 * 8 + VMpe->processorTemplates[VMpe->processorInstances[c0].processorReferenceNo].stack0Size;
//ap++;
//global base pointer
ap[5] = listInstance[c0].data + 256 * 8 +
VMpe->processorTemplates[VMpe->processorInstances[c0].processorReferenceNo].stack0Size +
VMpe->processorTemplates[VMpe->processorInstances[c0].processorReferenceNo].stackSize;
//ap++;
//constant/string base pointer.
//ap++;
}
{
int c1;
void* *dataBase = listInstance[c0].data;//global data base is at [5]
//printf("\t%d\n",VMpe->processorTemplates[VMpe->processorInstances[c0].processorReferenceNo].initNumGlobal);
for(c1=0;c1<VMpe->processorTemplates[VMpe->processorInstances[c0].processorReferenceNo].initNumGlobal;c1++)
{
//printf(")%d()%d(\n",VMpe->processorTemplates[VMpe->processorInstances[c0].processorReferenceNo].initDataGlobal[c1].offset,VMpe->processorTemplates[VMpe->processorInstances[c0].processorReferenceNo].initDataGlobal[c1].length);
//printf("X%ldX\n",*(long*)VMpe->processorTemplates[VMpe->processorInstances[c0].processorReferenceNo].initDataGlobal[c1].data);
memmove(
(char*)dataBase[5]+VMpe->processorTemplates[VMpe->processorInstances[c0].processorReferenceNo].initDataGlobal[c1].offset,
VMpe->processorTemplates[VMpe->processorInstances[c0].processorReferenceNo].initDataGlobal[c1].data,
VMpe->processorTemplates[VMpe->processorInstances[c0].processorReferenceNo].initDataGlobal[c1].length);
//read global data
}
for(c1=0;c1<VMpe->processorInstances[c0].initNum;c1++)
{
memmove(
(char*)dataBase[5]+VMpe->processorInstances[c0].initData[c1].offset,
VMpe->processorInstances[c0].initData[c1].data,
VMpe->processorInstances[c0].initData[c1].length);
//read global data
}
}
}
//Connections
for(c0=0;c0<VMpe->connectionMappingNum;c0++)
{
//find the address anyway
void* addr;
switch(VMpe->connectionMapping[c0].nodeDType)
{
case TYP_INST:
addr = *(void**)(listInstance[VMpe->connectionMapping[c0].nodeDNo].data + 5 * sizeof(void*));
break;
case TYP_MUTEX:
addr = (listMutex[VMpe->connectionMapping[c0].nodeDNo].content);
break;
default:
printf("In connection %d : unknown reference type ID:%d\n",c0,VMpe->connectionMapping[c0].nodeDType);
return;
}//now we have got the address
//put it on to the right instance
*(void**)(listInstance[VMpe->connectionMapping[c0].nodeSNo].data + VMpe->connectionMapping[c0].nodeSPort * sizeof(void*))=addr;
}
//constraint
//initialize the constrtaint
//1.allocate a map
triggerList = malloc(VMpe->processorInstanceNUM*sizeof(tgr));
//set the block to zero
//memset(constraintMap,0,VMpe->processorInstanceNUM * VMpe->processorInstanceNUM);
//set the initlalized val to zero
for(c0=0;c0<VMpe->processorInstanceNUM;c0++)
{
//set each trigger number to zero
triggerList[c0].number=0;
triggerList[c0].list = NULL;
}
for(c0=0;c0<VMpe->constraintNum;c0++)
{
static int c1;
//put the number into
triggerList[VMpe->constraintList[c0].nodeDNo].number = VMpe->constraintList[c0].nodeSNum;
triggerList[VMpe->constraintList[c0].nodeDNo].list = malloc(sizeof(int) * VMpe->constraintList[c0].nodeSNum);
for(c1=0;c1<VMpe->constraintList[c0].nodeSNum;c1++)
{
//heris the descent one
triggerList[VMpe->constraintList[c0].nodeDNo].list[c1] = VMpe->constraintList[c0].nodeSNoList[c1];
listInstance[VMpe->constraintList[c0].nodeSNoList[c1]].triggerVal++;
}
}
//assign them to each instances
for(c0=0;c0<VMpe->processorInstanceNUM;c0++)
{
//assign for each, just copy the address
listInstance[c0].triggerNum = triggerList[c0].number;
listInstance[c0].triggerList = triggerList[c0].list;
}
}
void debugVM(PBase *p,int howManyStack0Elem)
{
long* stack0p;
printf("\nVM Debug Start\t");
printf("ProcessorID:%lu/%d ",(unsigned long)p,p->performance);
printf("Next Instruction P:%lx No:%hu\n",(long)p->pc,*(unsigned short*)p->pc);
printf("Current Status:%x ",p->status);
printf("Current Flag:%x ",(p->eflag>>8) & 0xFF);
printf("datapointer : %lx \n",(long)(p->data));
printf("Jumping val(maybe)%ld\n",*(long*)(p->pc+2));
//printf("self0pointer:%lx \n",*(long*)(p->data));
//PRINTADDR(*(long*)p->data);
//printf("Stack0pointer : %lx \n",*(long*)(p->data + POINTER_STACK0));
stack0p=(long*)*(long*)(p->data + POINTER_STACK0);
printf("stack0TopValue :%lx:%lx:%ld/%lx/%ld/%lf\n",*(long*)(p->data+8),(long)stack0p,*(stack0p-1),*(stack0p-1),*(stack0p-1),*(double*)(stack0p-1));
printf("##%ld\t", *(long*)(p->data+40));
/*while(howManyStack0Elem--)
{
printf("stack0:%lx\n",*stack0p);
stack0p--;
}*/
printf("\nVM Debug End\n");
}
void *execDebug_old(void* no)
{
int error=0;
int ino = *(int*)no;
while(!error)
{//loop for each loadded processor
//execute one thread
printf("@%d\n",listInstance[ino].status);
while(!listInstance[ino].status)//trap not invoked^^^^^^^^^^^
{
//printf("Trying one step...\n");
debugVM(&listInstance[ino],4);
executionOneStep(&listInstance[ino]);
//printf("Ended.\n");
//debugVM(&listInstance[ino],4);
getchar();
//scanf("\n");
}
ino+=NUM_E_THREAD;
if(ino==listInstanceSize) ino = *(int*)no;
}
}
void *execNormal(void *initPointer)
{
/**
* here is the execution function, it belongs to different thread
* and the execution list cones to a circular link list
*/
//values on the stack whlie running
//int performance=INITIAL_PERFORMANCE_VAL;//how much step run on each instance
int delay;//this attrbute means sleep how many ms for each circle scan
IME *instanceMountingList;
int a0,a1;
int idleCounter;
//initialize the list pointer
instanceMountingList = (IME*)initPointer;
/*for(;;)//update the structure of execution unit
{//the execution step
//check the info of performance
//branch:idle or active
if(performance)
{
printf("running id : %ld\n",*(long*)instanceMountingList->list->instance);
//active section
//execute current instance, switch
//a0=performance;//resource counter
/*while(a0)
{
//loop enabled block
switch(instanceMountingList->list->instance->status)
{
case PROCESSOR_STATUS_RUNNING:
//running
//debugVM(instanceMountingList->list->instance,2);
executionOneStep(instanceMountingList->list->instance);
a0--;
break;
case PROCESSOR_STATUS_SYS:
//system call
APIHandler(instanceMountingList->list->instance);
a0--;
break;
case PROCESSOR_STATUS_HALT:
//halt the VM
pthread_mutex_unlock(&haltLock);
errno=0;
//maybe here we need to modify some global variables.
a0=-1;
break;
case PROCESSOR_STATUS_SUSPENDED:
//instance was suspended
//call the trigger handler
pthread_mutex_lock(&qLock);
//write the queue
waitingQueue[queueH].pid = instanceMountingList->list->instance;
waitingQueue[queueH].mTarget = NULL;
waitingQueue[queueH].opTyp = TRIGGER;
queueH++;
queueH %= M_WAITING_LIST_SIZE;
//awake the mutex handler
pthread_mutex_unlock(&rtLock);
pthread_mutex_unlock(&qLock);
a0=-1;
break;
case PROCESSOR_STATUS_REBOOT:
//instance is waiting for restart
//do something????
//change status to suspended
instanceMountingList->list->instance->status=PROCESSOR_STATUS_SUSPENDED;
break;
case PROCESSOR_STATUS_MWAIT:
//call the mutex handler
//wait for the queue mutex
pthread_mutex_lock(&qLock);
//write the queue
queueH++;
queueH %= M_WAITING_LIST_SIZE;
waitingQueue[queueH].pid = instanceMountingList->list->instance;
waitingQueue[queueH].mTarget = (mutex*)instanceMountingList->list->instance->exAddr;
waitingQueue[queueH].opTyp = MTX_HDL_TYP_WAIT;
//awake the mutex handler
pthread_mutex_unlock(&rtLock);
pthread_mutex_unlock(&qLock);
a0=-1;
break;
case PROCESSOR_STATUS_MTEST:
//call the mutex handler
pthread_mutex_lock(&qLock);
//write the queue
queueH++;
queueH %= M_WAITING_LIST_SIZE;
waitingQueue[queueH].pid = instanceMountingList->list->instance;
waitingQueue[queueH].mTarget = (mutex*)instanceMountingList->list->instance->exAddr;
waitingQueue[queueH].opTyp = MTX_HDL_TYP_TEST;
//awake the mutex handler
pthread_mutex_unlock(&rtLock);
pthread_mutex_unlock(&qLock);
a0=-1;
break;
case PROCESSOR_STATUS_MLEAVE:
//call the mutex handler
pthread_mutex_lock(&qLock);
//write the queue
queueH++;
queueH %= M_WAITING_LIST_SIZE;
waitingQueue[queueH].pid = instanceMountingList->list->instance;
waitingQueue[queueH].mTarget = (mutex*)instanceMountingList->list->instance->exAddr;
waitingQueue[queueH].opTyp = MTX_HDL_TYP_LEAVE;
//awake the mutex handler
pthread_mutex_unlock(&rtLock);
pthread_mutex_unlock(&qLock);
a0=-1;
break;
default:;//error/unknown status
}
if(a0 == -1) break;
}*
if(a0)
{//runs properly
//increase performance
if(performance < MAX_PERFORMANCE_VAL)
performance+=INITIAL_PERFORMANCE_VAL;
a1=0;
}
else
{
//bump
//set the performance to start
performance = INITIAL_PERFORMANCE_VAL;
a1++;
if(a1==instanceMountingList->INumber)
{
//all processor are idle
//set the performance to 0, goto the sleep section
performance = 0;
delay = INITIAL_DELAY_VAL;
}
}
instanceMountingList->list = instanceMountingList->list->next;
//TO DO : count the idle instance here
}
else
{
/*idle section*
usleep(delay);printf("thread is slept\n");
//loop : check the the list if exist one that is runnnig
instanceMountingList->start = instanceMountingList->list;
for(;;)
{
instanceMountingList->list = instanceMountingList->list->next;
//check the instance status
if(!instanceMountingList->list->instance->status)
{
//running instance detected
performance = INITIAL_PERFORMANCE_VAL;
break;
}
if(instanceMountingList->start - instanceMountingList->list)
continue;//not to the end
else
{
//the end, all instances are suspended
//set the sleep time longer
delay += INITIAL_DELAY_VAL;
break;
}
}
}
}*/
for(;;)
{
//running loop, check each of instance list
if(delay)
{
//sleeping section
//sleep
usleep(delay);
//check if exist any instance that at running state
for(a0=0;a0<instanceMountingList->INumber;a0++)
{
if(instanceMountingList->list->instance->status==PROCESSOR_STATUS_RUNNING)
{//detected
//close the enable flag
delay=0;
//set performance to start value
instanceMountingList->list->instance->performance = INITIAL_PERFORMANCE_VAL;
//jump out
break;
}
//check next
instanceMountingList->list = instanceMountingList->list->next;
}
//deal with the situation : nobody was awake
if(a0==instanceMountingList->INumber)
{
//increase the sleeping time
if(delay<MAX_DELAY_VAL)//deal with : oops, sleeping to die
{
//increase it
delay += INITIAL_DELAY_VAL;
}
}
}
else
{
//active section
//execution section
if(instanceMountingList->list->instance->performance)
{
//running section
//set the counter to 0
idleCounter=0;
a0=instanceMountingList->list->instance->performance;
//switch with status
while(a0--)
{//loop the n cycle
//debugVM(instanceMountingList->list->instance,1);
switch(instanceMountingList->list->instance->status)
{
case PROCESSOR_STATUS_RUNNING:
//execution
//printf("$$$$$$$$$$$%d\n",instanceMountingList->list->instance->status);
executionOneStep(instanceMountingList->list->instance);
//debugVM(instanceMountingList->list->instance,1);
//printf("???????????%d\n",instanceMountingList->list->instance->status);
//getchar();
if(a0==0)
{
if(instanceMountingList->list->instance->performance<MAX_PERFORMANCE_VAL)
instanceMountingList->list->instance->performance+=INITIAL_PERFORMANCE_VAL;
instanceMountingList->list = instanceMountingList->list->next;
}
break;
case PROCESSOR_STATUS_SYS:
//api calling
APIHandler(instanceMountingList->list->instance);
if(a0==0)
{
if(instanceMountingList->list->instance->performance<MAX_PERFORMANCE_VAL)
instanceMountingList->list->instance->performance+=INITIAL_PERFORMANCE_VAL;
instanceMountingList->list = instanceMountingList->list->next;
}
break;
default://meet up with the suspended section
//set the counter to 0
a0=0;
//set the performance to 0
instanceMountingList->list->instance->performance=0;
}
}
}
else
{
//suspended(board) section
//increase the counter
idleCounter++;
//switch with status
switch(instanceMountingList->list->instance->status)
{
case PROCESSOR_STATUS_HALT:
pthread_mutex_unlock(&haltExecLock);
errno=0;
return NULL;
break;
case PROCESSOR_STATUS_SUSPENDED:
//loop, search each "next instance"
for(a0=0;a0<instanceMountingList->list->instance->triggerNum;a0++)
{
//printf("awaking.............%ld\n",instanceMountingList->list->instance);
if(listInstance[instanceMountingList->list->instance->triggerList[a0]].status == PROCESSOR_STATUS_SUSPENDED)
{
//suspended & was the chosen one
//here is the mutex section
//
pthread_mutex_lock(&triggerLock);
listInstance[instanceMountingList->list->instance->triggerList[a0]].status = PROCESSOR_STATUS_RUNNING;
pthread_mutex_unlock(&triggerLock);
}
}
break;
//case PROCESSOR_STATUS_REBOOT:break;
case PROCESSOR_STATUS_MWAIT:
pthread_mutex_lock(&rtLock);//printf("W\n");
//queueH++;
//queueH %= M_WAITING_LIST_SIZE;
mutexHandlerArg.pid = instanceMountingList->list->instance;
mutexHandlerArg.mTarget = (mutex*)instanceMountingList->list->instance->exAddr;
mutexHandlerArg.opTyp = MTX_HDL_TYP_WAIT;
//awake the mutex handler
pthread_mutex_unlock(&rtExecLock);//printf("WSSS\n");
//pthread_mutex_unlock(&qLock);
break;
case PROCESSOR_STATUS_MTEST:
//
pthread_mutex_lock(&rtLock);
//queueH++;
//queueH %= M_WAITING_LIST_SIZE;
mutexHandlerArg.pid = instanceMountingList->list->instance;
mutexHandlerArg.mTarget = (mutex*)instanceMountingList->list->instance->exAddr;
mutexHandlerArg.opTyp = MTX_HDL_TYP_TEST;
//awake the mutex handler
pthread_mutex_unlock(&rtExecLock);
//pthread_mutex_unlock(&qLock);
break;
case PROCESSOR_STATUS_MLEAVE:
pthread_mutex_lock(&rtLock);
//queueH++;
//queueH %= M_WAITING_LIST_SIZE;
mutexHandlerArg.pid = instanceMountingList->list->instance;
mutexHandlerArg.mTarget = (mutex*)instanceMountingList->list->instance->exAddr;
mutexHandlerArg.opTyp = MTX_HDL_TYP_LEAVE;
//awake the mutex handler
pthread_mutex_unlock(&rtExecLock);
//pthread_mutex_unlock(&qLock);
//a0=-1;
break;
default:;//error handler
}
instanceMountingList->list = instanceMountingList->list->next;
}
//move to next instance
//check the counter if all of the instance are slept
if(idleCounter==instanceMountingList->INumber)
{
//sleep ready!
delay = INITIAL_DELAY_VAL;
}
}
}
}
void *execStep(void *initPointer)
{
/**
* here is the stepping execution function, it belongs to different thread
* and the execution list cones to a circular link list
* it will pause every step
*/
//values on the stack whlie running
//int performance=INITIAL_PERFORMANCE_VAL;//how much step run on each instance
int delay;//this attrbute means sleep how many ms for each circle scan
IME *instanceMountingList;
int a0,a1;
int idleCounter;
//initialize the list pointer
instanceMountingList = (IME*)initPointer;
for(;;)
{
//running loop, check each of instance list
if(delay)
{
//sleeping section
//sleep
usleep(delay);
//check if exist any instance that at running state
for(a0=0;a0<instanceMountingList->INumber;a0++)
{
if(instanceMountingList->list->instance->status==PROCESSOR_STATUS_RUNNING)
{//detected
//close the enable flag
delay=0;
//set performance to start value
instanceMountingList->list->instance->performance = INITIAL_PERFORMANCE_VAL;
//jump out
break;
}
//check next
instanceMountingList->list = instanceMountingList->list->next;
}
//deal with the situation : nobody was awake
if(a0==instanceMountingList->INumber)
{
//increase the sleeping time
if(delay<MAX_DELAY_VAL)//deal with : oops, sleeping to die
{
//increase it
delay += INITIAL_DELAY_VAL;
}
}
}
else
{
//active section
//execution section
if(instanceMountingList->list->instance->performance)
{
//running section
//set the counter to 0
idleCounter=0;
a0=instanceMountingList->list->instance->performance;
//switch with status
while(a0--)
{//loop the n cycle
//debugVM(instanceMountingList->list->instance,1);
switch(instanceMountingList->list->instance->status)
{
case PROCESSOR_STATUS_RUNNING:
//execution
//printf("$$$$$$$$$$$%d\n",instanceMountingList->list->instance->status);
executionOneStep(instanceMountingList->list->instance);
debugVM(instanceMountingList->list->instance,1);
//printf("???????????%d\n",instanceMountingList->list->instance->status);
getchar();
if(a0==0)
{
if(instanceMountingList->list->instance->performance<MAX_PERFORMANCE_VAL)
instanceMountingList->list->instance->performance+=INITIAL_PERFORMANCE_VAL;
instanceMountingList->list = instanceMountingList->list->next;
}
break;
case PROCESSOR_STATUS_SYS:
//api calling
APIHandler(instanceMountingList->list->instance);
if(a0==0)
{
if(instanceMountingList->list->instance->performance<MAX_PERFORMANCE_VAL)
instanceMountingList->list->instance->performance+=INITIAL_PERFORMANCE_VAL;
instanceMountingList->list = instanceMountingList->list->next;
}
break;
default://meet up with the suspended section
//set the counter to 0
a0=0;
//set the performance to 0
instanceMountingList->list->instance->performance=0;
}
}
}
else
{
//suspended(board) section
//increase the counter
idleCounter++;
//switch with status
switch(instanceMountingList->list->instance->status)
{
case PROCESSOR_STATUS_HALT:
pthread_mutex_unlock(&haltExecLock);
errno=0;
return NULL;
break;
case PROCESSOR_STATUS_SUSPENDED:
//loop, search each "next instance"
for(a0=0;a0<instanceMountingList->list->instance->triggerNum;a0++)
{
//printf("awaking.............%ld\n",instanceMountingList->list->instance);
if(listInstance[instanceMountingList->list->instance->triggerList[a0]].status == PROCESSOR_STATUS_SUSPENDED)
{
//suspended & was the chosen one
//here is the mutex section
//
pthread_mutex_lock(&triggerLock);
listInstance[instanceMountingList->list->instance->triggerList[a0]].status = PROCESSOR_STATUS_RUNNING;
pthread_mutex_unlock(&triggerLock);
}
}
break;
//case PROCESSOR_STATUS_REBOOT:break;
case PROCESSOR_STATUS_MWAIT:
pthread_mutex_lock(&rtLock);
//queueH++;
//queueH %= M_WAITING_LIST_SIZE;
mutexHandlerArg.pid = instanceMountingList->list->instance;
mutexHandlerArg.mTarget = (mutex*)instanceMountingList->list->instance->exAddr;
mutexHandlerArg.opTyp = MTX_HDL_TYP_WAIT;
//awake the mutex handler
pthread_mutex_unlock(&rtExecLock);
//pthread_mutex_unlock(&qLock);
break;
case PROCESSOR_STATUS_MTEST:
//
pthread_mutex_lock(&rtLock);
//queueH++;
//queueH %= M_WAITING_LIST_SIZE;
mutexHandlerArg.pid = instanceMountingList->list->instance;
mutexHandlerArg.mTarget = (mutex*)instanceMountingList->list->instance->exAddr;
mutexHandlerArg.opTyp = MTX_HDL_TYP_TEST;
//awake the mutex handler
pthread_mutex_unlock(&rtExecLock);
//pthread_mutex_unlock(&qLock);
break;
case PROCESSOR_STATUS_MLEAVE:
pthread_mutex_lock(&rtLock);
//queueH++;
//queueH %= M_WAITING_LIST_SIZE;
mutexHandlerArg.pid = instanceMountingList->list->instance;
mutexHandlerArg.mTarget = (mutex*)instanceMountingList->list->instance->exAddr;
mutexHandlerArg.opTyp = MTX_HDL_TYP_LEAVE;
//awake the mutex handler
pthread_mutex_unlock(&rtExecLock);
//pthread_mutex_unlock(&qLock);
//a0=-1;
break;
default:;//error handler
}
instanceMountingList->list = instanceMountingList->list->next;
}
//move to next instance
//check the counter if all of the instance are slept
if(idleCounter==instanceMountingList->INumber)
{
//sleep ready!
delay = INITIAL_DELAY_VAL;
}
}
}
}
void *execDebug(void *initPointer)
{
/**
* here is the debugging execution function, it belongs to different thread
* and the execution list cones to a circular link list
* it will show the info of every message
*/
//values on the stack whlie running
//int performance=INITIAL_PERFORMANCE_VAL;//how much step run on each instance
int delay;//this attrbute means sleep how many ms for each circle scan
IME *instanceMountingList;
int a0,a1;
int idleCounter;
//initialize the list pointer
instanceMountingList = (IME*)initPointer;
for(;;)
{
//running loop, check each of instance list
if(delay)
{
//sleeping section
//sleep
usleep(delay);
//check if exist any instance that at running state
for(a0=0;a0<instanceMountingList->INumber;a0++)
{
if(instanceMountingList->list->instance->status==PROCESSOR_STATUS_RUNNING)
{//detected
//close the enable flag
delay=0;
//set performance to start value
instanceMountingList->list->instance->performance = INITIAL_PERFORMANCE_VAL;
//jump out
break;
}
//check next
instanceMountingList->list = instanceMountingList->list->next;
}
//deal with the situation : nobody was awake
if(a0==instanceMountingList->INumber)
{
//increase the sleeping time
if(delay<MAX_DELAY_VAL)//deal with : oops, sleeping to die
{
//increase it
delay += INITIAL_DELAY_VAL;
}
}
}
else
{
//active section
//execution section
if(instanceMountingList->list->instance->performance)
{
//running section
//set the counter to 0
idleCounter=0;
a0=instanceMountingList->list->instance->performance;
//switch with status
while(a0--)
{//loop the n cycle
//debugVM(instanceMountingList->list->instance,1);
switch(instanceMountingList->list->instance->status)
{
case PROCESSOR_STATUS_RUNNING:
//execution
//printf("$$$$$$$$$$$%d\n",instanceMountingList->list->instance->status);
executionOneStep(instanceMountingList->list->instance);
debugVM(instanceMountingList->list->instance,1);
//printf("???????????%d\n",instanceMountingList->list->instance->status);
//getchar();
if(a0==0)
{
if(instanceMountingList->list->instance->performance<MAX_PERFORMANCE_VAL)
instanceMountingList->list->instance->performance+=INITIAL_PERFORMANCE_VAL;
instanceMountingList->list = instanceMountingList->list->next;
getchar();
}
break;
case PROCESSOR_STATUS_SYS:
//api calling
APIHandler(instanceMountingList->list->instance);
if(a0==0)
{
if(instanceMountingList->list->instance->performance<MAX_PERFORMANCE_VAL)
instanceMountingList->list->instance->performance+=INITIAL_PERFORMANCE_VAL;
instanceMountingList->list = instanceMountingList->list->next;
getchar();
}
break;
default://meet up with the suspended section
//set the counter to 0
a0=0;
//set the performance to 0
instanceMountingList->list->instance->performance=0;
getchar();
}
}
}
else
{
//suspended(board) section
//increase the counter
idleCounter++;
//switch with status
switch(instanceMountingList->list->instance->status)
{
case PROCESSOR_STATUS_HALT:
pthread_mutex_unlock(&haltExecLock);
errno=0;
return NULL;
break;
case PROCESSOR_STATUS_SUSPENDED:
//loop, search each "next instance"
for(a0=0;a0<instanceMountingList->list->instance->triggerNum;a0++)
{
//printf("awaking.............%ld\n",instanceMountingList->list->instance);
if(listInstance[instanceMountingList->list->instance->triggerList[a0]].status == PROCESSOR_STATUS_SUSPENDED)
{
//suspended & was the chosen one
//here is the mutex section
//
pthread_mutex_lock(&triggerLock);
listInstance[instanceMountingList->list->instance->triggerList[a0]].status = PROCESSOR_STATUS_RUNNING;
pthread_mutex_unlock(&triggerLock);
}
}
break;
//case PROCESSOR_STATUS_REBOOT:break;
case PROCESSOR_STATUS_MWAIT:
pthread_mutex_lock(&rtLock);
//queueH++;
//queueH %= M_WAITING_LIST_SIZE;
mutexHandlerArg.pid = instanceMountingList->list->instance;
mutexHandlerArg.mTarget = (mutex*)instanceMountingList->list->instance->exAddr;
mutexHandlerArg.opTyp = MTX_HDL_TYP_WAIT;
//printf("waiting mutex...%lx\n",mutexHandlerArg.mTarget->content);
//awake the mutex handler
pthread_mutex_unlock(&rtExecLock);
//pthread_mutex_unlock(&qLock);
break;
case PROCESSOR_STATUS_MTEST:
//
pthread_mutex_lock(&rtLock);
//queueH++;
//queueH %= M_WAITING_LIST_SIZE;
mutexHandlerArg.pid = instanceMountingList->list->instance;
mutexHandlerArg.mTarget = (mutex*)instanceMountingList->list->instance->exAddr;