source: trunk/Cosy/base/msgqueue.cc@ 10018

Last change on this file since 10018 was 8843, checked in by tbretz, 17 years ago
*** empty log message ***
File size: 2.6 KB
Line 
1#include "msgqueue.h"
2
3using namespace std;
4
5// --------------------------------------------------------------------------
6//
7// This creates the Message queue thread,
8//
9MsgQueue::MsgQueue() : MThread("MsqQueue"), fNextMsg(0), fNextPtr(0)
10{
11 RunThread();
12}
13
14// --------------------------------------------------------------------------
15//
16// The destructor terminates the thread.
17//
18MsgQueue::~MsgQueue()
19{
20 CancelThread();
21
22 fMuxMsg.Lock();
23
24 if (fNextPtr)
25 {
26 delete [] fNextPtr;
27 fNextPtr = 0;
28 }
29
30 fMuxMsg.UnLock();
31}
32
33// --------------------------------------------------------------------------
34//
35// This is the function which must be overloaded.
36// Here you process the messages. mp is a pointer which you can
37// specify when posting the message.
38//
39// If a new messages is posted while the old one is not yet
40// finished the fBreak flag is set. Please test this flag with
41// Break() and try to finish (or stop) the current action as soon
42// as possible. This makes sure, that before a new action is started
43// the old action can be finished correctly by the user.
44//
45Int_t MsgQueue::Proc(int msg, void *mp)
46{
47 return 0;
48}
49
50int MsgQueue::Break() const
51{
52 return fNextMsg>0 || IsThreadCanceled();
53}
54
55// --------------------------------------------------------------------------
56//
57// This is the thread which handles the processing.
58// As soon as a message is posted the fBreak flag is set (see PostMsg)
59// And as soon as the current action is finished the new action is executed
60// in this thread. This makes sure, that the calling program is not stalled.
61//
62Int_t MsgQueue::Thread()
63{
64 while (1)
65 {
66 Int_t msg = 0;
67 char *ptr = 0;
68
69 fMuxMsg.Lock();
70
71 if (fNextMsg)
72 {
73 msg = fNextMsg;
74 ptr = fNextPtr;
75
76 fNextMsg = 0;
77 fNextPtr = 0;
78 }
79
80 fMuxMsg.UnLock();
81
82 if (ptr)
83 {
84 Proc(msg, ptr);
85 delete [] ptr;
86 }
87
88 usleep(1);
89 TThread::CancelPoint();
90 }
91 return 0;
92}
93
94
95// --------------------------------------------------------------------------
96//
97// Use this function to post a message.
98// mp can be a pointer to a data structure. size should be the size of it.
99// size bytes of this structure are copied and a pointer to the copy
100// is forwarded to the Proc function.
101//
102void *MsgQueue::PostMsg(int msg, void *mp, int size)
103{
104 fMuxMsg.Lock();
105
106 fNextMsg = msg;
107
108 if (fNextPtr)
109 delete [] fNextPtr;
110 fNextPtr = new char[size];
111
112 memcpy(fNextPtr, mp, size);
113
114 fMuxMsg.UnLock();
115
116 return 0;
117}
Note: See TracBrowser for help on using the repository browser.