3535 * limitations under the License.
3636 */
3737
38+ #include "Arduino.h"
3839#include "xsAll.h"
3940#include "xs.h"
4041#include "mc.defines.h"
@@ -1298,42 +1299,84 @@ struct modMessageRecord {
12981299 void * refcon ;
12991300 uint16_t length ;
13001301 uint8_t marked ;
1301- uint8_t unused ;
1302+ uint8_t isStatic ; // this doubles as a flag to indicate entry is use gMessagePool
13021303 char message [1 ];
13031304};
13041305
13051306static modMessage gMessageQueue ;
13061307
13071308extern void esp_schedule ();
13081309
1310+ static void appendMessage (modMessage msg )
1311+ {
1312+ msg -> next = NULL ;
1313+ msg -> marked = 0 ;
1314+
1315+ modCriticalSectionBegin ();
1316+ if (NULL == gMessageQueue ) {
1317+ gMessageQueue = msg ;
1318+ esp_schedule ();
1319+ }
1320+ else {
1321+ modMessage walker ;
1322+
1323+ for (walker = gMessageQueue ; NULL != walker -> next ; walker = walker -> next )
1324+ ;
1325+ walker -> next = msg ;
1326+ }
1327+ modCriticalSectionEnd ();
1328+ }
1329+
13091330int modMessagePostToMachine (xsMachine * the , uint8_t * message , uint16_t messageLength , modMessageDeliver callback , void * refcon )
13101331{
13111332 modMessage msg = c_malloc (sizeof (modMessageRecord ) + messageLength );
13121333 if (!msg ) return -1 ;
13131334
1314- msg -> next = NULL ;
13151335 msg -> the = the ;
13161336 msg -> callback = callback ;
13171337 msg -> refcon = refcon ;
1318- msg -> marked = 0 ;
1338+ msg -> isStatic = 0 ;
13191339
13201340 if (message && messageLength )
13211341 c_memmove (msg -> message , message , messageLength );
13221342 msg -> length = messageLength ;
13231343
1324- // append to message queue
1325- if (NULL == gMessageQueue ) {
1326- gMessageQueue = msg ;
1327- esp_schedule ();
1344+ appendMessage (msg );
1345+
1346+ return 0 ;
1347+ }
1348+
1349+ #define kMessagePoolCount (2)
1350+ static modMessageRecord gMessagePool [kMessagePoolCount ];
1351+
1352+ int modMessagePostToMachineFromPool (xsMachine * the , modMessageDeliver callback , void * refcon )
1353+ {
1354+ modMessage msg ;
1355+ uint8_t i ;
1356+
1357+ modCriticalSectionBegin ();
1358+
1359+ for (i = 0 , msg = gMessagePool ; i < kMessagePoolCount ; i ++ , msg ++ ) {
1360+ if (!msg -> isStatic ) {
1361+ msg -> isStatic = 1 ;
1362+ break ;
1363+ }
13281364 }
1329- else {
1330- modMessage walker ;
13311365
1332- for (walker = gMessageQueue ; NULL != walker -> next ; walker = walker -> next )
1333- ;
1334- walker -> next = msg ;
1366+ modCriticalSectionEnd ();
1367+
1368+ if ((gMessagePool + kMessagePoolCount ) == msg ) {
1369+ modLog ("message pool full" );
1370+ return -1 ;
13351371 }
13361372
1373+ msg -> the = the ;
1374+ msg -> callback = callback ;
1375+ msg -> refcon = refcon ;
1376+ msg -> length = 0 ;
1377+
1378+ appendMessage (msg );
1379+
13371380 return 0 ;
13381381}
13391382
@@ -1349,12 +1392,18 @@ int modMessageService(void)
13491392 while (msg && msg -> marked ) {
13501393 modMessage next ;
13511394
1352- if (msg -> the )
1395+ if (msg -> callback )
13531396 (msg -> callback )(msg -> the , msg -> refcon , msg -> message , msg -> length );
1354- next = msg -> next ;
1355- c_free (msg );
13561397
1398+ modCriticalSectionBegin ();
1399+ next = msg -> next ;
13571400 gMessageQueue = next ;
1401+ modCriticalSectionEnd ();
1402+
1403+ if (msg -> isStatic )
1404+ msg -> isStatic = 0 ; // return to pool
1405+ else
1406+ c_free (msg );
13581407 msg = next ;
13591408 }
13601409
@@ -1368,7 +1417,7 @@ void modMachineTaskUninit(xsMachine *the)
13681417
13691418 while (msg ) {
13701419 if (msg -> the == the )
1371- msg -> the = NULL ;
1420+ msg -> callback = NULL ;
13721421 msg = msg -> next ;
13731422 }
13741423}
0 commit comments