2011-07-01 02:24:37 +04:00
|
|
|
/**
|
|
|
|
* FreeRDP: A Remote Desktop Protocol Client
|
|
|
|
* Semaphore Utils
|
|
|
|
*
|
|
|
|
* Copyright 2011 Marc-Andre Moreau <marcandre.moreau@gmail.com>
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2011-07-08 07:25:51 +04:00
|
|
|
#include <freerdp/utils/memory.h>
|
2011-07-01 02:24:37 +04:00
|
|
|
#include <freerdp/utils/semaphore.h>
|
2012-03-06 06:04:42 +04:00
|
|
|
#include <freerdp/utils/windows.h>
|
2011-07-01 02:24:37 +04:00
|
|
|
|
2011-07-08 07:25:51 +04:00
|
|
|
#if defined __APPLE__
|
|
|
|
|
|
|
|
#include <pthread.h>
|
|
|
|
#include <semaphore.h>
|
2011-07-01 02:24:37 +04:00
|
|
|
#include <mach/mach.h>
|
|
|
|
#include <mach/semaphore.h>
|
|
|
|
#include <mach/task.h>
|
2011-07-08 07:25:51 +04:00
|
|
|
#define freerdp_sem_t semaphore_t
|
|
|
|
|
|
|
|
#elif defined _WIN32
|
|
|
|
|
|
|
|
#define freerdp_sem_t HANDLE
|
|
|
|
|
|
|
|
#else
|
|
|
|
|
|
|
|
#include <pthread.h>
|
|
|
|
#include <semaphore.h>
|
|
|
|
#define freerdp_sem_t sem_t
|
|
|
|
|
2011-07-01 02:24:37 +04:00
|
|
|
#endif
|
|
|
|
|
2011-07-08 07:25:51 +04:00
|
|
|
freerdp_sem freerdp_sem_new(int iv)
|
2011-07-01 02:24:37 +04:00
|
|
|
{
|
2011-07-08 07:25:51 +04:00
|
|
|
freerdp_sem_t* sem;
|
|
|
|
|
2011-07-13 18:23:55 +04:00
|
|
|
sem = xnew(freerdp_sem_t);
|
2011-07-08 07:25:51 +04:00
|
|
|
|
|
|
|
#if defined __APPLE__
|
|
|
|
semaphore_create(mach_task_self(), sem, SYNC_POLICY_FIFO, iv);
|
|
|
|
#elif defined _WIN32
|
|
|
|
*sem = CreateSemaphore(NULL, 0, iv, NULL);
|
2011-07-01 02:24:37 +04:00
|
|
|
#else
|
2011-07-08 07:25:51 +04:00
|
|
|
sem_init(sem, 0, iv);
|
2011-07-01 02:24:37 +04:00
|
|
|
#endif
|
2011-07-08 07:25:51 +04:00
|
|
|
|
|
|
|
return sem;
|
|
|
|
}
|
|
|
|
|
|
|
|
void freerdp_sem_free(freerdp_sem sem)
|
|
|
|
{
|
|
|
|
#if defined __APPLE__
|
|
|
|
semaphore_destroy(mach_task_self(), *((freerdp_sem_t*)sem));
|
|
|
|
#elif defined _WIN32
|
|
|
|
CloseHandle(*((freerdp_sem_t*)sem));
|
|
|
|
#else
|
|
|
|
sem_destroy((freerdp_sem_t*)sem);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
xfree(sem);
|
2011-07-01 02:24:37 +04:00
|
|
|
}
|
|
|
|
|
2011-07-08 07:25:51 +04:00
|
|
|
void freerdp_sem_signal(freerdp_sem sem)
|
2011-07-01 02:24:37 +04:00
|
|
|
{
|
2011-07-08 07:25:51 +04:00
|
|
|
#if defined __APPLE__
|
|
|
|
semaphore_signal(*((freerdp_sem_t*)sem));
|
|
|
|
#elif defined _WIN32
|
|
|
|
ReleaseSemaphore(*((freerdp_sem_t*)sem), 1, NULL);
|
2011-07-01 02:24:37 +04:00
|
|
|
#else
|
2011-07-08 07:25:51 +04:00
|
|
|
sem_post((freerdp_sem_t*)sem);
|
2011-07-01 02:24:37 +04:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2011-07-08 07:25:51 +04:00
|
|
|
void freerdp_sem_wait(freerdp_sem sem)
|
2011-07-01 02:24:37 +04:00
|
|
|
{
|
2011-07-08 07:25:51 +04:00
|
|
|
#if defined __APPLE__
|
|
|
|
semaphore_wait(*((freerdp_sem_t*)sem));
|
|
|
|
#elif defined _WIN32
|
|
|
|
WaitForSingleObject(*((freerdp_sem_t*)sem), INFINITE);
|
2011-07-01 02:24:37 +04:00
|
|
|
#else
|
2011-07-08 07:25:51 +04:00
|
|
|
sem_wait((freerdp_sem_t*)sem);
|
2011-07-01 02:24:37 +04:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|