• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #ifndef CTHREAD_H_
34 #define CTHREAD_H_
35 
36 #include <inttypes.h>
37 #include "CSemaphore.h"
38 #include "pthread.h"
39 
40 using namespace std;
41 
42 
43 class CThread
44 {
45 
46 public:
47 
48     CThread(void);
49 
50     virtual ~CThread(void);
51 
52     virtual void run(void) = 0;
53 
54     void start(void);
55 
56     void join(void);
57 
58     void sleep(void);
59 
60     void wakeup(void);
61 
62     void terminate(void);
63 
64     bool isExiting(void);
65 
66     void setExiting(void);
67 
68 protected:
69 
70     bool shouldTerminate(void);
71 
72     void exit(int32_t exitcode);
73 
74 private:
75 
76     CSemaphore *m_sem;
77     pthread_t m_thread;
78     bool m_terminate;
79     bool m_isExiting;
80 
81 };
82 
83 extern "C" void *CThreadStartup(void *);
84 
85 #endif /*CTHREAD_H_*/
86 
87 /** @} */
88