Adding reference counting for thread_map object, so it could be easily
shared among other objects.
Using thread_map__put instead thread_map__delete and making
thread_map__delete static.
Signed-off-by: Jiri Olsa <jolsa@kernel.org>
Cc: Adrian Hunter <adrian.hunter@intel.com>
Cc: Andi Kleen <ak@linux.intel.com>
Cc: David Ahern <dsahern@gmail.com>
Cc: Namhyung Kim <namhyung@kernel.org>
Cc: Peter Zijlstra <a.p.zijlstra@chello.nl>
Cc: Stephane Eranian <eranian@google.com>
Link: http://lkml.kernel.org/r/1435012588-9007-5-git-send-email-jolsa@kernel.org
[ Adjustments to move it ahead of the "comm" patches ]
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
perf_evlist__delete(evlist);
} else {
cpu_map__put(cpus);
- thread_map__delete(threads);
+ thread_map__put(threads);
}
machines__destroy_kernel_maps(&machines);
machine__delete_threads(machine);
perf_evlist__delete(evlist);
} else {
cpu_map__put(cpus);
- thread_map__delete(threads);
+ thread_map__put(threads);
}
return err;
out_free_cpus:
cpu_map__put(cpus);
out_free_threads:
- thread_map__delete(threads);
+ thread_map__put(threads);
return err;
}
perf_event__process,
machine, 0, 500);
- thread_map__delete(map);
+ thread_map__put(map);
return err;
}
out_evsel_delete:
perf_evsel__delete(evsel);
out_thread_map_delete:
- thread_map__delete(threads);
+ thread_map__put(threads);
return err;
}
out_evsel_delete:
perf_evsel__delete(evsel);
out_thread_map_delete:
- thread_map__delete(threads);
+ thread_map__put(threads);
return err;
}
perf_evlist__delete(evlist);
} else {
cpu_map__put(cpus);
- thread_map__delete(threads);
+ thread_map__put(threads);
}
return err;
perf_evlist__munmap(evlist);
perf_evlist__close(evlist);
cpu_map__put(evlist->cpus);
- thread_map__delete(evlist->threads);
+ thread_map__put(evlist->threads);
evlist->cpus = NULL;
evlist->threads = NULL;
perf_evlist__purge(evlist);
return 0;
out_delete_threads:
- thread_map__delete(evlist->threads);
+ thread_map__put(evlist->threads);
evlist->threads = NULL;
return -1;
}
static void pyrf_thread_map__delete(struct pyrf_thread_map *pthreads)
{
- thread_map__delete(pthreads->threads);
+ thread_map__put(pthreads->threads);
pthreads->ob_type->tp_free((PyObject*)pthreads);
}
#include <unistd.h>
#include "strlist.h"
#include <string.h>
+#include "asm/bug.h"
#include "thread_map.h"
#include "util.h"
for (i = 0; i < items; i++)
thread_map__set_pid(threads, i, atoi(namelist[i]->d_name));
threads->nr = items;
+ atomic_set(&threads->refcnt, 1);
}
for (i=0; i<items; i++)
if (threads != NULL) {
thread_map__set_pid(threads, 0, tid);
threads->nr = 1;
+ atomic_set(&threads->refcnt, 1);
}
return threads;
goto out_free_threads;
threads->nr = 0;
+ atomic_set(&threads->refcnt, 1);
while (!readdir_r(proc, &dirent, &next) && next) {
char *end;
out:
strlist__delete(slist);
+ if (threads)
+ atomic_set(&threads->refcnt, 1);
return threads;
out_free_namelist:
if (threads != NULL) {
thread_map__set_pid(threads, 0, -1);
threads->nr = 1;
+ atomic_set(&threads->refcnt, 1);
}
return threads;
}
threads->nr = ntasks;
}
out:
+ if (threads)
+ atomic_set(&threads->refcnt, 1);
return threads;
out_free_threads:
return thread_map__new_by_tid_str(tid);
}
-void thread_map__delete(struct thread_map *threads)
+static void thread_map__delete(struct thread_map *threads)
{
- free(threads);
+ if (threads) {
+ WARN_ONCE(atomic_read(&threads->refcnt) != 0,
+ "thread map refcnt unbalanced\n");
+ free(threads);
+ }
+}
+
+struct thread_map *thread_map__get(struct thread_map *map)
+{
+ if (map)
+ atomic_inc(&map->refcnt);
+ return map;
+}
+
+void thread_map__put(struct thread_map *map)
+{
+ if (map && atomic_dec_and_test(&map->refcnt))
+ thread_map__delete(map);
}
size_t thread_map__fprintf(struct thread_map *threads, FILE *fp)
#include <sys/types.h>
#include <stdio.h>
+#include <linux/atomic.h>
struct thread_map_data {
pid_t pid;
};
struct thread_map {
+ atomic_t refcnt;
int nr;
struct thread_map_data map[];
};
struct thread_map *thread_map__new_by_uid(uid_t uid);
struct thread_map *thread_map__new(pid_t pid, pid_t tid, uid_t uid);
+struct thread_map *thread_map__get(struct thread_map *map);
+void thread_map__put(struct thread_map *map);
+
struct thread_map *thread_map__new_str(const char *pid,
const char *tid, uid_t uid);
-void thread_map__delete(struct thread_map *threads);
-
size_t thread_map__fprintf(struct thread_map *threads, FILE *fp);
static inline int thread_map__nr(struct thread_map *threads)