2012-05-29 22:14:26 +04:00
|
|
|
/**
|
|
|
|
* WinPR: Windows Portable Runtime
|
|
|
|
* Synchronization Functions
|
|
|
|
*
|
|
|
|
* Copyright 2012 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.
|
|
|
|
*/
|
|
|
|
|
2012-08-15 01:20:53 +04:00
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
#include "config.h"
|
|
|
|
#endif
|
|
|
|
|
2012-05-29 22:14:26 +04:00
|
|
|
#include <winpr/synch.h>
|
|
|
|
|
2012-09-18 20:57:19 +04:00
|
|
|
#include "synch.h"
|
2012-05-29 22:14:26 +04:00
|
|
|
|
2012-11-28 21:47:04 +04:00
|
|
|
#ifdef HAVE_UNISTD_H
|
|
|
|
#include <unistd.h>
|
|
|
|
#endif
|
2012-09-18 20:57:19 +04:00
|
|
|
|
|
|
|
#ifndef _WIN32
|
2012-05-29 22:14:26 +04:00
|
|
|
|
2013-05-17 01:32:58 +04:00
|
|
|
#include "../handle/handle.h"
|
|
|
|
|
2012-09-19 03:24:03 +04:00
|
|
|
HANDLE CreateSemaphoreW(LPSECURITY_ATTRIBUTES lpSemaphoreAttributes, LONG lInitialCount, LONG lMaximumCount, LPCWSTR lpName)
|
2012-05-29 22:14:26 +04:00
|
|
|
{
|
2012-09-19 03:24:03 +04:00
|
|
|
HANDLE handle;
|
2012-11-28 21:47:04 +04:00
|
|
|
WINPR_SEMAPHORE* semaphore;
|
2012-05-29 22:14:26 +04:00
|
|
|
|
2012-11-28 21:47:04 +04:00
|
|
|
semaphore = (WINPR_SEMAPHORE*) malloc(sizeof(WINPR_SEMAPHORE));
|
2013-08-29 12:42:08 +04:00
|
|
|
if (!semaphore)
|
|
|
|
return NULL;
|
2012-11-28 21:47:04 +04:00
|
|
|
|
|
|
|
semaphore->pipe_fd[0] = -1;
|
|
|
|
semaphore->pipe_fd[0] = -1;
|
|
|
|
semaphore->sem = (winpr_sem_t*) NULL;
|
2012-05-29 22:14:26 +04:00
|
|
|
|
2012-09-19 03:24:03 +04:00
|
|
|
if (semaphore)
|
|
|
|
{
|
2012-11-28 21:47:04 +04:00
|
|
|
#ifdef WINPR_PIPE_SEMAPHORE
|
|
|
|
|
|
|
|
if (pipe(semaphore->pipe_fd) < 0)
|
|
|
|
{
|
2013-03-29 02:06:34 +04:00
|
|
|
fprintf(stderr, "CreateSemaphoreW: failed to create semaphore\n");
|
2013-08-29 17:30:22 +04:00
|
|
|
free(semaphore);
|
2012-11-28 21:47:04 +04:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
while (lInitialCount > 0)
|
|
|
|
{
|
|
|
|
if (write(semaphore->pipe_fd[1], "-", 1) != 1)
|
2013-08-28 19:53:47 +04:00
|
|
|
{
|
|
|
|
close(semaphore->pipe_fd[0]);
|
|
|
|
close(semaphore->pipe_fd[1]);
|
2013-08-29 17:30:22 +04:00
|
|
|
free(semaphore);
|
2013-09-02 11:12:33 +04:00
|
|
|
return NULL;
|
2013-08-28 19:53:47 +04:00
|
|
|
}
|
2012-11-28 21:47:04 +04:00
|
|
|
|
|
|
|
lInitialCount--;
|
|
|
|
}
|
|
|
|
|
|
|
|
#else
|
|
|
|
semaphore->sem = (winpr_sem_t*) malloc(sizeof(winpr_sem_t));
|
2012-05-29 22:14:26 +04:00
|
|
|
#if defined __APPLE__
|
2012-11-28 21:47:04 +04:00
|
|
|
semaphore_create(mach_task_self(), semaphore->sem, SYNC_POLICY_FIFO, lMaximumCount);
|
2012-05-29 22:14:26 +04:00
|
|
|
#else
|
2012-11-28 21:47:04 +04:00
|
|
|
sem_init(semaphore->sem, 0, lMaximumCount);
|
|
|
|
#endif
|
|
|
|
|
2012-05-29 22:14:26 +04:00
|
|
|
#endif
|
2012-09-19 03:24:03 +04:00
|
|
|
}
|
|
|
|
|
2013-05-17 02:27:26 +04:00
|
|
|
WINPR_HANDLE_SET_TYPE(semaphore, HANDLE_TYPE_SEMAPHORE);
|
|
|
|
handle = (HANDLE) semaphore;
|
2012-05-29 22:14:26 +04:00
|
|
|
|
2012-09-19 03:24:03 +04:00
|
|
|
return handle;
|
2012-05-29 22:14:26 +04:00
|
|
|
}
|
|
|
|
|
2012-09-19 03:24:03 +04:00
|
|
|
HANDLE CreateSemaphoreA(LPSECURITY_ATTRIBUTES lpSemaphoreAttributes, LONG lInitialCount, LONG lMaximumCount, LPCSTR lpName)
|
2012-05-29 22:14:26 +04:00
|
|
|
{
|
2012-09-19 03:24:03 +04:00
|
|
|
return CreateSemaphoreW(lpSemaphoreAttributes, lInitialCount, lMaximumCount, NULL);
|
2012-05-29 22:14:26 +04:00
|
|
|
}
|
|
|
|
|
2012-09-19 03:24:03 +04:00
|
|
|
HANDLE OpenSemaphoreW(DWORD dwDesiredAccess, BOOL bInheritHandle, LPCWSTR lpName)
|
2012-05-29 22:14:26 +04:00
|
|
|
{
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2012-09-19 03:24:03 +04:00
|
|
|
HANDLE OpenSemaphoreA(DWORD dwDesiredAccess, BOOL bInheritHandle, LPCSTR lpName)
|
2012-05-29 22:14:26 +04:00
|
|
|
{
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
BOOL ReleaseSemaphore(HANDLE hSemaphore, LONG lReleaseCount, LPLONG lpPreviousCount)
|
|
|
|
{
|
2012-09-19 03:24:03 +04:00
|
|
|
ULONG Type;
|
|
|
|
PVOID Object;
|
2012-11-28 21:47:04 +04:00
|
|
|
WINPR_SEMAPHORE* semaphore;
|
2012-09-19 03:24:03 +04:00
|
|
|
|
|
|
|
if (!winpr_Handle_GetInfo(hSemaphore, &Type, &Object))
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
if (Type == HANDLE_TYPE_SEMAPHORE)
|
|
|
|
{
|
2012-11-28 21:47:04 +04:00
|
|
|
semaphore = (WINPR_SEMAPHORE*) Object;
|
|
|
|
|
|
|
|
#ifdef WINPR_PIPE_SEMAPHORE
|
|
|
|
|
|
|
|
if (semaphore->pipe_fd[0] != -1)
|
|
|
|
{
|
|
|
|
while (lReleaseCount > 0)
|
|
|
|
{
|
|
|
|
if (write(semaphore->pipe_fd[1], "-", 1) != 1)
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
lReleaseCount--;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#else
|
|
|
|
|
2012-05-29 22:14:26 +04:00
|
|
|
#if defined __APPLE__
|
2012-11-28 21:47:04 +04:00
|
|
|
semaphore_signal(*((winpr_sem_t*) semaphore->sem));
|
2012-05-29 22:14:26 +04:00
|
|
|
#else
|
2012-11-28 21:47:04 +04:00
|
|
|
sem_post((winpr_sem_t*) semaphore->sem);
|
|
|
|
#endif
|
|
|
|
|
2012-05-29 22:14:26 +04:00
|
|
|
#endif
|
2012-09-19 03:24:03 +04:00
|
|
|
return TRUE;
|
|
|
|
}
|
2012-05-29 22:14:26 +04:00
|
|
|
|
2012-09-19 03:24:03 +04:00
|
|
|
return FALSE;
|
2012-05-29 22:14:26 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|