@@ -80,6 +80,12 @@ fpu_control_t __fpu_control = _FPU_IEEE & ~(_FPU_MASK_IM | _FPU_MASK_ZM | _FPU_M
8080#include " nml_oi.hh"
8181#include " task.hh" // emcTaskCommand etc
8282
83+ /* time after which the user interface is declared dead
84+ * because it would'nt read any more messages
85+ */
86+ #define DEFAULT_EMC_UI_TIMEOUT 5.0
87+
88+
8389// command line args-- global so that other modules can access
8490int Argc;
8591char **Argv;
@@ -150,16 +156,46 @@ static void emctask_quit(int sig)
150156 signal (sig, emctask_quit);
151157}
152158
159+ /* make sure at least space bytes are available on
160+ * error channel; wait a bit to drain if needed
161+ */
162+ int emcErrorBufferOKtoWrite (int space, const char *caller)
163+ {
164+ // check channel for validity
165+ if (emcErrorBuffer == NULL )
166+ return -1 ;
167+ if (!emcErrorBuffer->valid ())
168+ return -1 ;
169+
170+ double send_errorchan_timout = etime () + DEFAULT_EMC_UI_TIMEOUT;
171+
172+ while (etime () < send_errorchan_timout) {
173+ if (emcErrorBuffer->get_space_available () < space) {
174+ esleep (0.01 );
175+ continue ;
176+ } else {
177+ break ;
178+ }
179+ }
180+ if (etime () >= send_errorchan_timout) {
181+ if (EMC_DEBUG & EMC_DEBUG_TASK_ISSUE) {
182+ rcs_print (" timeout waiting for error channel to drain, caller=`%s' request=%d\n " , caller,space);
183+ }
184+ return -1 ;
185+ } else {
186+ // printf("--- %d bytes available after %f seconds\n", space, etime() - send_errorchan_timout + DEFAULT_EMC_UI_TIMEOUT);
187+ }
188+ return 0 ;
189+ }
190+
191+
153192// implementation of EMC error logger
154193int emcOperatorError (int id, const char *fmt, ...)
155194{
156195 EMC_OPERATOR_ERROR error_msg;
157196 va_list ap;
158197
159- // check channel for validity
160- if (emcErrorBuffer == NULL )
161- return -1 ;
162- if (!emcErrorBuffer->valid ())
198+ if ( emcErrorBufferOKtoWrite (sizeof (error_msg) * 2 , " emcOperatorError" ))
163199 return -1 ;
164200
165201 if (NULL == fmt) {
@@ -191,10 +227,7 @@ int emcOperatorText(int id, const char *fmt, ...)
191227 EMC_OPERATOR_TEXT text_msg;
192228 va_list ap;
193229
194- // check channel for validity
195- if (emcErrorBuffer == NULL )
196- return -1 ;
197- if (!emcErrorBuffer->valid ())
230+ if ( emcErrorBufferOKtoWrite (sizeof (text_msg) * 2 , " emcOperatorText" ))
198231 return -1 ;
199232
200233 // write args to NML message (ignore int text code)
@@ -214,10 +247,7 @@ int emcOperatorDisplay(int id, const char *fmt, ...)
214247 EMC_OPERATOR_DISPLAY display_msg;
215248 va_list ap;
216249
217- // check channel for validity
218- if (emcErrorBuffer == NULL )
219- return -1 ;
220- if (!emcErrorBuffer->valid ())
250+ if ( emcErrorBufferOKtoWrite (sizeof (display_msg) * 2 , " emcOperatorDisplay" ))
221251 return -1 ;
222252
223253 // write args to NML message (ignore int display code)
0 commit comments