33 #include <hwsimulation.h>
59 thread->thread = pthread_self();
64 struct sched_param param = {
68 pthread_setschedparam(thread->thread, SCHED_RR, ¶m);
72 pthread_setname_np(thread->thread, thread->name);
83 int pri_val = 30 + prio * 5;
84 pthread_setschedprio(pthread_self(), pri_val);
91 struct pios_thread *thread = malloc(
sizeof(*thread));
95 if (pthread_attr_init(&attr)) {
96 perror(
"pthread_attr_init");
101 struct sched_param param = {
102 .sched_priority = 30 + prio * 5
105 pthread_attr_setinheritsched(&attr, PTHREAD_EXPLICIT_SCHED);
106 pthread_attr_setschedpolicy(&attr, SCHED_RR);
107 pthread_attr_setschedparam(&attr, ¶m);
110 thread->name = namep;
112 void *(*thr_func)(
void *) = (
void *) fp;
114 int ret = pthread_create(&thread->thread, &attr, thr_func, argp);
117 printf(
"Couldn't start thr (%s) ret=%d\n", namep, ret);
124 pthread_setname_np(thread->thread, thread->name);
127 printf(
"Started thread (%s) p=%p\n", namep, &thread->thread);
134 if (threadp != NULL) {
153 struct timespec monotime;
157 return monotime.tv_sec * 1000 + monotime.tv_nsec / 1000000;
160 #ifdef PIOS_INCLUDE_FAKETICK
161 static volatile uint32_t fake_tick_barrier;
163 void PIOS_Thread_FakeClock_UpdateBarrier(uint32_t increment)
165 pthread_mutex_lock(&fake_clock_mutex);
167 fake_tick_barrier = fake_clock + increment;
168 pthread_cond_broadcast(&fake_clock_cond);
170 pthread_mutex_unlock(&fake_clock_mutex);
173 void PIOS_Thread_FakeClock_Tick(
void)
175 bool blocked =
false;
177 pthread_mutex_lock(&fake_clock_mutex);
179 while ((fake_tick_barrier) && (fake_tick_barrier == fake_clock)) {
182 HwSimulationFakeTickBlockedSet(&val);
187 pthread_cond_wait(&fake_clock_cond, &fake_clock_mutex);
192 HwSimulationFakeTickBlockedSet(&val);
195 if (fake_clock == 0) {
201 pthread_cond_broadcast(&fake_clock_cond);
203 pthread_mutex_unlock(&fake_clock_mutex);
209 return fake_clock != 0;
220 static uint32_t
base = 0;
238 pthread_mutex_lock(&fake_clock_mutex);
240 uint32_t expiration = fake_clock + time_ms;
242 while ((expiration - fake_clock) <= time_ms) {
243 pthread_cond_wait(&fake_clock_cond,
247 pthread_mutex_unlock(&fake_clock_mutex);
252 usleep(1000 * time_ms);
257 *previous_ms += increment_ms;
261 uint32_t ms = *previous_ms - now;
263 if (ms > increment_ms) {
282 const uint32_t increment_ms)
286 uint32_t interval = now - prev_systime;
288 return increment_ms <= interval;
uint32_t PIOS_Thread_Systime(void)
int clock_gettime(clockid_t clk_id, struct timespec *t)
Main PiOS header to include all the compiled in PiOS options.
static uint32_t PIOS_Thread_GetClock_Impl()
#define PIOS_THREAD_TIMEOUT_MAX
void * PIOS_malloc_no_dma(size_t size)
static pthread_cond_t fake_clock_cond
void PIOS_Thread_ChangePriority(enum pios_thread_prio_e prio)
struct pios_thread * PIOS_Thread_Create(void(*fp)(void *), const char *namep, size_t stack_bytes, void *argp, enum pios_thread_prio_e prio)
bool PIOS_Thread_Period_Elapsed(const uint32_t prev_systime, const uint32_t increment_ms)
Determine if a period has elapsed since a datum.
void PIOS_Thread_Sleep_Until(uint32_t *previous_ms, uint32_t increment_ms)
static volatile uint32_t fake_clock
void PIOS_Thread_Sleep(uint32_t time_ms)
uint32_t PIOS_Thread_Get_Stack_Usage(struct pios_thread *threadp)
int printf(const char *format,...)
uint32_t PIOS_Thread_Get_Runtime(struct pios_thread *threadp)
bool __attribute__((weak))
void PIOS_Thread_Delete(struct pios_thread *threadp)
struct pios_thread * PIOS_Thread_WrapCurrentThread(const char *namep)
Creates a handle for the current thread.
bool PIOS_Thread_FakeClock_IsActive(void)
static pthread_mutex_t fake_clock_mutex