Implement C++11 <atomic> operations

Fixes https://github.com/harfbuzz/harfbuzz/issues/345
diff --git a/src/hb-atomic-private.hh b/src/hb-atomic-private.hh
index 12caaca..f234fe0 100644
--- a/src/hb-atomic-private.hh
+++ b/src/hb-atomic-private.hh
@@ -46,6 +46,25 @@
 /* Defined externally, i.e. in config.h; must have typedef'ed hb_atomic_int_impl_t as well. */
 
 
+#elif !defined(HB_NO_MT) && __cplusplus >= 201103L
+
+/* Prefer C++11 atomics. */
+
+#include <atomic>
+
+typedef int hb_atomic_int_impl_t;
+#define hb_atomic_int_impl_add(AI, V)		(reinterpret_cast<std::atomic<int> *> (&AI)->fetch_add (V))
+
+#define hb_atomic_ptr_impl_get(P)		(reinterpret_cast<std::atomic<void*> *> (P)->load ())
+static inline bool
+_hb_atomic_ptr_impl_cmplexch (const void **P, const void *O_, const void *N)
+{
+  const void *O = O_; // Need lvalue
+  return reinterpret_cast<std::atomic<const void*> *> (P)->compare_exchange_weak ((O), (N));
+}
+#define hb_atomic_ptr_impl_cmpexch(P,O,N)	(_hb_atomic_ptr_impl_cmplexch ((const void **) (P), (O), (N)))
+
+
 #elif !defined(HB_NO_MT) && (defined(_WIN32) || defined(__CYGWIN__))
 
 #include <windows.h>
@@ -162,16 +181,18 @@
 #endif
 
 
+#ifndef HB_ATOMIC_INT_INIT
 #define HB_ATOMIC_INT_INIT(V)          {V}
+#endif
 
 struct hb_atomic_int_t
 {
-  hb_atomic_int_impl_t v;
+  mutable hb_atomic_int_impl_t v;
 
   inline void set_unsafe (int v_) { v = v_; }
   inline int get_unsafe (void) const { return v; }
-  inline int inc (void) { return hb_atomic_int_impl_add (const_cast<hb_atomic_int_impl_t &> (v),  1); }
-  inline int dec (void) { return hb_atomic_int_impl_add (const_cast<hb_atomic_int_impl_t &> (v), -1); }
+  inline int inc (void) { return hb_atomic_int_impl_add (v,  1); }
+  inline int dec (void) { return hb_atomic_int_impl_add (v, -1); }
 };