The ROme OpTimistic Simulator  3.0.0
A General-Purpose Multithreaded Parallel/Distributed Simulation Platform
thread.c
Go to the documentation of this file.
1 
17 #include <arch/thread.h>
18 
53 #ifdef __POSIX
54 #include <sched.h>
55 #include <signal.h>
56 #include <unistd.h>
57 
58 #ifdef __MACOS
59 #include <mach/thread_act.h>
60 
61 int thread_affinity_set(thr_id_t thr, unsigned core)
62 {
63  thread_affinity_policy_data_t policy = {core};
64  thread_port_t mach_thread = pthread_mach_thread_np(thr);
65  kern_return_t ret = thread_policy_set(mach_thread, THREAD_AFFINITY_POLICY,
66  (thread_policy_t) &policy, 1);
67  return -(ret != KERN_SUCCESS);
68 }
69 
70 #else
71 
72 int thread_affinity_set(thr_id_t thr, unsigned core)
73 {
74  cpu_set_t cpuset;
75  CPU_ZERO(&cpuset);
76  CPU_SET(core, &cpuset);
77  return -(pthread_setaffinity_np(thr, sizeof(cpuset), &cpuset) != 0);
78 }
79 
80 #endif
81 
82 unsigned thread_cores_count(void)
83 {
84  long ret = sysconf(_SC_NPROCESSORS_ONLN);
85  return ret < 1 ? 1 : (unsigned)ret;
86 }
87 
88 int thread_start(thr_id_t *thr_p, thr_run_fnc t_fnc, void *t_fnc_arg)
89 {
90  return -(pthread_create(thr_p, NULL, t_fnc, t_fnc_arg) != 0);
91 }
92 
93 int thread_wait(thr_id_t thr, thr_ret_t *ret)
94 {
95  return -(pthread_join(thr, ret) != 0);
96 }
97 
98 #endif
99 
100 #ifdef __WINDOWS
101 #define WIN32_LEAN_AND_MEAN
102 #ifndef _WIN32_WINNT
103 #define _WIN32_WINNT _WIN32_WINNT_NT4
104 #endif
105 #include <windows.h>
106 
107 unsigned thread_cores_count(void)
108 {
109  SYSTEM_INFO sys_info;
110  GetSystemInfo(&sys_info);
111  return sys_info.dwNumberOfProcessors;
112 }
113 
114 int thread_start(thr_id_t *thr_p, thr_run_fnc t_fnc, void *t_fnc_arg)
115 {
116  *thr_p = CreateThread(NULL, 0, t_fnc, arg, 0, NULL);
117  return -(*thr_p == NULL);
118 }
119 
120 int thread_affinity_set(thr_id_t thr, unsigned core)
121 {
122  return -(SetThreadAffinityMask(thr, 1 << core) != 0);
123 }
124 
125 int thread_wait(thr_id_t thr, thr_ret_t *ret)
126 {
127  if (WaitForSingleObject(thr, INFINITE) == WAIT_FAILED)
128  return -1;
129 
130  if (ret)
131  return -(GetExitCodeThread(thr, ret) == 0);
132 
133  return 0;
134 }
135 
136 #endif
thr_run_fnc
thr_ret_t THREAD_CALL_CONV(* thr_run_fnc)(void *)
The function type of a new thread entry point.
Definition: thread.h:48
thread_wait
int thread_wait(thr_id_t thr, thr_ret_t *ret)
Wait for specified thread to complete execution.
thread_affinity_set
int thread_affinity_set(thr_id_t thr, unsigned core)
Sets a core affinity for a thread.
thread_cores_count
unsigned thread_cores_count(void)
Computes the count of available cores on the machine.
Definition: init_test.c:23
thread_start
int thread_start(thr_id_t *thr_p, thr_run_fnc t_fnc, void *t_fnc_arg)
Creates a thread.
thread.h
Generic architecture management facilities.