1 /*-------------------------------------------------------------------------
2 * drawElements C++ Base 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 Thread base class.
22 *//*--------------------------------------------------------------------*/
23
24 #include "deThread.hpp"
25 #include "deMemory.h"
26
27 #include <exception>
28 #include <stdexcept>
29 #include <new>
30
31 namespace de
32 {
33
34 /*--------------------------------------------------------------------*//*!
35 * \brief Thread constructor.
36 *//*--------------------------------------------------------------------*/
Thread(void)37 Thread::Thread(void) : m_thread(0)
38 {
39 deMemset(&m_attribs, 0, sizeof(m_attribs));
40 }
41
42 /*--------------------------------------------------------------------*//*!
43 * \brief Destroy thread.
44 *
45 * If the thread is currently running, OS is instructed to destroy it
46 * but the actual behavior is unspecified.
47 *//*--------------------------------------------------------------------*/
~Thread(void)48 Thread::~Thread(void)
49 {
50 if (m_thread)
51 deThread_destroy(m_thread);
52 }
53
54 /*--------------------------------------------------------------------*//*!
55 * \brief Set thread priority.
56 * \param priority deThreadPriority as described in deThread.h. Currently
57 * supported values are: DE_THREADPRIORITY_LOWEST,
58 * DE_THREADPRIORITY_LOW, DE_THREADPRIORITY_NORMAL,
59 * DE_THREADPRIORITY_HIGH, DE_THREADPRIORITY_HIGHEST.
60 *
61 * Sets priority for the thread start(). setPriority() has no effect
62 * if the thread is already running.
63 *//*--------------------------------------------------------------------*/
setPriority(deThreadPriority priority)64 void Thread::setPriority(deThreadPriority priority)
65 {
66 m_attribs.priority = priority;
67 }
68
threadFunc(void * arg)69 static void threadFunc(void *arg)
70 {
71 static_cast<Thread *>(arg)->run();
72 }
73
74 /*--------------------------------------------------------------------*//*!
75 * \brief Start thread.
76 *
77 * Starts thread that will execute the virtual run() method.
78 *
79 * The function will fail if the thread is currently running or has finished
80 * but no join() has been called.
81 *//*--------------------------------------------------------------------*/
start(void)82 void Thread::start(void)
83 {
84 DE_ASSERT(!m_thread);
85 m_thread = deThread_create(threadFunc, this, &m_attribs);
86 if (!m_thread)
87 throw std::bad_alloc();
88 }
89
90 /*--------------------------------------------------------------------*//*!
91 * \brief Wait for thread to finish and clean up current thread.
92 *
93 * This function will block until currently running thread has finished.
94 * Once the thread has finished, current thread state will be cleaned
95 * and thread can be re-launched using start().
96 *
97 * join() can only be called after a successful call to start().
98 *//*--------------------------------------------------------------------*/
join(void)99 void Thread::join(void)
100 {
101 DE_ASSERT(m_thread);
102 if (!deThread_join(m_thread))
103 throw std::runtime_error("Thread::join() failed");
104
105 deThread_destroy(m_thread);
106 m_thread = 0;
107 }
108
109 } // namespace de
110