1 | #include "msgqueue.h"
|
---|
2 |
|
---|
3 | #include <iostream.h>
|
---|
4 |
|
---|
5 | #include <unistd.h> // usleep
|
---|
6 | #include <sys/resource.h> // PRIO_PROCESS
|
---|
7 |
|
---|
8 | // --------------------------------------------------------------------------
|
---|
9 | //
|
---|
10 | // This creates the Message queue thread,
|
---|
11 | //
|
---|
12 | MsgQueue::MsgQueue() : fBreak(0)
|
---|
13 | {
|
---|
14 | fMp = new unsigned char;
|
---|
15 | pthread_create(&fThread, NULL, MapThread, this);
|
---|
16 | }
|
---|
17 |
|
---|
18 | // --------------------------------------------------------------------------
|
---|
19 | //
|
---|
20 | // The destructor terminates the thread.
|
---|
21 | //
|
---|
22 | MsgQueue::~MsgQueue()
|
---|
23 | {
|
---|
24 | cout << "~MsgQueue::MsgQueue" << endl;
|
---|
25 | pthread_cancel(fThread);
|
---|
26 | delete (unsigned char*)fMp;
|
---|
27 | }
|
---|
28 |
|
---|
29 | // --------------------------------------------------------------------------
|
---|
30 | //
|
---|
31 | // This is the function which must be overloaded.
|
---|
32 | // Here you process the messages. mp is a pointer which you can
|
---|
33 | // specify when posting the message.
|
---|
34 | //
|
---|
35 | // If a new messages is posted while the old one is not yet
|
---|
36 | // finished the fBreak flag is set. Please test this flag with
|
---|
37 | // Break() and try to finish (or stop) the current action as soon
|
---|
38 | // as possible. This makes sure, that before a new action is started
|
---|
39 | // the old action can be finished correctly by the user.
|
---|
40 | //
|
---|
41 | void *MsgQueue::Proc(int msg, void *mp)
|
---|
42 | {
|
---|
43 | return NULL;
|
---|
44 | }
|
---|
45 |
|
---|
46 | // --------------------------------------------------------------------------
|
---|
47 | //
|
---|
48 | // This is the thread mapper.
|
---|
49 | //
|
---|
50 | void *MsgQueue::MapThread(void *arg)
|
---|
51 | {
|
---|
52 | pthread_detach(pthread_self());
|
---|
53 |
|
---|
54 | setpriority(PRIO_PROCESS, 0, -5);
|
---|
55 |
|
---|
56 | ((MsgQueue*)arg)->Thread();
|
---|
57 |
|
---|
58 | return NULL;
|
---|
59 | }
|
---|
60 |
|
---|
61 | // --------------------------------------------------------------------------
|
---|
62 | //
|
---|
63 | // This is the thread which handles the processing.
|
---|
64 | // As soon as a message is posted the fBreak flag is set (see PostMsg)
|
---|
65 | // And as soon as the current action is finished the new action is executed
|
---|
66 | // in this thread. This makes sure, that the calling program is not stalled.
|
---|
67 | //
|
---|
68 | void MsgQueue::Thread()
|
---|
69 | {
|
---|
70 | //
|
---|
71 | // Tell the poster that processing is done
|
---|
72 | //
|
---|
73 | fStart = 0;
|
---|
74 | while (!fBreak)
|
---|
75 | usleep(1);
|
---|
76 |
|
---|
77 | while(1)
|
---|
78 | {
|
---|
79 | while (!fStart) usleep(1);
|
---|
80 | fStart = 0;
|
---|
81 |
|
---|
82 | //
|
---|
83 | // This makes sure that also a very fast Break() after
|
---|
84 | // a PostMsg is processed correctly
|
---|
85 | //
|
---|
86 | pthread_mutex_lock(&fMuxMsg);
|
---|
87 | fBreak = 0;
|
---|
88 | pthread_mutex_unlock(&fMuxMsg);
|
---|
89 |
|
---|
90 | cout << "Processing Msg 0x" << hex << fMsg << endl;
|
---|
91 | // --- bool quit = fMsg==WM_QUIT;
|
---|
92 | fRc=Proc(fMsg, fMp);
|
---|
93 | cout << "Msg 0x" << hex << fMsg << " processed." << endl;
|
---|
94 |
|
---|
95 | // --- if (quit)
|
---|
96 | // --- break;
|
---|
97 | }
|
---|
98 |
|
---|
99 | // --- fStart = 0;
|
---|
100 | // --- cout << "WM_QUIT posted... leaving msg queue." << endl;
|
---|
101 | }
|
---|
102 |
|
---|
103 | // --------------------------------------------------------------------------
|
---|
104 | //
|
---|
105 | // Use this function to post a message.
|
---|
106 | // mp can be a pointer to a data structure. size should be the size of it.
|
---|
107 | // size bytes of this structure are copied and a pointer to the copy
|
---|
108 | // is forwarded to the Proc function.
|
---|
109 | //
|
---|
110 | void *MsgQueue::PostMsg(int msg, void *mp, int size)
|
---|
111 | {
|
---|
112 | //
|
---|
113 | // Lock Mutex, put msg on stack and tell thread to process message
|
---|
114 | //
|
---|
115 |
|
---|
116 | //
|
---|
117 | // Make sure that only one Proc() is running and can be stopped
|
---|
118 | // stopped and the messages are processed serialized
|
---|
119 | //
|
---|
120 | cout << "Locking MsgQueue mutex..." << flush;
|
---|
121 | pthread_mutex_lock(&fMuxMsg);
|
---|
122 | cout << "done." << endl;
|
---|
123 |
|
---|
124 | //
|
---|
125 | // Set break state and wait until Proc() returned (break state deleted)
|
---|
126 | // This means, that a new command is invoked and (if forseen) the
|
---|
127 | // running command should stop execution.
|
---|
128 | //
|
---|
129 | // This is some kind of controlled user break without using signals
|
---|
130 | //
|
---|
131 | fBreak = 1;
|
---|
132 |
|
---|
133 | //
|
---|
134 | // copy return code from Proc() and set new message
|
---|
135 | //
|
---|
136 | void *rc = fRc;
|
---|
137 |
|
---|
138 | fMsg = msg;
|
---|
139 |
|
---|
140 | delete (unsigned char*)fMp;
|
---|
141 | fMp = new unsigned char[size];
|
---|
142 |
|
---|
143 | memcpy(fMp, mp, size);
|
---|
144 |
|
---|
145 | //
|
---|
146 | // Start Proc()
|
---|
147 | //
|
---|
148 | fStart = 1;
|
---|
149 | cout << "Releasing MsgQueue mutex..." << flush;
|
---|
150 | pthread_mutex_unlock(&fMuxMsg);
|
---|
151 | cout << "done." << endl;
|
---|
152 | while (fStart)
|
---|
153 | usleep(1);
|
---|
154 |
|
---|
155 | cout << "Returning rc = 0x" << hex << rc << endl;
|
---|
156 | return rc;
|
---|
157 | }
|
---|
158 |
|
---|