summaryrefslogtreecommitdiff
path: root/chip/host
diff options
context:
space:
mode:
authorShawn Nematbakhsh <shawnn@chromium.org>2018-01-08 14:01:19 -0800
committerchrome-bot <chrome-bot@chromium.org>2018-01-10 18:20:01 -0800
commit7bc128f7d1e7e6a59ed47cbb8ee9e944f17dc0b6 (patch)
tree7842c1de3b7b45cabf4ebe2c0316a13ca849bc11 /chip/host
parent8d1d243134b7234a6df5558e715ea497e0fb97b9 (diff)
downloadchrome-ec-7bc128f7d1e7e6a59ed47cbb8ee9e944f17dc0b6.tar.gz
chip/host: uart: Run uart_monitor_stdin() before task scheduling
After a call to pthread_create(), it is indeterminate which thread the caller or the new thread will next execute. Synchronize with the new thread and allow it to initialize (and print to console, before the print can potentially interfere with other prints) before proceeding. BUG=chromium:715011 BRANCH=None TEST=Run 'make runtests', verify 'Console input initialized' is seen before '--- Emulator initialized after reboot ---': ====== Emulator output ====== No flash storage found. Initializing to 0xff. No RAM data found. Initializing to 0x00. Console input initialized --- Emulator initialized after reboot --- Signed-off-by: Shawn Nematbakhsh <shawnn@chromium.org> Change-Id: Ieb622e9b7eea2d11d4a11a98bb503a44534f676c Reviewed-on: https://chromium-review.googlesource.com/854989 Commit-Ready: Shawn N <shawnn@chromium.org> Tested-by: Shawn N <shawnn@chromium.org> Reviewed-by: Vincent Palatin <vpalatin@chromium.org>
Diffstat (limited to 'chip/host')
-rw-r--r--chip/host/uart.c12
1 files changed, 12 insertions, 0 deletions
diff --git a/chip/host/uart.c b/chip/host/uart.c
index 9db7f1bfe8..cebd529d43 100644
--- a/chip/host/uart.c
+++ b/chip/host/uart.c
@@ -132,12 +132,16 @@ void uart_inject_char(char *s, int sz)
}
}
+static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
+static pthread_cond_t uart_monitor_initialized = PTHREAD_COND_INITIALIZER;
+
void *uart_monitor_stdin(void *d)
{
struct termios org_settings, new_settings;
char buf[INPUT_BUFFER_SIZE];
int rv;
+ pthread_mutex_lock(&mutex);
tcgetattr(0, &org_settings);
new_settings = org_settings;
new_settings.c_lflag &= ~(ECHO | ICANON);
@@ -145,6 +149,9 @@ void *uart_monitor_stdin(void *d)
new_settings.c_cc[VMIN] = 1;
printf("Console input initialized\n");
+ /* Allow uart_init to proceed now that UART monitor is initialized. */
+ pthread_cond_signal(&uart_monitor_initialized);
+ pthread_mutex_unlock(&mutex);
while (1) {
tcsetattr(0, TCSANOW, &new_settings);
rv = read(0, buf, INPUT_BUFFER_SIZE);
@@ -166,7 +173,12 @@ void *uart_monitor_stdin(void *d)
void uart_init(void)
{
+ /* Create UART monitor thread and wait for it to initialize. */
+ pthread_mutex_lock(&mutex);
pthread_create(&input_thread, NULL, uart_monitor_stdin, NULL);
+ pthread_cond_wait(&uart_monitor_initialized, &mutex);
+ pthread_mutex_unlock(&mutex);
+
stopped = 1; /* Not transmitting yet */
init_done = 1;
}