Skip to content

Commit fa57dc6

Browse files
committed
Merge remote-tracking branch 'origin/2.8' into 2.9
* origin/2.8: fix a race in communication between Task and Motion motion: fix indentation & comment, no behavior change
2 parents e134543 + 6451c03 commit fa57dc6

6 files changed

Lines changed: 34 additions & 19 deletions

File tree

src/emc/motion-logger/motion-logger.c

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -284,12 +284,11 @@ int main(int argc, char* argv[]) {
284284
init_comm_buffers();
285285

286286
while (1) {
287-
if (c->commandNum != c->tail) {
288-
// "split read"
289-
continue;
290-
}
287+
rtapi_mutex_get(&emcmotStruct->command_mutex);
288+
291289
if (c->commandNum == emcmotStatus->commandNumEcho) {
292290
// nothing new
291+
rtapi_mutex_give(&emcmotStruct->command_mutex);
293292
maybe_reopen_logfile();
294293
usleep(10 * 1000);
295294
continue;
@@ -682,6 +681,8 @@ int main(int argc, char* argv[]) {
682681
emcmotStatus->commandNumEcho = c->commandNum;
683682
emcmotStatus->commandStatus = EMCMOT_COMMAND_OK;
684683
emcmotStatus->tail = emcmotStatus->head;
684+
685+
rtapi_mutex_give(&emcmotStruct->command_mutex);
685686
}
686687

687688
return 0;

src/emc/motion/command.c

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -57,10 +57,12 @@
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+
}

src/emc/motion/motion.c

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -855,10 +855,8 @@ static int init_comm_buffers(void)
855855
emcmotErrorInit(emcmotError);
856856

857857
/* init command struct */
858-
emcmotCommand->head = 0;
859858
emcmotCommand->command = 0;
860859
emcmotCommand->commandNum = 0;
861-
emcmotCommand->tail = 0;
862860

863861
/* init status struct */
864862
emcmotStatus->head = 0;

src/emc/motion/motion.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,6 @@ extern "C" {
206206
memory, and all commands from higher level code come thru it.
207207
*/
208208
typedef struct emcmot_command_t {
209-
unsigned char head; /* flag count for mutex detect */
210209
cmd_code_t command; /* command code (enum) */
211210
int commandNum; /* increment this for new command */
212211
double motor_offset; /* offset from joint to motor position */
@@ -260,7 +259,6 @@ extern "C" {
260259
char direction; /* CANON_DIRECTION flag for spindle orient */
261260
double timeout; /* of wait for spindle orient to complete */
262261
unsigned char wait_for_spindle_at_speed; // EMCMOT_SPINDLE_ON now carries this, for next feed move
263-
unsigned char tail; /* flag count for mutex detect */
264262
int arcBlendOptDepth;
265263
int arcBlendEnable;
266264
int arcBlendFallbackEnable;

src/emc/motion/motion_struct.h

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,14 @@
1212
#ifndef MOTION_STRUCT_H
1313
#define MOTION_STRUCT_H
1414

15+
#include <rtapi_mutex.h>
16+
17+
1518
/* big comm structure, for upper memory */
1619
typedef struct emcmot_struct_t {
17-
struct emcmot_command_t command; /* struct used to pass commands/data
18-
to the RT module from usr space */
20+
rtapi_mutex_t command_mutex; // Used to protect access to `command`.
21+
struct emcmot_command_t command; /* struct used to pass commands/data from Task to Motion */
22+
1923
struct emcmot_status_t status; /* Struct used to store RT status */
2024
struct emcmot_config_t config; /* Struct used to store RT config */
2125
struct emcmot_error_t error; /* ring buffer for error messages */

src/emc/motion/usrmotintf.cc

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -75,24 +75,26 @@ int usrmotWriteEmcmotCommand(emcmot_command_t * c)
7575
{
7676
emcmot_status_t s;
7777
static int commandNum = 0;
78-
static unsigned char headCount = 0;
7978
double end;
8079

8180
if (!MOTION_ID_VALID(c->id)) {
8281
rcs_print("USRMOT: ERROR: invalid motion id: %d\n",c->id);
8382
return EMCMOT_COMM_INVALID_MOTION_ID;
8483
}
85-
c->head = ++headCount;
86-
c->tail = c->head;
84+
8785
c->commandNum = ++commandNum;
8886

8987
/* check for mapped mem still around */
9088
if (0 == emcmotCommand) {
9189
rcs_print("USRMOT: ERROR: can't connect to shared memory\n");
9290
return EMCMOT_COMM_ERROR_CONNECT;
9391
}
92+
9493
/* copy entire command structure to shared memory */
94+
rtapi_mutex_get(&emcmotStruct->command_mutex);
9595
*emcmotCommand = *c;
96+
rtapi_mutex_give(&emcmotStruct->command_mutex);
97+
9698
/* poll for receipt of command */
9799
/* set timeout for comm failure, now + timeout */
98100
end = etime() + EMCMOT_COMM_TIMEOUT;

0 commit comments

Comments
 (0)