5757#include <float.h>
5858#include "posemath.h"
5959#include "rtapi.h"
60+ #include "rtapi_mutex.h"
6061#include "hal.h"
6162#include "motion.h"
6263#include "tp.h"
6364#include "mot_priv.h"
65+ #include "motion_struct.h"
6466#include "rtapi_math.h"
6567#include "motion_types.h"
6668#include "homing.h"
@@ -374,11 +376,14 @@ STATIC int is_feed_type(int motion_type)
374376 }
375377}
376378
379+
377380/*
378381 emcmotCommandHandler() is called each main cycle to read the
379382 shared memory buffer
383+
384+ This function runs with the emcmotCommand struct locked.
380385 */
381- void emcmotCommandHandler (void * arg , long servo_period )
386+ void emcmotCommandHandler_locked (void * arg , long servo_period )
382387{
383388 int joint_num , spindle_num ;
384389 int n ,s0 ,s1 ;
@@ -389,11 +394,6 @@ void emcmotCommandHandler(void *arg, long servo_period)
389394 int abort = 0 ;
390395 char * emsg = "" ;
391396
392- /* check for split read */
393- if (emcmotCommand -> head != emcmotCommand -> tail ) {
394- emcmotInternal -> split ++ ;
395- return ; /* not really an error */
396- }
397397 if (emcmotCommand -> commandNum != emcmotStatus -> commandNumEcho ) {
398398 /* increment head count-- we'll be modifying emcmotStatus */
399399 emcmotStatus -> head ++ ;
@@ -1917,3 +1917,15 @@ void emcmotCommandHandler(void *arg, long servo_period)
19171917
19181918 return ;
19191919}
1920+
1921+
1922+ void emcmotCommandHandler (void * arg , long servo_period ) {
1923+ if (rtapi_mutex_try (& emcmotStruct -> command_mutex ) != 0 ) {
1924+ // Failed to take the mutex, because it is held by Task.
1925+ // This means Task is in the process of updating the command.
1926+ // Give up for now, and try again on the next invocation.
1927+ return ;
1928+ }
1929+ emcmotCommandHandler_locked (arg , servo_period );
1930+ rtapi_mutex_give (& emcmotStruct -> command_mutex );
1931+ }
0 commit comments