[profiler] Fix sampling thread shutdown logic to handle idle mode correctly.
authorAlex Rønne Petersen <alexrp@xamarin.com>
Thu, 13 Jul 2017 15:37:22 +0000 (17:37 +0200)
committerAlex Rønne Petersen <alexrp@xamarin.com>
Fri, 14 Jul 2017 18:24:59 +0000 (20:24 +0200)
mono/metadata/profiler-private.h
mono/metadata/profiler.c
mono/mini/mini-posix.c

index 45535bcf3d8aa7dc9d8334b704cbf5d47299184e..99cbf1430ee3bc9ebb921a948565bbe94b29d74c 100644 (file)
@@ -98,7 +98,8 @@ void mono_profiler_coverage_free (MonoMethod *method);
 gboolean mono_profiler_should_instrument_method (MonoMethod *method, gboolean entry);
 
 gboolean mono_profiler_sampling_enabled (void);
-void mono_profiler_sampling_thread_sleep (void);
+void mono_profiler_sampling_thread_post (void);
+void mono_profiler_sampling_thread_wait (void);
 
 static inline gboolean
 mono_profiler_allocations_enabled (void)
index 07d7015d7e4c5e618fef9dfee4ffedc461163384..eae4fd5751d0f3561928d8de0d95f6ba4d676286 100644 (file)
@@ -335,7 +335,7 @@ mono_profiler_set_sample_mode (MonoProfilerHandle handle, MonoProfilerSampleMode
        mono_profiler_state.sample_mode = mode;
        mono_profiler_state.sample_freq = freq;
 
-       mono_os_sem_post (&mono_profiler_state.sampling_semaphore);
+       mono_profiler_sampling_thread_post ();
 
        return TRUE;
 }
@@ -359,7 +359,13 @@ mono_profiler_sampling_enabled (void)
 }
 
 void
-mono_profiler_sampling_thread_sleep (void)
+mono_profiler_sampling_thread_post (void)
+{
+       mono_os_sem_post (&mono_profiler_state.sampling_semaphore);
+}
+
+void
+mono_profiler_sampling_thread_wait (void)
 {
        mono_os_sem_wait (&mono_profiler_state.sampling_semaphore, MONO_SEM_FLAGS_NONE);
 }
index 40a841b729f4fdfdd7ca9a5ae07bfe4ca57b36b0..fa77749eab481785047eb74550e29c304e0ad105 100644 (file)
@@ -640,7 +640,11 @@ init:
        mono_profiler_get_sample_mode (NULL, &mode, NULL);
 
        if (mode == MONO_PROFILER_SAMPLE_MODE_NONE) {
-               mono_profiler_sampling_thread_sleep ();
+               mono_profiler_sampling_thread_wait ();
+
+               if (!InterlockedRead (&sampling_thread_running))
+                       goto done;
+
                goto init;
        }
 
@@ -676,10 +680,11 @@ init:
                } FOREACH_THREAD_SAFE_END
        }
 
-       InterlockedWrite (&sampling_thread_exiting, 1);
-
        clock_cleanup ();
 
+done:
+       InterlockedWrite (&sampling_thread_exiting, 1);
+
        pthread_setschedparam (pthread_self (), old_policy, &old_sched);
 
        mono_thread_info_detach ();
@@ -692,6 +697,8 @@ mono_runtime_shutdown_stat_profiler (void)
 {
        InterlockedWrite (&sampling_thread_running, 0);
 
+       mono_profiler_sampling_thread_post ();
+
 #ifndef PLATFORM_MACOSX
        /*
         * There is a slight problem when we're using CLOCK_PROCESS_CPUTIME_ID: If