zhangzengfei
2022-07-20 c90c3e794bdd95127d0c34ff1d9e8759d18a0445
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
/******************************************************************************
* FILE:    MyThread.cpp
* Description:    
*    Implementation of the CCritSec, CAutoLock and CMyThread class.
*
* Modified Code History
* Mark    Date        By            Modification Reason
*******************************************************************************
* 01    2007-1-29    songxw    Initial creation.
******************************************************************************/
 
#include <stdio.h>
#include <signal.h>
#include "MyThread.h"
 #include <unistd.h>
 
CMyThread::CMyThread()
{
    m_nThreadID = 0;
    m_bThreadStarted = false;
    ResetStopEvent();
}
 
CMyThread::~CMyThread()
{
     //should we call StopThread here ??
}
 
int CMyThread::StartThread(int policy, int Priority, int StackSize)
{
    int iRet;
    CAutoLock lock(&m_APILock);
    
    if (ThreadStatus())
    {    
        return -1;
    }
        
    ResetStopEvent();
 
    //Set up POSIX thread attributes
    pthread_attr_t attr;
    pthread_attr_init(&attr);
    
    //set thread's policy
    iRet = pthread_attr_setschedpolicy(&attr, policy);
    if (iRet != 0)
    {
        perror("Thread set policy failure!");
    }
    
    //set thread's priority
    sched_param param;
    param.sched_priority = Priority;
    iRet = pthread_attr_setschedparam(&attr, &param);
    if (iRet < 0)
    {
        perror("Thread set prior failure!");
    }
    
    //set thread's stack size
    iRet = pthread_attr_setstacksize(&attr, StackSize);
    if (iRet != 0)
    {
        perror("Thread set stack size failure!");
    }
    
    pthread_attr_setdetachstate(&attr,PTHREAD_CREATE_DETACHED);
    
    //startup thread to run
    int nRet = pthread_create(&m_nThreadID, &attr, CMyThread::ThreadEntryPoint, this);
    if (nRet != 0)
    {
        // process error here
        
        pthread_attr_destroy(&attr);
        return -1;
    }
    pthread_attr_destroy(&attr);
    
    SetThreadStatus(true);
    
    return 0;    
}
 
int CMyThread::StopThread(unsigned long timeout)
{
    if (!ThreadStatus())
    {
        return 0;  //this should return 0
    }
 
    CAutoLock lock(&m_APILock);
    SetStopEvent();
 
    //TODO: pthread_join does not support timeout, need to implement a timer
    int nRet = pthread_join(m_nThreadID,NULL);
    if (nRet)
    {
        // handle error here
        return -1;
    }
    SetThreadStatus(false);
    
    return 0;
}
 
// kill the thread immediately, not implemented yet
int CMyThread::KillThread()
{
    pthread_kill(m_nThreadID, 0);
    
    return 0;
}
 
int CMyThread::CheckStopEvent()
{
    return GetStopEvent();
}
 
void *CMyThread::ThreadEntryPoint(void *pParam)
{
    CMyThread *obj = static_cast<CMyThread *>(pParam);
    
    obj->ThreadProc();
    //((CMyThread *)pParam)->ThreadProc();
    
    return NULL;
}