Skip to content

Commit 37eec14

Browse files
author
root
committed
first commit
0 parents  commit 37eec14

26 files changed

+1429
-0
lines changed

CCountingSem.cpp

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
2+
3+
#include "CCountingSem.h"
4+
5+
6+
7+
8+
9+
CCountingSem::CCountingSem()
10+
{
11+
12+
}
13+
14+
15+
16+
CCountingSem::~CCountingSem()
17+
{
18+
19+
20+
}
21+
22+

CCountingSem.h

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
2+
3+
#ifndef _CCountingSem_h_
4+
#define _CCountingSem_h_
5+
6+
/********************************************************************************************
7+
*
8+
*File name: CCountingSem.h
9+
*Author: Wu Yinghao
10+
*Version: 0.0.1
11+
*Date: 2013.5.20
12+
13+
*Description: this file is base class of CountingSem
14+
*
15+
********************************************************************************************/
16+
17+
class CCountingSem
18+
{
19+
public:
20+
typedef enum {
21+
kTimeout,
22+
kForever
23+
}Mode;
24+
25+
26+
CCountingSem();
27+
~CCountingSem();
28+
29+
//get the sem and sem -1
30+
virtual bool Get(Mode mode = kForever, unsigned long timeoutMS = 0) = 0;
31+
32+
//post the sem and sem +1
33+
virtual bool Post(void) = 0;
34+
35+
};
36+
37+
38+
39+
40+
41+
42+
43+
44+
45+
#endif
46+
47+
48+

CLinuxCountingSem.cpp

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
2+
#include "CLinuxCountingSem.h"
3+
#include <stdio.h>
4+
#include <stdlib.h>
5+
6+
7+
8+
CLinuxCountingSem::CLinuxCountingSem(unsigned int init_sem_count):
9+
CCountingSem()
10+
{
11+
int result;
12+
result = sem_init(&sem, 0, init_sem_count);
13+
14+
if (result != 0) {
15+
printf("CLinuxCountingSem: error\n");
16+
17+
}
18+
19+
20+
}
21+
22+
23+
CLinuxCountingSem::~CLinuxCountingSem()
24+
{
25+
26+
27+
}
28+
29+
30+
bool CLinuxCountingSem::Get(Mode mode , unsigned long )
31+
{
32+
33+
if(sem_wait(&sem)==0)
34+
return true;
35+
else
36+
return false;
37+
38+
39+
}
40+
41+
42+
bool CLinuxCountingSem::Post()
43+
{
44+
sem_post(&sem);
45+
return true;
46+
47+
}
48+

CLinuxCountingSem.h

+60
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
2+
3+
4+
#ifndef _CLinuxCountingSem_h_
5+
#define _CLinuxCountingSem_h_
6+
7+
/********************************************************************************************
8+
*
9+
*File name: CLinuxCountingSem.h
10+
*Author: Wu Yinghao
11+
*Version: 0.0.1
12+
*Date: 2013.5.20
13+
14+
*Description: this file is linux sem class which inheritance from CCountingSem
15+
*
16+
********************************************************************************************/
17+
18+
#include "CCountingSem.h"
19+
20+
21+
#include <sys/time.h>
22+
#include <sys/types.h>
23+
#include <pthread.h>
24+
#include <semaphore.h>
25+
#include <time.h>
26+
#include <unistd.h>
27+
#include <errno.h>
28+
29+
30+
class CLinuxCountingSem:public CCountingSem
31+
{
32+
public:
33+
CLinuxCountingSem(unsigned int init_sem_count);
34+
~CLinuxCountingSem();
35+
36+
37+
virtual bool Get(Mode mode = kForever, unsigned long timeoutMS = 0) ;
38+
39+
virtual bool Post(void) ;
40+
41+
42+
43+
44+
private:
45+
46+
sem_t sem;
47+
48+
49+
};
50+
51+
52+
53+
54+
55+
56+
57+
#endif
58+
59+
60+

CLinuxMsgQueue.cpp

