1e5c31af7Sopenharmony_ci/*-------------------------------------------------------------------------
2e5c31af7Sopenharmony_ci * drawElements C++ Base Library
3e5c31af7Sopenharmony_ci * -----------------------------
4e5c31af7Sopenharmony_ci *
5e5c31af7Sopenharmony_ci * Copyright 2014 The Android Open Source Project
6e5c31af7Sopenharmony_ci *
7e5c31af7Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
8e5c31af7Sopenharmony_ci * you may not use this file except in compliance with the License.
9e5c31af7Sopenharmony_ci * You may obtain a copy of the License at
10e5c31af7Sopenharmony_ci *
11e5c31af7Sopenharmony_ci *      http://www.apache.org/licenses/LICENSE-2.0
12e5c31af7Sopenharmony_ci *
13e5c31af7Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
14e5c31af7Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
15e5c31af7Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16e5c31af7Sopenharmony_ci * See the License for the specific language governing permissions and
17e5c31af7Sopenharmony_ci * limitations under the License.
18e5c31af7Sopenharmony_ci *
19e5c31af7Sopenharmony_ci *//*!
20e5c31af7Sopenharmony_ci * \file
21e5c31af7Sopenharmony_ci * \brief Thread base class.
22e5c31af7Sopenharmony_ci *//*--------------------------------------------------------------------*/
23e5c31af7Sopenharmony_ci
24e5c31af7Sopenharmony_ci#include "deThread.hpp"
25e5c31af7Sopenharmony_ci#include "deMemory.h"
26e5c31af7Sopenharmony_ci
27e5c31af7Sopenharmony_ci#include <exception>
28e5c31af7Sopenharmony_ci#include <stdexcept>
29e5c31af7Sopenharmony_ci#include <new>
30e5c31af7Sopenharmony_ci
31e5c31af7Sopenharmony_cinamespace de
32e5c31af7Sopenharmony_ci{
33e5c31af7Sopenharmony_ci
34e5c31af7Sopenharmony_ci/*--------------------------------------------------------------------*//*!
35e5c31af7Sopenharmony_ci * \brief Thread constructor.
36e5c31af7Sopenharmony_ci *//*--------------------------------------------------------------------*/
37e5c31af7Sopenharmony_ciThread::Thread (void)
38e5c31af7Sopenharmony_ci	: m_thread(0)
39e5c31af7Sopenharmony_ci{
40e5c31af7Sopenharmony_ci	deMemset(&m_attribs, 0, sizeof(m_attribs));
41e5c31af7Sopenharmony_ci}
42e5c31af7Sopenharmony_ci
43e5c31af7Sopenharmony_ci/*--------------------------------------------------------------------*//*!
44e5c31af7Sopenharmony_ci * \brief Destroy thread.
45e5c31af7Sopenharmony_ci *
46e5c31af7Sopenharmony_ci * If the thread is currently running, OS is instructed to destroy it
47e5c31af7Sopenharmony_ci * but the actual behavior is unspecified.
48e5c31af7Sopenharmony_ci *//*--------------------------------------------------------------------*/
49e5c31af7Sopenharmony_ciThread::~Thread (void)
50e5c31af7Sopenharmony_ci{
51e5c31af7Sopenharmony_ci	if (m_thread)
52e5c31af7Sopenharmony_ci		deThread_destroy(m_thread);
53e5c31af7Sopenharmony_ci}
54e5c31af7Sopenharmony_ci
55e5c31af7Sopenharmony_ci/*--------------------------------------------------------------------*//*!
56e5c31af7Sopenharmony_ci * \brief Set thread priority.
57e5c31af7Sopenharmony_ci * \param priority deThreadPriority as described in deThread.h. Currently
58e5c31af7Sopenharmony_ci *				   supported values are: DE_THREADPRIORITY_LOWEST,
59e5c31af7Sopenharmony_ci *				   DE_THREADPRIORITY_LOW, DE_THREADPRIORITY_NORMAL,
60e5c31af7Sopenharmony_ci *				   DE_THREADPRIORITY_HIGH, DE_THREADPRIORITY_HIGHEST.
61e5c31af7Sopenharmony_ci *
62e5c31af7Sopenharmony_ci * Sets priority for the thread start(). setPriority() has no effect
63e5c31af7Sopenharmony_ci * if the thread is already running.
64e5c31af7Sopenharmony_ci *//*--------------------------------------------------------------------*/
65e5c31af7Sopenharmony_civoid Thread::setPriority (deThreadPriority priority)
66e5c31af7Sopenharmony_ci{
67e5c31af7Sopenharmony_ci	m_attribs.priority = priority;
68e5c31af7Sopenharmony_ci}
69e5c31af7Sopenharmony_ci
70e5c31af7Sopenharmony_cistatic void threadFunc (void* arg)
71e5c31af7Sopenharmony_ci{
72e5c31af7Sopenharmony_ci	static_cast<Thread*>(arg)->run();
73e5c31af7Sopenharmony_ci}
74e5c31af7Sopenharmony_ci
75e5c31af7Sopenharmony_ci/*--------------------------------------------------------------------*//*!
76e5c31af7Sopenharmony_ci * \brief Start thread.
77e5c31af7Sopenharmony_ci *
78e5c31af7Sopenharmony_ci * Starts thread that will execute the virtual run() method.
79e5c31af7Sopenharmony_ci *
80e5c31af7Sopenharmony_ci * The function will fail if the thread is currently running or has finished
81e5c31af7Sopenharmony_ci * but no join() has been called.
82e5c31af7Sopenharmony_ci *//*--------------------------------------------------------------------*/
83e5c31af7Sopenharmony_civoid Thread::start (void)
84e5c31af7Sopenharmony_ci{
85e5c31af7Sopenharmony_ci	DE_ASSERT(!m_thread);
86e5c31af7Sopenharmony_ci	m_thread = deThread_create(threadFunc, this, &m_attribs);
87e5c31af7Sopenharmony_ci	if (!m_thread)
88e5c31af7Sopenharmony_ci		throw std::bad_alloc();
89e5c31af7Sopenharmony_ci}
90e5c31af7Sopenharmony_ci
91e5c31af7Sopenharmony_ci/*--------------------------------------------------------------------*//*!
92e5c31af7Sopenharmony_ci * \brief Wait for thread to finish and clean up current thread.
93e5c31af7Sopenharmony_ci *
94e5c31af7Sopenharmony_ci * This function will block until currently running thread has finished.
95e5c31af7Sopenharmony_ci * Once the thread has finished, current thread state will be cleaned
96e5c31af7Sopenharmony_ci * and thread can be re-launched using start().
97e5c31af7Sopenharmony_ci *
98e5c31af7Sopenharmony_ci * join() can only be called after a successful call to start().
99e5c31af7Sopenharmony_ci *//*--------------------------------------------------------------------*/
100e5c31af7Sopenharmony_civoid Thread::join (void)
101e5c31af7Sopenharmony_ci{
102e5c31af7Sopenharmony_ci	DE_ASSERT(m_thread);
103e5c31af7Sopenharmony_ci	if (!deThread_join(m_thread))
104e5c31af7Sopenharmony_ci		throw std::runtime_error("Thread::join() failed");
105e5c31af7Sopenharmony_ci
106e5c31af7Sopenharmony_ci	deThread_destroy(m_thread);
107e5c31af7Sopenharmony_ci	m_thread = 0;
108e5c31af7Sopenharmony_ci}
109e5c31af7Sopenharmony_ci
110e5c31af7Sopenharmony_ci} // de
111