Arnaldo Carvalho de Melo | 9d2f8e2 | 2012-10-06 15:43:20 -0300 | [diff] [blame] | 1 | #include "machine.h" |
| 2 | #include "map.h" |
| 3 | #include "thread.h" |
| 4 | #include <stdbool.h> |
| 5 | |
| 6 | static struct thread *__machine__findnew_thread(struct machine *machine, pid_t pid, |
| 7 | bool create) |
| 8 | { |
| 9 | struct rb_node **p = &machine->threads.rb_node; |
| 10 | struct rb_node *parent = NULL; |
| 11 | struct thread *th; |
| 12 | |
| 13 | /* |
| 14 | * Font-end cache - PID lookups come in blocks, |
| 15 | * so most of the time we dont have to look up |
| 16 | * the full rbtree: |
| 17 | */ |
| 18 | if (machine->last_match && machine->last_match->pid == pid) |
| 19 | return machine->last_match; |
| 20 | |
| 21 | while (*p != NULL) { |
| 22 | parent = *p; |
| 23 | th = rb_entry(parent, struct thread, rb_node); |
| 24 | |
| 25 | if (th->pid == pid) { |
| 26 | machine->last_match = th; |
| 27 | return th; |
| 28 | } |
| 29 | |
| 30 | if (pid < th->pid) |
| 31 | p = &(*p)->rb_left; |
| 32 | else |
| 33 | p = &(*p)->rb_right; |
| 34 | } |
| 35 | |
| 36 | if (!create) |
| 37 | return NULL; |
| 38 | |
| 39 | th = thread__new(pid); |
| 40 | if (th != NULL) { |
| 41 | rb_link_node(&th->rb_node, parent, p); |
| 42 | rb_insert_color(&th->rb_node, &machine->threads); |
| 43 | machine->last_match = th; |
| 44 | } |
| 45 | |
| 46 | return th; |
| 47 | } |
| 48 | |
| 49 | struct thread *machine__findnew_thread(struct machine *machine, pid_t pid) |
| 50 | { |
| 51 | return __machine__findnew_thread(machine, pid, true); |
| 52 | } |
| 53 | |
| 54 | struct thread *machine__find_thread(struct machine *machine, pid_t pid) |
| 55 | { |
| 56 | return __machine__findnew_thread(machine, pid, false); |
| 57 | } |