forked from chenshuo/muduo
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
19 changed files
with
300 additions
and
58 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,7 @@ | ||
set(base_SRCS | ||
UtcTime.cc) | ||
Thread.cc | ||
UtcTime.cc | ||
) | ||
|
||
add_library(muduo_base ${base_SRCS}) | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
#ifndef MUDUO_BASE_MUTEX_H | ||
#define MUDUO_BASE_MUTEX_H | ||
|
||
#include <pthread.h> | ||
|
||
#include <boost/noncopyable.hpp> | ||
|
||
namespace muduo | ||
{ | ||
|
||
class MutexLock : boost::noncopyable | ||
{ | ||
public: | ||
MutexLock() | ||
{ | ||
pthread_mutex_init(&mutex_, NULL); | ||
} | ||
|
||
~MutexLock() | ||
{ | ||
pthread_mutex_destroy(&mutex_); | ||
} | ||
|
||
void lock() | ||
{ | ||
pthread_mutex_lock(&mutex_); | ||
} | ||
|
||
void unlock() | ||
{ | ||
pthread_mutex_unlock(&mutex_); | ||
} | ||
|
||
pthread_mutex_t* getPthreadMutex() /* non-const */ | ||
{ | ||
return &mutex_; | ||
} | ||
|
||
private: | ||
|
||
pthread_mutex_t mutex_; | ||
}; | ||
|
||
class MutexLockGuard : boost::noncopyable | ||
{ | ||
public: | ||
explicit MutexLockGuard(MutexLock& mutex) : mutex_(mutex) | ||
{ | ||
mutex_.lock(); | ||
} | ||
|
||
~MutexLockGuard() | ||
{ | ||
mutex_.unlock(); | ||
} | ||
|
||
private: | ||
|
||
MutexLock& mutex_; | ||
}; | ||
|
||
} | ||
|
||
#define MutexLockGuard(x) error | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
#include <muduo/base/Thread.h> | ||
|
||
#include <unistd.h> | ||
#include <sys/syscall.h> | ||
#include <sys/types.h> | ||
#include <linux/unistd.h> | ||
|
||
namespace | ||
{ | ||
__thread pid_t t_tid = 0; | ||
|
||
pid_t gettid() | ||
{ | ||
return static_cast<pid_t>(::syscall(SYS_gettid)); | ||
} | ||
|
||
void* startThread(void* cb) | ||
{ | ||
muduo::Thread::ThreadFunc* func = static_cast<muduo::Thread::ThreadFunc*>(cb); | ||
t_tid = gettid(); | ||
(*func)(); | ||
return NULL; | ||
} | ||
} | ||
|
||
using namespace muduo; | ||
|
||
Thread::Thread(const ThreadFunc& func) | ||
: ptid_(0), | ||
func_(func) | ||
{ | ||
} | ||
|
||
Thread::~Thread() | ||
{ | ||
} | ||
|
||
void Thread::start() | ||
{ | ||
pthread_create(&ptid_, NULL, &startThread, &func_); | ||
} | ||
|
||
void Thread::join() | ||
{ | ||
pthread_join(ptid_, NULL); | ||
} | ||
|
||
pid_t CurrentThread::tid() | ||
{ | ||
if (t_tid == 0) { | ||
t_tid = gettid(); | ||
} | ||
return t_tid; | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
#ifndef MUDUO_BASE_THREAD_H | ||
#define MUDUO_BASE_THREAD_H | ||
|
||
#include <pthread.h> | ||
#include <boost/function.hpp> | ||
|
||
namespace muduo | ||
{ | ||
|
||
class Thread | ||
{ | ||
public: | ||
typedef boost::function<void ()> ThreadFunc; | ||
|
||
explicit Thread(const ThreadFunc&); | ||
~Thread(); | ||
|
||
void start(); | ||
void join(); | ||
|
||
private: | ||
|
||
pthread_t ptid_; | ||
ThreadFunc func_; | ||
}; | ||
|
||
namespace CurrentThread | ||
{ | ||
pid_t tid(); | ||
} | ||
|
||
} | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.