From 5f42ef3c92b3a1f5679e92ba3b2bb3a547c16da2 Mon Sep 17 00:00:00 2001 From: Sebastian Kloska Date: Tue, 10 Sep 2019 14:25:34 +0200 Subject: Proper setup and error checking of pthread_create * Replace all start_XXX functions with one start_thread * Don't make pthread_t static * Don't use pthread_attr_t. Simply pass NULL * Bail out when pthread_create() fails * Check if MAX_THREADS gets exceeded Signed-off-by: Sebastian Kloska --- src/system/dlt-system-process-handling.c | 42 +++++++++++++++++++++++++++----- 1 file changed, 36 insertions(+), 6 deletions(-) (limited to 'src/system/dlt-system-process-handling.c') diff --git a/src/system/dlt-system-process-handling.c b/src/system/dlt-system-process-handling.c index 8b89c03..af6ba66 100644 --- a/src/system/dlt-system-process-handling.c +++ b/src/system/dlt-system-process-handling.c @@ -119,32 +119,62 @@ void start_threads(DltSystemConfiguration *config) threads.threads[i] = 0; #if defined(DLT_SYSTEMD_WATCHDOG_ENABLE) - start_systemd_watchdog(config); + start_thread(config, watchdog_thread, "systemd watchdog"); #endif if (config->Shell.Enable) init_shell(); if (config->LogFile.Enable) - start_logfile(config); + start_thread(config, logfile_thread, "log file"); if (config->Filetransfer.Enable) - start_filetransfer(config); + start_thread(config, filetransfer_thread, "file transfer"); if (config->LogProcesses.Enable) - start_logprocess(config); + start_thread(config, logprocess_thread, "log process"); if (config->Syslog.Enable) - start_syslog(config); + start_thread(config, syslog_thread, "syslog"); #if defined(DLT_SYSTEMD_JOURNAL_ENABLE) if (config->Journal.Enable) - start_systemd_journal(config); + start_thread(config, journal_thread, "systemd journal"); #endif } +/** + * Start a thread and add it to the thread pool. + */ + +void start_thread(DltSystemConfiguration *conf, + void (thread)(void *), const char *name) +{ + if (threads.count == MAX_THREADS) { + DLT_LOG(dltsystem, DLT_LOG_ERROR, + DLT_STRING("Could not create thread for "), + DLT_STRING(name), + DLT_STRING("Out of thread slots.\n")); + return; + } + + DLT_LOG(dltsystem, DLT_LOG_DEBUG, DLT_STRING("Creating thread for "), + DLT_STRING(name), + DLT_STRING("\n")); + + pthread_t pt; + + if (pthread_create(&pt, NULL, (void *)thread, conf) == 0) + threads.threads[threads.count++] = pt; + else + DLT_LOG(dltsystem, DLT_LOG_ERROR, + DLT_STRING("Could not create thread for "), + DLT_STRING(name), + DLT_STRING("\n")); +} + /** * Wait for threads to exit. * There's not actually a condition currently -- cgit v1.2.1