| | |
| | | #include <iostream> |
| | | #include <vector> |
| | | #include <list> |
| | | #include <thread> |
| | | #include <mutex> |
| | | #include <semaphore.h> |
| | | #include <unistd.h> |
| | | |
| | |
| | | static list<struct msg_2_main_t> MessageBuffer; |
| | | |
| | | static sem_t sem_msg_income; |
| | | static pthread_mutex_t msg_mutex = PTHREAD_MUTEX_INITIALIZER; |
| | | static std::mutex msg_mutex; |
| | | |
| | | static void SendMsgToMainProcIndep(int cmd, const char *value); |
| | | static void *SendMsgToMainProcThread(void *p); |
| | | static void SendMsgToMainProcThread(void); |
| | | |
| | | static void SendMsgToMainProcIndep(int cmd, const char *value) |
| | | { |
| | |
| | | msg.value.clear(); |
| | | } |
| | | |
| | | pthread_mutex_lock(&msg_mutex); |
| | | lock_guard<mutex> lock(msg_mutex); |
| | | |
| | | MessageBuffer.push_front(msg); |
| | | pthread_mutex_unlock(&msg_mutex); |
| | | |
| | | sem_post(&sem_msg_income); |
| | | } |
| | | |
| | | static void *SendMsgToMainProcThread(void *p) { |
| | | static void SendMsgToMainProcThread(void) { |
| | | while (true) { |
| | | sem_wait(&sem_msg_income); |
| | | |
| | |
| | | struct msg_2_main_t msg; |
| | | int success; |
| | | |
| | | pthread_mutex_lock(&msg_mutex); |
| | | unique_lock<mutex> lock(msg_mutex); |
| | | msg = MessageBuffer.back(); |
| | | pthread_mutex_unlock(&msg_mutex); |
| | | lock.unlock(); |
| | | |
| | | if (msg.value.length() > 0) |
| | | success = SendMsgToMainProc(msg.cmd, msg.value.c_str()); |
| | |
| | | success = SendMsgToMainProc(msg.cmd, NULL); |
| | | |
| | | if (success == 0) { |
| | | pthread_mutex_lock(&msg_mutex); |
| | | lock.lock(); |
| | | MessageBuffer.pop_back(); |
| | | pthread_mutex_unlock(&msg_mutex); |
| | | lock.unlock(); |
| | | } else { |
| | | // 延迟重发 |
| | | DEBUG("发送失败"); |
| | |
| | | sem_init(&sem_msg_income, 0, 0); |
| | | MessageBuffer.clear(); |
| | | |
| | | pthread_mutex_init(&msg_mutex, NULL); |
| | | |
| | | pthread_t pid; |
| | | pthread_attr_t attr; |
| | | pthread_attr_init(&attr); |
| | | pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED); |
| | | pthread_create(&pid, &attr, SendMsgToMainProcThread, NULL); |
| | | std::thread(SendMsgToMainProcThread).detach(); |
| | | } |
| | | |
| | | void MA_NdkStart(void) |
| | |
| | | { |
| | | switch (cmd) { |
| | | case ID_MS_FILE: { |
| | | UploadDfuFile(value, length); |
| | | ////////////////////UploadDfuFile(value, length); |
| | | break; |
| | | } |
| | | default: |