[threads] Add function to stringify thread state names.
[mono.git] / mono / utils / mach-support-x86.c
index a028e65edafcfe99d507b755967f04c2f7761afc..5650e8b92c916b7a584ccfe073d9e915a30ff489 100644 (file)
 #define TLS_VECTOR_OFFSET_10_9 0xb0
 #define TLS_VECTOR_OFFSET_10_11 0x100
 
+
+/* This is 2 slots less than the known low */
+#define TLS_PROBE_LOW_WATERMARK 0x40
+/* This is 28 slots above the know high, which is more than the known high-low*/
+#define TLS_PROBE_HIGH_WATERMARK 0x120
+
+
 static int tls_vector_offset;
 
 void *
@@ -74,20 +81,27 @@ mono_mach_arch_get_thread_state_size ()
 kern_return_t
 mono_mach_arch_get_thread_state (thread_port_t thread, thread_state_t state, mach_msg_type_number_t *count)
 {
+#if defined(HOST_WATCHOS)
+       g_error ("thread_get_state() is not supported by this platform");
+#else
        x86_thread_state32_t *arch_state = (x86_thread_state32_t *) state;
        kern_return_t ret;
 
        *count = x86_THREAD_STATE32_COUNT;
-
        ret = thread_get_state (thread, x86_THREAD_STATE32, (thread_state_t) arch_state, count);
 
        return ret;
+#endif
 }
 
 kern_return_t
 mono_mach_arch_set_thread_state (thread_port_t thread, thread_state_t state, mach_msg_type_number_t count)
 {
+#if defined(HOST_WATCHOS)
+       g_error ("thread_set_state() is not supported by this platform");
+#else
        return thread_set_state (thread, x86_THREAD_STATE32, state, count);
+#endif 
 }
 
 void *
@@ -99,6 +113,7 @@ mono_mach_get_tls_address_from_thread (pthread_t thread, pthread_key_t key)
         */
        intptr_t *p = (intptr_t *) thread;
        intptr_t **tsd = (intptr_t **) ((char*)p + tls_vector_offset);
+       g_assert (tls_vector_offset != -1);
 
        return (void *) &tsd [key];     
 }
@@ -112,6 +127,7 @@ mono_mach_arch_get_tls_value_from_thread (pthread_t thread, guint32 key)
 void
 mono_mach_init (pthread_key_t key)
 {
+       int i;
        void *old_value = pthread_getspecific (key);
        void *canary = (void*)0xDEADBEEFu;
 
@@ -133,7 +149,17 @@ mono_mach_init (pthread_key_t key)
        if (mono_mach_arch_get_tls_value_from_thread (pthread_self (), key) == canary)
                goto ok;
 
-       g_error ("could not discover the mach TLS offset");
+       /*Fallback to scanning a large range of offsets*/
+       for (i = TLS_PROBE_LOW_WATERMARK; i <= TLS_PROBE_HIGH_WATERMARK; i += 4) {
+               tls_vector_offset = i;
+               if (mono_mach_arch_get_tls_value_from_thread (pthread_self (), key) == canary) {
+                       g_warning ("Found new TLS offset at %d", i);
+                       goto ok;
+               }
+       }
+
+       tls_vector_offset = -1;
+       g_warning ("could not discover the mach TLS offset");
 ok:
        pthread_setspecific (key, old_value);
 }