[MATH] Fixups for previous commit
diff --git a/src/hb-ot-layout-math-table.hh b/src/hb-ot-layout-math-table.hh
index f4ecf3f..7b3c0c7 100644
--- a/src/hb-ot-layout-math-table.hh
+++ b/src/hb-ot-layout-math-table.hh
@@ -38,18 +38,18 @@
 
 struct MATH
 {
-  static const hb_tag_t tableTag        = HB_OT_TAG_MATH;
+  static const hb_tag_t tableTag	= HB_OT_TAG_MATH;
 
   inline bool sanitize (hb_sanitize_context_t *c) const
   {
     TRACE_SANITIZE (this);
     return_trace (version.sanitize (c) &&
-                  likely (version.major == 1));
+		  likely (version.major == 1));
   }
 
 protected:
-  FixedVersion<>version;                 /* Version of the MATH table
-                                            initially set to 0x00010000u */
+  FixedVersion<>version;		 /* Version of the MATH table
+					  * initially set to 0x00010000u */
 public:
   DEFINE_SIZE_STATIC (4);
 };
diff --git a/src/hb-ot-layout.cc b/src/hb-ot-layout.cc
index 24d290c..e6316e1 100644
--- a/src/hb-ot-layout.cc
+++ b/src/hb-ot-layout.cc
@@ -61,7 +61,7 @@
   layout->gpos_blob = OT::Sanitizer<OT::GPOS>::sanitize (face->reference_table (HB_OT_TAG_GPOS));
   layout->gpos = OT::Sanitizer<OT::GPOS>::lock_instance (layout->gpos_blob);
 
-  // The MATH table is rarer so we only try and load it in _get_math
+  /* The MATH table is rarely used, so only try and load it in _get_math. */
   layout->math_blob = NULL;
   layout->math = NULL;
 
@@ -182,8 +182,7 @@
   hb_blob_destroy (layout->gdef_blob);
   hb_blob_destroy (layout->gsub_blob);
   hb_blob_destroy (layout->gpos_blob);
-
-  if (layout->math_blob) hb_blob_destroy (layout->math_blob);
+  hb_blob_destroy (layout->math_blob);
 
   free (layout);
 }
@@ -213,13 +212,22 @@
 
   hb_ot_layout_t * layout = hb_ot_layout_from_face (face);
 
-  // If the MATH table is not loaded yet, do it now.
-  if (!layout->math_blob) {
-    layout->math_blob = OT::Sanitizer<OT::MATH>::sanitize (face->reference_table (HB_OT_TAG_MATH));
-    layout->math = OT::Sanitizer<OT::MATH>::lock_instance (layout->math_blob);
+retry:
+  const OT::MATH *math = (const OT::MATH *) hb_atomic_ptr_get (&layout->math);
+
+  if (unlikely (!math))
+  {
+    hb_blob_t *blob = OT::Sanitizer<OT::MATH>::sanitize (face->reference_table (HB_OT_TAG_MATH));
+    math = OT::Sanitizer<OT::MATH>::lock_instance (blob);
+    if (!hb_atomic_ptr_cmpexch (&layout->math, NULL, math))
+    {
+      hb_blob_destroy (blob);
+      goto retry;
+    }
+    layout->math_blob = blob;
   }
 
-  return *layout->math;
+  return *math;
 }
 
 
@@ -1213,8 +1221,9 @@
   apply_string<GSUBProxy> (c, lookup, accel);
 }
 
+
 /*
- * OT::MATH
+ * MATH
  */
 
 /**
@@ -1228,7 +1237,7 @@
  *
  * Return value: #TRUE if face has a MATH table and #FALSE otherwise
  *
- * Since: ????
+ * Since: 1.4
  **/
 hb_bool_t
 hb_ot_layout_has_math_data (hb_face_t *face)
diff --git a/src/hb-ot-layout.h b/src/hb-ot-layout.h
index 7cbd794..e786790 100644
--- a/src/hb-ot-layout.h
+++ b/src/hb-ot-layout.h
@@ -304,6 +304,8 @@
 
 HB_EXTERN hb_bool_t
 hb_ot_layout_has_math_data (hb_face_t *face);
+
+
 HB_END_DECLS
 
 #endif /* HB_OT_LAYOUT_H */