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
|
|
|
|
2020-08-22 12:40:53 +00:00
|
|
|
//-------------------------------------------------------------------
|
|
|
|
// Class Semaphore
|
|
|
|
//-------------------------------------------------------------------
|
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
|
|
|
|
{
|
2020-08-22 12:40:53 +00:00
|
|
|
public:
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
void wait() { dispatch_semaphore_wait(sem, DISPATCH_TIME_FOREVER); }
|
|
|
|
void post() { dispatch_semaphore_signal(sem); }
|
|
|
|
int get_value() const { return value; }
|
|
|
|
|
|
|
|
private:
|
|
|
|
const int value;
|
|
|
|
dispatch_semaphore_t sem;
|
2018-11-15 00:48:57 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
#else
|
|
|
|
|
|
|
|
#include <errno.h>
|
|
|
|
#include <semaphore.h>
|
|
|
|
|
|
|
|
class Semaphore
|
|
|
|
{
|
2020-08-22 12:40:53 +00:00
|
|
|
public:
|
|
|
|
explicit Semaphore(int value) : value(value) { sem_init(&mutex, 0, value); }
|
|
|
|
~Semaphore() { sem_destroy(&mutex); }
|
|
|
|
void wait()
|
|
|
|
{
|
|
|
|
int r;
|
|
|
|
do {
|
|
|
|
r = sem_wait(&mutex);
|
|
|
|
} while (r == -1 && errno == EINTR);
|
|
|
|
}
|
|
|
|
void post() { sem_post(&mutex); }
|
|
|
|
int get_value() const { return value; }
|
|
|
|
|
|
|
|
private:
|
|
|
|
const int value;
|
|
|
|
sem_t mutex;
|
2018-11-15 00:48:57 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
2019-01-27 12:04:29 +00:00
|
|
|
#endif // S3FS_SEMAPHORE_H_
|
2020-08-22 12:40:53 +00:00
|
|
|
|
2020-08-19 12:03:46 +00:00
|
|
|
/*
|
|
|
|
* Local variables:
|
2020-08-22 12:40:53 +00:00
|
|
|
* tab-width: 4
|
|
|
|
* c-basic-offset: 4
|
2020-08-19 12:03:46 +00:00
|
|
|
* End:
|
2020-08-22 12:40:53 +00:00
|
|
|
* vim600: expandtab sw=4 ts=4 fdm=marker
|
|
|
|
* vim<600: expandtab sw=4 ts=4
|
2020-08-19 12:03:46 +00:00
|
|
|
*/
|