Skip to content

Commit

Permalink
java synchronized
Browse files Browse the repository at this point in the history
  • Loading branch information
ithewei committed Aug 28, 2020
1 parent 4c85b7b commit 26033d7
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 0 deletions.
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ unittest: prepare
$(CC) -g -Wall -std=c99 -I. -Ibase -o bin/connect_test unittest/connect_test.c base/hsocket.c base/htime.c
$(CC) -g -Wall -std=c99 -I. -Ibase -o bin/socketpair_test unittest/socketpair_test.c base/hsocket.c
$(CXX) -g -Wall -std=c++11 -I. -Ibase -o bin/defer_test unittest/defer_test.cpp
$(CXX) -g -Wall -std=c++11 -I. -Ibase -o bin/synchronized_test unittest/synchronized_test.cpp -pthread
$(CXX) -g -Wall -std=c++11 -I. -Ibase -o bin/hstring_test unittest/hstring_test.cpp base/hstring.cpp
$(CXX) -g -Wall -std=c++11 -I. -Ibase -o bin/threadpool_test unittest/threadpool_test.cpp -pthread
$(CXX) -g -Wall -std=c++11 -I. -Ibase -o bin/objectpool_test unittest/objectpool_test.cpp -pthread
Expand Down
15 changes: 15 additions & 0 deletions base/hmutex.h
Original file line number Diff line number Diff line change
Expand Up @@ -224,12 +224,27 @@ class RWLock {

void wrlock() { hrwlock_wrlock(&_rwlock); }
void wrunlock() { hrwlock_wrunlock(&_rwlock); }

void lock() { rdlock(); }
void unlock() { rdunlock(); }
protected:
hrwlock_t _rwlock;
};

template<class T>
class LockGuard {
public:
LockGuard(T& t) : _lock(t) { _lock.lock(); }
~LockGuard() { _lock.unlock(); }
protected:
T& _lock;
};

END_NAMESPACE_HV

// same as java synchronized(lock) { ... }
#define synchronized(lock) for (std::lock_guard<std::mutex> _lock_(lock), *p = &_lock_; p != NULL; p = NULL)

#endif // __cplusplus

#endif // HV_MUTEX_H_
2 changes: 2 additions & 0 deletions docs/apis.md
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,8 @@
- `hv::MutexLock`
- `hv::SpinLock`
- `hv::RWLock`
- `hv::LockGuard`
- synchronized

### hsocket.h
- INVALID_SOCKET
Expand Down
5 changes: 5 additions & 0 deletions unittest/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ target_include_directories(socketpair_test PRIVATE .. ../base)
add_executable(defer_test defer_test.cpp)
target_include_directories(defer_test PRIVATE .. ../base)

add_executable(synchronized_test synchronized_test.cpp)
target_include_directories(synchronized_test PRIVATE .. ../base)
target_link_libraries(synchronized_test -lpthread)

add_executable(hstring_test hstring_test.cpp ../base/hstring.cpp)
target_include_directories(hstring_test PRIVATE .. ../base)

Expand Down Expand Up @@ -69,6 +73,7 @@ add_custom_target(unittest DEPENDS
connect_test
socketpair_test
defer_test
synchronized_test
hstring_test
threadpool_test
objectpool_test
Expand Down
24 changes: 24 additions & 0 deletions unittest/synchronized_test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#include "hthread.h"
#include "hmutex.h"

#define THREAD_NUM 10
std::mutex g_mutex;

HTHREAD_ROUTINE(test_synchronized) {
synchronized(g_mutex) {
hv_delay(1000);
printf("tid=%ld time=%llus\n", hv_gettid(), (unsigned long long)time(NULL));
}
return 0;
}

int main() {
hthread_t threads[THREAD_NUM];
for (int i = 0; i < THREAD_NUM; ++i) {
threads[i] = hthread_create(test_synchronized, NULL);
}
for (int i = 0; i < THREAD_NUM; ++i) {
hthread_join(threads[i]);
}
return 0;
}

0 comments on commit 26033d7

Please sign in to comment.