Last change
on this file since 219 was 219, checked in by ansari, 26 years ago |
Creation module DPC/SysTools Reza 09/04/99
|
File size:
1.9 KB
|
Line | |
---|
1 | /* Classe d'objets avec une methode (DoPeriodic()) appelee periodiquement */
|
---|
2 | /* Eric Aubourg 04/96 */
|
---|
3 | /* Reza Ansari 06/96 */
|
---|
4 | /* LAL/IN2P3 (Orsay) DAPNIA/CEA (Saclay) */
|
---|
5 |
|
---|
6 | #include "defs.h"
|
---|
7 | #include <signal.h>
|
---|
8 | #include <unistd.h>
|
---|
9 |
|
---|
10 | #include "periodic.h"
|
---|
11 |
|
---|
12 | /* --Methode-- */
|
---|
13 | Periodic::Periodic(int dt, UsPeriodicAction act, void * usp)
|
---|
14 | {
|
---|
15 | mDt = dt;
|
---|
16 | mAct = act;
|
---|
17 | mUsp = usp;
|
---|
18 | if (mDt < 1) mDt = 5;
|
---|
19 | mDtms = 1000*mDt;
|
---|
20 | mFgact = false;
|
---|
21 | it = -1;
|
---|
22 | }
|
---|
23 |
|
---|
24 | /* --Methode-- */
|
---|
25 | Periodic::~Periodic()
|
---|
26 | {
|
---|
27 | if (mFgact) Stop();
|
---|
28 | }
|
---|
29 |
|
---|
30 | /* --Methode-- */
|
---|
31 | void Periodic::SetAction(UsPeriodicAction act, void * usp)
|
---|
32 | {
|
---|
33 | mAct = act;
|
---|
34 | mUsp = usp;
|
---|
35 | }
|
---|
36 |
|
---|
37 | /* --Methode-- */
|
---|
38 | void Periodic::SetInterval(int dt)
|
---|
39 | {
|
---|
40 | mDt = dt;
|
---|
41 | if (mDt < 1) mDt = 5;
|
---|
42 | mDtms = 1000*mDt;
|
---|
43 | }
|
---|
44 |
|
---|
45 | /* --Methode-- */
|
---|
46 | void Periodic::SetIntervalms(int dtms)
|
---|
47 | {
|
---|
48 | if (dtms < 1) dtms = 5;
|
---|
49 | mDtms = dtms;
|
---|
50 | mDt = mDtms/1000;
|
---|
51 | if (mDt < 1) mDt = 1;
|
---|
52 | }
|
---|
53 |
|
---|
54 | /* --Methode-- */
|
---|
55 | void Periodic::Start(int dt)
|
---|
56 | {
|
---|
57 | if (mFgact) return;
|
---|
58 | if (dt > 0) { mDt = dt; mDtms = dt*1000; }
|
---|
59 | if (!actifs) actifs = new PeriodicList;
|
---|
60 | it = 0;
|
---|
61 | if (actifs->size() == 0) {
|
---|
62 | signal(SIGALRM, CallBack);
|
---|
63 | alarm(1);
|
---|
64 | }
|
---|
65 | actifs->push_back(this);
|
---|
66 | mFgact = true;
|
---|
67 | return;
|
---|
68 | }
|
---|
69 |
|
---|
70 | /* --Methode-- */
|
---|
71 | void Periodic::Stop()
|
---|
72 | {
|
---|
73 | if (!mFgact) return;
|
---|
74 |
|
---|
75 | //remove(actifs->begin(), actifs->end(), this); // $CHECK$ - Reza
|
---|
76 | actifs->remove(this); // $CHECK$
|
---|
77 | if (actifs->size() == 0) {
|
---|
78 | signal(SIGALRM, SIG_IGN);
|
---|
79 | alarm(0);
|
---|
80 | delete actifs;
|
---|
81 | actifs = NULL;
|
---|
82 | }
|
---|
83 | it = -1; mFgact = false;
|
---|
84 | return;
|
---|
85 | }
|
---|
86 |
|
---|
87 | /* --Methode-- */
|
---|
88 | void Periodic::DoPeriodic()
|
---|
89 | {
|
---|
90 | if (mAct) mAct(mUsp);
|
---|
91 | }
|
---|
92 |
|
---|
93 | PeriodicList* Periodic::actifs = NULL;
|
---|
94 |
|
---|
95 | void Periodic::CallBack(int)
|
---|
96 | {
|
---|
97 | if (!actifs) return;
|
---|
98 |
|
---|
99 | for (PeriodicList::iterator i = actifs->begin(); i != actifs->end(); i++) {
|
---|
100 | Periodic* p = (Periodic*) *i;
|
---|
101 | p->it ++;
|
---|
102 | if (p->it >= p->mDt) {
|
---|
103 | p->it = 0;
|
---|
104 | p->DoPeriodic();
|
---|
105 | }
|
---|
106 | }
|
---|
107 | alarm(1);
|
---|
108 | }
|
---|
109 |
|
---|
Note:
See
TracBrowser
for help on using the repository browser.