The ROme OpTimistic Simulator  2.0.0
A General-Purpose Multithreaded Parallel/Distributed Simulation Platform
thread.c File Reference

Generic thread management facilities. More...

#include <stdbool.h>
#include <arch/thread.h>
#include <core/init.h>
#include <mm/mm.h>
+ Include dependency graph for thread.c:

Go to the source code of this file.

Functions

static void * __helper_create_thread (void *arg)
 
void create_threads (unsigned short int n, void *(*start_routine)(void *), void *arg)
 
void barrier_init (barrier_t *b, int t)
 
bool thread_barrier (barrier_t *b)
 

Variables

static tid_t os_tid
 
__thread unsigned int tid
 
__thread unsigned int local_tid
 
static unsigned int thread_counter = 0
 

Detailed Description

Generic thread management facilities.

This module provides generic facilities for thread management. In particular, helper functions to startup worker threads are exposed, and a function to synchronize multiple threads on a software barrier.

The software barrier also offers a leader election facility, so that once all threads are synchronized on the barrier, the function returns true to only one of them.

This file is part of ROOT-Sim (ROme OpTimistic Simulator).

ROOT-Sim is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; only version 3 of the License applies.

ROOT-Sim is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with ROOT-Sim; if not, write to the Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA

Author
Alessandro Pellegrini
Date
Jan 25, 2012

Definition in file thread.c.

Function Documentation

static void* __helper_create_thread ( void *  arg)
static

This helper function is the actual entry point for every thread created using the provided internal services. The goal of this function is to start a race on the thread_counter shared variable using a CAS. This race across all the threads is used to determine unique identifiers across all the worker threads. This is done in this helper function to silently set the new thread's tid before any simulation-specific function is ever called, so that any place in the simulator will find that already set. Additionally, when the created thread returns, it frees the memory used to maintain the real entry point and the pointer to its arguments.

Parameters
argA pointer to an internally defined structure keeping the real thread's entry point and its arguments
Returns
This function always returns NULL

Definition at line 103 of file thread.c.

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

void barrier_init ( barrier_t b,
int  t 
)

This function initializes a thread barrier. If more than the hereby specified number of threads try to synchronize on the barrier, the behaviour is undefined.

Parameters
bthe thread barrier to initialize
tthe number of threads which will synchronize on the barrier

Definition at line 180 of file thread.c.

+ Here is the caller graph for this function:

void create_threads ( unsigned short int  n,
void *(*)(void *)  start_routine,
void *  arg 
)

This function creates n threads, all having the same entry point and the same arguments. It creates a new thread starting from the __helper_create_thread function which silently sets the new thread's tid. Note that the arguments passed to __helper_create_thread are malloc'd here, and free'd there. This means that if start_routine does not return, there is a memory leak. Additionally, note that we don't make a copy of the arguments pointed by arg, so all the created threads will share them in memory. Changing passed arguments from one of the newly created threads will result in all the threads seeing the change.

Parameters
nThe number of threads which should be created
start_routineThe new threads' entry point
argA pointer to an array of arguments to be passed to the new threads' entry point

Definition at line 155 of file thread.c.

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

bool thread_barrier ( barrier_t b)

This function synchronizes all the threads. After a thread leaves this function, it is guaranteed that no other thread has (at least) not entered the function, therefore allowing to create a separation between the execution in portions of the code. If more threads than specified in the initialization of the barrier try to synchronize on it, the behaviour is undefined. The function additionally returns 'true' only to one of the calling threads, allowing the execution of portions of code in isolated mode after the barrier itself. This is like a leader election for free.

Parameters
bA pointer to the thread barrier to synchronize on
Returns
false to all threads, except for one which is elected as the leader

Definition at line 200 of file thread.c.

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

Variable Documentation

__thread unsigned int local_tid

The "local" id of the thread. This is a per-instance value which uniquely identifies a thread on a compute node.

Definition at line 72 of file thread.c.

tid_t os_tid
static

An OS-level thread id. We never do any join on worker threads, so there is no need to keep track of system ids. Internally, each thread is given a unique id in [0, n_thr], which is used to know who is who. This variable is used only when spawning threads, because the system wants us to know who is what thread from its point of view, although we don't care at all.

Definition at line 49 of file thread.c.

unsigned int thread_counter = 0
static

The thread counter is a global variable which is incremented atomically to assign a unique thread ID. Each spawned thread will compete using a CAS to update this counter. Once a thread succeeds, it can use the value it was keeping as its private local id. This variable is used only as a synchronization point upon initialization, later no one cares about its value.

Definition at line 83 of file thread.c.

__thread unsigned int tid

The id of the thread.

Definition at line 60 of file thread.c.