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.
|
|
|
|
*/
|
|
|
|
|
2022-02-16 12:08:00 +03:00
|
|
|
#include <winpr/config.h>
|
2012-08-15 01:20:53 +04:00
|
|
|
|
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
|
|
|
|
2023-01-10 16:30:23 +03:00
|
|
|
#ifdef WINPR_HAVE_UNISTD_H
|
2012-11-28 21:47:04 +04:00
|
|
|
#include <unistd.h>
|
|
|
|
#endif
|
2012-09-18 20:57:19 +04:00
|
|
|
|
|
|
|
#ifndef _WIN32
|
2012-05-29 22:14:26 +04:00
|
|
|
|
2015-03-11 19:57:01 +03:00
|
|
|
#include <errno.h>
|
2013-05-17 01:32:58 +04:00
|
|
|
#include "../handle/handle.h"
|
2014-08-18 19:22:22 +04:00
|
|
|
#include "../log.h"
|
2014-08-18 20:57:08 +04:00
|
|
|
#define TAG WINPR_TAG("synch.semaphore")
|
2015-03-11 17:11:17 +03:00
|
|
|
|
|
|
|
static BOOL SemaphoreCloseHandle(HANDLE handle);
|
|
|
|
|
|
|
|
static BOOL SemaphoreIsHandled(HANDLE handle)
|
|
|
|
{
|
2022-04-27 16:56:39 +03:00
|
|
|
return WINPR_HANDLE_IS_HANDLED(handle, HANDLE_TYPE_SEMAPHORE, FALSE);
|
2015-03-11 17:11:17 +03:00
|
|
|
}
|
|
|
|
|
2015-03-11 19:57:01 +03:00
|
|
|
static int SemaphoreGetFd(HANDLE handle)
|
|
|
|
{
|
2018-10-24 14:17:14 +03:00
|
|
|
WINPR_SEMAPHORE* sem = (WINPR_SEMAPHORE*)handle;
|
2015-03-11 19:57:01 +03:00
|
|
|
|
|
|
|
if (!SemaphoreIsHandled(handle))
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
return sem->pipe_fd[0];
|
|
|
|
}
|
|
|
|
|
|
|
|
static DWORD SemaphoreCleanupHandle(HANDLE handle)
|
2015-03-11 17:11:17 +03:00
|
|
|
{
|
2021-06-16 13:58:21 +03:00
|
|
|
SSIZE_T length;
|
2018-10-24 14:17:14 +03:00
|
|
|
WINPR_SEMAPHORE* sem = (WINPR_SEMAPHORE*)handle;
|
2015-03-11 19:57:01 +03:00
|
|
|
|
|
|
|
if (!SemaphoreIsHandled(handle))
|
|
|
|
return WAIT_FAILED;
|
|
|
|
|
|
|
|
length = read(sem->pipe_fd[0], &length, 1);
|
|
|
|
|
|
|
|
if (length != 1)
|
|
|
|
{
|
|
|
|
WLog_ERR(TAG, "semaphore read() failure [%d] %s", errno, strerror(errno));
|
|
|
|
return WAIT_FAILED;
|
|
|
|
}
|
|
|
|
|
|
|
|
return WAIT_OBJECT_0;
|
2015-03-11 17:11:17 +03:00
|
|
|
}
|
|
|
|
|
2018-10-24 14:17:14 +03:00
|
|
|
BOOL SemaphoreCloseHandle(HANDLE handle)
|
|
|
|
{
|
2019-11-06 17:24:51 +03:00
|
|
|
WINPR_SEMAPHORE* semaphore = (WINPR_SEMAPHORE*)handle;
|
2015-03-11 17:11:17 +03:00
|
|
|
|
|
|
|
if (!SemaphoreIsHandled(handle))
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
#ifdef WINPR_PIPE_SEMAPHORE
|
|
|
|
|
|
|
|
if (semaphore->pipe_fd[0] != -1)
|
|
|
|
{
|
|
|
|
close(semaphore->pipe_fd[0]);
|
|
|
|
semaphore->pipe_fd[0] = -1;
|
|
|
|
|
|
|
|
if (semaphore->pipe_fd[1] != -1)
|
|
|
|
{
|
|
|
|
close(semaphore->pipe_fd[1]);
|
|
|
|
semaphore->pipe_fd[1] = -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#else
|
|
|
|
#if defined __APPLE__
|
2019-11-06 17:24:51 +03:00
|
|
|
semaphore_destroy(mach_task_self(), *((winpr_sem_t*)semaphore->sem));
|
2015-03-11 17:11:17 +03:00
|
|
|
#else
|
2019-11-06 17:24:51 +03:00
|
|
|
sem_destroy((winpr_sem_t*)semaphore->sem);
|
2015-03-11 17:11:17 +03:00
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
free(semaphore);
|
|
|
|
return TRUE;
|
|
|
|
}
|
2013-05-17 01:32:58 +04:00
|
|
|
|
2019-11-06 17:24:51 +03:00
|
|
|
static HANDLE_OPS ops = { SemaphoreIsHandled,
|
|
|
|
SemaphoreCloseHandle,
|
|
|
|
SemaphoreGetFd,
|
|
|
|
SemaphoreCleanupHandle,
|
|
|
|
NULL,
|
|
|
|
NULL,
|
|
|
|
NULL,
|
|
|
|
NULL,
|
|
|
|
NULL,
|
2022-04-13 10:34:05 +03:00
|
|
|
NULL,
|
2019-11-06 17:24:51 +03:00
|
|
|
NULL,
|
|
|
|
NULL,
|
|
|
|
NULL,
|
|
|
|
NULL,
|
|
|
|
NULL,
|
|
|
|
NULL,
|
|
|
|
NULL,
|
|
|
|
NULL,
|
|
|
|
NULL,
|
|
|
|
NULL,
|
|
|
|
NULL };
|
2015-03-16 02:29:37 +03:00
|
|
|
|
2018-10-24 14:17:14 +03: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;
|
2014-08-18 21:34:47 +04:00
|
|
|
WINPR_SEMAPHORE* semaphore;
|
2019-11-06 17:24:51 +03:00
|
|
|
semaphore = (WINPR_SEMAPHORE*)calloc(1, sizeof(WINPR_SEMAPHORE));
|
2012-05-29 22:14:26 +04:00
|
|
|
|
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;
|
2017-10-28 11:59:23 +03:00
|
|
|
semaphore->pipe_fd[1] = -1;
|
2019-11-06 17:24:51 +03:00
|
|
|
semaphore->sem = (winpr_sem_t*)NULL;
|
2022-04-27 16:56:39 +03:00
|
|
|
semaphore->common.ops = &ops;
|
2012-11-28 21:47:04 +04:00
|
|
|
#ifdef WINPR_PIPE_SEMAPHORE
|
|
|
|
|
2017-01-28 10:36:08 +03:00
|
|
|
if (pipe(semaphore->pipe_fd) < 0)
|
|
|
|
{
|
|
|
|
WLog_ERR(TAG, "failed to create semaphore");
|
|
|
|
free(semaphore);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
while (lInitialCount > 0)
|
|
|
|
{
|
|
|
|
if (write(semaphore->pipe_fd[1], "-", 1) != 1)
|
2012-11-28 21:47:04 +04:00
|
|
|
{
|
2017-01-28 10:36:08 +03:00
|
|
|
close(semaphore->pipe_fd[0]);
|
|
|
|
close(semaphore->pipe_fd[1]);
|
2013-08-29 17:30:22 +04:00
|
|
|
free(semaphore);
|
2012-11-28 21:47:04 +04:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2017-01-28 10:36:08 +03:00
|
|
|
lInitialCount--;
|
|
|
|
}
|
2012-11-28 21:47:04 +04:00
|
|
|
|
|
|
|
#else
|
2019-11-06 17:24:51 +03:00
|
|
|
semaphore->sem = (winpr_sem_t*)malloc(sizeof(winpr_sem_t));
|
2018-10-24 14:17:14 +03:00
|
|
|
|
2017-01-28 10:36:08 +03:00
|
|
|
if (!semaphore->sem)
|
|
|
|
{
|
|
|
|
WLog_ERR(TAG, "failed to allocate semaphore memory");
|
|
|
|
free(semaphore);
|
|
|
|
return NULL;
|
|
|
|
}
|
2018-10-24 14:17:14 +03:00
|
|
|
|
2012-05-29 22:14:26 +04:00
|
|
|
#if defined __APPLE__
|
2018-10-24 14:17:14 +03:00
|
|
|
|
2019-11-06 17:24:51 +03:00
|
|
|
if (semaphore_create(mach_task_self(), semaphore->sem, SYNC_POLICY_FIFO, lMaximumCount) !=
|
|
|
|
KERN_SUCCESS)
|
2012-05-29 22:14:26 +04:00
|
|
|
#else
|
2017-01-28 10:36:08 +03:00
|
|
|
if (sem_init(semaphore->sem, 0, lMaximumCount) == -1)
|
2012-05-29 22:14:26 +04:00
|
|
|
#endif
|
2017-01-28 10:36:08 +03:00
|
|
|
{
|
|
|
|
WLog_ERR(TAG, "failed to create semaphore");
|
|
|
|
free(semaphore->sem);
|
|
|
|
free(semaphore);
|
|
|
|
return NULL;
|
2012-09-19 03:24:03 +04:00
|
|
|
}
|
|
|
|
|
2018-10-24 14:17:14 +03:00
|
|
|
#endif
|
2015-07-14 12:39:41 +03:00
|
|
|
WINPR_HANDLE_SET_TYPE_AND_MODE(semaphore, HANDLE_TYPE_SEMAPHORE, WINPR_FD_READ);
|
2019-11-06 17:24:51 +03:00
|
|
|
handle = (HANDLE)semaphore;
|
2012-09-19 03:24:03 +04:00
|
|
|
return handle;
|
2012-05-29 22:14:26 +04:00
|
|
|
}
|
|
|
|
|
2018-10-24 14:17:14 +03: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
|
|
|
{
|
2014-08-18 19:22:22 +04:00
|
|
|
WLog_ERR(TAG, "not implemented");
|
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
|
|
|
{
|
2014-08-18 19:22:22 +04:00
|
|
|
WLog_ERR(TAG, "not implemented");
|
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;
|
2015-07-03 10:36:58 +03:00
|
|
|
WINPR_HANDLE* Object;
|
2014-08-18 21:34:47 +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)
|
|
|
|
{
|
2019-11-06 17:24:51 +03:00
|
|
|
semaphore = (WINPR_SEMAPHORE*)Object;
|
2012-11-28 21:47:04 +04:00
|
|
|
#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
|
2019-02-26 17:33:51 +03:00
|
|
|
|
|
|
|
while (lReleaseCount > 0)
|
|
|
|
{
|
2012-05-29 22:14:26 +04:00
|
|
|
#if defined __APPLE__
|
2019-11-06 17:24:51 +03:00
|
|
|
semaphore_signal(*((winpr_sem_t*)semaphore->sem));
|
2012-05-29 22:14:26 +04:00
|
|
|
#else
|
2019-11-06 17:24:51 +03:00
|
|
|
sem_post((winpr_sem_t*)semaphore->sem);
|
2012-11-28 21:47:04 +04:00
|
|
|
#endif
|
2019-02-26 17:33:51 +03:00
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2016-11-25 14:40:11 +03:00
|
|
|
WLog_ERR(TAG, "calling %s on a handle that is not a semaphore", __FUNCTION__);
|
2012-09-19 03:24:03 +04:00
|
|
|
return FALSE;
|
2012-05-29 22:14:26 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|