8234512: Missing pieces from JDK-8224816
authoreosterlund
Wed, 20 Nov 2019 14:51:42 +0000
changeset 59157 b313bcb68b4c
parent 59156 14fa9e70ae71
child 59158 438337c846fb
8234512: Missing pieces from JDK-8224816 Reviewed-by: rehn, pliden, kbarrett, gziemski
src/hotspot/os/bsd/os_bsd.cpp
--- a/src/hotspot/os/bsd/os_bsd.cpp	Wed Nov 20 09:10:02 2019 -0500
+++ b/src/hotspot/os/bsd/os_bsd.cpp	Wed Nov 20 14:51:42 2019 +0000
@@ -3205,14 +3205,14 @@
 }
 
 #ifdef __APPLE__
-uint os::processor_id() {
-  static volatile int* volatile apic_to_cpu_mapping = NULL;
-  static volatile int next_cpu_id = 0;
-
-  volatile int* mapping = OrderAccess::load_acquire(&apic_to_cpu_mapping);
+static volatile int* volatile apic_to_processor_mapping = NULL;
+static volatile int next_processor_id = 0;
+
+static inline volatile int* get_apic_to_processor_mapping() {
+  volatile int* mapping = OrderAccess::load_acquire(&apic_to_processor_mapping);
   if (mapping == NULL) {
     // Calculate possible number space for APIC ids. This space is not necessarily
-    // in the range [0, number_of_cpus).
+    // in the range [0, number_of_processors).
     uint total_bits = 0;
     for (uint i = 0;; ++i) {
       uint eax = 0xb; // Query topology leaf
@@ -3238,33 +3238,39 @@
       mapping[i] = -1;
     }
 
-    if (!Atomic::replace_if_null(mapping, &apic_to_cpu_mapping)) {
+    if (!Atomic::replace_if_null(mapping, &apic_to_processor_mapping)) {
       FREE_C_HEAP_ARRAY(int, mapping);
-      mapping = OrderAccess::load_acquire(&apic_to_cpu_mapping);
+      mapping = OrderAccess::load_acquire(&apic_to_processor_mapping);
     }
   }
 
+  return mapping;
+}
+
+uint os::processor_id() {
+  volatile int* mapping = get_apic_to_processor_mapping();
+
   uint eax = 0xb;
   uint ebx;
   uint ecx = 0;
   uint edx;
 
-  asm ("cpuid\n\t" : "+a" (eax), "+b" (ebx), "+c" (ecx), "+d" (edx) : );
+  __asm__ ("cpuid\n\t" : "+a" (eax), "+b" (ebx), "+c" (ecx), "+d" (edx) : );
 
   // Map from APIC id to a unique logical processor ID in the expected
   // [0, num_processors) range.
 
   uint apic_id = edx;
-  int cpu_id = Atomic::load(&mapping[apic_id]);
-
-  while (cpu_id < 0) {
+  int processor_id = Atomic::load(&mapping[apic_id]);
+
+  while (processor_id < 0) {
     if (Atomic::cmpxchg(-2, &mapping[apic_id], -1)) {
-      Atomic::store(Atomic::add(1, &next_cpu_id) - 1, &mapping[apic_id]);
+      Atomic::store(Atomic::add(1, &next_processor_id) - 1, &mapping[apic_id]);
     }
-    cpu_id = Atomic::load(&mapping[apic_id]);
+    processor_id = Atomic::load(&mapping[apic_id]);
   }
 
-  return (uint)cpu_id;
+  return (uint)processor_id;
 }
 #endif