6pack,mkiss: fix lock inconsistency
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / tools / perf / util / thread_map.c
bloba5df131b77c322f673ba0edd1bf16352744d9122
1 #include <dirent.h>
2 #include <stdlib.h>
3 #include <stdio.h>
4 #include "thread_map.h"
6 /* Skip "." and ".." directories */
7 static int filter(const struct dirent *dir)
9 if (dir->d_name[0] == '.')
10 return 0;
11 else
12 return 1;
15 struct thread_map *thread_map__new_by_pid(pid_t pid)
17 struct thread_map *threads;
18 char name[256];
19 int items;
20 struct dirent **namelist = NULL;
21 int i;
23 sprintf(name, "/proc/%d/task", pid);
24 items = scandir(name, &namelist, filter, NULL);
25 if (items <= 0)
26 return NULL;
28 threads = malloc(sizeof(*threads) + sizeof(pid_t) * items);
29 if (threads != NULL) {
30 for (i = 0; i < items; i++)
31 threads->map[i] = atoi(namelist[i]->d_name);
32 threads->nr = items;
35 for (i=0; i<items; i++)
36 free(namelist[i]);
37 free(namelist);
39 return threads;
42 struct thread_map *thread_map__new_by_tid(pid_t tid)
44 struct thread_map *threads = malloc(sizeof(*threads) + sizeof(pid_t));
46 if (threads != NULL) {
47 threads->map[0] = tid;
48 threads->nr = 1;
51 return threads;
54 struct thread_map *thread_map__new(pid_t pid, pid_t tid)
56 if (pid != -1)
57 return thread_map__new_by_pid(pid);
58 return thread_map__new_by_tid(tid);
61 void thread_map__delete(struct thread_map *threads)
63 free(threads);