d810d0de |
1 | // $Id$ |
2 | // Main authors: Matevz Tadel & Alja Mrak-Tadel: 2006, 2007 |
3 | |
4 | /************************************************************************** |
5 | * Copyright(c) 1998-2008, ALICE Experiment at CERN, all rights reserved. * |
6 | * See http://aliceinfo.cern.ch/Offline/AliRoot/License.html for * |
51346b82 |
7 | * full copyright notice. * |
d810d0de |
8 | **************************************************************************/ |
87f60d9e |
9 | #include <TObject.h> |
10 | #include <TList.h> |
11 | #include <TTimer.h> |
12 | #include <TROOT.h> |
13 | |
14 | #include <TThread.h> |
15 | #include <TMutex.h> |
16 | #include <TCondition.h> |
17 | |
18 | class CommandQueue : public TObject |
19 | { |
20 | public: |
21 | class Command : public TObject |
22 | { |
23 | public: |
24 | TString fCommand; |
25 | TCondition *fAwakeCond; // awake thread after command executed |
26 | |
27 | Command(const TString& cmd, TCondition* cond) : fCommand(cmd), fAwakeCond(cond) {} |
28 | |
29 | ClassDef(Command, 0); // An entry in the command-queue. |
30 | }; |
31 | |
32 | TList fQueue; |
33 | TMutex fQueueLock; |
34 | TTimer fQueueTimer; |
35 | Bool_t fQueueTimerSet; |
36 | |
37 | CommandQueue() |
38 | { |
39 | fQueueTimer.Connect("Timeout()", "CommandQueue", this, "ProcessQueue()"); |
40 | fQueueTimerSet = kFALSE; |
41 | } |
42 | |
43 | // Destructor, cleanup missing. |
44 | |
45 | //-------------------------------- |
46 | |
47 | void RegisterCommand(const TString& cmd, TCondition* cond=0) |
48 | { |
49 | fQueueLock.Lock(); |
50 | fQueue.Add(new Command(cmd, cond)); |
51 | if (!fQueueTimerSet) { |
52 | // Force execution in main thread when it is free. |
53 | fQueueTimer.Start(0, kTRUE); |
54 | fQueueTimerSet = kTRUE; |
55 | } |
56 | fQueueLock.UnLock(); |
57 | } |
58 | |
59 | virtual void ProcessQueue() |
60 | { |
61 | Int_t n_proccessed = 0; |
62 | |
63 | while (1) { |
64 | fQueueLock.Lock(); |
65 | Command* c = (Command*) fQueue.First(); |
66 | fQueue.RemoveFirst(); |
67 | fQueueLock.UnLock(); |
68 | |
69 | { // Put this into virtual void ProcessComand(Command*); |
70 | // Need also exception handling. |
71 | gROOT->ProcessLineFast(c->fCommand); |
72 | ++n_proccessed; |
73 | if (c->fAwakeCond != 0) { |
74 | c->fAwakeCond->GetMutex()->Lock(); |
75 | c->fAwakeCond->Signal(); |
76 | c->fAwakeCond->GetMutex()->UnLock(); |
77 | } |
78 | } |
79 | delete c; |
80 | |
81 | fQueueLock.Lock(); |
82 | if (fQueue.IsEmpty()) { |
83 | fQueueTimerSet = kFALSE; |
84 | fQueueLock.UnLock(); |
85 | break; |
86 | } |
87 | fQueueLock.UnLock(); |
88 | } |
89 | |
90 | printf("CommandQueue::ProcessQueue() processed %d commands.\n", n_proccessed); |
91 | } |
92 | |
93 | ClassDef(CommandQueue,0); // Command queue for exection in global CINT context. |
94 | }; |
95 | |
96 | //============================================================================= |
97 | // Global variable |
98 | //============================================================================= |
99 | |
100 | CommandQueue* g_cmd_queue = 0; |
101 | |
102 | void command_queue() |
103 | { |
104 | g_cmd_queue = new CommandQueue; |
105 | printf("Starting command-queue ...\n"); |
106 | } |
107 | |
108 | // ============================================================================ |
84aff7a4 |
109 | // TEveUtil side |
87f60d9e |
110 | // ============================================================================ |
111 | |
84aff7a4 |
112 | #include <TEve.h> |
113 | #include <TEveManager.h> |
114 | #include <TEvePointSet.h> |
87f60d9e |
115 | |
116 | #include <TRandom.h> |
117 | |
51346b82 |
118 | void make_crap(void* arg) |
87f60d9e |
119 | { |
120 | Int_t num = 1024; |
121 | TRandom rnd(0); |
122 | |
84aff7a4 |
123 | TEvePointSet* ps = new TEvePointSet("Testus", num); |
87f60d9e |
124 | for (Int_t i=0; i<num; ++i) { |
125 | ps->SetNextPoint(rnd.Uniform(-100, 100), |
126 | rnd.Uniform(-100, 100), |
127 | rnd.Uniform(-100, 100)); |
128 | } |
fbc350a3 |
129 | ps->SetMainColor(kRed); |
84aff7a4 |
130 | printf("make_crap() -> produced TEvePointSet* %p)\n", ps); |
87f60d9e |
131 | |
132 | ((CommandQueue*)arg)->RegisterCommand |
84aff7a4 |
133 | (Form("register_crap((TEveElement*)0x%lx)", ps)); |
87f60d9e |
134 | } |
135 | |
84aff7a4 |
136 | void register_crap(TEveElement* el) |
87f60d9e |
137 | { |
84aff7a4 |
138 | printf("register_crap(TEveElement* %p)\n", el); |
139 | gEve->AddElement(el); |
140 | gEve->Redraw3D(); |
87f60d9e |
141 | } |
142 | |
143 | void test() |
144 | { |
145 | TThread* thr = new TThread(make_crap, g_cmd_queue); |
146 | thr->Run(); |
147 | } |