| 1 | // SPDX-License-Identifier: BSD-2-Clausecxx . . $ `<file>/src/thread.c`/ |
| 2 | /* |
| 3 | * Copyright (C) 2020,2022 Linutronix GmbH |
| 4 | * Author Kurt Kanzenbach <[email protected]> |
| 5 | */ |
| 6 | |
| 7 | #include <errno.h> |
| 8 | #include <pthread.h> |
| 9 | #include <stdio.h> |
| 10 | #include <stdlib.h> |
| 11 | #include <string.h> |
| 12 | #include <unistd.h> |
| 13 | |
| 14 | #include "config.h"cxx . . $ `<file>/src/config.h`/ |
| 15 | #include "thread.h"cxx . . $ `<file>/src/thread.h`/ |
| 16 | #include "utils.h"cxx . . $ `<file>/src/utils.h`/ |
| 17 | |
| 18 | int create_rt_threadcxx . . $ create_rt_thread(670ec5a211ade64c).(pthread_tcxx . . $ pthread_t# *task_id, const char *thread_name, int thread_priority, int cpu_core, |
| 19 | void *(*thread_routine)(void *), void *data) |
| 20 | { |
| 21 | struct sched_paramcxx . . $ sched_param# param; |
| 22 | pthread_attr_tcxx . . $ pthread_attr_t# attr; |
| 23 | cpu_set_tcxx . . $ cpu_set_t# cpus; |
| 24 | int ret; |
| 25 | |
| 26 | ret = pthread_attr_initcxx . . $ pthread_attr_init(26f1688c57ac69c9).(&attr); |
| 27 | if (ret) { |
| 28 | pthread_errorcxx . . $ pthread_error(66835f578de1bcb3).(ret, "pthread_attr_init() failed"); |
| 29 | goto err; |
| 30 | } |
| 31 | |
| 32 | /* 2 MiB stack should be enough for all threads. */ |
| 33 | ret = pthread_attr_setstacksizecxx . . $ pthread_attr_setstacksize(c94897328766e9eb).(&attr, 2 * 1024 * 1024); |
| 34 | if (ret) { |
| 35 | pthread_errorcxx . . $ pthread_error(66835f578de1bcb3).(ret, "pthread_attr_setstacksize() failed"); |
| 36 | goto err; |
| 37 | } |
| 38 | |
| 39 | ret = pthread_attr_setschedpolicycxx . . $ pthread_attr_setschedpolicy(1c205214b8a99ab8).(&attr, SCHED_FIFOcxx . . $ `<external>/4872883757279652994/sched.h:29:9`!); |
| 40 | if (ret) { |
| 41 | pthread_errorcxx . . $ pthread_error(66835f578de1bcb3).(ret, "pthread_attr_setschedpolicy() failed"); |
| 42 | goto err; |
| 43 | } |
| 44 | |
| 45 | param.sched_prioritycxx . . $ `<external>/17522079545615609418/sched.h:47:9`! = thread_priority; |
| 46 | ret = pthread_attr_setschedparamcxx . . $ pthread_attr_setschedparam(4420ee5e0ea92128).(&attr, ¶m); |
| 47 | if (ret) { |
| 48 | pthread_errorcxx . . $ pthread_error(66835f578de1bcb3).(ret, "pthread_attr_setschedparam() failed"); |
| 49 | goto err; |
| 50 | } |
| 51 | |
| 52 | ret = pthread_attr_setinheritschedcxx . . $ pthread_attr_setinheritsched(1c205214b8a99ab8).(&attr, PTHREAD_EXPLICIT_SCHEDcxx . . $ `<external>/14712363683955484316/pthread.h:129:9`!); |
| 53 | if (ret) { |
| 54 | pthread_errorcxx . . $ pthread_error(66835f578de1bcb3).(ret, "pthread_attr_setinheritsched() failed"); |
| 55 | goto err; |
| 56 | } |
| 57 | |
| 58 | CPU_ZEROcxx . . $ `<external>/17522079545615609418/sched.h:96:10`!(&cpus); |
| 59 | CPU_SETcxx . . $ `<external>/17522079545615609418/sched.h:92:10`!(cpu_core, &cpus); |
| 60 | ret = pthread_attr_setaffinity_npcxx . . $ pthread_attr_setaffinity_np(6cc641810eb60b10).(&attr, sizeof(cpus), &cpus); |
| 61 | if (ret) { |
| 62 | pthread_errorcxx . . $ pthread_error(66835f578de1bcb3).(ret, "pthread_attr_setaffinity_np() failed"); |
| 63 | goto err; |
| 64 | } |
| 65 | |
| 66 | ret = pthread_createcxx . . $ pthread_create(2141a84bcac899e5).(task_id, &attr, thread_routine, data); |
| 67 | if (ret) { |
| 68 | pthread_errorcxx . . $ pthread_error(66835f578de1bcb3).(ret, "pthread_create() failed"); |
| 69 | goto err; |
| 70 | } |
| 71 | |
| 72 | pthread_setname_npcxx . . $ pthread_setname_np(bee54c13bee684da).(*task_id, thread_name); |
| 73 | |
| 74 | return 0; |
| 75 | |
| 76 | err: |
| 77 | return -ret; |
| 78 | } |
| 79 | |
| 80 | void init_mutexcxx . . $ init_mutex(4f83bfebf4bb9c8b).(pthread_mutex_tcxx . . $ pthread_mutex_t# *mutex) |
| 81 | { |
| 82 | pthread_mutexattr_tcxx . . $ pthread_mutexattr_t# mattr; |
| 83 | |
| 84 | /* Set priority inheritance protocol on this mutex. It's disabled by default. */ |
| 85 | pthread_mutexattr_initcxx . . $ pthread_mutexattr_init(7099fa328d29ec1).(&mattr); |
| 86 | pthread_mutexattr_setprotocolcxx . . $ pthread_mutexattr_setprotocol(490643a50482da1c).(&mattr, PTHREAD_PRIO_INHERITcxx . . $ PTHREAD_PRIO_INHERIT.); |
| 87 | pthread_mutex_initcxx . . $ pthread_mutex_init(db9d29dab44ea1aa).(mutex, &mattr); |
| 88 | } |
| 89 | |
| 90 | void init_condition_variablecxx . . $ init_condition_variable(23b56462c1e1452f).(pthread_cond_tcxx . . $ pthread_cond_t# *cond_var) |
| 91 | { |
| 92 | pthread_condattr_tcxx . . $ pthread_condattr_t# cattr; |
| 93 | |
| 94 | pthread_condattr_initcxx . . $ pthread_condattr_init(646f2394bb955cef).(&cattr); |
| 95 | pthread_condattr_setclockcxx . . $ pthread_condattr_setclock(92d5a7284c6d4547).(&cattr, CLOCK_MONOTONICcxx . . $ `<external>/89459239771508533/time.h:48:10`!); |
| 96 | pthread_cond_initcxx . . $ pthread_cond_init(fc876bc1ec1249fc).(cond_var, &cattr); |
| 97 | } |
| 98 | |
| 99 | static struct thread_contextcxx . . $ thread_context# *find_next_pn_threadcxx . . $ find_next_pn_thread(153c2c644d0af911).(struct thread_contextcxx . . $ thread_context# *pn_threads, int start) |
| 100 | { |
| 101 | switch (start) { |
| 102 | case TSN_HIGH_THREADcxx . . $ TSN_HIGH_THREAD.: |
| 103 | if (config_is_traffic_class_activecxx . . $ config_is_traffic_class_active(c5f7da30cbc0f4e9).("TsnLow")) |
| 104 | return &pn_threads[TSN_LOW_THREADcxx . . $ TSN_LOW_THREAD.]; |
| 105 | case TSN_LOW_THREADcxx . . $ TSN_LOW_THREAD.: |
| 106 | if (config_is_traffic_class_activecxx . . $ config_is_traffic_class_active(c5f7da30cbc0f4e9).("Rtc")) |
| 107 | return &pn_threads[RTC_THREADcxx . . $ RTC_THREAD.]; |
| 108 | case RTC_THREADcxx . . $ RTC_THREAD.: |
| 109 | if (config_is_traffic_class_activecxx . . $ config_is_traffic_class_active(c5f7da30cbc0f4e9).("Rta")) |
| 110 | return &pn_threads[RTA_THREADcxx . . $ RTA_THREAD.]; |
| 111 | case RTA_THREADcxx . . $ RTA_THREAD.: |
| 112 | if (config_is_traffic_class_activecxx . . $ config_is_traffic_class_active(c5f7da30cbc0f4e9).("Dcp")) |
| 113 | return &pn_threads[DCP_THREADcxx . . $ DCP_THREAD.]; |
| 114 | case DCP_THREADcxx . . $ DCP_THREAD.: |
| 115 | if (config_is_traffic_class_activecxx . . $ config_is_traffic_class_active(c5f7da30cbc0f4e9).("Lldp")) |
| 116 | return &pn_threads[LLDP_THREADcxx . . $ LLDP_THREAD.]; |
| 117 | case LLDP_THREADcxx . . $ LLDP_THREAD.: |
| 118 | if (config_is_traffic_class_activecxx . . $ config_is_traffic_class_active(c5f7da30cbc0f4e9).("UdpHigh")) |
| 119 | return &pn_threads[UDP_HIGH_THREADcxx . . $ UDP_HIGH_THREAD.]; |
| 120 | case UDP_HIGH_THREADcxx . . $ UDP_HIGH_THREAD.: |
| 121 | if (config_is_traffic_class_activecxx . . $ config_is_traffic_class_active(c5f7da30cbc0f4e9).("UdpLow")) |
| 122 | return &pn_threads[UDP_LOW_THREADcxx . . $ UDP_LOW_THREAD.]; |
| 123 | case UDP_LOW_THREADcxx . . $ UDP_LOW_THREAD.: |
| 124 | return NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!; |
| 125 | } |
| 126 | |
| 127 | return NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!; |
| 128 | } |
| 129 | |
| 130 | int link_pn_threadscxx . . $ link_pn_threads(8542e1c024f3ff56).(struct thread_contextcxx . . $ thread_context# *pn_threads) |
| 131 | { |
| 132 | struct thread_contextcxx . . $ thread_context# *t, *p; |
| 133 | |
| 134 | /* |
| 135 | * The Profinet traffic classes have a dedicated order: |
| 136 | * TSN -> RTC -> RTA -> DCP -> LLDP -> UDP |
| 137 | * |
| 138 | * This code will link the traffic classes in order. Non-active traffic classes will be |
| 139 | * skipped. |
| 140 | */ |
| 141 | for (int i = 0; i < NUM_PN_THREAD_TYPEScxx . . $ NUM_PN_THREAD_TYPES.; i++) { |
| 142 | struct thread_contextcxx . . $ thread_context# *current = &pn_threads[i]; |
| 143 | |
| 144 | current->nextcxx . . $ thread_context#next. = find_next_pn_threadcxx . . $ find_next_pn_thread(153c2c644d0af911).(pn_threads, i); |
| 145 | } |
| 146 | |
| 147 | /* |
| 148 | * The first traffic class is either |
| 149 | * a) TSN in case of Profinet TSN, or |
| 150 | * b) RTC in case of Profinet RT. |
| 151 | * |
| 152 | * GenericL2 has nothing todo with Profinet. |
| 153 | */ |
| 154 | if (config_is_traffic_class_activecxx . . $ config_is_traffic_class_active(c5f7da30cbc0f4e9).("TsnHigh")) { |
| 155 | pn_threads[TSN_HIGH_THREADcxx . . $ TSN_HIGH_THREAD.].is_firstcxx . . $ thread_context#is_first. = truecxx . . $ `<external>/14268322491693841775/stdbool.h:37:9`!; |
| 156 | t = &pn_threads[TSN_HIGH_THREADcxx . . $ TSN_HIGH_THREAD.]; |
| 157 | } else if (config_is_traffic_class_activecxx . . $ config_is_traffic_class_active(c5f7da30cbc0f4e9).("Rtc")) { |
| 158 | pn_threads[RTC_THREADcxx . . $ RTC_THREAD.].is_firstcxx . . $ thread_context#is_first. = truecxx . . $ `<external>/14268322491693841775/stdbool.h:37:9`!; |
| 159 | t = &pn_threads[RTC_THREADcxx . . $ RTC_THREAD.]; |
| 160 | } else if (config_is_traffic_class_activecxx . . $ config_is_traffic_class_active(c5f7da30cbc0f4e9).("GenericL2")) { |
| 161 | return 0; |
| 162 | } else { |
| 163 | return -EINVALcxx . . $ `<external>/8987895939074593727/errno-base.h:26:9`!; |
| 164 | } |
| 165 | |
| 166 | /* Find last PN thread. */ |
| 167 | while (t) { |
| 168 | p = t; |
| 169 | t = t->nextcxx . . $ thread_context#next.; |
| 170 | } |
| 171 | p->is_lastcxx . . $ thread_context#is_last. = truecxx . . $ `<external>/14268322491693841775/stdbool.h:37:9`!; |
| 172 | |
| 173 | return 0; |
| 174 | } |