Anti-Sway Capstone 1.0
Loading...
Searching...
No Matches
thread-lib.h File Reference

Thread Library. More...

#include <stdbool.h>
#include <pthread.h>
#include "MyRio.h"
#include "AIO.h"
#include "NiFpga.h"
#include "DIIRQ.h"
#include "TimerIRQ.h"
#include "io.h"
#include "setup.h"
Include dependency graph for thread-lib.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Data Structures

struct  ThreadResource
 Parameter for Threading Functions. More...
 

Macros

#define BTI_US   5000u
 The timestep, in microseconds (us)
 
#define BTI_MS   5u
 The timestep, in milliseconds (ms)
 
#define BTI_S   0.005
 The timestep, in seconds (s)
 
#define g   9.81
 Acceleration due to Gravity (m/s^2)
 
#define PI   3.141592653549
 Pi.
 
#define l   0.47
 Length of Rope (m)
 
#define m_dt   2.092
 Mass of the double Trolley (kg)
 
#define m_st   0.664
 Mass of the single Trolley (kg)
 
#define m_p   0.765
 Mass of User (kg)
 
#define START_THREAD(thread, function, resource)
 Starts a thread.
 
#define REGISTER_TIMER(resource)    Irq_RegisterTimerIrq(&timer, &(resource.irq_context), BTI_US)
 Registers the global timer with a thread.
 
#define STOP_THREAD(thread, resource)
 Stops a thread in this process.
 
#define UNREGISTER_TIMER(resource)    Irq_UnregisterTimerIrq(&timer, resource.irq_context)
 Unregisters the global timer with a thread.
 
#define TIMER_TRIGGER(irq_assert, resource)
 
#define EXIT_THREAD()
 

Variables

NiFpga_Session myrio_session
 The MyRio Session.
 

Detailed Description

Thread Library.

Author
Anti-Sway Team: Nguyen, Tri; Espinola, Malachi; Tevy, Vattanary; Hokenstad, Ethan; Neff, Callen
Version
0.1
Date
2024-06-03

Macro Definition Documentation

◆ EXIT_THREAD

#define EXIT_THREAD ( )
Value:
pthread_exit(NULL); \
return NULL

Kills a Thread

Postcondition
The thread associated with the function that calls this is now gone.

◆ REGISTER_TIMER

#define REGISTER_TIMER ( resource)     Irq_RegisterTimerIrq(&timer, &(resource.irq_context), BTI_US)

Registers the global timer with a thread.

Registers the timer (global) with a particular thread (via its resource)

Parameters
resourceThe ThreadResource associated with a thread
Precondition
No other thread is using the global timer
Postcondition
The thread associated with resource is now associated with the global timer

◆ START_THREAD

#define START_THREAD ( thread,
function,
resource )
Value:
VERIFY(error, pthread_create(&thread, NULL, function, &resource))
static int error
Local Error Code.
Definition anti-sway.c:79
ThreadResource resource
Thread Resources (Shared Resources)
Definition idle.c:36
NiFpga_Bool irq_thread_rdy
stop signal
Definition thread-lib.h:38

Starts a thread.

Starts a new thread within this process.

Parameters
threadThe pthread_t ID variable to hold the thread's ID
functionThe Thread Function to execute for the thread
resourceThe ThreadResource to give the function
Precondition
An integer variable named error must be declared in this context
Postcondition
thread will contain the new PID (Process ID) of the thread
A new thread that runs function will now be running concurrently
Returns
EXIT_FAILURE upon failure to initialize the thread

◆ STOP_THREAD

#define STOP_THREAD ( thread,
resource )
Value:
VERIFY(error, pthread_join(thread, NULL))

Stops a thread in this process.

Signals a Thread using a ThreadResource object to stop

Parameters
threadThe pthread_t holding the ID of the thread to stop
resourceThe ThreadResource associated with the thread
Returns
EXIT_FAUILURE upon failure
Precondition
The thread uses resource, and calls EXIT_THREAD() when resource.irq_thread_rdy is set to false
Postcondition
The thread associated with pthread_t is now done

◆ TIMER_TRIGGER

#define TIMER_TRIGGER ( irq_assert,
resource )
Value:
Irq_Wait(resource->irq_context, \
TIMERIRQNO, \
&irq_assert, \
(NiFpga_Bool *) &(resource->irq_thread_rdy)); \
NiFpga_WriteU32(myrio_session, IRQTIMERWRITE, BTI_US); \
NiFpga_WriteBool(myrio_session, IRQTIMERSETTIME, NiFpga_True)
NiFpga_IrqContext irq_context
context
Definition thread-lib.h:37
NiFpga_Session myrio_session
The MyRio Session.
#define BTI_US
The timestep, in microseconds (us)
Definition thread-lib.h:46

Waits for a timer trigger (at the appropriate time step)

Parameters
irq_assertA uint32_t that shall hold the assertion code
resourceA pointer to a ThreadResource for the thread associated with the global timer
Postcondition
irq_assert will be non-zero iff the timer has waited for the standard time step (BTI_S/MS/US)
The timer will trigger after waiting for the standard time step (BTI_S/MS/US)

◆ UNREGISTER_TIMER

#define UNREGISTER_TIMER ( resource)     Irq_UnregisterTimerIrq(&timer, resource.irq_context)

Unregisters the global timer with a thread.

Dissasociates a thread with a timer (via its resource)

Parameters
resourceThe ThreadResource to disassociate the global timer with
Postcondition
The thread associated with resource is now disassociated with timer