FreeSWITCH - mod_xml_rpc 소스 분석 6threadwindows.c
3515 단어 freeswitchabyss
라인 struct abyss_thread {
HANDLE handle;
void * userHandle;
TThreadProc * func;
TThreadDoneFn * threadDone;
};
typedef struct abyss_thread TThread;
TThread는 라이브러리 내부의 라인에 대한 봉인입니다.ThreadCreate 함수의 역할은 TThread 구조체 변수를 만들고 라인을 시작하는 것입니다.TThread 구조체 변수는 루트 주 함수에 제공됩니다.사용자 코드는 사용자 정의 데이터를 userHandle에 저장할 수 있도록 라인에 제공하려고 합니다.사용자 코드의 스레드 주 함수는 TThreadProc 유형이어야 합니다.또한 사용자 코드는 TThreadDoneFn 형식의 스레드 완성 처리 함수를 제공할 수 있습니다.useSigchld 매개 변수는 윈도우즈 플랫폼에서 의미가 없기 때문에 사용하지 않았을 수도 있습니다.CREATE 를 사용했으므로 스레드가 생성된 후 즉시 시작되지 않습니다.SUSPENDED 매개변수.루틴이 생성된 후 핸들은 TThread 변수의handle 속성에 저장됩니다.void
ThreadCreate(TThread ** const threadPP,
void * const userHandle,
TThreadProc * const func,
TThreadDoneFn * const threadDone,
bool const useSigchld,
const char ** const errorP) {
TThread * threadP;
MALLOCVAR(threadP);
if (threadP == NULL)
xmlrpc_asprintf(errorP,
"Can't allocate memory for thread descriptor.");
else {
DWORD z;
threadP->userHandle = userHandle;
threadP->func = func;
threadP->threadDone = threadDone;
threadP->handle = (HANDLE)_beginthreadex(NULL,
THREAD_STACK_SIZE,
threadRun,
threadP,
CREATE_SUSPENDED,
&z);
if (threadP->handle == NULL)
xmlrpc_asprintf(errorP, "_beginthreadex() failed.");
else {
*errorP = NULL;
*threadPP = threadP;
}
if (*errorP)
free(threadP);
}
}
static uint32_t WINAPI
threadRun(void * const arg) {
struct abyss_thread * const threadP = arg;
threadP->func(threadP->userHandle);
threadP->threadDone(threadP->userHandle);
return 0;
}
TThread에 루틴 핸들이 저장되어 있기 때문에 다른 모든 루틴 봉인 함수는 TThread의handle 핸들을 대상으로 조작됩니다.
Mutex struct abyss_mutex {
HANDLE winMutex;
};
typedef struct abyss_mutex TMutex;
abyss_mutex는 윈도우즈 플랫폼 아래에 문맥만 봉인되어 있습니다.MutexCreate 함수 처리 과정도 이 점을 설명한다.신청 공간이 TMutex를 만든 후 Create Mutex 함수를 호출하여 윈도우즈 플랫폼에서 mutex 대상을 만들고 성공한 후에 커서를 TMutex의win Mutex 속성에 부여합니다.다른 Mutex 함수는 스레드 함수와 마찬가지로 Mutex 핸들만 다룹니다.bool
MutexCreate(TMutex ** const mutexPP) {
TMutex * mutexP;
bool succeeded;
MALLOCVAR(mutexP);
if (mutexP) {
mutexP->winMutex = CreateMutex(NULL, FALSE, NULL);
succeeded = (mutexP->winMutex != NULL);
} else
succeeded = FALSE;
if (!succeeded)
free(mutexP);
*mutexPP = mutexP;
TraceMsg( "Created Mutex %s
", (succeeded ? "ok" : "FAILED") );
return succeeded;
}
struct abyss_thread {
HANDLE handle;
void * userHandle;
TThreadProc * func;
TThreadDoneFn * threadDone;
};
typedef struct abyss_thread TThread;
void
ThreadCreate(TThread ** const threadPP,
void * const userHandle,
TThreadProc * const func,
TThreadDoneFn * const threadDone,
bool const useSigchld,
const char ** const errorP) {
TThread * threadP;
MALLOCVAR(threadP);
if (threadP == NULL)
xmlrpc_asprintf(errorP,
"Can't allocate memory for thread descriptor.");
else {
DWORD z;
threadP->userHandle = userHandle;
threadP->func = func;
threadP->threadDone = threadDone;
threadP->handle = (HANDLE)_beginthreadex(NULL,
THREAD_STACK_SIZE,
threadRun,
threadP,
CREATE_SUSPENDED,
&z);
if (threadP->handle == NULL)
xmlrpc_asprintf(errorP, "_beginthreadex() failed.");
else {
*errorP = NULL;
*threadPP = threadP;
}
if (*errorP)
free(threadP);
}
}
static uint32_t WINAPI
threadRun(void * const arg) {
struct abyss_thread * const threadP = arg;
threadP->func(threadP->userHandle);
threadP->threadDone(threadP->userHandle);
return 0;
}
struct abyss_mutex {
HANDLE winMutex;
};
typedef struct abyss_mutex TMutex;
abyss_mutex는 윈도우즈 플랫폼 아래에 문맥만 봉인되어 있습니다.MutexCreate 함수 처리 과정도 이 점을 설명한다.신청 공간이 TMutex를 만든 후 Create Mutex 함수를 호출하여 윈도우즈 플랫폼에서 mutex 대상을 만들고 성공한 후에 커서를 TMutex의win Mutex 속성에 부여합니다.다른 Mutex 함수는 스레드 함수와 마찬가지로 Mutex 핸들만 다룹니다.
bool
MutexCreate(TMutex ** const mutexPP) {
TMutex * mutexP;
bool succeeded;
MALLOCVAR(mutexP);
if (mutexP) {
mutexP->winMutex = CreateMutex(NULL, FALSE, NULL);
succeeded = (mutexP->winMutex != NULL);
} else
succeeded = FALSE;
if (!succeeded)
free(mutexP);
*mutexPP = mutexP;
TraceMsg( "Created Mutex %s
", (succeeded ? "ok" : "FAILED") );
return succeeded;
}