+88
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
2+
3+
#include "CLinuxMsgQueue.h"
4+
#include "COperatingSystemFactory.h"
5+
6+
7+
CLinuxMsgQueue::CLinuxMsgQueue(const char *pName):
8+
CMsgQueue(pName)
9+
{
10+
11+
p_mutex=COperatingSystemFactory::newMutex("Msg Mutex");
12+
p_sem=COperatingSystemFactory::newCountingSem(0);
13+
14+
15+
}
16+
17+
18+
19+
CLinuxMsgQueue::~CLinuxMsgQueue()
20+
{
21+
22+
}
23+
24+
25+
26+
27+
28+
bool CLinuxMsgQueue::recvMsg(unsigned int &m_msg_code,void *&p_msg)
29+
{
30+
31+
bool result;
32+
Elements queue_element;
33+
34+
p_sem->Get();
35+
36+
p_mutex->Lock();
37+
38+
if (m_queue.empty()) {
39+
40+
41+
return false;
42+
43+
}
44+
45+
46+
queue_element = m_queue.front();
47+
m_queue.pop_front();
48+
49+
50+
m_msg_code = queue_element.msg_code;
51+
p_msg = queue_element.p_message;
52+
53+
p_mutex->UnLock();
54+
55+
return true;
56+
57+
58+
}
59+
60+
61+
62+
63+
bool CLinuxMsgQueue::sendMsg(unsigned int m_msg_code,void *p_msg)
64+
{
65+
bool result;
66+
Elements queue_element;
67+
68+
69+
70+
queue_element.msg_code=m_msg_code;
71+
queue_element.p_message=p_msg;
72+
73+
p_mutex->Lock();
74+
75+
m_queue.push_back(queue_element);
76+
77+
78+
p_mutex->UnLock();
79+
80+
p_sem->Post();
81+
82+
83+
return true;
84+
85+
86+
}
87+
88+

CLinuxMsgQueue.h

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
2+
3+
#ifndef _CLinuxMsg_h_
4+
#define _CLinuxMsg_h_
5+
#include <deque>
6+
#include "CMsgQueue.h"
7+
#include "CMutex.h"
8+
#include "CCountingSem.h"
9+
10+
11+
class CLinuxMsgQueue : public CMsgQueue
12+
{
13+
public:
14+
CLinuxMsgQueue(const char *pName=NULL);
15+
~CLinuxMsgQueue();
16+
17+
virtual bool recvMsg(unsigned int &m_msg_code,void *&p_msg);
18+
virtual bool sendMsg(unsigned int m_msg_code,void *p_msg);
19+
20+
private:
21+
22+
std::deque<Elements> m_queue;
23+
24+
25+
CMutex *p_mutex;
26+
CCountingSem *p_sem;
27+
28+
};
29+
30+
31+
32+
33+
34+
35+
36+
37+
38+
39+
#endif
40+
41+
42+

CLinuxMutex.cpp

+85
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
2+
3+
4+
5+
#include "CLinuxMutex.h"
6+
7+
8+
9+
10+
11+
CLinuxMutex::CLinuxMutex(const char *pName):
12+
CMutex(pName)
13+
{
14+
15+
m_thread = (pthread_t) 0;
16+
17+
pthread_mutexattr_init(&m_mtex_attr);
18+
if (pthread_mutexattr_settype(&m_mtex_attr, PTHREAD_MUTEX_ERRORCHECK_NP))
19+
printf("CLinuxMutex::CnlLinuxMutex() : Failed to set mutex attibutes\n");
20+
21+
22+
pthread_mutex_init(&m_mutex, &m_mtex_attr);
23+
24+
25+
26+
}
27+
28+
29+
CLinuxMutex::~CLinuxMutex()
30+
{
31+
32+
}
33+
34+
bool CLinuxMutex::Lock()
35+
{
36+
37+
bool locked = false;
38+
int result;
39+
40+
41+
result = pthread_mutex_lock(&m_mutex);
42+
43+
44+
switch (result) {
45+
case 0:
46+
m_thread = pthread_self();
47+
48+
locked = true;
49+
break;
50+
case EDEADLK:
51+
locked = true;
52+
break;
53+
default:
54+
break;
55+
}
56+
57+
return locked;
58+
59+
}
60+
61+
bool CLinuxMutex::UnLock()
62+
{
63+
int result;
64+
bool unlocked = true;
65+
66+
if (m_thread!= pthread_self()) {
67+
unlocked = false;
68+
printf("CnlLinuxMutex::Unlock() : Error - Mutex not locked or not owned by the thread! \n");
69+
70+
} else {
71+
result = pthread_mutex_unlock(&m_mutex);
72+
if(result<0)
73+
unlocked=false;
74+
}
75+
return unlocked;
76+
77+
}
78+
79+
80+
81+
82+
83+
84+
85+

0 commit comments

Comments
 (0)