machine.c 1.2 KB

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