Subversion Repositories HomeAutomation

Rev

Rev 1124 | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | SVN | RSS feed

Rev 1124 Rev 1196
1
/***************************************************************************
1
/***************************************************************************
2
 *   Copyright (C) November 29, 2008 by Mattias Runge                             *
2
 *   Copyright (C) November 29, 2008 by Mattias Runge                             *
3
 *   mattias@runge.se                                                      *
3
 *   mattias@runge.se                                                      *
4
 *   thread.h                                            *
4
 *   thread.h                                            *
5
 *                                                                         *
5
 *                                                                         *
6
 *   This program is free software; you can redistribute it and/or modify  *
6
 *   This program is free software; you can redistribute it and/or modify  *
7
 *   it under the terms of the GNU General Public License as published by  *
7
 *   it under the terms of the GNU General Public License as published by  *
8
 *   the Free Software Foundation; either version 2 of the License, or     *
8
 *   the Free Software Foundation; either version 2 of the License, or     *
9
 *   (at your option) any later version.                                   *
9
 *   (at your option) any later version.                                   *
10
 *                                                                         *
10
 *                                                                         *
11
 *   This program is distributed in the hope that it will be useful,       *
11
 *   This program is distributed in the hope that it will be useful,       *
12
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
12
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
13
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
13
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
14
 *   GNU General Public License for more details.                          *
14
 *   GNU General Public License for more details.                          *
15
 *                                                                         *
15
 *                                                                         *
16
 *   You should have received a copy of the GNU General Public License     *
16
 *   You should have received a copy of the GNU General Public License     *
17
 *   along with this program; if not, write to the                         *
17
 *   along with this program; if not, write to the                         *
18
 *   Free Software Foundation, Inc.,                                       *
18
 *   Free Software Foundation, Inc.,                                       *
19
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
19
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
20
 ***************************************************************************/
20
 ***************************************************************************/
21
 
21
 
22
#ifndef _THREAD_H
22
#ifndef _THREAD_H
23
#define _THREAD_H
23
#define _THREAD_H
24
 
24
 
25
#include <pthread.h>
25
#include <pthread.h>
26
#include <iostream>
26
#include <iostream>
27
 
27
 
28
template <class T>
28
template <class T>
29
class Thread
29
class Thread
30
{
30
{
31
public:
31
public:
32
    Thread()
32
    Thread()
33
    {
33
    {
34
        myIsCreated = false;
34
        myIsCreated = false;
35
        myError = 0;
35
        myError = 0;
36
    };
36
    };
37
    ~Thread()
37
    virtual ~Thread()
38
    {
38
    {
39
        stop();
39
        stop();
40
    };
40
    };
41
 
41
 
42
    bool stop()
42
    bool stop()
43
    {
43
    {
44
       
44
 
45
        if (!myIsCreated)
45
        if (!myIsCreated)
46
        {
46
        {
47
            //cout << "Thread is not running, can not be stopped " << endl;
47
            //cout << "Thread is not running, can not be stopped " << endl;
48
            return true;
48
            return true;
49
        }
49
        }
50
       
50
 
51
        //cout << "Thread cancel to run " << endl;
51
        //cout << "Thread cancel to run " << endl;
52
       
52
 
53
        //myError = pthread_cancel(myHandle);
53
        myError = pthread_cancel(myHandle);
54
        myError = pthread_detach(myHandle);
54
        //myError = pthread_detach(myHandle);
55
        //cout << "Thread cancel returned " << myError << endl;
55
        //cout << "Thread cancel returned " << myError << endl;
56
       
56
 
57
       
57
 
58
       
58
 
59
        //if (joinThread)
59
        //if (joinThread)
60
        {
60
        {
61
            //join();
61
            join();
62
        }
62
        }
63
       
63
 
64
        myIsCreated = false;
64
        myIsCreated = false;
65
 
65
 
66
        return myError == 0;
66
        return myError == 0;
67
    };
67
    };
68
   
68
 
69
    bool start()
69
    bool start()
70
    {
70
    {
71
        myError = pthread_create(&myHandle, NULL, T::doThread, (void*)this);
71
        myError = pthread_create(&myHandle, NULL, T::doThread, (void*)this);
72
       
72
 
73
        if (myError != 0)
73
        if (myError != 0)
74
        {
74
        {
75
            myIsCreated = false;
75
            myIsCreated = false;
76
            return false;
76
            return false;
77
        }
77
        }
78
 
78
 
79
        myIsCreated = true;
79
        myIsCreated = true;
80
        return true;
80
        return true;
81
    };
81
    };
82
 
82
 
83
    bool join()
83
    bool join()
84
    {
84
    {
85
        if (!myIsCreated)
85
        if (!myIsCreated)
86
            return false;
86
            return false;
87
 
87
 
88
        myError = pthread_join(myHandle, NULL);
88
        myError = pthread_join(myHandle, NULL);
89
 
89
 
90
        if (myError != 0)
90
        if (myError != 0)
91
        {
91
        {
92
            myIsCreated = false;
92
            myIsCreated = false;
93
            return false;
93
            return false;
94
        }
94
        }
95
 
95
 
96
        myIsCreated = true;
96
        myIsCreated = true;
97
        return true;
97
        return true;
98
    };
98
    };
99
 
99
 
100
    int getError()
100
    int getError()
101
    {
101
    {
102
        return myError;
102
        return myError;
103
    };
103
    };
104
 
104
 
105
    static void *doThread(void* ptr)
105
    static void *doThread(void* ptr)
106
    {
106
    {
107
        ((T*)ptr)->run();
107
        ((T*)ptr)->run();
108
    };
108
    };
109
 
109
 
110
    virtual void run() { };
110
    virtual void run() { };
111
 
111
 
112
private:
112
private:
113
    bool myIsCreated;
113
    bool myIsCreated;
114
    pthread_t myHandle;
114
    pthread_t myHandle;
115
    int myError;
115
    int myError;
116
};
116
};
117
 
117
 
118
#endif  /* _THREAD_H */
118
#endif  /* _THREAD_H */
119
 
119
 
120
 
120