Skip to contentSkip to conversation

perf sched: Factor out destroy_tasks()

PATCH Applied Namhyung Kim 2 participants 2 messages Sep 8, 2022 – Sep 9, 2022 perf events

lore
Share & tools
lore

Namhyung Kim

author maintainer
#0 lore raw

PATCH 1/1 perf sched: Factor out destroy_tasks()

Add destroy_tasks() as a counterpart of create_tasks() and put the thread safety notations there. After join, it destroys semaphores too.

---
 tools/perf/builtin-sched.c | 24 ++++++++++++++++++++++--
 1 file changed, 22 insertions(+), 2 deletions(-)
tools/perf/builtin-sched.c +22 -2
index a92610eac4bf..f93737eef07b 100644
@@ -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;
 }
 
Signature · 1 line
2.37.2.789.g6183377224-goog
maintainer Applied
#1 lore raw

Em Thu, Sep 08, 2022 at 03:54:48PM -0700, Namhyung Kim escreveu:

Quoted 2 lines

Add destroy_tasks() as a counterpart of create_tasks() and put the thread safety notations there. After join, it destroys semaphores too.

Thanks, applied.

- Arnaldo

Quoted 49 lines
---
 tools/perf/builtin-sched.c | 24 ++++++++++++++++++++++--
 1 file changed, 22 insertions(+), 2 deletions(-)
tools/perf/builtin-sched.c +22 -2
index a92610eac4bf..f93737eef07b 100644
@@ -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;
 }
 
Signature · 1 line
2.37.2.789.g6183377224-goog
Signature · 1 line
- Arnaldo