1 | #include "msgqueue.h"
|
---|
2 |
|
---|
3 | #include <iostream.h>
|
---|
4 |
|
---|
5 | #include <sys/resource.h> // PRIO_PROCESS
|
---|
6 |
|
---|
7 | MsgQueue::MsgQueue() : fBreak(0)
|
---|
8 | {
|
---|
9 | fMp = new unsigned char;
|
---|
10 | pthread_create(&fThread, NULL, MapThread, this);
|
---|
11 | }
|
---|
12 |
|
---|
13 | MsgQueue::~MsgQueue()
|
---|
14 | {
|
---|
15 | pthread_cancel(fThread);
|
---|
16 | delete fMp;
|
---|
17 | }
|
---|
18 |
|
---|
19 | void *MsgQueue::Proc(int msg, void *mp)
|
---|
20 | {
|
---|
21 | return NULL;
|
---|
22 | }
|
---|
23 |
|
---|
24 | void *MsgQueue::MapThread(void *arg)
|
---|
25 | {
|
---|
26 | pthread_detach(pthread_self());
|
---|
27 |
|
---|
28 | setpriority(PRIO_PROCESS, 0, -5);
|
---|
29 |
|
---|
30 | ((MsgQueue*)arg)->Thread();
|
---|
31 |
|
---|
32 | return NULL;
|
---|
33 | }
|
---|
34 |
|
---|
35 | void MsgQueue::Thread()
|
---|
36 | {
|
---|
37 | //
|
---|
38 | // Tell the poster that processing is done
|
---|
39 | //
|
---|
40 | fStart = 0;
|
---|
41 | while (!fBreak)
|
---|
42 | usleep(1);
|
---|
43 |
|
---|
44 | while(1)
|
---|
45 | {
|
---|
46 | while (!fStart) usleep(1);
|
---|
47 | fStart = 0;
|
---|
48 |
|
---|
49 | //
|
---|
50 | // This makes sure that also a very fast Break() after
|
---|
51 | // a PostMsg is processed correctly
|
---|
52 | //
|
---|
53 | pthread_mutex_lock(&fMuxMsg);
|
---|
54 | fBreak = 0;
|
---|
55 | pthread_mutex_unlock(&fMuxMsg);
|
---|
56 |
|
---|
57 | fRc=Proc(fMsg, fMp);
|
---|
58 | }
|
---|
59 | }
|
---|
60 |
|
---|
61 | void *MsgQueue::PostMsg(int msg, void *mp, int size)
|
---|
62 | {
|
---|
63 | //
|
---|
64 | // Lock Mutex, put msg on stack and tell thread to process message
|
---|
65 | //
|
---|
66 |
|
---|
67 | //
|
---|
68 | // Make sure that only one Proc() is running and can be stopped
|
---|
69 | // stopped and the messages are processed serialized
|
---|
70 | //
|
---|
71 | pthread_mutex_lock(&fMuxMsg);
|
---|
72 |
|
---|
73 | //
|
---|
74 | // Set break state and wait until Proc() returned (break state deleted)
|
---|
75 | // This means, that a new command is invoked and (if forseen) the
|
---|
76 | // running command should stop execution.
|
---|
77 | //
|
---|
78 | // This is some kind of controlled user break without using signals
|
---|
79 | //
|
---|
80 | fBreak = 1;
|
---|
81 |
|
---|
82 | //
|
---|
83 | // copy return code from Proc() and set new message
|
---|
84 | //
|
---|
85 | void *rc = fRc;
|
---|
86 |
|
---|
87 | fMsg = msg;
|
---|
88 |
|
---|
89 | delete fMp;
|
---|
90 | fMp = new unsigned char[size];
|
---|
91 |
|
---|
92 | memcpy(fMp, mp, size);
|
---|
93 |
|
---|
94 | //
|
---|
95 | // Start Proc()
|
---|
96 | //
|
---|
97 | fStart = 1;
|
---|
98 | pthread_mutex_unlock(&fMuxMsg);
|
---|
99 | while (fStart) usleep(1);
|
---|
100 |
|
---|
101 | return rc;
|
---|
102 | }
|
---|
103 |
|
---|