-
Notifications
You must be signed in to change notification settings - Fork 201
/
CLinuxOperatingSystem.cpp
executable file
·126 lines (69 loc) · 2.28 KB
/
CLinuxOperatingSystem.cpp
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
#include "COperatingSystemFactory.h"
#include "CLinuxOperatingSystem.h"
#include "CThread.h"
#include "CCountingSem.h"
#include "CLinuxCountingSem.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <assert.h>
static void threadEntry(unsigned long *threadParm)
{
CThread *pThread = (CThread *) threadParm[0];
CCountingSem *pSemaphore = (CCountingSem *) threadParm[1];
//printf("Thread3....%ld,OS:%ld\n",threadParm[0],(unsigned long) pThread);
if(pThread==NULL)
{
printf("Thread Error...\n");
}else
{
//printf("Begain to Run....%ld\n",(unsigned long)pThread);
pThread->threadEntry(pSemaphore);
}
pthread_exit(0);
}
CLinuxOperatingSystem::CLinuxOperatingSystem():
COperatingSystem()
{
mThreadId = 0;
}
CLinuxOperatingSystem::~CLinuxOperatingSystem()
{
}
bool CLinuxOperatingSystem::createThread(CThread *mThread,unsigned long stack_size)
{
int rc;
unsigned long arguments[4];
bool result;
if (mThread == NULL)
{
printf("CLinuxOperatingSystem::CreateThread() : Thread pointer is NULL! Can't begin the thread... ");
return false;
}
p_thread = mThread;
p_sem=COperatingSystemFactory::newCountingSem(0);
pthread_attr_init(&mThreadAttr); //线程属性初始化
pthread_attr_setschedpolicy(&mThreadAttr, SCHED_FIFO);
pthread_attr_setscope(&mThreadAttr, PTHREAD_SCOPE_SYSTEM);
pthread_attr_setstacksize(&mThreadAttr, stack_size);
//pthread_attr_setinheritsched(&mThreadAttr, PTHREAD_EXPLICIT_SCHED);
memset(arguments, 0, sizeof(arguments));
arguments[0] = (unsigned long) p_thread; //参数传递
arguments[1] = (unsigned long) p_sem;
//printf("Thread2....%ld,OS:%ld\n",arguments[0],(unsigned long) p_thread);
rc = pthread_create(&mThreadId, &mThreadAttr, (void * (*) (void *))threadEntry, arguments); //启动ThreadEntrypoint函数创建实在线程
if (rc != 0){
printf("CLinuxOperatingSystem::CreateThread() : Failed to create and start the thread! ");
p_thread = NULL;
mThreadId = 0;
return false;
}
pthread_detach(mThreadId);
result = p_sem->Get();
return true;
}
void CLinuxOperatingSystem::sleepSec(unsigned long sec)
{
sleep(sec);
}