]> git.uio.no Git - u/mrichter/AliRoot.git/blob - EVE/hlt-macros/command_queue.C
92e3abbe7f48b04eb4b1a9b0e234782c3a32660d
[u/mrichter/AliRoot.git] / EVE / hlt-macros / command_queue.C
1 #include <TObject.h>
2 #include <TList.h>
3 #include <TTimer.h>
4 #include <TROOT.h>
5
6 #include <TThread.h>
7 #include <TMutex.h>
8 #include <TCondition.h>
9
10 class CommandQueue : public TObject
11 {
12 public:
13   class Command : public TObject
14   {
15   public:
16     TString     fCommand;
17     TCondition *fAwakeCond; // awake thread after command executed
18
19     Command(const TString& cmd, TCondition* cond) : fCommand(cmd), fAwakeCond(cond) {}
20
21     ClassDef(Command, 0); // An entry in the command-queue.
22   };
23
24   TList  fQueue;
25   TMutex fQueueLock;
26   TTimer fQueueTimer;
27   Bool_t fQueueTimerSet;
28
29   CommandQueue()
30   {
31     fQueueTimer.Connect("Timeout()", "CommandQueue", this, "ProcessQueue()");
32     fQueueTimerSet = kFALSE;
33   }
34
35   // Destructor, cleanup missing.
36
37   //--------------------------------
38
39   void RegisterCommand(const TString& cmd, TCondition* cond=0)
40   {
41     fQueueLock.Lock();
42     fQueue.Add(new Command(cmd, cond));
43     if (!fQueueTimerSet) {
44       // Force execution in main thread when it is free.
45       fQueueTimer.Start(0, kTRUE);
46       fQueueTimerSet = kTRUE;
47     }
48     fQueueLock.UnLock();
49   }
50
51   virtual void ProcessQueue()
52   {
53     Int_t n_proccessed = 0;
54
55     while (1) {
56       fQueueLock.Lock();
57       Command* c = (Command*) fQueue.First();
58       fQueue.RemoveFirst();
59       fQueueLock.UnLock();
60
61       { // Put this into virtual void ProcessComand(Command*);
62         // Need also exception handling.
63         gROOT->ProcessLineFast(c->fCommand);
64         ++n_proccessed;
65         if (c->fAwakeCond != 0) {
66           c->fAwakeCond->GetMutex()->Lock();
67           c->fAwakeCond->Signal();
68           c->fAwakeCond->GetMutex()->UnLock();
69         }
70       }
71       delete c;
72
73       fQueueLock.Lock();
74       if (fQueue.IsEmpty()) {
75         fQueueTimerSet = kFALSE;
76         fQueueLock.UnLock();
77         break;
78       }
79       fQueueLock.UnLock();
80     }
81
82     printf("CommandQueue::ProcessQueue() processed %d commands.\n", n_proccessed);
83   }
84
85   ClassDef(CommandQueue,0); // Command queue for exection in global CINT context.
86 };
87
88 //=============================================================================
89 // Global variable
90 //=============================================================================
91
92 CommandQueue* g_cmd_queue = 0;
93
94 void command_queue()
95 {
96   g_cmd_queue = new CommandQueue;
97   printf("Starting command-queue ...\n");
98 }
99
100 // ============================================================================
101 // Reve side
102 // ============================================================================
103
104 #include <Reve/Reve.h>
105 #include <Reve/ReveManager.h>
106 #include <Reve/PointSet.h>
107
108 #include <TRandom.h>
109
110 void make_crap(void* arg) 
111 {
112   Int_t num = 1024;
113   TRandom rnd(0);
114
115   Reve::PointSet* ps = new Reve::PointSet("Testus", num);
116   for (Int_t i=0; i<num; ++i) {
117     ps->SetNextPoint(rnd.Uniform(-100, 100),
118                      rnd.Uniform(-100, 100),
119                      rnd.Uniform(-100, 100));
120   }
121   ps->SetMainColor((Color_t)kRed);
122   printf("make_crap() -> produced Reve::PointSet* %p)\n", ps);
123
124   ((CommandQueue*)arg)->RegisterCommand
125     (Form("register_crap((Reve::RenderElement*)0x%lx)", ps));
126 }
127
128 void register_crap(Reve::RenderElement* el)
129 {
130   printf("register_crap(Reve::RenderElement* %p)\n", el);
131   gReve->AddRenderElement(el);
132   gReve->Redraw3D();
133 }
134
135 void test()
136 {
137   TThread* thr = new TThread(make_crap, g_cmd_queue);
138   thr->Run();
139 }