perf tools: Use atomic_t to implement thread__{get,put} refcnt

Fixing bugs in 'perf top' where the used thread unsafe 'struct thread'
refcount implementation was falling apart because we really use two
threads.
Acked-by: default avatarDavid Ahern <dsahern@gmail.com>
Cc: Adrian Hunter <adrian.hunter@intel.com>
Cc: Borislav Petkov <bp@suse.de>
Cc: Don Zickus <dzickus@redhat.com>
Cc: Frederic Weisbecker <fweisbec@gmail.com>
Cc: Jiri Olsa <jolsa@redhat.com>
Cc: Namhyung Kim <namhyung@kernel.org>
Cc: Stephane Eranian <eranian@google.com>
Link: http://lkml.kernel.org/n/tip-hil2hol294u5ntcuof4jhmn6@git.kernel.orgSigned-off-by: default avatarArnaldo Carvalho de Melo <acme@redhat.com>
parent da6d8567
...@@ -53,7 +53,7 @@ struct thread *thread__new(pid_t pid, pid_t tid) ...@@ -53,7 +53,7 @@ struct thread *thread__new(pid_t pid, pid_t tid)
goto err_thread; goto err_thread;
list_add(&comm->list, &thread->comm_list); list_add(&comm->list, &thread->comm_list);
atomic_set(&thread->refcnt, 0);
} }
return thread; return thread;
...@@ -84,13 +84,13 @@ void thread__delete(struct thread *thread) ...@@ -84,13 +84,13 @@ void thread__delete(struct thread *thread)
struct thread *thread__get(struct thread *thread) struct thread *thread__get(struct thread *thread)
{ {
++thread->refcnt; atomic_inc(&thread->refcnt);
return thread; return thread;
} }
void thread__put(struct thread *thread) void thread__put(struct thread *thread)
{ {
if (thread && --thread->refcnt == 0) { if (thread && atomic_dec_and_test(&thread->refcnt)) {
list_del_init(&thread->node); list_del_init(&thread->node);
thread__delete(thread); thread__delete(thread);
} }
......
#ifndef __PERF_THREAD_H #ifndef __PERF_THREAD_H
#define __PERF_THREAD_H #define __PERF_THREAD_H
#include <linux/atomic.h>
#include <linux/rbtree.h> #include <linux/rbtree.h>
#include <linux/list.h> #include <linux/list.h>
#include <unistd.h> #include <unistd.h>
...@@ -21,7 +22,7 @@ struct thread { ...@@ -21,7 +22,7 @@ struct thread {
pid_t tid; pid_t tid;
pid_t ppid; pid_t ppid;
int cpu; int cpu;
int refcnt; atomic_t refcnt;
char shortname[3]; char shortname[3];
bool comm_set; bool comm_set;
bool dead; /* if set thread has exited */ bool dead; /* if set thread has exited */
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment