1 /** @addtogroup MCD_MCDIMPL_DAEMON_SRV 2 * @{ 3 * @file 4 * 5 * Thread implementation (pthread abstraction). 6 * 7 * <!-- Copyright Giesecke & Devrient GmbH 2009 - 2012 --> 8 * 9 * Redistribution and use in source and binary forms, with or without 10 * modification, are permitted provided that the following conditions 11 * are met: 12 * 1. Redistributions of source code must retain the above copyright 13 * notice, this list of conditions and the following disclaimer. 14 * 2. Redistributions in binary form must reproduce the above copyright 15 * notice, this list of conditions and the following disclaimer in the 16 * documentation and/or other materials provided with the distribution. 17 * 3. The name of the author may not be used to endorse or promote 18 * products derived from this software without specific prior 19 * written permission. 20 * 21 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS 22 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 23 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 24 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY 25 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE 27 * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 28 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 29 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 30 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 31 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 32 */ 33 #include "CThread.h" 34 35 #include "log.h" 36 37 38 //------------------------------------------------------------------------------ 39 CThread::CThread(void) : 40 m_terminate(false), m_isExiting(false) 41 { 42 m_sem = new CSemaphore(); 43 } 44 45 46 //------------------------------------------------------------------------------ 47 CThread::~CThread( 48 void 49 ) 50 { 51 delete m_sem; 52 } 53 54 55 //------------------------------------------------------------------------------ 56 void CThread::terminate( 57 void 58 ) 59 { 60 m_terminate = true; 61 } 62 63 64 //------------------------------------------------------------------------------ 65 bool CThread::isExiting( 66 void 67 ) 68 { 69 return m_isExiting; 70 } 71 72 73 //------------------------------------------------------------------------------ 74 void CThread::setExiting( 75 void 76 ) 77 { 78 m_isExiting = true; 79 } 80 81 82 //------------------------------------------------------------------------------ 83 void CThread::exit( 84 int32_t exitcode 85 ) 86 { 87 setExiting(); 88 pthread_exit((void *)exitcode); 89 } 90 91 92 //------------------------------------------------------------------------------ 93 bool CThread::shouldTerminate( 94 void 95 ) 96 { 97 return m_terminate; 98 } 99 100 101 //------------------------------------------------------------------------------ 102 void CThread::start( 103 void 104 ) 105 { 106 int ret; 107 ret = pthread_create(&m_thread, NULL, CThreadStartup, this); 108 if (0 != ret) 109 LOG_E("pthread_create failed with error code %d", ret); 110 } 111 112 113 //------------------------------------------------------------------------------ 114 void CThread::join( 115 void 116 ) 117 { 118 int ret; 119 ret = pthread_join(m_thread, NULL); 120 if (0 != ret) 121 LOG_E("pthread_join failed with error code %d", ret); 122 } 123 124 125 //------------------------------------------------------------------------------ 126 void CThread::sleep( 127 void 128 ) 129 { 130 m_sem->wait(); 131 } 132 133 134 //------------------------------------------------------------------------------ 135 void CThread::wakeup( 136 void 137 ) 138 { 139 m_sem->signal(); 140 } 141 142 143 //------------------------------------------------------------------------------ 144 void *CThreadStartup( 145 void *_tgtObject 146 ) 147 { 148 CThread *tgtObject = (CThread *) _tgtObject; 149 tgtObject->run(); 150 return NULL; 151 } 152 153 /** @} */ 154