mirror of
https://github.com/morgan9e/FreeRDP
synced 2026-04-15 00:44:19 +09:00
libwinpr-synch: implemented semaphores + thread wait
This commit is contained in:
@@ -39,7 +39,11 @@ BOOL CloseHandle(HANDLE hObject)
|
||||
if (!winpr_Handle_GetInfo(hObject, &Type, &Object))
|
||||
return FALSE;
|
||||
|
||||
if (Type == HANDLE_TYPE_MUTEX)
|
||||
if (Type == HANDLE_TYPE_THREAD)
|
||||
{
|
||||
return TRUE;
|
||||
}
|
||||
else if (Type == HANDLE_TYPE_MUTEX)
|
||||
{
|
||||
pthread_mutex_destroy((pthread_mutex_t*) Object);
|
||||
winpr_Handle_Remove(Object);
|
||||
@@ -69,6 +73,18 @@ BOOL CloseHandle(HANDLE hObject)
|
||||
|
||||
return TRUE;
|
||||
}
|
||||
else if (Type == HANDLE_TYPE_SEMAPHORE)
|
||||
{
|
||||
#if defined __APPLE__
|
||||
semaphore_destroy(mach_task_self(), *((winpr_sem_t*) Object));
|
||||
#else
|
||||
sem_destroy((winpr_sem_t*) Object);
|
||||
#endif
|
||||
winpr_Handle_Remove(Object);
|
||||
free(Object);
|
||||
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
@@ -53,6 +53,11 @@ HANDLE CreateEventW(LPSECURITY_ATTRIBUTES lpEventAttributes, BOOL bManualReset,
|
||||
{
|
||||
event->bManualReset = bManualReset;
|
||||
|
||||
if (!event->bManualReset)
|
||||
{
|
||||
printf("CreateEventW: auto-reset events not yet implemented\n");
|
||||
}
|
||||
|
||||
event->pipe_fd[0] = -1;
|
||||
event->pipe_fd[1] = -1;
|
||||
|
||||
|
||||
@@ -35,39 +35,30 @@
|
||||
|
||||
#ifndef _WIN32
|
||||
|
||||
HANDLE CreateSemaphoreA(LPSECURITY_ATTRIBUTES lpSemaphoreAttributes, LONG lInitialCount, LONG lMaximumCount, LPCSTR lpName)
|
||||
{
|
||||
winpr_sem_t* hSemaphore;
|
||||
|
||||
hSemaphore = malloc(sizeof(winpr_sem_t));
|
||||
|
||||
#if defined __APPLE__
|
||||
semaphore_create(mach_task_self(), hSemaphore, SYNC_POLICY_FIFO, lMaximumCount);
|
||||
#else
|
||||
sem_init(hSemaphore, 0, lMaximumCount);
|
||||
#endif
|
||||
|
||||
return (HANDLE) hSemaphore;
|
||||
}
|
||||
|
||||
HANDLE CreateSemaphoreW(LPSECURITY_ATTRIBUTES lpSemaphoreAttributes, LONG lInitialCount, LONG lMaximumCount, LPCWSTR lpName)
|
||||
{
|
||||
winpr_sem_t* hSemaphore;
|
||||
HANDLE handle;
|
||||
winpr_sem_t* semaphore;
|
||||
|
||||
hSemaphore = malloc(sizeof(winpr_sem_t));
|
||||
semaphore = (winpr_sem_t*) malloc(sizeof(winpr_sem_t));
|
||||
|
||||
if (semaphore)
|
||||
{
|
||||
#if defined __APPLE__
|
||||
semaphore_create(mach_task_self(), hSemaphore, SYNC_POLICY_FIFO, lMaximumCount);
|
||||
semaphore_create(mach_task_self(), semaphore, SYNC_POLICY_FIFO, lMaximumCount);
|
||||
#else
|
||||
sem_init(hSemaphore, 0, lMaximumCount);
|
||||
sem_init(semaphore, 0, lMaximumCount);
|
||||
#endif
|
||||
}
|
||||
|
||||
return (HANDLE) hSemaphore;
|
||||
handle = winpr_Handle_Insert(HANDLE_TYPE_SEMAPHORE, (PVOID) semaphore);
|
||||
|
||||
return handle;
|
||||
}
|
||||
|
||||
HANDLE OpenSemaphoreA(DWORD dwDesiredAccess, BOOL bInheritHandle, LPCSTR lpName)
|
||||
HANDLE CreateSemaphoreA(LPSECURITY_ATTRIBUTES lpSemaphoreAttributes, LONG lInitialCount, LONG lMaximumCount, LPCSTR lpName)
|
||||
{
|
||||
return NULL;
|
||||
return CreateSemaphoreW(lpSemaphoreAttributes, lInitialCount, lMaximumCount, NULL);
|
||||
}
|
||||
|
||||
HANDLE OpenSemaphoreW(DWORD dwDesiredAccess, BOOL bInheritHandle, LPCWSTR lpName)
|
||||
@@ -75,15 +66,30 @@ HANDLE OpenSemaphoreW(DWORD dwDesiredAccess, BOOL bInheritHandle, LPCWSTR lpName
|
||||
return NULL;
|
||||
}
|
||||
|
||||
HANDLE OpenSemaphoreA(DWORD dwDesiredAccess, BOOL bInheritHandle, LPCSTR lpName)
|
||||
{
|
||||
return NULL;
|
||||
}
|
||||
|
||||
BOOL ReleaseSemaphore(HANDLE hSemaphore, LONG lReleaseCount, LPLONG lpPreviousCount)
|
||||
{
|
||||
#if defined __APPLE__
|
||||
semaphore_signal(*((winpr_sem_t*) hSemaphore));
|
||||
#else
|
||||
sem_post((winpr_sem_t*) hSemaphore);
|
||||
#endif
|
||||
ULONG Type;
|
||||
PVOID Object;
|
||||
|
||||
return 1;
|
||||
if (!winpr_Handle_GetInfo(hSemaphore, &Type, &Object))
|
||||
return FALSE;
|
||||
|
||||
if (Type == HANDLE_TYPE_SEMAPHORE)
|
||||
{
|
||||
#if defined __APPLE__
|
||||
semaphore_signal(*((winpr_sem_t*) Object));
|
||||
#else
|
||||
sem_post((winpr_sem_t*) Object);
|
||||
#endif
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
@@ -47,8 +47,18 @@ DWORD WaitForSingleObject(HANDLE hHandle, DWORD dwMilliseconds)
|
||||
if (!winpr_Handle_GetInfo(hHandle, &Type, &Object))
|
||||
return WAIT_FAILED;
|
||||
|
||||
if (Type == HANDLE_TYPE_THREAD)
|
||||
{
|
||||
if (dwMilliseconds != INFINITE)
|
||||
printf("WaitForSingleObject: timeout not implemented for thread wait\n");
|
||||
|
||||
pthread_join((pthread_t) Object, NULL);
|
||||
}
|
||||
if (Type == HANDLE_TYPE_MUTEX)
|
||||
{
|
||||
if (dwMilliseconds != INFINITE)
|
||||
printf("WaitForSingleObject: timeout not implemented for mutex wait\n");
|
||||
|
||||
pthread_mutex_lock((pthread_mutex_t*) Object);
|
||||
}
|
||||
else if (Type == HANDLE_TYPE_EVENT)
|
||||
@@ -78,6 +88,14 @@ DWORD WaitForSingleObject(HANDLE hHandle, DWORD dwMilliseconds)
|
||||
if (status != 1)
|
||||
return WAIT_TIMEOUT;
|
||||
}
|
||||
else if (Type == HANDLE_TYPE_SEMAPHORE)
|
||||
{
|
||||
#if defined __APPLE__
|
||||
semaphore_wait(*((winpr_sem_t*) Object));
|
||||
#else
|
||||
sem_wait((winpr_sem_t*) Object);
|
||||
#endif
|
||||
}
|
||||
|
||||
return WAIT_OBJECT_0;
|
||||
}
|
||||
|
||||
@@ -80,12 +80,15 @@ HANDLE CreateRemoteThread(HANDLE hProcess, LPSECURITY_ATTRIBUTES lpThreadAttribu
|
||||
HANDLE CreateThread(LPSECURITY_ATTRIBUTES lpThreadAttributes, SIZE_T dwStackSize,
|
||||
LPTHREAD_START_ROUTINE lpStartAddress, LPVOID lpParameter, DWORD dwCreationFlags, LPDWORD lpThreadId)
|
||||
{
|
||||
HANDLE handle;
|
||||
pthread_t thread;
|
||||
|
||||
pthread_create(&thread, 0, (pthread_start_routine) lpStartAddress, lpParameter);
|
||||
pthread_detach(thread);
|
||||
|
||||
return winpr_Handle_Insert(HANDLE_TYPE_THREAD, (void*) thread);
|
||||
handle = winpr_Handle_Insert(HANDLE_TYPE_THREAD, (void*) thread);
|
||||
|
||||
return handle;
|
||||
}
|
||||
|
||||
VOID ExitThread(DWORD dwExitCode)
|
||||
|
||||
Reference in New Issue
Block a user