2018-11-15 00:48:57 +00:00
|
|
|
/*
|
|
|
|
* s3fs - FUSE-based file system backed by Amazon S3
|
|
|
|
*
|
|
|
|
* Copyright(C) 2007 Randy Rizun <rrizun@gmail.com>
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
* as published by the Free Software Foundation; either version 2
|
|
|
|
* of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
*/
|
|
|
|
|
2019-01-27 12:04:29 +00:00
|
|
|
#ifndef S3FS_SEMAPHORE_H_
|
|
|
|
#define S3FS_SEMAPHORE_H_
|
2018-11-15 00:48:57 +00:00
|
|
|
|
|
|
|
// portability wrapper for sem_t since macOS does not implement it
|
|
|
|
|
|
|
|
#ifdef __APPLE__
|
|
|
|
|
|
|
|
#include <dispatch/dispatch.h>
|
|
|
|
|
|
|
|
class Semaphore
|
|
|
|
{
|
|
|
|
public:
|
2019-01-19 01:16:56 +00:00
|
|
|
explicit Semaphore(int value) : value(value), sem(dispatch_semaphore_create(value)) {}
|
|
|
|
~Semaphore() {
|
|
|
|
// macOS cannot destroy a semaphore with posts less than the initializer
|
|
|
|
for(int i = 0; i < get_value(); ++i){
|
|
|
|
post();
|
|
|
|
}
|
|
|
|
dispatch_release(sem);
|
|
|
|
}
|
2018-11-15 00:48:57 +00:00
|
|
|
void wait() { dispatch_semaphore_wait(sem, DISPATCH_TIME_FOREVER); }
|
|
|
|
void post() { dispatch_semaphore_signal(sem); }
|
2019-01-19 01:16:56 +00:00
|
|
|
int get_value() const { return value; }
|
2018-11-15 00:48:57 +00:00
|
|
|
private:
|
2019-01-19 01:16:56 +00:00
|
|
|
const int value;
|
2018-11-15 00:48:57 +00:00
|
|
|
dispatch_semaphore_t sem;
|
|
|
|
};
|
|
|
|
|
|
|
|
#else
|
|
|
|
|
|
|
|
#include <errno.h>
|
|
|
|
#include <semaphore.h>
|
|
|
|
|
|
|
|
class Semaphore
|
|
|
|
{
|
|
|
|
public:
|
2019-01-19 01:16:56 +00:00
|
|
|
explicit Semaphore(int value) : value(value) { sem_init(&mutex, 0, value); }
|
2018-11-15 00:48:57 +00:00
|
|
|
~Semaphore() { sem_destroy(&mutex); }
|
|
|
|
void wait()
|
|
|
|
{
|
|
|
|
int r;
|
|
|
|
do {
|
|
|
|
r = sem_wait(&mutex);
|
|
|
|
} while (r == -1 && errno == EINTR);
|
|
|
|
}
|
|
|
|
void post() { sem_post(&mutex); }
|
2019-01-19 01:16:56 +00:00
|
|
|
int get_value() const { return value; }
|
2018-11-15 00:48:57 +00:00
|
|
|
private:
|
2019-01-19 01:16:56 +00:00
|
|
|
const int value;
|
2018-11-15 00:48:57 +00:00
|
|
|
sem_t mutex;
|
|
|
|
};
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
2019-01-27 12:04:29 +00:00
|
|
|
#endif // S3FS_SEMAPHORE_H_
|