![]() |
The ROme OpTimistic Simulator
2.0.0
A General-Purpose Multithreaded Parallel/Distributed Simulation Platform
|
Generic thread management facilities. More...
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 |
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
Definition in file thread.c.
|
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.
arg | A pointer to an internally defined structure keeping the real thread's entry point and its arguments |
Definition at line 103 of file thread.c.
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.
b | the thread barrier to initialize |
t | the number of threads which will synchronize on the barrier |
Definition at line 180 of file thread.c.
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.
n | The number of threads which should be created |
start_routine | The new threads' entry point |
arg | A pointer to an array of arguments to be passed to the new threads' entry point |
Definition at line 155 of file thread.c.
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.
b | A pointer to the thread barrier to synchronize on |
Definition at line 200 of file thread.c.
__thread unsigned int local_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.
|
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.