Commit 31fbd123 authored by Yedidya Feldblum's avatar Yedidya Feldblum Committed by Facebook GitHub Bot

cut posix semaphore polyfill

Summary: Prefer not to have polyfills. The class `NativeSemaphore` replaces this polyfill.

Reviewed By: Orvid

Differential Revision: D27971239

fbshipit-source-id: 39bd8b2d9be895a947faee4d522420535378fdc3
parent 2a4fe7ca
/*
* Copyright (c) Facebook, Inc. and its affiliates.
*
* 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.
*/
#include <folly/portability/Semaphore.h>
#include <folly/portability/Windows.h>
#include <cerrno>
#include <mutex>
#ifdef _WIN32
namespace folly::portability::semaphore {
struct sem_t_ {
std::mutex mtx{};
HANDLE sema{INVALID_HANDLE_VALUE};
int32_t value{0};
};
int sem_init(sem_t* s, int shared, unsigned int value) {
// Don't support cross-process shared semaphores.
if (shared != 0) {
return -1;
}
auto sem = CreateSemaphoreA(nullptr, 0, SEM_VALUE_MAX, nullptr);
if (sem == 0) {
return -1;
}
auto ret = new sem_t_();
ret->sema = sem;
ret->value = value;
*s = ret;
return 0;
}
int sem_destroy(sem_t* s) {
if (!CloseHandle((*s)->sema)) {
return -1;
}
delete *s;
*s = nullptr;
return 0;
}
int sem_post(sem_t* s) {
std::lock_guard<std::mutex> lock{(*s)->mtx};
if ((*s)->value < SEM_VALUE_MAX) {
if (++(*s)->value <= 0 && !ReleaseSemaphore((*s)->sema, 1, nullptr)) {
--(*s)->value;
errno = EINVAL;
return -1;
}
} else {
errno = ERANGE;
return -1;
}
return 0;
}
int sem_trywait(sem_t* s) {
std::lock_guard<std::mutex> lock{(*s)->mtx};
if ((*s)->value > 0) {
(*s)->value--;
return 0;
} else {
errno = EAGAIN;
return -1;
}
}
int sem_wait(sem_t* s) {
int32_t value = 0;
{
std::lock_guard<std::mutex> lock{(*s)->mtx};
value = --(*s)->value;
}
if (value < 0) {
if (WaitForSingleObject((*s)->sema, INFINITE) != WAIT_OBJECT_0) {
errno = EINVAL;
return -1;
}
}
return 0;
}
} // namespace folly::portability::semaphore
#endif
/*
* Copyright (c) Facebook, Inc. and its affiliates.
*
* 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.
*/
#pragma once
#ifndef _WIN32
#include <semaphore.h>
#else
#include <limits.h>
#include <folly/Portability.h>
#define SEM_VALUE_MAX INT_MAX
namespace folly::portability::semaphore {
using sem_t = struct sem_t_*;
int sem_init(sem_t* s, int shared, unsigned int value);
int sem_destroy(sem_t* s);
int sem_post(sem_t* s);
int sem_trywait(sem_t* s);
int sem_wait(sem_t* s);
} // namespace folly::portability::semaphore
FOLLY_PUSH_WARNING
FOLLY_CLANG_DISABLE_WARNING("-Wheader-hygiene")
/* using override */ using namespace folly::portability::semaphore;
FOLLY_POP_WARNING
#endif
......@@ -23,7 +23,6 @@
#include <folly/Benchmark.h>
#include <folly/portability/GFlags.h>
#include <folly/portability/GTest.h>
#include <folly/portability/Semaphore.h>
#include <folly/test/DeterministicSchedule.h>
using namespace folly;
......
......@@ -21,7 +21,6 @@
#include <folly/portability/GMock.h>
#include <folly/portability/GTest.h>
#include <folly/portability/Semaphore.h>
#include <folly/portability/Unistd.h>
#include <folly/test/DeterministicSchedule.h>
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment