perf sched: Factor out destroy_tasks()
authorNamhyung Kim <namhyung@kernel.org>
Thu, 8 Sep 2022 22:54:48 +0000 (15:54 -0700)
committerArnaldo Carvalho de Melo <acme@redhat.com>
Tue, 4 Oct 2022 11:55:21 +0000 (08:55 -0300)
Add destroy_tasks() as a counterpart of create_tasks() and put the
thread safety notations there.  After join, it destroys semaphores too.

Signed-off-by: Namhyung Kim <namhyung@kernel.org>
Cc: Adrian Hunter <adrian.hunter@intel.com>
Cc: Ian Rogers <irogers@google.com>
Cc: Ingo Molnar <mingo@kernel.org>
Cc: Jiri Olsa <jolsa@kernel.org>
Cc: Peter Zijlstra <peterz@infradead.org>
Link: https://lore.kernel.org/r/20220908225448.4105056-1-namhyung@kernel.org
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
tools/perf/builtin-sched.c

index a92610eac4bf6533f55e280a4cd3806e509580d8..f93737eef07ba0fcda59cccd415963459cb1715d 100644 (file)
@@ -690,6 +690,27 @@ static void create_tasks(struct perf_sched *sched)
        }
 }
 
+static void destroy_tasks(struct perf_sched *sched)
+       UNLOCK_FUNCTION(sched->start_work_mutex)
+       UNLOCK_FUNCTION(sched->work_done_wait_mutex)
+{
+       struct task_desc *task;
+       unsigned long i;
+       int err;
+
+       mutex_unlock(&sched->start_work_mutex);
+       mutex_unlock(&sched->work_done_wait_mutex);
+       /* Get rid of threads so they won't be upset by mutex destrunction */
+       for (i = 0; i < sched->nr_tasks; i++) {
+               task = sched->tasks[i];
+               err = pthread_join(task->thread, NULL);
+               BUG_ON(err);
+               sem_destroy(&task->sleep_sem);
+               sem_destroy(&task->ready_for_work);
+               sem_destroy(&task->work_done_sem);
+       }
+}
+
 static void wait_for_tasks(struct perf_sched *sched)
        EXCLUSIVE_LOCKS_REQUIRED(sched->work_done_wait_mutex)
        EXCLUSIVE_LOCKS_REQUIRED(sched->start_work_mutex)
@@ -3324,8 +3345,7 @@ static int perf_sched__replay(struct perf_sched *sched)
                run_one_test(sched);
 
        sched->thread_funcs_exit = true;
-       mutex_unlock(&sched->start_work_mutex);
-       mutex_unlock(&sched->work_done_wait_mutex);
+       destroy_tasks(sched);
        return 0;
 }