diff --git a/CMakeLists.txt b/CMakeLists.txt index 73544c5..12654c5 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -54,8 +54,6 @@ message( "copy net \n") file(GLOB NET ${PROJECT_SOURCE_DIR}/general/src/net/*.h) file(COPY ${NET} DESTINATION ${LIBRARY_OUTPUT_PATH}/inc/net/ FILE_PERMISSIONS OWNER_READ OWNER_WRITE GROUP_WRITE GROUP_READ WORLD_READ) - - message( "copy threadsafe library \n") file(GLOB THREADSAFECODE ${PROJECT_SOURCE_DIR}/general/src/threadsafe/*.hpp) message("threadsafe " ${THREADSAFECODE}) diff --git a/general/CMakeLists.txt b/general/CMakeLists.txt index ae5f886..505a4b0 100644 --- a/general/CMakeLists.txt +++ b/general/CMakeLists.txt @@ -18,6 +18,7 @@ elseif ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "Intel") # using Intel C++ elseif ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "MSVC") # using Visual Studio C++ + add_compile_options(/D_CRT_SECURE_NO_WARNINGS_CRT_SECURE_NO_WARNINGS /D_CRT_SECURE_NO_WARNINGS) endif() SET(LIBRARY_OUTPUT_PATH ${PROJECT_SOURCE_DIR}/obj) @@ -27,6 +28,7 @@ INCLUDE_DIRECTORIES (./) INCLUDE_DIRECTORIES (inc) INCLUDE_DIRECTORIES (encrypt) INCLUDE_DIRECTORIES (pattern) + include_directories(third/include) aux_source_directory(src DIRSRCS) aux_source_directory(src/pattern PaternSrc) diff --git a/general/inc/loger.h b/general/inc/loger.h index 0915ca3..dbf78e1 100644 --- a/general/inc/loger.h +++ b/general/inc/loger.h @@ -12,33 +12,30 @@ #include using namespace std; +typedef enum Mode{ + Mode_Daily, // 每天保存一次日志 + MODE_Monthly, // 每个月保存一次日志 + MODE_Weekly // 每周保存一次日志 +}ESaveMode; -namespace Loger{ +class Loger { +private: + string mCurrentPath; + FILE *mFile; // 日志文件 + ESaveMode mMode; // 工作模式 + string mCurrentDate; // 当天 + int error; - typedef enum Mode{ - Mode_Daily, // 每天保存一次日志 - MODE_Monthly, // 每个月保存一次日志 - MODE_Weekly // 每周保存一次日志 - }ESaveMode; - typedef class _C_Loger { - private: - string mCurrentPath; - FILE *mFile; // 日志文件 - ESaveMode mMode; // 工作模式 - string mCurrentDate; // 当天 - int error; - - public: - _C_Loger(FILE *p); - _C_Loger(string path); - #define DEBUG_FILE_POSITION __FILE__,__LINE__ - int Debug(string,string,int); - int Log(); - int LogFile(); - void operator+(const string&); - void operator<<(const string&); - }Loger; -} +public: + Loger(FILE *p); + Loger(string path); + #define DEBUG_FILE_POSITION __FILE__,__LINE__ + int Debug(string,string,int); + int Log(); + int LogFile(); + void operator+(const string&); + void operator<<(const string&); +}Loger; #endif //CPP11FEATURETEST_LOGER_H diff --git a/general/src/algorithm/ringbuffer.hpp b/general/src/algorithm/ringbuffer.hpp deleted file mode 100644 index 5df8b3a..0000000 --- a/general/src/algorithm/ringbuffer.hpp +++ /dev/null @@ -1,94 +0,0 @@ -#include -#include -using namespace std; - -template -class RingBuffer{ -public: - enum Error{ - Error = -10, - ERROR_FULL = -11, - ERROR_NO_ENOUGH_WRITE = -12, - }; - RingBuffer(uint16_t len) { - mBuffer = new T[len]; - mHead = mTail = mBuffer; - if (mBuffer == nullptr){ - this->mLen = 0; - } - mLen = len; - }; - uint16_t ReadableCnt(){ - if (mHead == mTail) return 0; - if (mHead < mTail) - return mTail - mHead; - return mLen - (mHead - mTail); - } - uint16_t WriteableCnt(){ - return mLen - this->ReadableCnt(); - } - int16_t Read(T *data, uint16_t count) - { - //assert(rb != NULL); - //assert(data != NULL); - if (mHead< mTail) // 头部小于尾部,正常情况下 - { - int copy_sz = min(count, this->ReadableCnt()); - memcpy(data, mHead, copy_sz * sizeof(T)); - mHead += copy_sz; - return copy_sz; - } - else // 头部大于尾部 - { - if (count < mLen -(mHead - mBuffer)) // (rb->rb_head - rb->rb_buff) 是从头部到数组物理结束的长度,如果小于说明不需要读取数组头部的,只需要copy一次 - { - int copy_sz = count*sizeof(T); - memcpy(data, mHead, copy_sz*sizeof(T)); - mHead += copy_sz; - return copy_sz; - } - else - { -//反之需要copy两次,因为数组本身不是有环的 - int copy_sz = mLen - (mHead - mBuffer); - memcpy(data, mHead, copy_sz * sizeof(T)); - mHead = mBuffer; - copy_sz += Read( (T*)data + copy_sz*sizeof(T), count - copy_sz); - return copy_sz; - } - } - } - int16_t Write(const T *in,uint16_t len){ - if (len >= WriteableCnt()) - return ERROR_NO_ENOUGH_WRITE; - if (mHead <= mTail) // 头部小于尾部 - { - int tail_avail_sz = mLen - (mTail - this->mBuffer); - if (len <= tail_avail_sz) // 是否需要循环到0开始写 - { - memcpy(mTail, in, len*sizeof(T)); - mTail += len; - if (mTail == mBuffer + mLen) // 刚好相等的情况 - mTail = mBuffer; - return len; - } - else - { - memcpy(mTail, in, tail_avail_sz*sizeof(T)); - mTail = mBuffer; - return tail_avail_sz + Write( (T*)in + tail_avail_sz* sizeof(T), (len - tail_avail_sz)); - } - } - else - { - memcpy(mTail, in, len*sizeof(T)); // 头部大于尾部 - mTail += len; - return len; - } - } -private: - T *mBuffer; - T* mHead ; - T* mTail; - uint16_t mLen; -}; \ No newline at end of file diff --git a/general/src/debug.cpp b/general/src/debug.cpp index 9d78901..79082d2 100644 --- a/general/src/debug.cpp +++ b/general/src/debug.cpp @@ -18,6 +18,7 @@ void itoa(int i,char* string) *string='\0'; } #endif + int PrintDumpObjvoid (void *dst,int rowNum,int num,bool ifAsii){ char out [2048] = {0}; int row = num / rowNum; diff --git a/general/src/function/daemon b/general/src/function/daemon deleted file mode 100644 index 8fcb304..0000000 Binary files a/general/src/function/daemon and /dev/null differ diff --git a/general/src/function/daemon.cpp b/general/src/function/daemon.cpp index b557af9..defd2cb 100644 --- a/general/src/function/daemon.cpp +++ b/general/src/function/daemon.cpp @@ -258,31 +258,20 @@ static BOOL KillProcessByName(const TCHAR *lpszProcessName) while (flag != 0) { - printf("kill process find %s\r\n", processInfo.szExeFile); if (strcmp(processInfo.szExeFile, lpszProcessName) == 0) - { - // Terminate the process. - pid = processInfo.th32ProcessID; - HANDLE hProcess = OpenProcess(PROCESS_ALL_ACCESS, TRUE, pid); - printf("kill process pid %d\r\n", pid); - if (TerminateProcess(hProcess, 0) != TRUE) - { // Failed to terminate it. - retval = FALSE; - break; } } - flag = Process32Next(hSnapshot, &processInfo); } // while (flag != 0) @@ -552,84 +541,52 @@ int DaemonizeMonitor::StartMonitor() for (auto itr = m_running_pid.begin(); itr != m_running_pid.end(); itr++) { - bool found = false; - if (EnumProcesses(Proc_pid, sizeof(Proc_pid), &Retn_bytes)) - { - Proc_count = Retn_bytes / sizeof(DWORD); - SetProcessPrivilege("SeDebugPrivilege", 1); - for (i = 0; i < Proc_count; i++) - { - hProcess = OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, FALSE, Proc_pid[i]); - if (hProcess != NULL) - { - EnumProcessModules(hProcess, hMod, sizeof(hMod), &Retn_bytes2); - GetModuleFileNameEx(hProcess, hMod[0], szModName, sizeof(szModName)); - // printf("PID=%d Path=%s\n", Proc_pid[i], szModName); - if (itr->second == Proc_pid[i]) { - found = true; - break; } } - CloseHandle(hProcess); } - SetProcessPrivilege("SeDebugPrivilege", 0); if (!found) { - // 没找到该应用实例就重启应 - auto it = m_path_process.begin(); - while (it != m_path_process.end()) - { - if (*it == itr->first) - { - it = m_path_process.erase(it); } - else - it++; } - for (auto it = m_path_process.begin(); it != m_path_process.end(); it++) { - std::cout << *it << std::endl; } - this->AddNewProcess(itr->first); } } } } - return 0; - #endif #ifdef linux @@ -673,53 +630,36 @@ int DaemonizeMonitor::StartMonitor() } } } - #endif } int DaemonizeMonitor::StopMonitor(string) { - #ifdef _WIN32 - return 0; - #endif } int DaemonizeMonitor::StopProcess(string path) { - if (path == "") { - return -1; } - #ifdef _WIN32 if (m_running_pid.find(path) != m_running_pid.end()) { - DWORD pid = m_running_pid.at(path); - m_running_pid.erase(path); - if (KillProcess(pid)) - return 0; - else { - return -1; } } - return 0; - #endif - #ifdef linux - #endif } \ No newline at end of file diff --git a/general/src/function/threadpool.cpp b/general/src/function/threadpool.cpp new file mode 100644 index 0000000..e0a3970 --- /dev/null +++ b/general/src/function/threadpool.cpp @@ -0,0 +1,129 @@ +#include "threadpool.h" + +unsigned int CoreCount() +{ + unsigned count = 1; // 至少一个 + #if defined (LINUX) + count = sysconf(_SC_NPROCESSORS_CONF); + #elif defined (WINDOWS) + SYSTEM_INFO si; + GetSystemInfo(&si); + count = si.dwNumberOfProcessors; + #endif + return count; +} + +namespace general{ + CThreadPool::CThreadPool(int num) { + this->mThreadCnt = num; + mStarted = false; + + } + + int CThreadPool::AddTask(Task *t) { + if ( nullptr == t){ + return -1; + } + if(!mStoping){ + { + std::unique_lock lck(this->mMutex); + this->mTasks.push(t); + } + mQueueAvaliableCondition.notify_one(); + return 0; + }else{ + return -2; + } + } + + void CThreadPool::Start() { + mStarted = true; + mThreads.reserve(this->mThreadCnt); + for (int i = 0; i < mThreadCnt; i++) + { + mThreads.emplace_back(new std::thread(&ThreadPool::Process, this, i)); + } + } + + Task *CThreadPool::PopTask() { + std::unique_lock lk(this->mMutex); + while (mTasks.empty() && mStarted) + { + mQueueAvaliableCondition.wait(lk); + } + if(!mStarted){ + return nullptr; + } + Task *ret = this->mTasks.front(); + this->mTasks.pop(); + return ret; + } + + void CThreadPool::Process(int id) { + while (mStarted) + { + Task *task = PopTask(); + if (nullptr == task) + { + continue; + } + else + { + task->Run(); + task->OnDone(); + } + } + } + + void CThreadPool::Stop() { + { + std::lock_guard lk(mMutex); + mStarted = false; + mQueueAvaliableCondition.notify_all(); + } + + for (auto &th : mThreads) + { + th->join(); + } + mStarted = false; + } + void CThreadPool::StopAll() { + { + while(this->mTasks.size() > 0) + std::this_thread::sleep_for(std::chrono::milliseconds(100)); + mStarted = false; + mQueueAvaliableCondition.notify_all(); + } + + for (auto &th : mThreads) + { + th->join(); + } + mStarted = false; + } + bool CThreadPool::Started(){ + return this->mStarted; + } + CThreadPool::~CThreadPool(){ + if (this->mStarted) + { + std::cout << "desruction" << std::endl; + this->mStarted = false; + { + std::lock_guard lk(mMutex); + mStarted = false; + mQueueAvaliableCondition.notify_all(); + } + for (size_t i = 0; i != this->mThreads.size(); ++i) + { + if (mThreads[i]->joinable()) + { + mThreads[i]->join(); + } + } + } + } +} + + diff --git a/general/src/function/threadpool.h b/general/src/function/threadpool.h new file mode 100644 index 0000000..06f41d2 --- /dev/null +++ b/general/src/function/threadpool.h @@ -0,0 +1,96 @@ +/* + * @Author: your name + * @Date: 2021-10-09 10:03:45 + * @LastEditTime: 2021-11-25 15:26:02 + * @LastEditors: Please set LastEditors + * @Description: 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE + * @FilePath: \cpp11\threadpool.h + */ +#pragma once +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include "threadpool.h" + +extern "C"{ + #if !defined (_WIN32) && !defined (_WIN64) + #define LINUX + #include + #else + #define WINDOWS + #include + #endif + unsigned int CoreCount(); +} + +namespace general{ + class CThreadPool; + enum PRIORITY { + MIN = 1, NORMAL = 25, MAX = 50 + }; + + class Task{ + public: + Task() { + } + void SetPriority(int priority) { + if (priority>(PRIORITY::MAX)) + { + priority = (PRIORITY::MAX); + } + else if (priority>(PRIORITY::MAX)) + { + priority = (PRIORITY::MIN); + } + } + virtual void Run() = 0; + virtual void OnDone() = 0; + protected: + int priority_; + }; + + class Worker{ + public: + // The required constructor for this example. + explicit Worker(int& evenCount) + : m_evenCount(evenCount) { } + + void operator()(CThreadPool *pool) const { + + } + private: + // Default assignment operator to silence warning C4512. + Worker& operator=(const Worker&); + int& m_evenCount; + }; + + typedef class CThreadPool { + public: + CThreadPool(int num); + ~CThreadPool(); + int AddTask(Task *); + void Start(); + void Stop(); + bool Started(); + void StopAll(); + + private: + CThreadPool(); + void Process(int id); + uint32_t mThreadCnt; + bool mStarted; + bool mStoping; + Task *PopTask(); + std::vector mThreads; + std::queue mTasks; + std::mutex mMutex; + std::condition_variable mQueueAvaliableCondition; + }ThreadPool; +} + diff --git a/general/src/loger.cpp b/general/src/loger.cpp index 520bc61..b582b61 100644 --- a/general/src/loger.cpp +++ b/general/src/loger.cpp @@ -3,6 +3,7 @@ // #include "loger.h" using namespace Loger; + string getTimeDate() { time_t timep; @@ -21,7 +22,7 @@ string getTime() return string(tmp); } -int _C_Loger::Debug(string dat,string function,int line){ +int Loger::Debug(string dat,string function,int line){ // 还没有过天 if(getTimeDate() == this->mCurrentDate){ string tmp = getTime(); @@ -51,7 +52,7 @@ int _C_Loger::Debug(string dat,string function,int line){ return 0; } -void _C_Loger::operator<<(const string& wb){ +void Loger::operator<<(const string& wb){ // 还没有过天 if(getTimeDate() == this->mCurrentDate){ string tmp = getTime(); @@ -67,7 +68,6 @@ void _C_Loger::operator<<(const string& wb){ if (this->mFile == nullptr){ this->error = true; } - string tmp = getTime(); tmp += ":"; tmp += wb; @@ -90,12 +90,13 @@ bool file_existed(string path) { return true; } } -_C_Loger::_C_Loger(FILE *p){ + +Loger::Loger(FILE *p){ this->mFile = p; this->mCurrentDate = getTime(); } -_C_Loger::_C_Loger(string path) { +Loger::Loger(string path) { this->mCurrentDate = getTimeDate(); this->mCurrentPath = path + this->mCurrentDate; this->mFile = fopen(this->mCurrentPath.c_str(),"a+"); diff --git a/general/src/pattern/ringbuffer.hpp b/general/src/pattern/ringbuffer.hpp index 446f754..1e508b4 100644 --- a/general/src/pattern/ringbuffer.hpp +++ b/general/src/pattern/ringbuffer.hpp @@ -175,8 +175,6 @@ int RingBuffer::Add(T *data,uint64_t len){ } - - template int RingBuffer::Take(T *data,uint64_t len){ if(data == nullptr) diff --git a/general/src/threadsafe/thread_safe_list.hpp b/general/src/threadsafe/thread_safe_list.hpp index 8b0d0fb..0fca7d3 100644 --- a/general/src/threadsafe/thread_safe_list.hpp +++ b/general/src/threadsafe/thread_safe_list.hpp @@ -118,14 +118,13 @@ public: return ret; } } - uint32_t Length() { return m_data.size(); } - + private: - std::mutex m_mux; + std::mutex m_mux; std::condition_variable m_cv; std::list m_data; }; diff --git a/general/src/threadsafe/threadsafe_queue.hpp b/general/src/threadsafe/threadsafe_queue.hpp index ca8914c..fb0b248 100644 --- a/general/src/threadsafe/threadsafe_queue.hpp +++ b/general/src/threadsafe/threadsafe_queue.hpp @@ -60,7 +60,7 @@ public: /* * 从队列中弹出一个元素,如果队列为空返回false * */ - bool try_pop(value_type& value){ + bool try_pop(value_type& value) { std::lock_guard lk(mut); if(data_queue.empty()) return false; @@ -71,14 +71,14 @@ public: /* * 返回队列是否为空 * */ - auto empty() const->decltype(data_queue.empty()) { + auto empty() const->decltype(data_queue.empty()) {s std::lock_guard lk(mut); return data_queue.empty(); } /* * 返回队列中元素数个 * */ - auto size() const->decltype(data_queue.size()){ + auto size() const->decltype(data_queue.size()) { std::lock_guard lk(mut); return data_queue.size(); } diff --git a/general/src/threadsafe/threadsafe_vector.hpp b/general/src/threadsafe/threadsafe_vector.hpp index 3cc9a7d..3f0f96d 100644 --- a/general/src/threadsafe/threadsafe_vector.hpp +++ b/general/src/threadsafe/threadsafe_vector.hpp @@ -11,16 +11,30 @@ #include template -class ThreadSafeVector{ +class ThreadSafeVector { public: typedef typename std::vector::iterator iterator; typedef typename std::vector::const_iterator const_iterator; - Val& operator [](uin32_t index) - { - + ThreadSafeVector(size_type n){ + this->m_data = new vector(n); + } + + const Val& operator [](uin32_t index) + { + std::unique_lock lck(m_mutex); + return *m_data[index]; + } + const Val& at(uin32_t index) + { + std::unique_lock lck(m_mutex); + return m_data->at(index); + } + iterator Insert(TYP dat,iterator pos){ + std::unique_lock lck(m_mutex); + *m_data.insert(pos,dat); } - private: - std::vector m_data; + std::vector *m_data; std::mutex m_mutex; -} \ No newline at end of file +} + diff --git a/general/src/utils.cpp b/general/src/utils.cpp index 5aac482..a516e61 100644 --- a/general/src/utils.cpp +++ b/general/src/utils.cpp @@ -1,20 +1,7 @@ -/* - * @Author: your name - * @Date: 2020-10-11 17:37:54 - * @LastEditTime: 2021-07-24 00:46:47 - * @LastEditors: Please set LastEditors - * @Description: In User Settings Edit - * @FilePath: \generallib\general\src\utils.cpp - */ -// -// Created by 29019 on 2019/5/2. -// #if __cplusplus >= 201103L #pragma message("编译器支持c++11 ") #endif // __cplusplus >= 201103L - #include "utils.h" - #ifdef linux int itoa(int n ,char * const s,int radix){ @@ -73,3 +60,5 @@ inline ENV_COMPILER CurrentEnvCompiler() #endif return UNKNOWN; } + + diff --git a/test/src/uv_test/test.cpp b/test/src/uv_test/test.cpp index 5a7fed2..65684f1 100644 --- a/test/src/uv_test/test.cpp +++ b/test/src/uv_test/test.cpp @@ -28,7 +28,6 @@ void async_cb(uv_async_t* handle) printf("async_cb called!\n"); printf("thread id:%lu.\n", id); - //uv_close((uv_handle_t*)&async, close_cb); //如果async没有关闭,消息队列是会阻塞的 } void async_cb2(uv_async_t* handle) @@ -36,7 +35,7 @@ void async_cb2(uv_async_t* handle) int id = GetCurrentThreadId(); printf("async_cb2 called!\n"); printf("thread id:%lu.\n", id); - //uv_close((uv_handle_t*)&async, close_cb); //如果async没有关闭,消息队列是会阻塞的 + }