Skip to content

Commit 344e2af

Browse files
committed
commit code first
0 parents  commit 344e2af

30 files changed

+1351
-0
lines changed

Diff for: .gitignore

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
.DS_Store
2+
.mvn
3+
*.iml
4+
.idea
5+
.vscode
6+
target
7+
build
8+
*.lock
9+
*.log

Diff for: Makefile

+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
#目标文件名
2+
TARGET:=libscm.so
3+
TARGET_TEST:=scm_test
4+
5+
CC:= g++
6+
7+
SRC=./src
8+
SRC_TEST=./test
9+
10+
#头文件路径
11+
EXPORT_DIR=./export
12+
INCLUDE_DIR=${SRC}/include
13+
14+
#输出路径
15+
BUILD_DIR=./build
16+
17+
#头文件路径包含
18+
INC_DIR=-I${EXPORT_DIR} -I${INCLUDE_DIR} -I${SRC}
19+
INC_DIR_TEST=${INC_DIR} -I${SRC_TEST} -I${SRC_TEST}
20+
21+
#编译的代码文件
22+
SOURCES=${SRC}/locks/*.cpp ${SRC}/concurrent/*.cpp ${SRC}/utils/*.cpp ${SRC}/memory/*.c
23+
SOURCES_TEST=${SRC_TEST}/*.cpp
24+
25+
#链接的库
26+
LIB=-lpthread
27+
LIB_TEST=-L${BUILD_DIR} -lscm
28+
29+
OPTIONS=-std=c++11 -Wall -g -fPIC
30+
31+
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:${BUILD_DIR}
32+
33+
all:
34+
@echo "start build..."
35+
@if [ ! -d $(BUILD_DIR) ]; then mkdir ${BUILD_DIR}; fi;
36+
@${CC} ${OPTIONS} ${INC_DIR} -shared ${LIB} -o ${BUILD_DIR}/${TARGET} ${SOURCES}
37+
@echo "build success!"
38+
39+
.PHONY:test
40+
test:
41+
@echo "start build the test source code..."
42+
@${CC} ${OPTIONS} ${INC_DIR_TEST} ${LIB_TEST} -o ${BUILD_DIR}/${TARGET_TEST} ${SOURCES_TEST}
43+
@echo "build test code success! start execute test..."
44+
@${BUILD_DIR}/${TARGET_TEST}
45+
@echo "test finished!"
46+
.PHONY:clean
47+
clean:
48+
@if [ -d $(BUILD_DIR) ]; then rm -r -f ${BUILD_DIR}; fi;
49+
@echo "cleaned up."
50+
51+

Diff for: README.md

+101
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
# simple c++ middleware
2+
3+
As it's name, it's a light simple middleware library for c++ developers without any third library, and only for <code>Linux</code> and <code>MacOS</code> based raw OS kernal clang API and asm.
4+
5+
* Memory pool
6+
* Lock based <code>AQS</code>
7+
* Blocking queue
8+
* Thread pool
9+
* High performance share memory management(todo).
10+
11+
## Backgroud
12+
When we want to develop a small system, properly need depend on some third big library even if we only use a litle part of it.
13+
14+
## Building and test
15+
16+
Clean the old target. Use <code>make clean</code> command:
17+
```
18+
[root@localhost simple-cpp-middleware]# make clean
19+
cleaned up.
20+
```
21+
22+
How to build it. Execute Makefile <code>make</code> command to build the library as:
23+
```
24+
[root@localhost simple-cpp-middleware]# make
25+
start build...
26+
build success!
27+
```
28+
29+
Test it use the <code>make test</code> command like:
30+
31+
```
32+
[root@localhost simple-cpp-middleware]# make test
33+
start build the test source code...
34+
build test code success! start execute test...
35+
test proccess is ready, start test>>>
36+
...
37+
...
38+
test result output
39+
...
40+
...
41+
test is over, press [Enter] to finish.
42+
43+
test finished!
44+
```
45+
And also you can use group command once like <code>make clean all test</code>.
46+
```
47+
[root@localhost simple-cpp-middleware]# make clean all test
48+
cleaned up.
49+
start build...
50+
build success!
51+
start build the test source code...
52+
build test code success! start execute test...
53+
test proccess is ready, start test>>>
54+
...
55+
...
56+
test result output
57+
...
58+
...
59+
test is over, press [Enter] to finish.
60+
61+
test finished!
62+
```
63+
64+
## API document
65+
### Memory pool include the memory_pool.h
66+
```
67+
file: demo.c
68+
69+
#include "memory_pool.h"
70+
71+
int main(int argc, char const *argv[]) {
72+
//At first alloc 10M space from system to init memory pool
73+
mem_init(1024 * 1024 * 10);
74+
75+
//Malloc some memory from memory pool
76+
void * buffer = mem_malloc(1024);
77+
78+
//Use the memory do something
79+
const char* data = "some data...";
80+
memcpy(buffer, data, strlen(data));
81+
82+
//At last release the memory
83+
mem_free(buffer);
84+
85+
return 0;
86+
}
87+
```
88+
### Lock and thread usage
89+
```
90+
#include "ReentrantLock.h"
91+
#include "Thread.h"
92+
93+
int main(int argc, char const *argv[]) {
94+
//At first init a Lock instance.
95+
ReentrantLock lock;
96+
97+
//Ant start a thread
98+
99+
return 0;
100+
}
101+
```

Diff for: export/Atomic.h

+65
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
2+
3+
#ifndef __SCM_ATOMIC_H__
4+
#define __SCM_ATOMIC_H__
5+
6+
#include "scm.h"
7+
8+
namespace SCM {
9+
10+
//Atomic Virtual
11+
template<typename T>
12+
struct Atomic {
13+
virtual void set(const T& t) = 0;
14+
15+
virtual T get() = 0;
16+
};
17+
18+
class AtomicByte: public Atomic<unsigned char> {
19+
public:
20+
virtual void set(const unsigned char& t);
21+
22+
virtual unsigned char get();
23+
};
24+
25+
//INT16 Atomic
26+
class AtomicShort: public Atomic<short> {
27+
public:
28+
virtual void set(const short& t);
29+
30+
virtual short get();
31+
32+
short incAndGet();
33+
34+
short getAndInc();
35+
};
36+
37+
//INT32 Atomic
38+
class AtomicInt: public Atomic<int> {
39+
40+
public:
41+
virtual void set(const int& t);
42+
43+
virtual int get();
44+
45+
int incAndGet();
46+
47+
int getAndInc();
48+
};
49+
50+
//INT64 Atomic
51+
class AtomicInt64: public Atomic<SCM_INT64> {
52+
public:
53+
virtual void set(const SCM_INT64& t);
54+
55+
virtual SCM_INT64 get();
56+
57+
SCM_INT64 incAndGet();
58+
59+
SCM_INT64 getAndInc();
60+
};
61+
62+
};
63+
64+
65+
#endif //__SCM_ATOMIC_H__

Diff for: export/BlockingQueue.h

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
2+
3+
#ifndef _SCM_BLOCKING_QUEUE_H
4+
#define _SCM_BLOCKING_QUEUE_H
5+
6+
#include "scm.h"
7+
8+
namespace SCM {
9+
template<typename T>
10+
struct BlockingQueue {
11+
virtual bool add(const T& e) = 0;
12+
13+
virtual bool offer(const T& e) = 0;
14+
15+
virtual void put(const T& e) = 0;
16+
17+
virtual bool offer(const T& e, const SCM_INT64& timeout) = 0;
18+
19+
virtual T take() = 0;
20+
21+
virtual bool poll(T& d, const SCM_INT64& timeout) = 0;
22+
};
23+
}
24+
25+
#endif

Diff for: export/LinkedBlockingQueue.h

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
2+
3+
#ifndef _SCM_LINKED_BLOCKING_QUEUE_H
4+
#define _SCM_LINKED_BLOCKING_QUEUE_H
5+
6+
#include "BlockingQueue.h"
7+
#include <list>
8+
9+
namespace SCM {
10+
11+
class Lock;
12+
class Condition;
13+
14+
template<typename T>
15+
class LinkedBlockingQueue : public BlockingQueue<T> {
16+
private:
17+
int _capacity;
18+
Lock * _lock;
19+
std::list<T> _queue;
20+
Condition* _not_empty_cond;
21+
Condition* _not_full_cond;
22+
bool _not_empty_wait;
23+
bool _not_full_wait;
24+
private:
25+
bool is_full() const {return _queue.size() >= _capacity;}
26+
bool is_empty() const {return _queue.empty();}
27+
public:
28+
LinkedBlockingQueue(int capacity);
29+
virtual ~LinkedBlockingQueue();
30+
31+
virtual bool add(const T& e) override;
32+
33+
virtual bool offer(const T& e) override;
34+
35+
virtual void put(const T& e) override;
36+
37+
virtual bool offer(const T& e, const SCM_INT64& timeout) override;
38+
39+
virtual T take() override;
40+
41+
virtual bool poll(T& d, const SCM_INT64& timeout) override;
42+
};
43+
}
44+
45+
#endif

Diff for: export/Lock.h

+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/**
2+
* A lock is a tool for controlling access to a shared resource by multiple threads.
3+
* @author majianzheng
4+
*/
5+
6+
#ifndef _SCM_LOCK_H
7+
#define _SCM_LOCK_H
8+
9+
#include "scm.h"
10+
11+
namespace SCM {
12+
struct Condition {
13+
virtual void await() = 0;
14+
15+
virtual bool await(SCM_INT64 time) = 0;
16+
17+
virtual void signal() = 0;
18+
19+
virtual void signalAll() = 0;
20+
};
21+
22+
23+
struct Lock {
24+
virtual void lock() = 0;
25+
26+
virtual bool tryLock(const SCM_INT64& time) = 0;
27+
28+
virtual void unlock() = 0;
29+
30+
virtual Condition* newCondition() = 0;
31+
};
32+
33+
class ScopeLock {
34+
private:
35+
Lock* _lock;
36+
public:
37+
ScopeLock(Lock* lock) {
38+
_lock = lock;
39+
_lock->lock();
40+
}
41+
~ScopeLock() {
42+
_lock->unlock();
43+
}
44+
};
45+
}
46+
//区域锁
47+
#define SCOPE_LOCK(lock_ptr) SCM::ScopeLock scope_lock(lock_ptr)
48+
49+
#endif

Diff for: export/ReentrantLock.h

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/**
2+
* A lock is a tool for controlling access to a shared resource by multiple threads.
3+
* @author majianzheng
4+
*/
5+
6+
#ifndef _SCM_REENTRANT_LOCK_H
7+
#define _SCM_REENTRANT_LOCK_H
8+
9+
#include "Lock.h"
10+
11+
namespace SCM
12+
{
13+
class ReentrantLock : public Lock {
14+
15+
public:
16+
virtual void lock() override;
17+
18+
virtual bool tryLock(const SCM_INT64 &time) override;
19+
20+
virtual void unlock() override;
21+
22+
virtual Condition *newCondition() override;
23+
};
24+
} // namespace SCM
25+
26+
27+
28+
#endif

0 commit comments

Comments
 (0)