-
Notifications
You must be signed in to change notification settings - Fork 0
/
thread.cpp
1340 lines (1038 loc) · 38.8 KB
/
thread.cpp
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 "std.h"
#include "rope.h"
#include "thread.h"
#include "lock.h"
#include "hash.h"
#include "symbol.h"
#include "excep.h"
#include "SpmtThread.h"
#include "RopeVM.h"
#include "DebugScaffold.h"
#include "Loggers.h"
#include "Helper.h"
#include "frame.h"
#ifdef TRACETHREAD
#define TRACE(fmt, ...) jam_printf(fmt, ## __VA_ARGS__)
#else
#define TRACE(fmt, ...)
#endif
#define HASHTABSZE 1<<4
HashTable thread_id_map;
/* Size of Java stack to use if no size is given */
static int dflt_stack_size;
/* Thread create/destroy lock and condvar */
static pthread_mutex_t lock;
static pthread_cond_t cv;
/* lock and condvar used by main thread to wait for
* all non-daemon threads to die */
static pthread_mutex_t exit_lock;
static pthread_cond_t exit_cv;
/* Monitor for sleeping threads to do a timed-wait against */
static Monitor sleep_mon;
/* Thread specific key holding thread's Thread pntr */
static pthread_key_t threadKey;
/* Attributes for spawned threads */
static pthread_attr_t attributes;
/* The main thread info - head of the thread list */
static Thread main_thread;
/* Main thread ExecEnv */
//static ExecEnv main_ee;
/* Various field offsets into java.lang.Thread &
java.lang.VMThread - cached at startup and used
in thread creation */
static int vmData_offset;
static int daemon_offset;
static int group_offset;
static int priority_offset;
static int name_offset;
static int vmthread_offset;
static int thread_offset;
static int threadId_offset;
/* Method table indexes of Thread.run method and
* ThreadGroup.removeThread - cached at startup */
static int run_mtbl_idx;
static int rmveThrd_mtbl_idx;
static MethodBlock *addThread_mb;
static MethodBlock *init_mb;
/* Cached java.lang.Thread class */
static Class *thread_class;
static Class *vmthread_class;
/* Count of non-daemon threads still running in VM */
static int non_daemon_thrds = 0;
/* Counts used in the ThreadMXBean management API. The
counts start from one to include the main thread */
static int threads_count = 1;
static int peak_threads_count = 1;
static long long total_started_threads_count = 1;
/* Guards against threads starting while the "world is stopped" */
static int all_threads_suspended = FALSE;
static int threads_waiting_to_start = 0;
static int main_exited = FALSE;
/* Bitmap - used for generating unique thread ID's */
#define MAP_INC 32
static unsigned int *tidBitmap = NULL;
static int tidBitmapSize = 0;
/* Mark a threadID value as no longer used */
#define freeThreadID(n) tidBitmap[(n-1)>>5] &= ~(1<<((n-1)&0x1f))
/* Generate a new thread ID - assumes the thread queue
* lock is held */
static int genThreadID() {
int i = 0;
retry:
for(; i < tidBitmapSize; i++) {
if(tidBitmap[i] != 0xffffffff) {
int n = ffs(~tidBitmap[i]);
tidBitmap[i] |= 1 << (n-1);
return (i<<5) + n;
}
}
tidBitmap = (unsigned int *)sysRealloc(tidBitmap,
(tidBitmapSize + MAP_INC) * sizeof(unsigned int));
memset(tidBitmap + tidBitmapSize, 0, MAP_INC * sizeof(unsigned int));
tidBitmapSize += MAP_INC;
goto retry;
}
int threadIsAlive(Thread *thread) {
return thread->state != 0;
}
int threadInterrupted(Thread *thread) {
int r = thread->interrupted;
thread->interrupted = FALSE;
return r;
}
int threadIsInterrupted(Thread *thread) {
return thread->interrupted;
}
void threadSleep(Thread *thread, long long ms, int ns) {
/* A sleep of zero should just yield, but a wait
of zero is "forever". Therefore wait for a tiny
amount -- this should yield and we also get the
interrupted checks. */
if(ms == 0 && ns == 0)
ns = 1;
monitorLock(&sleep_mon, thread);
monitorWait(&sleep_mon, thread, ms, ns);
monitorUnlock(&sleep_mon, thread);
}
void threadYield(Thread *thread) {
sched_yield();
}
void threadInterrupt(Thread *thread) {
Monitor *mon;
Thread *self = threadSelf();
/* monitorWait sets wait_mon _before_ checking interrupted status. Therefore,
if wait_mon is null, interrupted status will be noticed. This guards
against a race-condition leading to an interrupt being missed. The memory
barrier ensures correct ordering on SMP systems. */
thread->interrupted = TRUE;
MBARRIER();
if((mon = thread->wait_mon) != NULL && thread->wait_next != NULL) {
int locked;
thread->interrupting = TRUE;
/* Ensure the thread has actually entered the wait
(else the signal will be lost) */
while(!(locked = !pthread_mutex_trylock(&mon->lock)) && mon->owner == NULL)
sched_yield();
pthread_cond_signal(&thread->wait_cv);
if(locked)
pthread_mutex_unlock(&mon->lock);
}
/* Handle the case where the thread is blocked in a system call.
This will knock it out with an EINTR. The suspend signal
handler will just return (as in the user doing a kill), and do
nothing otherwise. */
/* Note, under Linuxthreads pthread_kill obtains a lock on the thread
being signalled. If another thread is suspending all threads,
and the interrupting thread is earlier in the thread list than the
thread being interrupted, it can be suspended holding the lock.
When the suspending thread tries to signal the interrupted thread
it will deadlock. To prevent this, disable suspension. */
fastDisableSuspend(self);
pthread_kill(thread->tid, SIGUSR1);
fastEnableSuspend(self);
}
void *getStackTop(Thread *thread) {
assert(false);
return thread->stack_top;
}
void *getStackBase(Thread *thread) {
assert(false);
return thread->stack_base;
}
Thread *threadSelf0(Object *vmThread) {
return (Thread*)(INST_DATA(vmThread)[vmData_offset]);
}
Thread *threadSelf() {
//std::cerr << "threadSelf";
return (Thread*)pthread_getspecific(threadKey);
}
void setThreadSelf(Thread *thread) {
pthread_setspecific(threadKey, thread);
pthread_cond_init(&thread->wait_cv, NULL);
}
const char *getThreadStateString(Thread *thread) {
switch(thread->state) {
case CREATING:
case STARTED:
return "NEW";
case RUNNING:
case SUSPENDED:
return "RUNNABLE";
case WAITING:
return "WAITING";
case TIMED_WAITING:
return "TIMED_WAITING";
case BLOCKED:
return "BLOCKED";
}
return "INVALID";
}
int getThreadsCount() {
return threads_count;
}
int getPeakThreadsCount() {
return peak_threads_count;
}
long long getTotalStartedThreadsCount() {
return total_started_threads_count;
}
void resetPeakThreadsCount() {
Thread *self = threadSelf();
/* Grab the thread lock to protect against
concurrent update by threads starting/dying */
disableSuspend(self);
pthread_mutex_lock(&lock);
peak_threads_count = threads_count;
pthread_mutex_unlock(&lock);
enableSuspend(self);
}
//void initialiseJavaStack(ExecEnv *ee) {
void initialiseJavaStack(Thread* thread) {
//assert(false);
Frame* top = new Frame(1, 1);
MethodBlock* mb = new MethodBlock;
top->prev = NULL;
top->mb = mb;
//thread->ee->last_frame = top;
//assert(g_get_current_spmt_thread()->is_certain_mode());
thread->get_current_spmt_thread()->get_current_mode()->frame = top;
}
long long javaThreadId(Thread *thread) {
//return *(long long*)&(INST_DATA(thread->ee->thread)[threadId_offset]);
return *(long long*)&(INST_DATA(thread->thread)[threadId_offset]);
}
Thread *findHashedThread(Thread *thread, long long id) {
#define FOUND(ptr) ptr
#define DELETED ((void*)-1)
#define PREPARE(thread_id) thread
#define SCAVENGE(ptr) ptr == DELETED
#define HASH(thread_id) (int)thread_id
#define COMPARE(thread_id, ptr, hash1, hash2) ptr != DELETED && \
(hash1 == hash2 && thread_id == javaThreadId(ptr))
Thread *ptr;
/* Add if absent, scavenge, locked */
findHashEntry(thread_id_map, id, ptr, (thread != NULL), TRUE, TRUE, Thread*);
return ptr;
}
void addThreadToHash(Thread *thread) {
findHashedThread(thread, javaThreadId(thread));
}
Thread *findThreadById(long long id) {
return findHashedThread(NULL, id);
}
void deleteThreadFromHash(Thread *thread) {
#undef HASH
#undef COMPARE
#define HASH(ptr) (int)javaThreadId(ptr)
#define COMPARE(ptr1, ptr2, hash1, hash2) ptr1 == ptr2
deleteHashEntry(thread_id_map, thread, TRUE);
}
Object *initJavaThread(Thread *thread, char is_daemon, const char *name) {
Object *vmthread, *jlthread, *thread_name = NULL;
/* Create the java.lang.Thread object and the VMThread */
if((vmthread = allocObject(vmthread_class)) == NULL ||
(jlthread = allocObject(thread_class)) == NULL)
return NULL;
//thread->ee->thread = jlthread;
thread->thread = jlthread;
INST_DATA(vmthread)[vmData_offset] = (uintptr_t)thread;
INST_DATA(vmthread)[thread_offset] = (uintptr_t)jlthread;
/* Create the string for the thread name. If null is specified
the initialiser method will generate a name of the form Thread-X */
if(name != NULL && (thread_name = Cstr2String(name)) == NULL)
return NULL;
/* Call the initialiser method -- this is for use by threads
created or attached by the VM "outside" of Java */
DummyFrame dummy;
executeMethod(&dummy, jlthread, init_mb, vmthread, thread_name, NORM_PRIORITY, is_daemon);
/* Add thread to thread ID map hash table. */
addThreadToHash(thread);
return jlthread;
}
void initThread(Thread *thread, char is_daemon, void *stack_base) {
/* Create the thread stack and store the thread structure in
thread-specific memory */
//initialiseJavaStack(thread->ee);
initialiseJavaStack(thread);
setThreadSelf(thread);
/* Record the thread's stack base */
thread->stack_base = stack_base;
/* Grab thread list lock. This also stops suspension */
pthread_mutex_lock(&lock);
/* If all threads are suspended (i.e. GC in progress) we cannot start
until the threads are resumed. Record we're waiting and wait until
the world is restarted... */
threads_waiting_to_start++;
while(all_threads_suspended)
pthread_cond_wait(&cv, &lock);
threads_waiting_to_start--;
/* add to thread list... After this point (once we release the lock)
we are suspendable */
if((thread->next = main_thread.next))
main_thread.next->prev = thread;
thread->prev = &main_thread;
main_thread.next = thread;
/* Keep track of threads counts */
if(++threads_count > peak_threads_count)
peak_threads_count = threads_count;
total_started_threads_count++;
/* If we're not a deamon thread the main thread must
wait until we exit */
if(!is_daemon)
non_daemon_thrds++;
/* Get a thread ID (used in thin locking) and
record we're now running */
thread->id = genThreadID();
thread->state = RUNNING;
pthread_cond_broadcast(&cv);
pthread_mutex_unlock(&lock);
}
Thread *attachThread(char *name, char is_daemon, void *stack_base, Thread *thread, Object *group) {
Object *java_thread;
/* Create the ExecEnv for the thread */
// ExecEnv *ee = (ExecEnv*)sysMalloc(sizeof(ExecEnv));
// memset(ee, 0, sizeof(ExecEnv));
thread->tid = pthread_self();
//thread->ee = ee;
/* Complete initialisation of the thread structure, create the thread
stack and add the thread to the thread list */
initThread(thread, is_daemon, stack_base);
/* Initialise the Java-level thread objects representing this thread */
if((java_thread = initJavaThread(thread, is_daemon, name)) == NULL)
return NULL;
/* Initialiser doesn't handle the thread group */
INST_DATA(java_thread)[group_offset] = (uintptr_t)group;
DummyFrame dummy;
executeMethod(&dummy, group, addThread_mb, java_thread);
/* We're now attached to the VM...*/
TRACE("Thread 0x%x id: %d attached\n", thread, thread->id);
return thread;
}
void detachThread(Thread *thread) {
Object *group, *excep;
//ExecEnv *ee = thread->ee;
//Object *jThread = ee->thread;
Object *jThread = thread->thread;
Object *vmthread = (Object*)INST_DATA(jThread)[vmthread_offset];
/* Get the thread's group */
group = (Object *)INST_DATA(jThread)[group_offset];
/* If there's an uncaught exception, call uncaughtException on the thread's
exception handler, or the thread's group if this is unset */
if((excep = exceptionOccurred())) {
FieldBlock *fb = findField(thread_class, SYMBOL(exceptionHandler),
SYMBOL(sig_java_lang_Thread_UncaughtExceptionHandler));
Object *thread_handler = fb == NULL ? NULL : (Object *)INST_DATA(jThread)[fb->offset];
Object *handler = thread_handler == NULL ? group : thread_handler;
MethodBlock *uncaught_exp = lookupMethod(handler->classobj, SYMBOL(uncaughtException),
SYMBOL(_java_lang_Thread_java_lang_Throwable__V));
if(uncaught_exp) {
clearException();
DummyFrame dummy;
executeMethod(&dummy, handler, uncaught_exp, jThread, excep);
} else
printException();
}
/* remove thread from thread group */
DummyFrame dummy;
executeMethod(&dummy, group, (CLASS_CB(group->classobj))->method_table[rmveThrd_mtbl_idx], jThread);
/* set VMThread ref in Thread object to null - operations after this
point will result in an IllegalThreadStateException */
INST_DATA(jThread)[vmthread_offset] = 0;
/* Remove thread from the ID map hash table */
deleteThreadFromHash(thread);
/* Disable suspend to protect lock operation */
disableSuspend(thread);
/* Grab global lock, and update thread structures protected by
it (thread list, thread ID and number of daemon threads) */
pthread_mutex_lock(&lock);
/* remove from thread list... */
if((thread->prev->next = thread->next))
thread->next->prev = thread->prev;
/* One less live thread */
threads_count--;
/* Recycle the thread's thread ID */
freeThreadID(thread->id);
/* Handle daemon thread status */
if(!INST_DATA(jThread)[daemon_offset])
non_daemon_thrds--;
pthread_mutex_unlock(&lock);
/* notify any threads waiting on VMThread object -
these are joining this thread */
objectLock(vmthread);
objectNotifyAll(vmthread);
objectUnlock(vmthread);
/* It is safe to free the thread's ExecEnv and stack now as these are
only used within the thread. It is _not_ safe to free the native
thread structure as another thread may be concurrently accessing it.
However, they must have a reference to the VMThread -- therefore, it
is safe to free during GC when the VMThread is determined to be no
longer reachable. */
// sysFree(ee->stack);
//sysFree(ee);
/* If no more daemon threads notify the main thread (which
may be waiting to exit VM). Note, this is not protected
by lock, but main thread checks again */
if(non_daemon_thrds == 0) {
/* No need to bother with disabling suspension
* around lock, as we're no longer on thread list */
pthread_mutex_lock(&exit_lock);
pthread_cond_signal(&exit_cv);
pthread_mutex_unlock(&exit_lock);
}
TRACE("Thread 0x%x id: %d detached from VM\n", thread, thread->id);
}
void *threadStart(void *arg) {
Thread *thread = (Thread *)arg;
//Object *jThread = thread->ee->thread;
Object *jThread = thread->thread;
/* Parent thread created thread with suspension disabled.
This is inherited so we need to enable */
enableSuspend(thread);
/* Complete initialisation of the thread structure, create the thread
stack and add the thread to the thread list */
initThread(thread, INST_DATA(jThread)[daemon_offset], &thread);
/* Add thread to thread ID map hash table. */
addThreadToHash(thread);
/* Execute the thread's run method */
DummyFrame dummy;
executeMethod(&dummy, jThread, CLASS_CB(jThread->classobj)->method_table[run_mtbl_idx]);
/* Run has completed. Detach the thread from the VM and exit */
detachThread(thread);
TRACE("Thread 0x%x id: %d exited\n", thread, thread->id);
return NULL;
}
void createJavaThread(Object *jThread, long long stack_size) {
//ExecEnv *ee;
Thread *thread;
Thread *self = threadSelf();
Object *vmthread = allocObject(vmthread_class);
if(vmthread == NULL)
return;
disableSuspend(self);
pthread_mutex_lock(&lock);
if(INST_DATA(jThread)[vmthread_offset]) {
pthread_mutex_unlock(&lock);
enableSuspend(self);
signalException(java_lang_IllegalThreadStateException, "thread already started");
return;
}
//ee = (ExecEnv*)sysMalloc(sizeof(ExecEnv));
thread = new Thread;
//memset(ee, 0, sizeof(ExecEnv));
// thread->ee = ee;
// ee->thread = jThread;
thread->thread = jThread;
// ee->stack_size = stack_size;
INST_DATA(vmthread)[vmData_offset] = (uintptr_t)thread;
INST_DATA(vmthread)[thread_offset] = (uintptr_t)jThread;
INST_DATA(jThread)[vmthread_offset] = (uintptr_t)vmthread;
pthread_mutex_unlock(&lock);
if(pthread_create(&thread->tid, &attributes, threadStart, thread)) {
INST_DATA(jThread)[vmthread_offset] = 0;
//sysFree(ee);
enableSuspend(self);
signalException(java_lang_OutOfMemoryError, "can't create thread");
return;
}
pthread_mutex_lock(&lock);
/* Wait for thread to start */
while(thread->state == 0)
pthread_cond_wait(&cv, &lock);
pthread_mutex_unlock(&lock);
enableSuspend(self);
}
static void initialiseSignals();
Thread *attachJNIThread(char *name, char is_daemon, Object *group) {
Thread *thread = new Thread;
void *stack_base = nativeStackBase();
/* If no group is given add it to the main group */
if(group == NULL)
//group = (Object*)INST_DATA(main_ee.thread)[group_offset];
group = (Object*)INST_DATA(main_thread.thread)[group_offset];
/* Initialise internal thread structure */
// memset(thread, 0, sizeof(Thread));
/* Externally created threads will not inherit signal state */
initialiseSignals();
/* Initialise the thread and add it to the VM thread list */
return attachThread(name, is_daemon, stack_base, thread, group);
}
void detachJNIThread(Thread *thread) {
/* The JNI spec says we should release all locks held by this thread.
We don't do this (yet), and only remove the thread from the VM. */
detachThread(thread);
}
static void *shell(void *args) {
void *start = ((void**)args)[1];
Thread *self = ((Thread**)args)[2];
if(main_exited)
return NULL;
/* VM helper threads should be added to the system group, but this doesn't
exist. As the root group is main, we add it to that for now... */
attachThread(((char**)args)[0], TRUE, &self, self,
//(Object*)INST_DATA(main_ee.thread)[group_offset]);
(Object*)INST_DATA(main_thread.thread)[group_offset]);
sysFree(args);
(*(void(*)(Thread*))start)(self);
return NULL;
}
void createVMThread(const char *name, void (*start)(Thread*)) {
Thread *thread = new Thread;
void **args = (void**)sysMalloc(3 * sizeof(void*));
pthread_t tid;
args[0] = const_cast<char*>(name);
args[1] = (void*)start;
args[2] = thread;
pthread_create(&tid, &attributes, shell, args);
/* Wait for thread to start */
pthread_mutex_lock(&lock);
while(thread->state == 0)
pthread_cond_wait(&cv, &lock);
pthread_mutex_unlock(&lock);
}
void suspendThread(Thread *thread) {
thread->suspend = TRUE;
MBARRIER();
if(!thread->blocking) {
TRACE("Sending suspend signal to thread 0x%x id: %d\n", thread, thread->id);
pthread_kill(thread->tid, SIGUSR1);
}
while(thread->blocking != SUSP_BLOCKING && thread->state != SUSPENDED) {
TRACE("Waiting for thread 0x%x id: %d to suspend\n", thread, thread->id);
sched_yield();
}
}
void resumeThread(Thread *thread) {
thread->suspend = FALSE;
MBARRIER();
if(!thread->blocking) {
TRACE("Sending resume signal to thread 0x%x id: %d\n", thread, thread->id);
pthread_kill(thread->tid, SIGUSR1);
}
while(thread->state == SUSPENDED) {
TRACE("Waiting for thread 0x%x id: %d to resume\n", thread, thread->id);
sched_yield();
}
}
void suspendAllThreads(Thread *self) {
Thread *thread;
TRACE("Thread 0x%x id: %d is suspending all threads\n", self, self->id);
pthread_mutex_lock(&lock);
for(thread = &main_thread; thread != NULL; thread = thread->next) {
if(thread == self)
continue;
thread->suspend = TRUE;
MBARRIER();
if(!thread->blocking) {
TRACE("Sending suspend signal to thread 0x%x id: %d\n", thread, thread->id);
pthread_kill(thread->tid, SIGUSR1);
}
}
for(thread = &main_thread; thread != NULL; thread = thread->next) {
if(thread == self)
continue;
while(thread->blocking != SUSP_BLOCKING && thread->state != SUSPENDED) {
TRACE("Waiting for thread 0x%x id: %d to suspend\n", thread, thread->id);
sched_yield();
}
}
all_threads_suspended = TRUE;
TRACE("All threads suspended...\n");
pthread_mutex_unlock(&lock);
}
void resumeAllThreads(Thread *self) {
Thread *thread;
TRACE("Thread 0x%x id: %d is resuming all threads\n", self, self->id);
pthread_mutex_lock(&lock);
for(thread = &main_thread; thread != NULL; thread = thread->next) {
if(thread == self)
continue;
thread->suspend = FALSE;
MBARRIER();
if(!thread->blocking) {
TRACE("Sending resume signal to thread 0x%x id: %d\n", thread, thread->id);
pthread_kill(thread->tid, SIGUSR1);
}
}
for(thread = &main_thread; thread != NULL; thread = thread->next) {
while(thread->state == SUSPENDED) {
TRACE("Waiting for thread 0x%x id: %d to resume\n", thread, thread->id);
sched_yield();
}
}
all_threads_suspended = FALSE;
if(threads_waiting_to_start) {
TRACE("%d threads waiting to start...\n", threads_waiting_to_start);
pthread_cond_broadcast(&cv);
}
TRACE("All threads resumed...\n");
pthread_mutex_unlock(&lock);
}
static void suspendLoop(Thread *thread) {
char old_state = thread->state;
sigjmp_buf env;
sigset_t mask;
sigsetjmp(env, FALSE);
thread->stack_top = &env;
thread->state = SUSPENDED;
MBARRIER();
sigfillset(&mask);
sigdelset(&mask, SIGUSR1);
sigdelset(&mask, SIGTERM);
while(thread->suspend && !thread->blocking)
sigsuspend(&mask);
thread->state = old_state;
MBARRIER();
}
static void suspendHandler(int sig) {
Thread *thread = threadSelf();
suspendLoop(thread);
}
/* The "slow" disable. Normally used when a thread enters
a blocking code section, such as waiting on a lock. */
void disableSuspend0(Thread *thread, void *stack_top) {
sigset_t mask;
thread->stack_top = stack_top;
thread->blocking = SUSP_BLOCKING;
MBARRIER();
sigemptyset(&mask);
sigaddset(&mask, SIGUSR1);
pthread_sigmask(SIG_BLOCK, &mask, NULL);
}
void enableSuspend(Thread *thread) {
sigset_t mask;
thread->blocking = FALSE;
MBARRIER();
if(thread->suspend) {
TRACE("Thread 0x%x id: %d is self suspending\n", thread, thread->id);
suspendLoop(thread);
TRACE("Thread 0x%x id: %d resumed\n", thread, thread->id);
}
sigemptyset(&mask);
sigaddset(&mask, SIGUSR1);
pthread_sigmask(SIG_UNBLOCK, &mask, NULL);
}
/* Fast disable/enable suspend doesn't modify the signal mask
or save the thread context. It's used in critical code
sections where the thread cannot be suspended (such as
modifying the loaded classes hash table). Thread supension
blocks until the thread self-suspends. */
void fastEnableSuspend(Thread *thread) {
thread->blocking = FALSE;
MBARRIER();
if(thread->suspend) {
sigset_t mask;
sigemptyset(&mask);
sigaddset(&mask, SIGUSR1);
pthread_sigmask(SIG_BLOCK, &mask, NULL);
TRACE("Thread 0x%x id: %d is fast self suspending\n", thread, thread->id);
suspendLoop(thread);
TRACE("Thread 0x%x id: %d resumed\n", thread, thread->id);
pthread_sigmask(SIG_UNBLOCK, &mask, NULL);
}
}
void dumpThreadsLoop(Thread *self) {
//return; // do nothing for now
//assert(false);
char buffer[256];
Thread *thread;
sigset_t mask;
int sig;
sigemptyset(&mask);
sigaddset(&mask, SIGQUIT);
sigaddset(&mask, SIGINT);
disableSuspend0(self, &self);
for(;;) {
sigwait(&mask, &sig);
/* If it was an interrupt (e.g. Ctrl-C) terminate the VM */
if(sig == SIGINT)
exitVM(0);
/* It must be a SIGQUIT. Do a thread dump */
suspendAllThreads(self);
jam_printf("\n------ JamVM version %s Full Thread Dump -------\n", VERSION);
for(thread = &main_thread; thread != NULL; thread = thread->next) {
//uintptr_t *thr_data = INST_DATA(thread->ee->thread);
uintptr_t *thr_data = INST_DATA(thread->thread);
int priority = thr_data[priority_offset];
int daemon = thr_data[daemon_offset];
assert(false);
//Frame *last = thread->ee->last_frame;
Frame* last = threadSelf()->get_current_spmt_thread()->get_current_mode()->frame;
/* Get thread name; we don't use String2Cstr(), as this mallocs memory
and may deadlock with a thread suspended in malloc/realloc/free */
String2Buff((Object*)thr_data[name_offset], buffer, sizeof(buffer));
jam_printf("\n\"%s\"%s %p priority: %d tid: %p id: %d state: %s (%d)\n",
buffer, daemon ? " (daemon)" : "", thread, priority, thread->tid,
thread->id, getThreadStateString(thread), thread->state);
while(last->prev != NULL) {
for(; last->mb != NULL; last = last->prev) {
MethodBlock *mb = last->mb;
ClassBlock *cb = CLASS_CB(mb->classobj);
/* Convert slashes in class name to dots. Similar to above,
we don't use slash2dots(), as this mallocs memory */
slash2dots2buff(cb->name, buffer, sizeof(buffer));
jam_printf("\tat %s.%s(", buffer, mb->name);
if(mb->is_native())
jam_printf("Native method");
else
if(cb->source_file_name == NULL)
jam_printf("Unknown source");
else {
int line = mapPC2LineNo(mb, last->last_pc);
jam_printf("%s", cb->source_file_name);
if(line != -1)
jam_printf(":%d", line);
}
jam_printf(")\n");
}
last = last->prev;
}
}
resumeAllThreads(self);
}
}
static void initialiseSignals() {
struct sigaction act;
sigset_t mask;
act.sa_handler = suspendHandler;
sigemptyset(&act.sa_mask);
act.sa_flags = 0;
sigaction(SIGUSR1, &act, NULL);
sigemptyset(&mask);
sigaddset(&mask, SIGQUIT);
sigaddset(&mask, SIGINT);
sigaddset(&mask, SIGPIPE);
sigprocmask(SIG_BLOCK, &mask, NULL);
}
/* garbage collection support */
extern void scanThread(Thread *thread);
void scanThreads() {
Thread *thread;
pthread_mutex_lock(&lock);
for(thread = &main_thread; thread != NULL; thread = thread->next)
scanThread(thread);
pthread_mutex_unlock(&lock);
}
int systemIdle(Thread *self) {
Thread *thread;
for(thread = &main_thread; thread != NULL; thread = thread->next)
if(thread != self && thread->state < WAITING)
return FALSE;
return TRUE;
}
void exitVM(int status) {
main_exited = TRUE;
/* Execute System.exit() to run any registered shutdown hooks.
In the unlikely event that System.exit() can't be found, or
it returns, fall through and exit. */
if(!VMInitialising()) {
Class *system = findSystemClass(SYMBOL(java_lang_System));
if(system) {
MethodBlock *exit = findMethod(system, SYMBOL(exit), SYMBOL(_I__V));
if(exit) {
DummyFrame dummy;
executeStaticMethod(&dummy, system, exit, status);
}
}
}
jamvm_exit(status);
}
void mainThreadWaitToExitVM() {
Thread *self = threadSelf();
TRACE("Waiting for %d non-daemon threads to exit\n", non_daemon_thrds);
disableSuspend(self);
pthread_mutex_lock(&exit_lock);
self->state = WAITING;
while(non_daemon_thrds)
pthread_cond_wait(&exit_cv, &exit_lock);
pthread_mutex_unlock(&exit_lock);
enableSuspend(self);
}
void mainThreadSetContextClassLoader(Object *loader) {
FieldBlock *fb = findField(thread_class, SYMBOL(contextClassLoader),
SYMBOL(sig_java_lang_ClassLoader));
if(fb != NULL)
//INST_DATA(main_ee.thread)[fb->offset] = (uintptr_t)loader;