]> pd.if.org Git - liblfds/blob - liblfds/liblfds6.1.1/test/src/abstraction_thread_start.c
Initial import (all versions, including the new 7.1.0)
[liblfds] / liblfds / liblfds6.1.1 / test / src / abstraction_thread_start.c
1 #include "internal.h"\r
2 \r
3 \r
4 \r
5 \r
6 \r
7 /****************************************************************************/\r
8 #if (defined _WIN32 && defined _MSC_VER && !defined WIN_KERNEL_BUILD)\r
9 \r
10   /* TRD : any Windows (user-mode) on any CPU with the Microsoft C compiler\r
11 \r
12            _WIN32             indicates 64-bit or 32-bit Windows\r
13            _MSC_VER           indicates Microsoft C compiler\r
14            !WIN_KERNEL_BUILD  indicates Windows user-mode\r
15   */\r
16 \r
17   int abstraction_thread_start( thread_state_t *thread_state, unsigned int cpu, thread_function_t thread_function, void *thread_user_state )\r
18   {\r
19     int\r
20       rv = 0;\r
21 \r
22     DWORD\r
23       thread_id;\r
24 \r
25     DWORD_PTR\r
26       affinity_mask,\r
27       result;\r
28 \r
29     assert( thread_state != NULL );\r
30     // TRD : cpu can be any value in its range\r
31     assert( thread_function != NULL );\r
32     // TRD : thread_user_state can be NULL\r
33 \r
34     affinity_mask = (DWORD_PTR) (1 << cpu);\r
35 \r
36     *thread_state = CreateThread( NULL, 0, thread_function, thread_user_state, NO_FLAGS, &thread_id );\r
37 \r
38     result = SetThreadAffinityMask( *thread_state, affinity_mask );\r
39 \r
40     if( *thread_state != NULL and result != 0 )\r
41       rv = 1;\r
42 \r
43     return( rv );\r
44   }\r
45 \r
46 #endif\r
47 \r
48 \r
49 \r
50 \r
51 \r
52 /****************************************************************************/\r
53 #if (defined _WIN32 && defined _MSC_VER && defined WIN_KERNEL_BUILD)\r
54 \r
55   /* TRD : any Windows on any CPU with the Microsoft C compiler\r
56 \r
57            _WIN32            indicates 64-bit or 32-bit Windows\r
58            _MSC_VER          indicates Microsoft C compiler\r
59            WIN_KERNEL_BUILD  indicates Windows kernel\r
60   */\r
61 \r
62   int abstraction_thread_start( thread_state_t *thread_state, unsigned int cpu, thread_function_t thread_function, void *thread_user_state )\r
63   {\r
64     int\r
65       rv = 0;\r
66 \r
67     KAFFINITY\r
68       affinity_mask\r
69 \r
70     NTSTATUS\r
71       nts_create,\r
72       nts_affinity;\r
73 \r
74     assert( thread_state != NULL );\r
75     // TRD : cpu can be any value in its range\r
76     assert( thread_function != NULL );\r
77     // TRD : thread_user_state can be NULL\r
78 \r
79     affinity_mask = 1 << cpu;\r
80 \r
81     nts_create = PsCreateSystemThread( thread_state, THREAD_ALL_ACCESS, NULL, NULL, NULL, thread_function, thread_user_state );\r
82 \r
83     nts_affinity = ZwSetInformationThread( thread_state, ThreadAffinityMask, &affinity_mask, sizeof(KAFFINITY) );\r
84 \r
85     if( nts_create == STATUS_SUCCESS and nts_affinity == STATUS_SUCCESS )\r
86       rv = 1;\r
87 \r
88     return( rv );\r
89   }\r
90 \r
91 #endif\r
92 \r
93 \r
94 \r
95 \r
96 \r
97 /****************************************************************************/\r
98 #if (defined __unix__)\r
99 \r
100   /* TRD : any UNIX on any CPU with any compiler\r
101 \r
102            I assumed pthreads is available on any UNIX.\r
103 \r
104            __unix__   indicates Solaris, Linux, HPUX, etc\r
105   */\r
106 \r
107   int abstraction_thread_start( thread_state_t *thread_state, unsigned int cpu, thread_function_t thread_function, void *thread_user_state )\r
108   {\r
109     int\r
110       rv = 0,\r
111       rv_create;\r
112 \r
113     pthread_attr_t\r
114       attr;\r
115 \r
116     cpu_set_t\r
117       cpuset;\r
118 \r
119     assert( thread_state != NULL );\r
120     // TRD : cpu can be any value in its range\r
121     assert( thread_function != NULL );\r
122     // TRD : thread_user_state can be NULL\r
123 \r
124     pthread_attr_init( &attr );\r
125 \r
126     CPU_ZERO( &cpuset );\r
127     CPU_SET( cpu, &cpuset );\r
128     pthread_attr_setaffinity_np( &attr, sizeof(cpuset), &cpuset );\r
129 \r
130     rv_create = pthread_create( thread_state, &attr, thread_function, thread_user_state );\r
131 \r
132     if( rv_create == 0 )\r
133       rv = 1;\r
134 \r
135     pthread_attr_destroy( &attr );\r
136 \r
137     return( rv );\r
138   }\r
139 \r
140 #endif\r
141 \r