Debian Codegraph @

src/thread.c

raw symbols SCIP
1 // SPDX-License-Identifier: BSD-2-Clause
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"
15 #include "thread.h"
16 #include "utils.h"
17
18 int create_rt_thread(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_param param;
22 pthread_attr_t attr;
23 cpu_set_t cpus;
24 int ret;
25
26 ret = pthread_attr_init(&attr);
27 if (ret) {
28 pthread_error(ret, "pthread_attr_init() failed");
29 goto err;
30 }
31
32 /* 2 MiB stack should be enough for all threads. */
33 ret = pthread_attr_setstacksize(&attr, 2 * 1024 * 1024);
34 if (ret) {
35 pthread_error(ret, "pthread_attr_setstacksize() failed");
36 goto err;
37 }
38
39 ret = pthread_attr_setschedpolicy(&attr, SCHED_FIFO);
40 if (ret) {
41 pthread_error(ret, "pthread_attr_setschedpolicy() failed");
42 goto err;
43 }
44
45 param.sched_priority = thread_priority;
46 ret = pthread_attr_setschedparam(&attr, &param);
47 if (ret) {
48 pthread_error(ret, "pthread_attr_setschedparam() failed");
49 goto err;
50 }
51
52 ret = pthread_attr_setinheritsched(&attr, PTHREAD_EXPLICIT_SCHED);
53 if (ret) {
54 pthread_error(ret, "pthread_attr_setinheritsched() failed");
55 goto err;
56 }
57
58 CPU_ZERO(&cpus);
59 CPU_SET(cpu_core, &cpus);
60 ret = pthread_attr_setaffinity_np(&attr, sizeof(cpus), &cpus);
61 if (ret) {
62 pthread_error(ret, "pthread_attr_setaffinity_np() failed");
63 goto err;
64 }
65
66 ret = pthread_create(task_id, &attr, thread_routine, data);
67 if (ret) {
68 pthread_error(ret, "pthread_create() failed");
69 goto err;
70 }
71
72 pthread_setname_np(*task_id, thread_name);
73
74 return 0;
75
76 err:
77 return -ret;
78 }
79
80 void init_mutex(pthread_mutex_t *mutex)
81 {
82 pthread_mutexattr_t mattr;
83
84 /* Set priority inheritance protocol on this mutex. It's disabled by default. */
85 pthread_mutexattr_init(&mattr);
86 pthread_mutexattr_setprotocol(&mattr, PTHREAD_PRIO_INHERIT);
87 pthread_mutex_init(mutex, &mattr);
88 }
89
90 void init_condition_variable(pthread_cond_t *cond_var)
91 {
92 pthread_condattr_t cattr;
93
94 pthread_condattr_init(&cattr);
95 pthread_condattr_setclock(&cattr, CLOCK_MONOTONIC);
96 pthread_cond_init(cond_var, &cattr);
97 }
98
99 static struct thread_context *find_next_pn_thread(struct thread_context *pn_threads, int start)
100 {
101 switch (start) {
102 case TSN_HIGH_THREAD:
103 if (config_is_traffic_class_active("TsnLow"))
104 return &pn_threads[TSN_LOW_THREAD];
105 case TSN_LOW_THREAD:
106 if (config_is_traffic_class_active("Rtc"))
107 return &pn_threads[RTC_THREAD];
108 case RTC_THREAD:
109 if (config_is_traffic_class_active("Rta"))
110 return &pn_threads[RTA_THREAD];
111 case RTA_THREAD:
112 if (config_is_traffic_class_active("Dcp"))
113 return &pn_threads[DCP_THREAD];
114 case DCP_THREAD:
115 if (config_is_traffic_class_active("Lldp"))
116 return &pn_threads[LLDP_THREAD];
117 case LLDP_THREAD:
118 if (config_is_traffic_class_active("UdpHigh"))
119 return &pn_threads[UDP_HIGH_THREAD];
120 case UDP_HIGH_THREAD:
121 if (config_is_traffic_class_active("UdpLow"))
122 return &pn_threads[UDP_LOW_THREAD];
123 case UDP_LOW_THREAD:
124 return NULL;
125 }
126
127 return NULL;
128 }
129
130 int link_pn_threads(struct thread_context *pn_threads)
131 {
132 struct 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_TYPES; i++) {
142 struct thread_context *current = &pn_threads[i];
143
144 current->next = find_next_pn_thread(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_active("TsnHigh")) {
155 pn_threads[TSN_HIGH_THREAD].is_first = true;
156 t = &pn_threads[TSN_HIGH_THREAD];
157 } else if (config_is_traffic_class_active("Rtc")) {
158 pn_threads[RTC_THREAD].is_first = true;
159 t = &pn_threads[RTC_THREAD];
160 } else if (config_is_traffic_class_active("GenericL2")) {
161 return 0;
162 } else {
163 return -EINVAL;
164 }
165
166 /* Find last PN thread. */
167 while (t) {
168 p = t;
169 t = t->next;
170 }
171 p->is_last = true;
172
173 return 0;
174 }