@@ -779,23 +779,31 @@ public String generateCCode(List<ByteCodeClass> allClasses) {
779779 b .append (";\n }\n \n " );
780780 }
781781
782- // static setter
782+ // Static object fields may interact with heap bookkeeping, so they keep thread context.
783783 b .append ("void set_static_" );
784784 b .append (clsName );
785785 b .append ("_" );
786786 b .append (bf .getFieldName ().replace ('$' , '_' ));
787- b .append ("(CODENAME_ONE_THREAD_STATE, " );
787+ b .append ("(" );
788+ if (bf .isObjectType ()) {
789+ b .append ("CODENAME_ONE_THREAD_STATE, " );
790+ }
788791 b .append (bf .getCDefinition ());
789792 b .append (" __cn1StaticVal) {\n __STATIC_INITIALIZER_" );
790793 b .append (bf .getClsName ());
794+ if (bf .isObjectType ()) {
795+ b .append ("(threadStateData);\n " );
796+ } else {
797+ b .append ("(getThreadLocalData());\n " );
798+ }
791799 if (bf .isVolatile ()) {
792- b .append ("(threadStateData); \n atomic_store_explicit(&STATIC_FIELD_" );
800+ b .append ("atomic_store_explicit(&STATIC_FIELD_" );
793801 b .append (bf .getClsName ());
794802 b .append ("_" );
795803 b .append (bf .getFieldName ());
796804 b .append (", __cn1StaticVal, memory_order_release);" );
797805 } else {
798- b .append ("(threadStateData); \n STATIC_FIELD_" );
806+ b .append ("STATIC_FIELD_" );
799807 b .append (bf .getClsName ());
800808 b .append ("_" );
801809 b .append (bf .getFieldName ());
@@ -852,11 +860,12 @@ public String generateCCode(List<ByteCodeClass> allClasses) {
852860 b .append (";\n }\n \n " );
853861 }
854862
863+ // Instance field setters don't use thread context directly.
855864 b .append ("void set_field_" );
856865 b .append (clsName );
857866 b .append ("_" );
858867 b .append (fld .getFieldName ());
859- b .append ("(CODENAME_ONE_THREAD_STATE, " );
868+ b .append ("(" );
860869 b .append (fld .getCDefinition ());
861870 if (fld .isObjectType ()) {
862871 b .append (" __cn1Val, JAVA_OBJECT __cn1T) {\n " ).append (nullCheck ).append (" " );
@@ -1089,7 +1098,7 @@ public String generateCCode(List<ByteCodeClass> allClasses) {
10891098
10901099 b .append ("JAVA_OBJECT __VALUE_OF_" ).append (clsName ).append ("(CODENAME_ONE_THREAD_STATE, JAVA_OBJECT value) {\n " );
10911100 if (enumValuesField != null ) {
1092- b .append (" JAVA_ARRAY values = (JAVA_ARRAY)get_static_" ).append (clsName ).append ("_" ).append (enumValuesField .replace ('$' , '_' )).append ("(threadStateData );\n " );
1101+ b .append (" JAVA_ARRAY values = (JAVA_ARRAY)get_static_" ).append (clsName ).append ("_" ).append (enumValuesField .replace ('$' , '_' )).append ("();\n " );
10931102 b .append (" JAVA_ARRAY_OBJECT* data = (JAVA_ARRAY_OBJECT*)values->data;\n " );
10941103 b .append (" int len = values->length;\n " );
10951104 b .append (" for (int i=0; i<len; i++) {\n " );
@@ -1541,21 +1550,24 @@ public String generateCHeader() {
15411550 b .append (bf .getFieldName ());
15421551 b .append (";\n " );
15431552
1544- b .append ("extern void" );
1545- b .append (" set_static_" );
1546- b .append (clsName );
1547- b .append ("_" );
1548- b .append (bf .getFieldName ());
1549- b .append ("(CODENAME_ONE_THREAD_STATE, " );
1550- b .append (bf .getCDefinition ());
1551- b .append (" v);\n " );
1553+ b .append ("extern void" );
1554+ b .append (" set_static_" );
1555+ b .append (clsName );
1556+ b .append ("_" );
1557+ b .append (bf .getFieldName ());
1558+ b .append ("(" );
1559+ if (bf .isObjectType ()) {
1560+ b .append ("CODENAME_ONE_THREAD_STATE, " );
1561+ }
1562+ b .append (bf .getCDefinition ());
1563+ b .append (" v);\n " );
15521564 }
15531565 } else {
15541566 b .append ("#define get_static_" );
15551567 b .append (clsName );
15561568 b .append ("_" );
15571569 b .append (bf .getFieldName ());
1558- b .append ("(threadStateArgument ) get_static_" );
1570+ b .append ("() get_static_" );
15591571 b .append (bf .getClsName ());
15601572 b .append ("_" );
15611573 b .append (bf .getFieldName ());
@@ -1565,11 +1577,19 @@ public String generateCHeader() {
15651577 b .append (clsName );
15661578 b .append ("_" );
15671579 b .append (bf .getFieldName ());
1568- b .append ("(threadStateArgument, valueArgument) set_static_" );
1569- b .append (bf .getClsName ());
1570- b .append ("_" );
1571- b .append (bf .getFieldName ());
1572- b .append ("(threadStateArgument, valueArgument)\n " );
1580+ if (bf .isObjectType ()) {
1581+ b .append ("(threadStateArgument, valueArgument) set_static_" );
1582+ b .append (bf .getClsName ());
1583+ b .append ("_" );
1584+ b .append (bf .getFieldName ());
1585+ b .append ("(threadStateArgument, valueArgument)\n " );
1586+ } else {
1587+ b .append ("(valueArgument) set_static_" );
1588+ b .append (bf .getClsName ());
1589+ b .append ("_" );
1590+ b .append (bf .getFieldName ());
1591+ b .append ("(valueArgument)\n " );
1592+ }
15731593 }
15741594 }
15751595 }
@@ -1586,7 +1606,7 @@ public String generateCHeader() {
15861606 b .append (clsName );
15871607 b .append ("_" );
15881608 b .append (fld .getFieldName ());
1589- b .append ("(CODENAME_ONE_THREAD_STATE, " );
1609+ b .append ("(" );
15901610 b .append (fld .getCDefinition ());
15911611 b .append (" __cn1Val, JAVA_OBJECT __cn1T);\n " );
15921612 }
0 commit comments