提交 97da4435 编写于 作者: Y yangguangzhao

add timeout mechanism when MakeRemoteBinder

Signed-off-by: Nyangguangzhao <yangguangzhao1@huawei.com>
上级 c1970b93
......@@ -27,8 +27,6 @@
#include "rpc_errno.h"
#include "rpc_log.h"
#define USECTONSEC 1000
static RpcSkeleton g_rpcSkeleton = {
.lock = PTHREAD_MUTEX_INITIALIZER,
.isServerCreated = -1
......
......@@ -35,6 +35,7 @@ extern "C" {
#define GET_SYSTEM_ABILITY_TRANSACTION 1
#define ID_DIGITS 10
#define DEFAULT_SEND_WAIT_TIME 4
#define USECTONSEC 1000
enum DBinderCode {
MESSAGE_AS_INVOKER = 1,
......
......@@ -20,6 +20,7 @@
#include <stdbool.h>
#include <stdlib.h>
#include <pthread.h>
#include <sys/time.h>
#include "utils_list.h"
#include "securec.h"
......@@ -271,37 +272,56 @@ static void DetachThreadLockInfo(ThreadLockInfo *threadLockInfo)
pthread_mutex_unlock(&g_threadLockInfoList.mutex);
}
static int32_t InvokerRemoteDBinder(DBinderServiceStub *dBinderServiceStub, uint32_t seqNumber)
static ThreadLockInfo *NewThreadLock(void)
{
if (dBinderServiceStub == NULL) {
RPC_LOG_ERROR("InvokerRemoteDBinder dBinderServiceStub is NULL");
return ERR_FAILED;
}
int32_t ret = ERR_FAILED;
ThreadLockInfo *threadLockInfo = (ThreadLockInfo *)malloc(sizeof(ThreadLockInfo));
if (threadLockInfo == NULL) {
RPC_LOG_ERROR("threadLockInfo malloc failed");
return ERR_FAILED;
return NULL;
}
if (pthread_mutex_init(&threadLockInfo->mutex, NULL) != 0) {
RPC_LOG_ERROR("threadLockInfo mutex init failed");
free(threadLockInfo);
return ERR_FAILED;
return NULL;
}
if (pthread_cond_init(&threadLockInfo->condition, NULL) != 0) {
RPC_LOG_ERROR("threadLockInfo condition init failed");
pthread_mutex_destroy(&threadLockInfo->mutex);
free(threadLockInfo);
return NULL;
}
return threadLockInfo;
}
static int32_t GetWaitTime(struct timespec *waitTime)
{
struct timeval now;
if (gettimeofday(&now, NULL) != 0) {
RPC_LOG_ERROR("gettimeofday failed");
return ERR_FAILED;
}
waitTime->tv_sec = now.tv_sec + DEFAULT_SEND_WAIT_TIME;
waitTime->tv_nsec = now.tv_usec * USECTONSEC;
return ERR_NONE;
}
static int32_t InvokerRemoteDBinder(DBinderServiceStub *dBinderServiceStub, uint32_t seqNumber)
{
if (dBinderServiceStub == NULL) {
RPC_LOG_ERROR("InvokerRemoteDBinder dBinderServiceStub is NULL");
return ERR_FAILED;
}
int32_t ret = ERR_FAILED;
ThreadLockInfo *threadLockInfo = NewThreadLock();
if (threadLockInfo == NULL) {
return ret;
}
threadLockInfo->seqNumber = seqNumber;
ret = AttachThreadLockInfo(threadLockInfo);
if (ret != ERR_NONE) {
RPC_LOG_ERROR("AttachThreadLockInfo failed");
pthread_mutex_destroy(&threadLockInfo->mutex);
pthread_cond_destroy(&threadLockInfo->condition);
free(threadLockInfo);
return ret;
}
......@@ -311,7 +331,23 @@ static int32_t InvokerRemoteDBinder(DBinderServiceStub *dBinderServiceStub, uint
if (ret != ERR_NONE) {
RPC_LOG_ERROR("send entry to remote dbinderService failed");
} else {
pthread_cond_wait(&threadLockInfo->condition, &threadLockInfo->mutex);
struct timespec waitTime;
ret = GetWaitTime(&waitTime);
if (ret != ERR_NONE) {
DetachThreadLockInfo(threadLockInfo);
pthread_mutex_unlock(&threadLockInfo->mutex);
free(threadLockInfo);
return ERR_FAILED;
}
ret = pthread_cond_timedwait(&threadLockInfo->condition, &threadLockInfo->mutex, &waitTime);
if (ret == ETIMEDOUT) {
RPC_LOG_ERROR("InvokerRemoteDBinder wait for reply timeout");
DetachThreadLockInfo(threadLockInfo);
pthread_mutex_unlock(&threadLockInfo->mutex);
free(threadLockInfo);
return ERR_FAILED;
}
RPC_LOG_INFO("InvokerRemoteDBinder wakeup!");
}
......@@ -320,8 +356,8 @@ static int32_t InvokerRemoteDBinder(DBinderServiceStub *dBinderServiceStub, uint
ret = ERR_FAILED;
}
pthread_mutex_unlock(&threadLockInfo->mutex);
DetachThreadLockInfo(threadLockInfo);
pthread_mutex_unlock(&threadLockInfo->mutex);
free(threadLockInfo);
return ret;
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册