1 /*-------------------------------------------------------------------------
2 * drawElements Thread Library
3 * ---------------------------
4 *
5 * Copyright 2014 The Android Open Source Project
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 *
19 *//*!
20 * \file
21 * \brief Unix implementation of thread management.
22 *//*--------------------------------------------------------------------*/
23
24 #include "deThread.h"
25
26 #if (DE_OS == DE_OS_UNIX || DE_OS == DE_OS_OSX || DE_OS == DE_OS_ANDROID || DE_OS == DE_OS_SYMBIAN || DE_OS == DE_OS_IOS || DE_OS == DE_OS_QNX)
27
28 #include "deMemory.h"
29 #include "deInt32.h"
30
31 #if !defined(_XOPEN_SOURCE) || (_XOPEN_SOURCE < 500)
32 # error "You are using too old posix API!"
33 #endif
34
35 #include <unistd.h>
36 #include <pthread.h>
37 #include <sched.h>
38 #if (DE_OS == DE_OS_UNIX) || (DE_OS == DE_OS_ANDROID)
39 # include <sys/syscall.h>
40 #endif
41
42 #if (DE_OS == DE_OS_OSX) || (DE_OS == DE_OS_IOS)
43 # if !defined(_SC_NPROCESSORS_CONF)
44 # define _SC_NPROCESSORS_CONF 57
45 # endif
46 # if !defined(_SC_NPROCESSORS_ONLN)
47 # define _SC_NPROCESSORS_ONLN 58
48 # endif
49 #endif
50
51 typedef struct Thread_s
52 {
53 pthread_t thread;
54 deThreadFunc func;
55 void* arg;
56 } Thread;
57
58 DE_STATIC_ASSERT(sizeof(deThread) >= sizeof(Thread*));
59
startThread(void * entryPtr)60 static void* startThread (void* entryPtr)
61 {
62 Thread* thread = (Thread*)entryPtr;
63 deThreadFunc func = thread->func;
64 void* arg = thread->arg;
65
66 /* Start actual thread. */
67 func(arg);
68
69 return DE_NULL;
70 }
71
deThread_create(deThreadFunc func,void * arg,const deThreadAttributes * attributes)72 deThread deThread_create (deThreadFunc func, void* arg, const deThreadAttributes* attributes)
73 {
74 pthread_attr_t attr;
75 Thread* thread = (Thread*)deCalloc(sizeof(Thread));
76
77 if (!thread)
78 return 0;
79
80 thread->func = func;
81 thread->arg = arg;
82
83 if (pthread_attr_init(&attr) != 0)
84 {
85 deFree(thread);
86 return 0;
87 }
88
89 /* \todo [2009-11-12 pyry] Map attributes. */
90 DE_UNREF(attributes);
91
92 if (pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE) != 0)
93 {
94 pthread_attr_destroy(&attr);
95 deFree(thread);
96 return 0;
97 }
98
99 if (pthread_create(&thread->thread, &attr, startThread, thread) != 0)
100 {
101 pthread_attr_destroy(&attr);
102 deFree(thread);
103 return 0;
104 }
105 DE_ASSERT(thread->thread);
106
107 pthread_attr_destroy(&attr);
108
109 return (deThread)thread;
110 }
111
deThread_join(deThread threadptr)112 deBool deThread_join (deThread threadptr)
113 {
114 Thread* thread = (Thread*)threadptr;
115 int ret;
116
117 DE_ASSERT(thread->thread);
118 ret = pthread_join(thread->thread, DE_NULL);
119
120 /* If join fails for some reason, at least mark as detached. */
121 if (ret != 0)
122 pthread_detach(thread->thread);
123
124 /* Thread is no longer valid as far as we are concerned. */
125 thread->thread = 0;
126
127 return (ret == 0);
128 }
129
deThread_destroy(deThread threadptr)130 void deThread_destroy (deThread threadptr)
131 {
132 Thread* thread = (Thread*)threadptr;
133
134 if (thread->thread)
135 {
136 /* Not joined, detach. */
137 int ret = pthread_detach(thread->thread);
138 DE_ASSERT(ret == 0);
139 DE_UNREF(ret);
140 }
141
142 deFree(thread);
143 }
144
deSleep(deUint32 milliseconds)145 void deSleep (deUint32 milliseconds)
146 {
147 /* Maximum value for usleep is 10^6. */
148 deUint32 seconds = milliseconds / 1000;
149
150 milliseconds = milliseconds - seconds * 1000;
151
152 if (seconds > 0)
153 sleep(seconds);
154
155 usleep((useconds_t)milliseconds * (useconds_t)1000);
156 }
157
deYield(void)158 void deYield (void)
159 {
160 sched_yield();
161 }
162
163 #if (DE_OS == DE_OS_UNIX) || (DE_OS == DE_OS_ANDROID)
164
deGetNumAvailableLogicalCores(void)165 deUint32 deGetNumAvailableLogicalCores (void)
166 {
167 #if !defined(__FreeBSD__)
168 unsigned long mask = 0;
169 const unsigned int maskSize = sizeof(mask);
170 long ret;
171
172 deMemset(&mask, 0, sizeof(mask));
173
174 ret = syscall(__NR_sched_getaffinity, 0, maskSize, &mask);
175
176 if (ret > 0)
177 {
178 return (deUint32)dePop64(mask);
179 }
180 else
181 {
182 #endif
183 #if defined(_SC_NPROCESSORS_ONLN)
184 const long count = sysconf(_SC_NPROCESSORS_ONLN);
185
186 if (count <= 0)
187 return 1;
188 else
189 return (deUint32)count;
190 #else
191 return 1;
192 #endif
193
194 #if !defined(__FreeBSD__)
195 }
196 #endif
197 }
198
199 #else
200
deGetNumAvailableLogicalCores(void)201 deUint32 deGetNumAvailableLogicalCores (void)
202 {
203 #if defined(_SC_NPROCESSORS_ONLN)
204 const long count = sysconf(_SC_NPROCESSORS_ONLN);
205
206 if (count <= 0)
207 return 1;
208 else
209 return (deUint32)count;
210 #else
211 return 1;
212 #endif
213 }
214
215 #endif
216
deGetNumTotalLogicalCores(void)217 deUint32 deGetNumTotalLogicalCores (void)
218 {
219 #if defined(_SC_NPROCESSORS_CONF)
220 const long count = sysconf(_SC_NPROCESSORS_CONF);
221
222 if (count <= 0)
223 return 1;
224 else
225 return (deUint32)count;
226 #else
227 return 1;
228 #endif
229 }
230
deGetNumTotalPhysicalCores(void)231 deUint32 deGetNumTotalPhysicalCores (void)
232 {
233 /* \todo [2015-04-09 pyry] Parse /proc/cpuinfo perhaps? */
234 return deGetNumTotalLogicalCores();
235 }
236
237 #endif /* DE_OS */
238