1ed7a3501SAxel Dörfler /*
2f6581478SAugustin Cavalier * Copyright 2000, Georges-Edouard Berenger. All rights reserved.
3f6581478SAugustin Cavalier * Distributed under the terms of the MIT License.
4ed7a3501SAxel Dörfler */
5ed7a3501SAxel Dörfler
6ed7a3501SAxel Dörfler #include "ThreadBarMenu.h"
7a12827f2SAxel Dörfler
8ed7a3501SAxel Dörfler #include "PriorityMenu.h"
9a12827f2SAxel Dörfler #include "ProcessController.h"
10a12827f2SAxel Dörfler #include "ThreadBarMenuItem.h"
11a12827f2SAxel Dörfler
12ed7a3501SAxel Dörfler #include <stdlib.h>
13ed7a3501SAxel Dörfler #include <stdio.h>
14ed7a3501SAxel Dörfler
15ed7a3501SAxel Dörfler #define EXTRA 20
16ed7a3501SAxel Dörfler
17a12827f2SAxel Dörfler
ThreadBarMenu(const char * title,team_id team,int32 threadCount)18a12827f2SAxel Dörfler ThreadBarMenu::ThreadBarMenu(const char *title, team_id team, int32 threadCount)
19a12827f2SAxel Dörfler : BMenu(title),
20a12827f2SAxel Dörfler fThreadsRecCount(threadCount + EXTRA),
21a12827f2SAxel Dörfler fTeam(team)
22ed7a3501SAxel Dörfler {
23ed7a3501SAxel Dörfler SetFont(be_plain_font);
24ed7a3501SAxel Dörfler fThreadsRec = (ThreadRec*) malloc(sizeof(ThreadRec) * fThreadsRecCount);
25ed7a3501SAxel Dörfler Init();
26ed7a3501SAxel Dörfler fRound = 0; // for syslog
27ed7a3501SAxel Dörfler AddNew();
28ed7a3501SAxel Dörfler }
29ed7a3501SAxel Dörfler
30a12827f2SAxel Dörfler
~ThreadBarMenu()31ed7a3501SAxel Dörfler ThreadBarMenu::~ThreadBarMenu()
32ed7a3501SAxel Dörfler {
33ed7a3501SAxel Dörfler free(fThreadsRec);
34ed7a3501SAxel Dörfler if (gCurrentThreadBarMenu == this)
35ed7a3501SAxel Dörfler gCurrentThreadBarMenu = NULL;
36ed7a3501SAxel Dörfler }
37ed7a3501SAxel Dörfler
38a12827f2SAxel Dörfler
39a12827f2SAxel Dörfler void
Init()40a12827f2SAxel Dörfler ThreadBarMenu::Init()
41ed7a3501SAxel Dörfler {
42ed7a3501SAxel Dörfler int k = 0;
43ed7a3501SAxel Dörfler while (k < fThreadsRecCount)
44ed7a3501SAxel Dörfler fThreadsRec[k++].thread = -1;
45ed7a3501SAxel Dörfler fRound = 1;
46ed7a3501SAxel Dörfler }
47ed7a3501SAxel Dörfler
48a12827f2SAxel Dörfler
49a12827f2SAxel Dörfler void
Reset(team_id team)50a12827f2SAxel Dörfler ThreadBarMenu::Reset(team_id team)
51ed7a3501SAxel Dörfler {
52ed7a3501SAxel Dörfler fTeam = team;
53ed7a3501SAxel Dörfler RemoveItems(0, CountItems(), true);
54ed7a3501SAxel Dörfler Init();
55ed7a3501SAxel Dörfler }
56ed7a3501SAxel Dörfler
57a12827f2SAxel Dörfler
58a12827f2SAxel Dörfler void
AttachedToWindow()59a12827f2SAxel Dörfler ThreadBarMenu::AttachedToWindow()
60ed7a3501SAxel Dörfler {
61ed7a3501SAxel Dörfler BMenu::AttachedToWindow();
62ed7a3501SAxel Dörfler }
63ed7a3501SAxel Dörfler
64a12827f2SAxel Dörfler
65a12827f2SAxel Dörfler void
Draw(BRect r)66a12827f2SAxel Dörfler ThreadBarMenu::Draw(BRect r)
67ed7a3501SAxel Dörfler {
68ed7a3501SAxel Dörfler gCurrentThreadBarMenu = this;
69ed7a3501SAxel Dörfler BMenu::Draw(r);
70ed7a3501SAxel Dörfler }
71ed7a3501SAxel Dörfler
72a12827f2SAxel Dörfler
73a12827f2SAxel Dörfler void
AddNew()74a12827f2SAxel Dörfler ThreadBarMenu::AddNew()
75ed7a3501SAxel Dörfler {
76ed7a3501SAxel Dörfler thread_info info;
77ed7a3501SAxel Dörfler int32 cookie = 0;
78ed7a3501SAxel Dörfler int32 k = 0;
79ed7a3501SAxel Dörfler while (get_next_thread_info(fTeam, &cookie, &info) == B_OK) {
80ed7a3501SAxel Dörfler int lastk = k;
81ed7a3501SAxel Dörfler while (k < fThreadsRecCount && fThreadsRec[k].thread != info.thread)
82ed7a3501SAxel Dörfler k++;
83ed7a3501SAxel Dörfler if (k == fThreadsRecCount) {
84ed7a3501SAxel Dörfler k = 0;
85ed7a3501SAxel Dörfler while (k < lastk && fThreadsRec[k].thread != info.thread)
86ed7a3501SAxel Dörfler k++;
87ed7a3501SAxel Dörfler if (k == lastk)
88ed7a3501SAxel Dörfler k = fThreadsRecCount; // flag that the search didn't work.
89ed7a3501SAxel Dörfler }
90ed7a3501SAxel Dörfler if (k == fThreadsRecCount) {
91*425ac1b6SAlexander von Gluck IV // printf("*** Thread %d %s/%s, user %lld, kernel %lld\n", info.thread, info.name, info.user_time, info.kernel_time);
92ed7a3501SAxel Dörfler // this is a new thread...
93ed7a3501SAxel Dörfler k = 0;
94ed7a3501SAxel Dörfler while (k < fThreadsRecCount && !(fThreadsRec[k].thread == -1 || fThreadsRec[k].last_round+1 < fRound))
95ed7a3501SAxel Dörfler k++;
96ed7a3501SAxel Dörfler if (k == fThreadsRecCount) {
97ed7a3501SAxel Dörfler fThreadsRecCount += EXTRA;
98ed7a3501SAxel Dörfler fThreadsRec = (ThreadRec*) realloc(fThreadsRec, sizeof(ThreadRec)*fThreadsRecCount);
99ed7a3501SAxel Dörfler lastk = k;
100ed7a3501SAxel Dörfler while (lastk < fThreadsRecCount)
101ed7a3501SAxel Dörfler fThreadsRec[lastk++].thread = -1;
102ed7a3501SAxel Dörfler }
103ed7a3501SAxel Dörfler fThreadsRec[k].thread = info.thread;
104ed7a3501SAxel Dörfler BMessage* kill_thread = new BMessage('KlTh');
105ed7a3501SAxel Dörfler kill_thread->AddInt32("thread", info.thread);
106ed7a3501SAxel Dörfler
107ed7a3501SAxel Dörfler PriorityMenu* prio = new PriorityMenu(info.thread, info.priority);
108ed7a3501SAxel Dörfler prio->SetFont(be_plain_font);
109ed7a3501SAxel Dörfler ThreadBarMenuItem* threadbarmenuitem = new ThreadBarMenuItem(info.name, info.thread, prio, kill_thread);
110ed7a3501SAxel Dörfler threadbarmenuitem->SetTarget(gPCView);
111ed7a3501SAxel Dörfler AddItem(threadbarmenuitem);
112ed7a3501SAxel Dörfler }
113ed7a3501SAxel Dörfler fThreadsRec[k].last_round = fRound;
114ed7a3501SAxel Dörfler }
115ed7a3501SAxel Dörfler fRound++;
116ed7a3501SAxel Dörfler }
117ed7a3501SAxel Dörfler
118a12827f2SAxel Dörfler
119a12827f2SAxel Dörfler void
Update()120a12827f2SAxel Dörfler ThreadBarMenu::Update()
121ed7a3501SAxel Dörfler {
122ed7a3501SAxel Dörfler AddNew();
123ed7a3501SAxel Dörfler int32 k, del;
124ed7a3501SAxel Dörfler del = -1;
125ed7a3501SAxel Dörfler ThreadBarMenuItem *item;
126a12827f2SAxel Dörfler
127ed7a3501SAxel Dörfler for (k = 0; (item = (ThreadBarMenuItem*) ItemAt(k)) != NULL; k++) {
128ed7a3501SAxel Dörfler item->BarUpdate();
129ed7a3501SAxel Dörfler item->DrawBar(false);
130ed7a3501SAxel Dörfler if (item->fKernel < 0) {
131ed7a3501SAxel Dörfler if (del < 0)
132ed7a3501SAxel Dörfler del = k;
133ed7a3501SAxel Dörfler } else if (del >= 0) {
134ed7a3501SAxel Dörfler RemoveItems(del, k-del, true);
135ed7a3501SAxel Dörfler k = del;
136ed7a3501SAxel Dörfler del = -1;
137ed7a3501SAxel Dörfler }
138ed7a3501SAxel Dörfler }
139ed7a3501SAxel Dörfler if (del >= 0)
140ed7a3501SAxel Dörfler RemoveItems(del, k-del, true);
141ed7a3501SAxel Dörfler }
142