@@ -401,11 +401,14 @@ STATIC int is_feed_type(int motion_type)
401401 }
402402}
403403
404+
404405/*
405406 emcmotCommandHandler() is called each main cycle to read the
406407 shared memory buffer
408+
409+ This function runs with the emcmotCommand struct locked.
407410 */
408- void emcmotCommandHandler (void * arg , long servo_period )
411+ void emcmotCommandHandler_locked (void * arg , long servo_period )
409412{
410413 int joint_num , axis_num , spindle_num ;
411414 int n ,s0 ,s1 ;
@@ -417,11 +420,6 @@ void emcmotCommandHandler(void *arg, long servo_period)
417420 int abort = 0 ;
418421 char * emsg = "" ;
419422
420- /* check for split read */
421- if (emcmotCommand -> head != emcmotCommand -> tail ) {
422- emcmotDebug -> split ++ ;
423- return ; /* not really an error */
424- }
425423 if (emcmotCommand -> commandNum != emcmotStatus -> commandNumEcho ) {
426424 /* increment head count-- we'll be modifying emcmotStatus */
427425 emcmotStatus -> head ++ ;
@@ -2021,3 +2019,15 @@ void emcmotCommandHandler(void *arg, long servo_period)
20212019
20222020 return ;
20232021}
2022+
2023+
2024+ void emcmotCommandHandler (void * arg , long servo_period ) {
2025+ if (rtapi_mutex_try (& emcmotStruct -> command_mutex ) != 0 ) {
2026+ // Failed to take the mutex, because it is held by Task.
2027+ // This means Task is in the process of updating the command.
2028+ // Give up for now, and try again on the next invocation.
2029+ return ;
2030+ }
2031+ emcmotCommandHandler_locked (arg , servo_period );
2032+ rtapi_mutex_give (& emcmotStruct -> command_mutex );
2033+ }
0 commit comments