13c827367444ee418f129b2c238299f49d3264554Jarkko Poyry/*------------------------------------------------------------------------- 23c827367444ee418f129b2c238299f49d3264554Jarkko Poyry * drawElements C++ Base Library 33c827367444ee418f129b2c238299f49d3264554Jarkko Poyry * ----------------------------- 43c827367444ee418f129b2c238299f49d3264554Jarkko Poyry * 53c827367444ee418f129b2c238299f49d3264554Jarkko Poyry * Copyright 2014 The Android Open Source Project 63c827367444ee418f129b2c238299f49d3264554Jarkko Poyry * 73c827367444ee418f129b2c238299f49d3264554Jarkko Poyry * Licensed under the Apache License, Version 2.0 (the "License"); 83c827367444ee418f129b2c238299f49d3264554Jarkko Poyry * you may not use this file except in compliance with the License. 93c827367444ee418f129b2c238299f49d3264554Jarkko Poyry * You may obtain a copy of the License at 103c827367444ee418f129b2c238299f49d3264554Jarkko Poyry * 113c827367444ee418f129b2c238299f49d3264554Jarkko Poyry * http://www.apache.org/licenses/LICENSE-2.0 123c827367444ee418f129b2c238299f49d3264554Jarkko Poyry * 133c827367444ee418f129b2c238299f49d3264554Jarkko Poyry * Unless required by applicable law or agreed to in writing, software 143c827367444ee418f129b2c238299f49d3264554Jarkko Poyry * distributed under the License is distributed on an "AS IS" BASIS, 153c827367444ee418f129b2c238299f49d3264554Jarkko Poyry * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 163c827367444ee418f129b2c238299f49d3264554Jarkko Poyry * See the License for the specific language governing permissions and 173c827367444ee418f129b2c238299f49d3264554Jarkko Poyry * limitations under the License. 183c827367444ee418f129b2c238299f49d3264554Jarkko Poyry * 193c827367444ee418f129b2c238299f49d3264554Jarkko Poyry *//*! 203c827367444ee418f129b2c238299f49d3264554Jarkko Poyry * \file 213c827367444ee418f129b2c238299f49d3264554Jarkko Poyry * \brief Thread base class. 223c827367444ee418f129b2c238299f49d3264554Jarkko Poyry *//*--------------------------------------------------------------------*/ 233c827367444ee418f129b2c238299f49d3264554Jarkko Poyry 243c827367444ee418f129b2c238299f49d3264554Jarkko Poyry#include "deThread.hpp" 253c827367444ee418f129b2c238299f49d3264554Jarkko Poyry#include "deMemory.h" 263c827367444ee418f129b2c238299f49d3264554Jarkko Poyry 273c827367444ee418f129b2c238299f49d3264554Jarkko Poyry#include <exception> 283c827367444ee418f129b2c238299f49d3264554Jarkko Poyry#include <stdexcept> 293c827367444ee418f129b2c238299f49d3264554Jarkko Poyry#include <new> 303c827367444ee418f129b2c238299f49d3264554Jarkko Poyry 313c827367444ee418f129b2c238299f49d3264554Jarkko Poyrynamespace de 323c827367444ee418f129b2c238299f49d3264554Jarkko Poyry{ 333c827367444ee418f129b2c238299f49d3264554Jarkko Poyry 343c827367444ee418f129b2c238299f49d3264554Jarkko Poyry/*--------------------------------------------------------------------*//*! 353c827367444ee418f129b2c238299f49d3264554Jarkko Poyry * \brief Thread constructor. 363c827367444ee418f129b2c238299f49d3264554Jarkko Poyry *//*--------------------------------------------------------------------*/ 373c827367444ee418f129b2c238299f49d3264554Jarkko PoyryThread::Thread (void) 383c827367444ee418f129b2c238299f49d3264554Jarkko Poyry : m_thread(0) 393c827367444ee418f129b2c238299f49d3264554Jarkko Poyry{ 403c827367444ee418f129b2c238299f49d3264554Jarkko Poyry deMemset(&m_attribs, 0, sizeof(m_attribs)); 413c827367444ee418f129b2c238299f49d3264554Jarkko Poyry} 423c827367444ee418f129b2c238299f49d3264554Jarkko Poyry 433c827367444ee418f129b2c238299f49d3264554Jarkko Poyry/*--------------------------------------------------------------------*//*! 443c827367444ee418f129b2c238299f49d3264554Jarkko Poyry * \brief Destroy thread. 453c827367444ee418f129b2c238299f49d3264554Jarkko Poyry * 463c827367444ee418f129b2c238299f49d3264554Jarkko Poyry * If the thread is currently running, OS is instructed to destroy it 473c827367444ee418f129b2c238299f49d3264554Jarkko Poyry * but the actual behavior is unspecified. 483c827367444ee418f129b2c238299f49d3264554Jarkko Poyry *//*--------------------------------------------------------------------*/ 493c827367444ee418f129b2c238299f49d3264554Jarkko PoyryThread::~Thread (void) 503c827367444ee418f129b2c238299f49d3264554Jarkko Poyry{ 513c827367444ee418f129b2c238299f49d3264554Jarkko Poyry if (m_thread) 523c827367444ee418f129b2c238299f49d3264554Jarkko Poyry deThread_destroy(m_thread); 533c827367444ee418f129b2c238299f49d3264554Jarkko Poyry} 543c827367444ee418f129b2c238299f49d3264554Jarkko Poyry 553c827367444ee418f129b2c238299f49d3264554Jarkko Poyry/*--------------------------------------------------------------------*//*! 563c827367444ee418f129b2c238299f49d3264554Jarkko Poyry * \brief Set thread priority. 573c827367444ee418f129b2c238299f49d3264554Jarkko Poyry * \param priority deThreadPriority as described in deThread.h. Currently 583c827367444ee418f129b2c238299f49d3264554Jarkko Poyry * supported values are: DE_THREADPRIORITY_LOWEST, 593c827367444ee418f129b2c238299f49d3264554Jarkko Poyry * DE_THREADPRIORITY_LOW, DE_THREADPRIORITY_NORMAL, 603c827367444ee418f129b2c238299f49d3264554Jarkko Poyry * DE_THREADPRIORITY_HIGH, DE_THREADPRIORITY_HIGHEST. 613c827367444ee418f129b2c238299f49d3264554Jarkko Poyry * 623c827367444ee418f129b2c238299f49d3264554Jarkko Poyry * Sets priority for the thread start(). setPriority() has no effect 633c827367444ee418f129b2c238299f49d3264554Jarkko Poyry * if the thread is already running. 643c827367444ee418f129b2c238299f49d3264554Jarkko Poyry *//*--------------------------------------------------------------------*/ 653c827367444ee418f129b2c238299f49d3264554Jarkko Poyryvoid Thread::setPriority (deThreadPriority priority) 663c827367444ee418f129b2c238299f49d3264554Jarkko Poyry{ 673c827367444ee418f129b2c238299f49d3264554Jarkko Poyry m_attribs.priority = priority; 683c827367444ee418f129b2c238299f49d3264554Jarkko Poyry} 693c827367444ee418f129b2c238299f49d3264554Jarkko Poyry 703c827367444ee418f129b2c238299f49d3264554Jarkko Poyrystatic void threadFunc (void* arg) 713c827367444ee418f129b2c238299f49d3264554Jarkko Poyry{ 723c827367444ee418f129b2c238299f49d3264554Jarkko Poyry static_cast<Thread*>(arg)->run(); 733c827367444ee418f129b2c238299f49d3264554Jarkko Poyry} 743c827367444ee418f129b2c238299f49d3264554Jarkko Poyry 753c827367444ee418f129b2c238299f49d3264554Jarkko Poyry/*--------------------------------------------------------------------*//*! 763c827367444ee418f129b2c238299f49d3264554Jarkko Poyry * \brief Start thread. 773c827367444ee418f129b2c238299f49d3264554Jarkko Poyry * 783c827367444ee418f129b2c238299f49d3264554Jarkko Poyry * Starts thread that will execute the virtual run() method. 793c827367444ee418f129b2c238299f49d3264554Jarkko Poyry * 803c827367444ee418f129b2c238299f49d3264554Jarkko Poyry * The function will fail if the thread is currently running or has finished 813c827367444ee418f129b2c238299f49d3264554Jarkko Poyry * but no join() has been called. 823c827367444ee418f129b2c238299f49d3264554Jarkko Poyry *//*--------------------------------------------------------------------*/ 833c827367444ee418f129b2c238299f49d3264554Jarkko Poyryvoid Thread::start (void) 843c827367444ee418f129b2c238299f49d3264554Jarkko Poyry{ 853c827367444ee418f129b2c238299f49d3264554Jarkko Poyry DE_ASSERT(!m_thread); 863c827367444ee418f129b2c238299f49d3264554Jarkko Poyry m_thread = deThread_create(threadFunc, this, &m_attribs); 873c827367444ee418f129b2c238299f49d3264554Jarkko Poyry if (!m_thread) 883c827367444ee418f129b2c238299f49d3264554Jarkko Poyry throw std::bad_alloc(); 893c827367444ee418f129b2c238299f49d3264554Jarkko Poyry} 903c827367444ee418f129b2c238299f49d3264554Jarkko Poyry 913c827367444ee418f129b2c238299f49d3264554Jarkko Poyry/*--------------------------------------------------------------------*//*! 923c827367444ee418f129b2c238299f49d3264554Jarkko Poyry * \brief Wait for thread to finish and clean up current thread. 933c827367444ee418f129b2c238299f49d3264554Jarkko Poyry * 943c827367444ee418f129b2c238299f49d3264554Jarkko Poyry * This function will block until currently running thread has finished. 953c827367444ee418f129b2c238299f49d3264554Jarkko Poyry * Once the thread has finished, current thread state will be cleaned 963c827367444ee418f129b2c238299f49d3264554Jarkko Poyry * and thread can be re-launched using start(). 973c827367444ee418f129b2c238299f49d3264554Jarkko Poyry * 983c827367444ee418f129b2c238299f49d3264554Jarkko Poyry * join() can only be called after a successful call to start(). 993c827367444ee418f129b2c238299f49d3264554Jarkko Poyry *//*--------------------------------------------------------------------*/ 1003c827367444ee418f129b2c238299f49d3264554Jarkko Poyryvoid Thread::join (void) 1013c827367444ee418f129b2c238299f49d3264554Jarkko Poyry{ 1023c827367444ee418f129b2c238299f49d3264554Jarkko Poyry DE_ASSERT(m_thread); 1033c827367444ee418f129b2c238299f49d3264554Jarkko Poyry if (!deThread_join(m_thread)) 1043c827367444ee418f129b2c238299f49d3264554Jarkko Poyry throw std::runtime_error("Thread::join() failed"); 1053c827367444ee418f129b2c238299f49d3264554Jarkko Poyry 1063c827367444ee418f129b2c238299f49d3264554Jarkko Poyry deThread_destroy(m_thread); 1073c827367444ee418f129b2c238299f49d3264554Jarkko Poyry m_thread = 0; 1083c827367444ee418f129b2c238299f49d3264554Jarkko Poyry} 1093c827367444ee418f129b2c238299f49d3264554Jarkko Poyry 1103c827367444ee418f129b2c238299f49d3264554Jarkko Poyry} // de 111