The ROme OpTimistic Simulator  2.0.0
A General-Purpose Multithreaded Parallel/Distributed Simulation Platform
thread.h
Go to the documentation of this file.
1 
36 #pragma once
37 
38 #include <stdbool.h>
39 #include <arch/atomic.h>
40 
41 #if defined(OS_LINUX)
42 
43 #include <sched.h>
44 #include <unistd.h>
45 #include <pthread.h>
46 
48 #define get_cores() (sysconf( _SC_NPROCESSORS_ONLN ))
49 
51 typedef pthread_t tid_t;
52 
54 #define new_thread(entry, arg) pthread_create(&os_tid, NULL, entry, arg)
55 
61 static inline void set_affinity(int core)
62 {
63  cpu_set_t cpuset;
64  CPU_ZERO(&cpuset);
65  CPU_SET(core, &cpuset);
66  // 0 is the current thread
67  sched_setaffinity(0, sizeof(cpuset), &cpuset);
68 }
69 
70 #elif defined(OS_WINDOWS)
71 
72 #include <windows.h>
73 
75 #define get_cores() ({\
76  SYSTEM_INFO _sysinfo;\
77  GetSystemInfo( &_sysinfo );\
78  _sysinfo.dwNumberOfProcessors;\
79  })
80 
82 typedef HANDLE tid_t;
83 
85 #define new_thread(entry, arg) CreateThread(NULL, 0, entry, arg, 0, &os_tid)
86 
88 #define set_affinity(core) SetThreadAffinityMask(GetCurrentThread(), 1<<core)
89 
90 #else /* OS_LINUX || OS_WINDOWS */
91 #error Unsupported operating system
92 #endif
93 
94 
97  void *(*start_routine)(void *);
98  void *arg;
99 };
100 
102 typedef struct {
109 } barrier_t;
110 
117 #define HALF_UINT_BITS (sizeof(unsigned int) * 8 / 2)
118 
119 
129 #define MAX_KERNELS ((1 << HALF_UINT_BITS) - 1)
130 
131 
137 #define MAX_THREADS_PER_KERNEL ((1 << HALF_UINT_BITS) - 1)
138 
139 
152 #define to_global_tid(kid, local_tid) ( (kid << HALF_UINT_BITS) | local_tid )
153 
155 #define master_thread() (local_tid == 0)
156 
167 #define thread_barrier_reset(b) do { \
168  (atomic_set((&b->c1), (b)->num_threads)); \
169  (atomic_set((&b->c2), (b)->num_threads)); \
170  (atomic_set((&b->barr), -1)); \
171  } while (0)
172 
173 
174 extern __thread unsigned int tid;
175 extern __thread unsigned int local_tid;
176 
177 extern void barrier_init(barrier_t * b, int t);
178 extern bool thread_barrier(barrier_t * b);
179 extern void create_threads(unsigned short int n, void *(*start_routine)(void *), void *arg);
180 
atomic_t c2
Second synchronization counter.
Definition: thread.h:105
pthread_t tid_t
How do we identify a thread?
Definition: thread.h:51
bool thread_barrier(barrier_t *b)
Definition: thread.c:200
void barrier_init(barrier_t *b, int t)
Definition: thread.c:180
void * arg
Arguments to be passed to start_routine.
Definition: thread.h:98
This structure is used to call the thread creation helper function.
Definition: thread.h:96
void *(* start_routine)(void *)
A pointer to the entry point of the next-to-be thread.
Definition: thread.h:97
atomic_t c1
First synchronization counter.
Definition: thread.h:104
int num_threads
Number of threads which will synchronize on the barrier.
Definition: thread.h:103
void create_threads(unsigned short int n, void *(*start_routine)(void *), void *arg)
Definition: thread.c:155
Atomic operations.
atomic_t barr
Definition: thread.h:106
__thread unsigned int tid
Definition: thread.c:60
__thread unsigned int local_tid
Definition: thread.c:72
static void set_affinity(int core)
Definition: thread.h:61
Thread barrier definition.
Definition: thread.h:102