My Project
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Macros
Macros | Functions | Variables
worker_thread.c File Reference

Threads that hold MSUs. More...

#include "worker_thread.h"
#include "msu_type.h"
#include "local_msu.h"
#include "logging.h"
#include "thread_message.h"
#include "msu_message.h"
#include "controller_communication.h"
#include <stdlib.h>

Go to the source code of this file.

Macros

#define MAX_DEDOS_THREAD_ID   32
 The maximum ID that can be assigned to a worker thread. More...
 
#define CHECK_MSG_SIZE(msg, target)
 Checks whether the size of the message is equal to the size of the target struct. More...
 
#define DEFAULT_WAIT_TIMEOUT_S   1
 Default amount of time to wait before sem_trywait should return. More...
 

Functions

static void * init_worker_thread (struct dedos_thread *thread)
 Allocates and returns a new worker thread structure. More...
 
void stop_all_worker_threads ()
 Signals all worker threads to stop. More...
 
static void destroy_worker_thread (struct dedos_thread *thread, void *v_worker_thread)
 Destroys all MSUs on a worker thread and frees the associated structure. More...
 
struct worker_threadget_worker_thread (int id)
 
static int get_msu_index (struct worker_thread *thread, int msu_id)
 Gets the index in worker_thread::msus at which the msu_id resides. More...
 
static int remove_idx_from_msu_list (struct worker_thread *thread, int idx)
 Removes the MSU at the given index from the worker_thread::msus. More...
 
int unregister_msu_with_thread (struct local_msu *msu)
 Removes an MSU from the list of MSUs within its thread. More...
 
int register_msu_with_thread (struct local_msu *msu)
 Registers an MSU as one that should be run on its assigned thread. More...
 
static int create_msu_on_thread (struct worker_thread *thread, struct ctrl_create_msu_msg *msg)
 Creates a new MSU on this thread based on the provided message. More...
 
static int del_msu_from_thread (struct worker_thread *thread, struct ctrl_delete_msu_msg *msg, int ack_id)
 Removes an MSU from this thread based on the provided messages. More...
 
static int worker_mod_msu_route (struct worker_thread *thread, struct ctrl_msu_route_msg *msg)
 Modifies the MSU's routes, either adding or removing a route subscription. More...
 
static int process_worker_thread_msg (struct worker_thread *thread, struct thread_msg *msg)
 Processes a message which has been sent to the worker thread. More...
 
static double timediff_s (struct timespec *t1, struct timespec *t2)
 Returns the difference in time in seconds, t2 - t1. More...
 
static struct timespec * next_timeout (struct worker_thread *thread)
 Returns the next time at which the worker thread should exit its semaphore wait. More...
 
int enqueue_worker_timeout (struct worker_thread *thread, struct timespec *interval)
 Signals that the given thread should break when waiting on its semaphore once interval time has passed. More...
 
static int worker_thread_loop (struct dedos_thread *thread, void *v_worker_thread)
 The main worker thread loop. More...
 
int create_worker_thread (unsigned int thread_id, enum blocking_mode mode)
 Starts a new worker thread with the given thread ID and pinned/unpinned status. More...
 

Variables

static struct worker_threadworker_threads [32]
 Static struct to keep track of worker threads. More...
 
static struct timespec cur_time
 Static structure for holding current time, so it can be returned from next_timeout. More...
 

Detailed Description

Threads that hold MSUs.

Definition in file worker_thread.c.

Macro Definition Documentation

