1 /*-------------------------------------------------------------------------
2 * drawElements C++ Base Library
3 * -----------------------------
5 * Copyright 2014 The Android Open Source Project
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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.
21 * \brief Thread base class.
22 *//*--------------------------------------------------------------------*/
24 #include "deThread.hpp"
34 /*--------------------------------------------------------------------*//*!
35 * \brief Thread constructor.
36 *//*--------------------------------------------------------------------*/
40 deMemset(&m_attribs, 0, sizeof(m_attribs));
43 /*--------------------------------------------------------------------*//*!
44 * \brief Destroy thread.
46 * If the thread is currently running, OS is instructed to destroy it
47 * but the actual behavior is unspecified.
48 *//*--------------------------------------------------------------------*/
49 Thread::~Thread (void)
52 deThread_destroy(m_thread);
55 /*--------------------------------------------------------------------*//*!
56 * \brief Set thread priority.
57 * \param priority deThreadPriority as described in deThread.h. Currently
58 * supported values are: DE_THREADPRIORITY_LOWEST,
59 * DE_THREADPRIORITY_LOW, DE_THREADPRIORITY_NORMAL,
60 * DE_THREADPRIORITY_HIGH, DE_THREADPRIORITY_HIGHEST.
62 * Sets priority for the thread start(). setPriority() has no effect
63 * if the thread is already running.
64 *//*--------------------------------------------------------------------*/
65 void Thread::setPriority (deThreadPriority priority)
67 m_attribs.priority = priority;
70 static void threadFunc (void* arg)
72 static_cast<Thread*>(arg)->run();
75 /*--------------------------------------------------------------------*//*!
76 * \brief Start thread.
78 * Starts thread that will execute the virtual run() method.
80 * The function will fail if the thread is currently running or has finished
81 * but no join() has been called.
82 *//*--------------------------------------------------------------------*/
83 void Thread::start (void)
86 m_thread = deThread_create(threadFunc, this, &m_attribs);
88 throw std::bad_alloc();
91 /*--------------------------------------------------------------------*//*!
92 * \brief Wait for thread to finish and clean up current thread.
94 * This function will block until currently running thread has finished.
95 * Once the thread has finished, current thread state will be cleaned
96 * and thread can be re-launched using start().
98 * join() can only be called after a successful call to start().
99 *//*--------------------------------------------------------------------*/
100 void Thread::join (void)
103 if (!deThread_join(m_thread))
104 throw std::runtime_error("Thread::join() failed");
106 deThread_destroy(m_thread);