00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040 #include "arch.h"
00041 ARCH_COPYRIGHT_APACHE(thread, c, "$URL: https://svn.apache.org/path/name/thread.c $ $Id: thread.c 0 09/28/2005 dlydick $");
00042
00043
00044
00045 #include <strings.h>
00046
00047 #include "jvmcfg.h"
00048 #include "cfmacros.h"
00049 #include "classfile.h"
00050 #include "exit.h"
00051 #include "gc.h"
00052 #include "jvm.h"
00053 #include "jvmclass.h"
00054 #include "linkage.h"
00055 #include "method.h"
00056 #include "util.h"
00057 #include "utf.h"
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070 rvoid thread_init()
00071 {
00072 for (CURRENT_THREAD = jvm_thread_index_null;
00073 CURRENT_THREAD < JVMCFG_MAX_THREADS;
00074 CURRENT_THREAD++)
00075 {
00076 jvm_thread_index thridx = CURRENT_THREAD;
00077
00078
00079
00080 THREAD(thridx).id = thridx;
00081
00082 THREAD(thridx).name = HEAP_GET_DATA(THREAD_NAME_MAX_LEN,rfalse);
00083 sprintfLocal(THREAD(thridx).name, "thread %d", thridx);
00084
00085 THREAD(thridx).thread_objhash = jvm_object_hash_null;
00086
00087 THREAD(thridx).priority = THREAD_PRIORITY_MIN;
00088
00089 THREAD(thridx).status = THREAD_STATUS_EMPTY;
00090
00091 THREAD(thridx).prev_state = THREAD_STATE_DEAD;
00092 THREAD(thridx).this_state = THREAD_STATE_DEAD;
00093 THREAD(thridx).next_state = THREAD_STATE_DEAD;
00094
00095 THREAD(thridx).jointarget = jvm_thread_index_null;
00096 THREAD(thridx).sleeptime = 0;
00097 THREAD(thridx).locktarget = jvm_object_hash_null;
00098
00099
00100
00101
00102 PUT_PC_IMMEDIATE(thridx,
00103 jvm_class_index_null,
00104 jvm_method_index_bad,
00105 jvm_attribute_index_bad,
00106 jvm_attribute_index_bad,
00107 jvm_pc_offset_bad);
00108
00109 THREAD(thridx).pass_instruction_count = 0;
00110 THREAD(thridx).thread_instruction_count = 0;
00111
00112 THREAD(thridx).stack = (jint *) rnull;
00113 THREAD(thridx).sp = JVMCFG_NULL_SP;
00114 THREAD(thridx).fp = JVMCFG_NULL_SP;
00115 THREAD(thridx).fp_end_program = JVMCFG_NULL_SP;
00116
00117 }
00118
00119
00120
00121
00122
00123
00124
00125 THREAD(jvm_thread_index_null).priority = THREAD_PRIORITY_MAX;
00126 THREAD(jvm_thread_index_null).status = THREAD_STATUS_INUSE |
00127 THREAD_STATUS_NULL;
00128 THREAD(jvm_thread_index_null).this_state = THREAD_STATE_NEW;
00129 THREAD(jvm_thread_index_null).next_state = THREAD_STATE_NEW;
00130
00131 THREAD(JVMCFG_SYSTEM_THREAD).priority = THREAD_PRIORITY_MIN;
00132 THREAD(JVMCFG_SYSTEM_THREAD).status = THREAD_STATUS_EMPTY;
00133
00134 THREAD(JVMCFG_GC_THREAD).priority = THREAD_PRIORITY_MAX;
00135 THREAD(JVMCFG_GC_THREAD).status = THREAD_STATUS_INUSE;
00136 THREAD(JVMCFG_GC_THREAD).this_state = THREAD_STATE_NEW;
00137 THREAD(JVMCFG_GC_THREAD).next_state = THREAD_STATE_NEW;
00138
00139
00140
00141 pjvm->thread_new_last = JVMCFG_GC_THREAD;
00142
00143
00144 CURRENT_THREAD = JVMCFG_SYSTEM_THREAD;
00145
00146
00147 jvm_thread_initialized = rtrue;
00148
00149 return;
00150
00151 }
00152
00153
00154
00155
00156
00157
00158 const rchar *thread_state_names[THREAD_STATE_MAX_STATE + 1 + 1] =
00159 {
00160 THREAD_STATE_NEW_DESC,
00161 THREAD_STATE_START_DESC,
00162 THREAD_STATE_RUNNABLE_DESC,
00163 THREAD_STATE_RUNNING_DESC,
00164 THREAD_STATE_COMPLETE_DESC,
00165 THREAD_STATE_BLOCKINGEVENT_DESC,
00166 THREAD_STATE_BLOCKED_DESC,
00167 THREAD_STATE_UNBLOCKED_DESC,
00168 THREAD_STATE_SYNCHRONIZED_DESC,
00169 THREAD_STATE_RELEASE_DESC,
00170 THREAD_STATE_WAIT_DESC,
00171 THREAD_STATE_NOTIFY_DESC,
00172 THREAD_STATE_LOCK_DESC,
00173 THREAD_STATE_ACQUIRE_DESC,
00174 THREAD_STATE_DEAD_DESC,
00175 THREAD_STATE_BADLOGIC_DESC,
00176
00177
00178 THREAD_STATE_ILLEGAL_DESC
00179 };
00180
00181
00182
00183
00184
00185
00186
00187
00188
00189
00190 const rchar *thread_state_get_name(rushort state)
00191 {
00192
00193
00194
00195
00196
00197
00198
00199
00200
00201 if (THREAD_STATE_MAX_STATE < state)
00202 {
00203
00204 return(thread_state_names[THREAD_STATE_MAX_STATE + 1]);
00205 }
00206
00207 return(thread_state_names[state]);
00208
00209 }
00210
00211
00212
00213
00214
00215
00216
00217
00218
00219
00220
00221
00222
00223
00224
00225
00226
00227
00228 static jvm_thread_index thread_allocate_slot(rvoid)
00229 {
00230 jvm_thread_index thridx =
00231 (JVMCFG_MAX_THREADS == (1 + pjvm->thread_new_last))
00232 ? 1 + pjvm->thread_new_last
00233 : JVMCFG_FIRST_THREAD;
00234
00235
00236 jvm_thread_index count = 0;
00237
00238 while(rtrue)
00239 {
00240 if (THREAD(thridx).status & THREAD_STATUS_INUSE)
00241 {
00242
00243 thridx++;
00244
00245 if (thridx == JVMCFG_MAX_THREADS)
00246 {
00247 thridx = JVMCFG_FIRST_THREAD;
00248 }
00249
00250
00251 if (pjvm->thread_new_last == JVMCFG_MAX_THREADS - 1)
00252 {
00253 pjvm->thread_new_last = JVMCFG_FIRST_THREAD;
00254 }
00255
00256
00257
00258 count++;
00259
00260 if (count == (JVMCFG_MAX_THREADS - JVMCFG_FIRST_THREAD))
00261 {
00262
00263 exit_throw_exception(EXIT_JVM_THREAD,
00264 JVMCLASS_JAVA_LANG_OUTOFMEMORYERROR);
00265
00266 }
00267
00268
00269 continue;
00270 }
00271
00272
00273
00274
00275 THREAD(thridx).status =
00276 THREAD_STATUS_INUSE | THREAD_STATUS_NULL;
00277
00278
00279
00280
00281 pjvm->thread_new_last = thridx;
00282
00283 return(thridx);
00284 }
00285
00286 return(jvm_thread_index_null);
00287
00288 }
00289
00290
00291
00292
00293
00294
00295
00296
00297
00298
00299
00300
00301
00302
00303
00304
00305
00306
00307
00308
00309
00310
00311
00312
00313
00314
00315
00316
00317
00318
00319
00320
00321
00322
00323
00324
00325
00326
00327
00328
00329
00330
00331
00332
00333
00334
00335
00336
00337
00338
00339
00340
00341
00342
00343
00344
00345
00346
00347
00348
00349
00350
00351
00352
00353
00354
00355
00356
00357
00358
00359
00360
00361
00362
00363
00364
00365 static jvm_thread_index thread_new_common(jvm_thread_index thridx,
00366 jvm_class_index clsidx,
00367 jvm_method_index mthidx,
00368 jvm_attribute_index codeatridx,
00369 jvm_attribute_index excpatridx,
00370 rint priority,
00371 rboolean isdaemon)
00372 {
00373
00374
00375
00376
00377
00378
00379
00380
00381 THREAD(thridx).status = THREAD_STATUS_INUSE | THREAD_STATUS_NULL;
00382
00383
00384 ClassFile *pcfs = CLASS_OBJECT_LINKAGE(clsidx)->pcfs;
00385 Code_attribute *pca = (Code_attribute *)
00386 &pcfs->methods[mthidx]->attributes[codeatridx];
00387
00388
00389 if (JVMCFG_MAX_SP <= GET_SP(thridx) +
00390 JVMREG_STACK_MIN_FRAME_HEIGHT +
00391 JVMREG_STACK_PC_HEIGHT +
00392 pca->max_stack +
00393 pca->max_locals)
00394 {
00395 exit_throw_exception(EXIT_THREAD_STACK,
00396 JVMCLASS_JAVA_LANG_STACKOVERFLOWERROR);
00397
00398 }
00399
00400
00401
00402
00403
00404
00405
00406
00407
00408
00409
00410 if (JVMCFG_SYSTEM_THREAD == thridx)
00411 {
00412 THREAD(thridx).thread_objhash = jvm_object_hash_null;
00413 }
00414 else
00415 {
00416 jvm_class_index clsidxTHR =
00417 class_find_by_prchar(JVMCLASS_JAVA_LANG_THREAD);
00418
00419 if (jvm_class_index_null == clsidxTHR)
00420 {
00421
00422 THREAD(thridx).status = THREAD_STATUS_EMPTY;
00423
00424
00425 exit_throw_exception(EXIT_JVM_THREAD,
00426 JVMCLASS_JAVA_LANG_OUTOFMEMORYERROR);
00427
00428 }
00429
00430 jvm_table_linkage *ptl = CLASS_OBJECT_LINKAGE(clsidxTHR);
00431
00432 jvm_object_hash objhashTHR =
00433 object_instance_new(OBJECT_STATUS_EMPTY,
00434 ptl->pcfs,
00435 ptl->clsidx,
00436 LOCAL_CONSTANT_NO_ARRAY_DIMS,
00437 (jint *) rnull,
00438 rtrue,
00439 thridx);
00440
00441 THREAD(thridx).thread_objhash = objhashTHR;
00442 (rvoid) GC_OBJECT_MKREF_FROM_OBJECT(jvm_object_hash_null,
00443 THREAD(thridx).thread_objhash);
00444 }
00445
00446
00447
00448
00449
00450
00451 if (rnull == THREAD(thridx).stack)
00452 {
00453 THREAD(thridx).stack =
00454 (jint *) HEAP_GET_STACK(JVMCFG_STACK_SIZE, rfalse);
00455 }
00456
00457
00458
00459
00460 PUT_SP_IMMEDIATE(thridx, JVMCFG_NULL_SP);
00461
00462
00463
00464
00465
00466
00467
00468
00469 PUT_SP_WORD(thridx, GET_SP(thridx), CLASSFILE_MAGIC);
00470
00471
00472
00473
00474
00475 PUT_FP_IMMEDIATE(thridx, GET_SP(thridx));
00476
00477
00478 PUT_PC_IMMEDIATE(thridx,
00479 jvm_class_index_null,
00480 jvm_method_index_bad,
00481 jvm_attribute_index_bad,
00482 jvm_attribute_index_bad,
00483 jvm_pc_offset_bad);
00484
00485
00486
00487
00488
00489
00490
00491
00492
00493
00494
00495
00496
00497
00498 PUSH_FRAME(thridx, pca->max_locals);
00499
00500
00501
00502
00503
00504
00505
00506
00507
00508
00509
00510
00511
00512
00513
00514
00515
00516 THREAD(thridx).priority = priority;
00517 threadstate_request_new(thridx);
00518 threadstate_activate_new(thridx);
00519
00520
00521
00522
00523
00524
00525
00526
00527
00528 if (rtrue == isdaemon)
00529 {
00530 THREAD(thridx).status |= THREAD_STATUS_ISDAEMON;
00531 }
00532
00533
00534 THREAD(thridx).status &= ~THREAD_STATUS_NULL;
00535
00536
00537 PUT_PC_IMMEDIATE(thridx,
00538 clsidx,
00539 mthidx,
00540 codeatridx,
00541 excpatridx,
00542 CODE_CONSTRAINT_START_PC);
00543
00544 return(thridx);
00545
00546 }
00547
00548
00549
00550
00551
00552
00553
00554
00555
00556 static jvm_thread_index thread_new_system(jvm_class_index clsidx,
00557 jvm_method_index mthidx,
00558 jvm_attribute_index codeatridx,
00559 jvm_attribute_index excpatridx,
00560 rint priority,
00561 rboolean isdaemon)
00562 {
00563
00564 if (THREAD(JVMCFG_SYSTEM_THREAD).status & THREAD_STATUS_INUSE)
00565 {
00566
00567 exit_throw_exception(EXIT_JVM_INTERNAL,
00568 JVMCLASS_JAVA_LANG_INTERNALERROR);
00569
00570 }
00571
00572
00573 return(thread_new_common(JVMCFG_SYSTEM_THREAD,
00574 clsidx,
00575 mthidx,
00576 codeatridx,
00577 excpatridx,
00578 priority,
00579 isdaemon));
00580
00581 }
00582
00583
00584
00585
00586
00587
00588
00589
00590
00591
00592 static jvm_thread_index thread_new(jvm_class_index clsidx,
00593 jvm_method_index mthidx,
00594 jvm_attribute_index codeatridx,
00595 jvm_attribute_index excpatridx,
00596 rint priority,
00597 rboolean isdaemon)
00598 {
00599 jvm_thread_index thridx = thread_allocate_slot();
00600
00601
00602 return(thread_new_common(thridx,
00603 clsidx,
00604 mthidx,
00605 codeatridx,
00606 excpatridx,
00607 priority,
00608 isdaemon));
00609
00610 }
00611
00612
00613
00614
00615
00616
00617
00618
00619
00620
00621
00622
00623
00624
00625
00626
00627
00628
00629
00630
00631
00632
00633
00634
00635
00636
00637
00638
00639
00640
00641
00642
00643
00644
00645
00646
00647
00648
00649
00650
00651
00652
00653
00654
00655
00656
00657
00658
00659
00660
00661
00662
00663
00664
00665
00666
00667
00668
00669
00670
00671
00672
00673
00674
00675
00676
00677
00678
00679
00680
00681
00682
00683 jvm_thread_index thread_class_load(rchar *clsname,
00684 rchar *mthname,
00685 rchar *mthdesc,
00686 rint priority,
00687 rboolean isdaemon,
00688 rboolean usesystemthread,
00689 rboolean find_registerNatives)
00690 {
00691
00692
00693
00694
00695
00696
00697 jvm_class_index clsidx = class_load_from_prchar(clsname,
00698 find_registerNatives,
00699 (jint *) rnull);
00700
00701
00702 ClassFile *pcfs = CLASS_OBJECT_LINKAGE(clsidx)->pcfs;
00703 ClassFile *pcfs_recurse = pcfs;
00704
00705
00706
00707
00708
00709 if (CONSTANT_CP_DEFAULT_INDEX == pcfs_recurse->super_class)
00710 {
00711 pcfs_recurse = (ClassFile *) rnull;
00712 }
00713 else
00714 {
00715 pcfs_recurse =
00716 CLASS_OBJECT_LINKAGE(
00717 class_find_by_prchar(
00718 PTR_CP1_CLASS_NAME_STRNAME(pcfs_recurse,
00719 pcfs_recurse->super_class)))
00720 ->pcfs;
00721 }
00722
00723
00724 while(rnull != pcfs_recurse)
00725 {
00726
00727 if (ACC_INTERFACE & pcfs_recurse->access_flags)
00728 {
00729 exit_throw_exception(EXIT_JVM_CLASS,
00730 JVMCLASS_JAVA_LANG_INCOMPATIBLECLASSCHANGEERROR);
00731
00732 }
00733
00734
00735
00736
00737
00738
00739
00740
00741
00742
00743 if (CONSTANT_CP_DEFAULT_INDEX == pcfs_recurse->super_class)
00744 {
00745 break;
00746 }
00747
00748
00749 if (0 == utf_prchar_classname_strcmp(
00750 clsname,
00751 pcfs_recurse,
00752 pcfs_recurse->super_class))
00753 {
00754
00755
00756
00757
00758 if (0 == strcmp(clsname,
00759 JVMCLASS_JAVA_LANG_CLASSCIRCULARITYERROR))
00760 {
00761 exit_throw_exception(EXIT_JVM_CLASS,
00762 JVMCLASS_JAVA_LANG_CLASSCIRCULARITYERROR);
00763
00764 }
00765 }
00766
00767
00768 pcfs_recurse =
00769 CLASS_OBJECT_LINKAGE(
00770 class_find_by_prchar(
00771 PTR_CP1_CLASS_NAME_STRNAME(pcfs_recurse,
00772 pcfs_recurse->super_class)))
00773 ->pcfs;
00774
00775 }
00776
00777
00778
00779
00780
00781
00782
00783
00784
00785
00786 jvm_method_index mthidx = method_find_by_prchar(clsidx,
00787 mthname,
00788 mthdesc);
00789
00790
00791 if (jvm_method_index_bad == mthidx)
00792 {
00793 exit_throw_exception(EXIT_JVM_CLASS,
00794 JVMCLASS_JAVA_LANG_NOSUCHMETHODERROR);
00795
00796 }
00797
00798
00799 jvm_attribute_index codeatridx =
00800 pcfs->methods[mthidx]->LOCAL_method_binding.codeatridxJVM;
00801
00802
00803 jvm_attribute_index excpatridx =
00804 pcfs->methods[mthidx]->LOCAL_method_binding.excpatridxJVM;
00805
00806
00807
00808
00809
00810
00811 if (jvm_attribute_index_bad == codeatridx)
00812 {
00813 exit_throw_exception(EXIT_JVM_ATTRIBUTE,
00814 JVMCLASS_JAVA_LANG_NOSUCHMETHODERROR);
00815
00816 }
00817
00818
00819 if (jvm_attribute_index_native == codeatridx)
00820 {
00821 exit_throw_exception(EXIT_JVM_INTERNAL,
00822 JVMCLASS_JAVA_LANG_NOSUCHMETHODERROR);
00823
00824 }
00825
00826
00827
00828
00829
00830 if (rtrue == usesystemthread)
00831 {
00832 return(thread_new_system(clsidx,
00833 mthidx,
00834 codeatridx,
00835 excpatridx,
00836 priority,
00837 isdaemon));
00838 }
00839 else
00840 {
00841 return(thread_new(clsidx,
00842 mthidx,
00843 codeatridx,
00844 excpatridx,
00845 priority,
00846 isdaemon));
00847 }
00848
00849 }
00850
00851
00852
00853
00854
00855
00856
00857
00858
00859
00860
00861
00862
00863
00864 rboolean thread_die(jvm_thread_index thridx)
00865 {
00866 if (THREAD_STATE_DEAD == THREAD(thridx).this_state)
00867 {
00868
00869
00870
00871
00872
00873
00874
00875
00876 HEAP_FREE_STACK(THREAD(thridx).stack);
00877 THREAD(thridx).stack = (jint *) rnull;
00878
00879 THREAD(thridx).status &= ~THREAD_STATUS_INUSE;
00880
00881 return(rtrue);
00882 }
00883
00884 return(rfalse);
00885
00886 }
00887
00888
00889
00890
00891
00892
00893
00894
00895
00896
00897
00898
00899
00900 rvoid thread_shutdown()
00901 {
00902 jvm_thread_index thridx;
00903
00904 for (thridx = jvm_thread_index_null;
00905 thridx < JVMCFG_MAX_THREADS;
00906 thridx++)
00907 {
00908 if (THREAD_STATUS_INUSE & THREAD(thridx).status)
00909 {
00910
00911
00912
00913 if (rnull != THREAD(thridx).stack)
00914 {
00915
00916 jvm_sp fp = FIRST_STACK_FRAME(thridx);
00917
00918
00919
00920
00921
00922 if (JVMCFG_NULL_SP == fp)
00923 {
00924 continue;
00925 }
00926
00927
00928
00929
00930
00931 while(!CHECK_FINAL_STACK_FRAME_GENERIC(thridx, fp))
00932 {
00933
00934
00935
00936
00937 fp = NEXT_STACK_FRAME_GENERIC(thridx, fp);
00938
00939 POP_FRAME(thridx);
00940 }
00941
00942
00943
00944
00945
00946
00947 threadstate_request_badlogic(thridx);
00948 threadstate_activate_badlogic(thridx);
00949 threadstate_process_badlogic(thridx);
00950 threadstate_request_complete(thridx);
00951 threadstate_activate_complete(thridx);
00952 threadstate_process_complete(thridx);
00953 threadstate_request_dead(thridx);
00954 threadstate_activate_dead(thridx);
00955 threadstate_process_dead(thridx);
00956 }
00957 }
00958
00959
00960
00961
00962
00963 if (rnull != THREAD(thridx).name)
00964 {
00965 HEAP_FREE_STACK(THREAD(thridx).name);
00966
00967 }
00968 }
00969
00970
00971 GC_RUN(rfalse);
00972
00973
00974 jvm_thread_initialized = rfalse;
00975
00976 return;
00977
00978 }
00979
00980
00981
00982
00983
00984
00985
00986
00987
00988
00989
00990
00991
00992
00993
00994
00995
00996
00997
00998
00999
01000
01001
01002
01003
01004
01005
01006
01007
01008
01009
01010
01011
01012
01013
01014
01015
01016
01017
01018 int thread_exception_setup(jvm_thread_index thridx)
01019 {
01020 THREAD(thridx).pnonlocal_ThrowableEvent =
01021 HEAP_GET_DATA(sizeof(jmp_buf), rfalse);
01022
01023 return(setjmp(*THREAD(thridx).pnonlocal_ThrowableEvent));
01024
01025 }
01026
01027
01028
01029
01030
01031
01032
01033
01034
01035
01036
01037
01038
01039
01040
01041
01042
01043
01044
01045
01046
01047
01048
01049
01050
01051
01052
01053
01054
01055
01056
01057
01058
01059
01060
01061
01062
01063
01064
01065
01066
01067
01068
01069
01070
01071 rvoid thread_throw_exception(jvm_thread_index thridx,
01072 rushort thread_status_bits,
01073 rchar *exception_name)
01074 {
01075
01076 switch (thread_status_bits)
01077 {
01078 case THREAD_STATUS_THREW_EXCEPTION:
01079 case THREAD_STATUS_THREW_ERROR:
01080 case THREAD_STATUS_THREW_THROWABLE:
01081 case THREAD_STATUS_THREW_UNCAUGHT:
01082 break;
01083 default:
01084 exit_throw_exception(EXIT_JVM_INTERNAL,
01085 JVMCLASS_JAVA_LANG_INTERNALERROR);
01086
01087 }
01088
01089 THREAD(thridx).status |= thread_status_bits;
01090
01091 THREAD(thridx).pThrowableEvent = exception_name;
01092
01093
01094 int rc = (int) thread_status_bits;
01095
01096 longjmp(*THREAD(thridx).pnonlocal_ThrowableEvent, rc);
01097
01098
01099 }
01100
01101
01102
01103