#define CHECK_MSG_SIZE (   msg,
  target 
)
Value:
if (msg->data_size != sizeof(target)) { \
log_warn("Message data size does not match size" \
"of target type " #target ); \
break; \
}
#define log_warn(fmt,...)
Definition: logging.h:113

Checks whether the size of the message is equal to the size of the target struct.

Definition at line 214 of file worker_thread.c.

#define DEFAULT_WAIT_TIMEOUT_S   1

Default amount of time to wait before sem_trywait should return.

Definition at line 267 of file worker_thread.c.

#define MAX_DEDOS_THREAD_ID   32

The maximum ID that can be assigned to a worker thread.

Definition at line 36 of file worker_thread.c.

Function Documentation

static int create_msu_on_thread ( struct worker_thread thread,
struct ctrl_create_msu_msg msg 
)
static

Creates a new MSU on this thread based on the provided message.

Definition at line 131 of file worker_thread.c.

int create_worker_thread ( unsigned int  thread_id,
enum blocking_mode  mode 
)

Starts a new worker thread with the given thread ID and pinned/unpinned status.

Blocks until thread has been started

Definition at line 382 of file worker_thread.c.

static int del_msu_from_thread ( struct worker_thread thread,
struct ctrl_delete_msu_msg msg,
int  ack_id 
)
static

Removes an MSU from this thread based on the provided messages.

Definition at line 147 of file worker_thread.c.

static void destroy_worker_thread ( struct dedos_thread thread,
void *  v_worker_thread 
)
static

Destroys all MSUs on a worker thread and frees the associated structure.

Definition at line 70 of file worker_thread.c.

int enqueue_worker_timeout ( struct worker_thread thread,
struct timespec *  interval 
)

Signals that the given thread should break when waiting on its semaphore once interval time has passed.

Returns
0 on success, -1 on error

Definition at line 301 of file worker_thread.c.

static int get_msu_index ( struct worker_thread thread,
int  msu_id 
)
static

Gets the index in worker_thread::msus at which the msu_id resides.

Definition at line 88 of file worker_thread.c.

struct worker_thread* get_worker_thread ( int  id)
Returns
the worker thread with the given ID, or NULL if not found

Definition at line 79 of file worker_thread.c.

static void* init_worker_thread ( struct dedos_thread thread)
static

Allocates and returns a new worker thread structure.

Definition at line 42 of file worker_thread.c.

static struct timespec* next_timeout ( struct worker_thread thread)
static

Returns the next time at which the worker thread should exit its semaphore wait.

Definition at line 278 of file worker_thread.c.

static int process_worker_thread_msg ( struct worker_thread thread,
struct thread_msg msg 
)
static

Processes a message which has been sent to the worker thread.

Definition at line 222 of file worker_thread.c.

int register_msu_with_thread ( struct local_msu msu)

Registers an MSU as one that should be run on its assigned thread.

Returns
0 on success, -1 on error

Definition at line 119 of file worker_thread.c.

static int remove_idx_from_msu_list ( struct worker_thread thread,
int  idx 
)
static

Removes the MSU at the given index from the worker_thread::msus.

Definition at line 98 of file worker_thread.c.

void stop_all_worker_threads ( )

Signals all worker threads to stop.

Definition at line 54 of file worker_thread.c.

static double timediff_s ( struct timespec *  t1,
struct timespec *  t2 
)
static

Returns the difference in time in seconds, t2 - t1.

Definition at line 270 of file worker_thread.c.

int unregister_msu_with_thread ( struct local_msu msu)

Removes an MSU from the list of MSUs within its thread.

Returns
0 on success, -1 on error

Definition at line 110 of file worker_thread.c.

static int worker_mod_msu_route ( struct worker_thread thread,
struct ctrl_msu_route_msg msg 
)
static

Modifies the MSU's routes, either adding or removing a route subscription.

Definition at line 180 of file worker_thread.c.

static int worker_thread_loop ( struct dedos_thread thread,
void *  v_worker_thread 
)
static

The main worker thread loop.

Checks for exit signal, processes messages

Definition at line 345 of file worker_thread.c.

Variable Documentation

struct timespec cur_time
static

Static structure for holding current time, so it can be returned from next_timeout.

Definition at line 275 of file worker_thread.c.

struct worker_thread* worker_threads[32]
static

Static struct to keep track of worker threads.

Definition at line 39 of file worker_thread.c.