Home | History | Annotate | Download | only in decpp
      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  *//*--------------------------------------------------------------------*/
     37 Thread::Thread (void)
     38 	: m_thread(0)
     39 {
     40 	deMemset(&m_attribs, 0, sizeof(m_attribs));
     41 }
     42 
     43 /*--------------------------------------------------------------------*//*!
     44  * \brief Destroy thread.
     45  *
     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)
     50 {
     51 	if (m_thread)
     52 		deThread_destroy(m_thread);
     53 }
     54 
     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.
     61  *
     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)
     66 {
     67 	m_attribs.priority = priority;
     68 }
     69 
     70 static void threadFunc (void* arg)
     71 {
     72 	static_cast<Thread*>(arg)->run();
     73 }
     74 
     75 /*--------------------------------------------------------------------*//*!
     76  * \brief Start thread.
     77  *
     78  * Starts thread that will execute the virtual run() method.
     79  *
     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)
     84 {
     85 	DE_ASSERT(!m_thread);
     86 	m_thread = deThread_create(threadFunc, this, &m_attribs);
     87 	if (!m_thread)
     88 		throw std::bad_alloc();
     89 }
     90 
     91 /*--------------------------------------------------------------------*//*!
     92  * \brief Wait for thread to finish and clean up current thread.
     93  *
     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().
     97  *
     98  * join() can only be called after a successful call to start().
     99  *//*--------------------------------------------------------------------*/
    100 void Thread::join (void)
    101 {
    102 	DE_ASSERT(m_thread);
    103 	if (!deThread_join(m_thread))
    104 		throw std::runtime_error("Thread::join() failed");
    105 
    106 	deThread_destroy(m_thread);
    107 	m_thread = 0;
    108 }
    109 
    110 } // de
    111