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)
27
28#include "deMemory.h"
29
30#if !defined(_XOPEN_SOURCE) || (_XOPEN_SOURCE < 500)
31#	error You are using too old posix API!
32#endif
33
34#include <unistd.h>
35#include <pthread.h>
36#include <sched.h>
37
38typedef struct Thread_s
39{
40	pthread_t		thread;
41	deThreadFunc	func;
42	void*			arg;
43} Thread;
44
45DE_STATIC_ASSERT(sizeof(deThread) >= sizeof(Thread*));
46
47static void* startThread (void* entryPtr)
48{
49	Thread*			thread	= (Thread*)entryPtr;
50	deThreadFunc	func	= thread->func;
51	void*			arg		= thread->arg;
52
53	/* Start actual thread. */
54	func(arg);
55
56	return DE_NULL;
57}
58
59deThread deThread_create (deThreadFunc func, void* arg, const deThreadAttributes* attributes)
60{
61	pthread_attr_t	attr;
62	Thread*			thread	= (Thread*)deCalloc(sizeof(Thread));
63
64	if (!thread)
65		return 0;
66
67	thread->func	= func;
68	thread->arg		= arg;
69
70	if (pthread_attr_init(&attr) != 0)
71	{
72		deFree(thread);
73		return 0;
74	}
75
76	/* \todo [2009-11-12 pyry] Map attributes. */
77	DE_UNREF(attributes);
78
79	if (pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE) != 0)
80	{
81		pthread_attr_destroy(&attr);
82		deFree(thread);
83		return 0;
84	}
85
86	if (pthread_create(&thread->thread, &attr, startThread, thread) != 0)
87	{
88		pthread_attr_destroy(&attr);
89		deFree(thread);
90		return 0;
91	}
92	DE_ASSERT(thread->thread);
93
94	pthread_attr_destroy(&attr);
95
96	return (deThread)thread;
97}
98
99deBool deThread_join (deThread threadptr)
100{
101	Thread*		thread	= (Thread*)threadptr;
102	int			ret;
103
104	DE_ASSERT(thread->thread);
105	ret = pthread_join(thread->thread, DE_NULL);
106
107	/* If join fails for some reason, at least mark as detached. */
108	if (ret != 0)
109		pthread_detach(thread->thread);
110
111	/* Thread is no longer valid as far as we are concerned. */
112	thread->thread = 0;
113
114	return (ret == 0);
115}
116
117void deThread_destroy (deThread threadptr)
118{
119	Thread* thread = (Thread*)threadptr;
120
121	if (thread->thread)
122	{
123		/* Not joined, detach. */
124		int ret = pthread_detach(thread->thread);
125		DE_ASSERT(ret == 0);
126		DE_UNREF(ret);
127	}
128
129	deFree(thread);
130}
131
132void deSleep (deUint32 milliseconds)
133{
134	/* Maximum value for usleep is 10^6. */
135	deUint32 seconds = milliseconds / 1000;
136
137	milliseconds = milliseconds - seconds * 1000;
138
139	if (seconds > 0)
140		sleep(seconds);
141
142	usleep((useconds_t)milliseconds * (useconds_t)1000);
143}
144
145void deYield (void)
146{
147	sched_yield();
148}
149
150#endif /* DE_OS */
151