File: thread.h

package info (click to toggle)
meshlab 2020.09%2Bdfsg1-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 45,124 kB
  • sloc: cpp: 400,238; ansic: 31,952; javascript: 1,578; sh: 387; yacc: 238; lex: 139; python: 86; makefile: 29
file content (112 lines) | stat: -rwxr-xr-x 2,862 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
#ifndef MT_THREAD_H
#define MT_THREAD_H

#include <memory.h>

#include "base.h"

#include <pthread.h>
#include <signal.h>

namespace mt
{

class thread
{
    MT_PREVENT_COPY(thread)

    public:

            typedef thread this_type;
            typedef void   base_type;

            thread(void) : flags(0)
            {
                    ;
            }

            virtual ~thread(void)
            {
                    ;
            }

            virtual bool start(void)
            {
                    if ((this->flags & thread_started) != 0) return false;

                    pthread_create(&(this->tid), 0, this_type::thread_func, reinterpret_cast<void *>(this));

                    /*
                    sched_param sp;
                    memset(&sp, 0, sizeof(sched_param));
                    sp.sched_priority = sched_get_priority_min(SCHED_OTHER);
                    sp.sched_priority = 0; // normal
                    pthread_setschedparam(this->tid, SCHED_OTHER, &sp);
                    */

                    this->flags |= thread_started;

                    return true;
            }

            virtual bool wait(void)
            {
                    if ((this->flags & thread_started) == 0) return false;
                    pthread_join(this->tid, 0);
                    this->flags &= ~thread_started;
                    return true;
            }

            virtual bool kill(void)
            {
                    if ((this->flags & thread_started) == 0) return false;
                    pthread_kill(this->tid, 0);
                    this->flags &= ~(thread_started | thread_running);
                    return true;
            }

            bool is_started(void) const
            {
                    return ((this->flags & thread_started) != 0);
            }

            bool is_running(void) const
            {
                    return ((this->flags & thread_running) != 0);
            }

    protected:

            virtual void run(void)
            {
                    ;
            }

    private:

            enum thread_flags
            {
                    thread_none    = (     0),
                    thread_started = (1 << 0),
                    thread_running = (1 << 1)
            };

            volatile unsigned int flags;
            pthread_t tid;

            static void * thread_func(void * param)
            {
                    this_type * p_this = reinterpret_cast<this_type *>(param);
                    MT_ASSERT(p_this != 0);

                    p_this->flags |= thread_running;
                    p_this->run();
                    p_this->flags &= ~thread_running;

                    return 0;
            }
};

}

#endif // MT_THREAD_H