-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathActor.cpp
159 lines (139 loc) · 2.47 KB
/
Actor.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
/*******************************************************************************
* Copyright (c) 2018 Rediscover.
*
*******************************************************************************/
#include "Actor.h"
// #define ACTOR_DEBUG 1
#ifdef ACTOR_DEBUG
#define alog(fmt, args...) printf(fmt, ## args)
#else
#define alog(fmt, args...) /* Don't do anything in release builds */
#endif
/*!
Constructor for `Actor`
*/
Actor::Actor()
{
}
/*!
Every actor must be started.
The only action to be executed is starting the thread.
*/
void Actor::start()
{
alog("Actor::start()\r\n");
th.start(mbed::callback(this, &Actor::startRun));
}
/*!
This is the default thread method.
This method does nothing and has to be redefined
in the subclasses.
*/
void Actor::run()
{
alog("Actor::run()\r\n");
while (true)
{
ThisThread::sleep_for(2000);
}
}
/*!
Send a message without parameter to this actor.
\param `tpe` type of the message
*/
void Actor::send(MsgType tpe)
{
Msg *msg = alloc();
if (msg != NULL)
{
msg->type = tpe;
alog("Actor::send() %c\r\n", msg->type);
put(msg);
}
}
/*!
Send a message with a `double` parameter to this actor.
\param `tpe` type of the message
\param `par1` first paremeter
*/
void Actor::send(MsgType tpe, double par1)
{
Msg *msg = alloc();
if (msg != NULL)
{
msg->type = tpe;
msg->arg1 = par1;
alog("Actor::send() %c %lf\r\n", msg->type, msg->arg1);
put(msg);
}
}
/*!
Allocate a message in the `Mail` mailbox.
*/
Msg *Actor::alloc()
{
Msg *m = mbox.alloc();
/*
if (m == NULL)
{
alog("m null\r\n");
}
*/
return m;
}
/*!
Add a message to the mailbox.
*/
void Actor::put(Msg *m)
{
mbox.put(m);
}
/*
osEvent Actor::get()
{
return mbox.get();
}
*/
/*
osEvent Actor::get()
{
while (true)
{
osEvent ev = mbox.get();
if (ev.status == osEventMail)
{
return ev;
}
}
}
*/
/*!
Wait for a message to arrive in the mailbox.
*/
Msg *Actor::get()
{
while (true)
{
osEvent ev = mbox.get();
if (ev.status == osEventMail)
{
return (Msg *)ev.value.p;
}
}
}
/*!
Release a message.
*/
void Actor::free(Msg *m)
{
mbox.free(m);
}
/*!
This is a small helper method which is started as the
thread function. When started, it will start the virtual `run()` method.
*/
void Actor::startRun()
{
alog("Actor::startRun()\r\n");
run();
}