source: trunk/MagicSoft/Cosy/base/msgqueue.cc@ 774

Last change on this file since 774 was 732, checked in by tbretz, 24 years ago
*** empty log message ***
File size: 1.7 KB
Line 
1#include "msgqueue.h"
2
3#include <iostream.h>
4
5#include <sys/resource.h> // PRIO_PROCESS
6
7MsgQueue::MsgQueue() : fBreak(0)
8{
9 fMp = new unsigned char;
10 pthread_create(&fThread, NULL, MapThread, this);
11}
12
13MsgQueue::~MsgQueue()
14{
15 pthread_cancel(fThread);
16 delete fMp;
17}
18
19void *MsgQueue::Proc(int msg, void *mp)
20{
21 return NULL;
22}
23
24void *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
35void 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
61void *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 //
76 fBreak = 1;
77
78 //
79 // copy return code from Proc() and set new message
80 //
81 void *rc = fRc;
82
83 fMsg = msg;
84
85 delete fMp;
86 fMp = new unsigned char[size];
87
88 memcpy(fMp, mp, size);
89
90 //
91 // Start Proc()
92 //
93 fStart = 1;
94 pthread_mutex_unlock(&fMuxMsg);
95 while (fStart) usleep(1);
96
97 return rc;
98}
99
Note: See TracBrowser for help on using the repository browser.