2010-11-16 14:47:38 -05:00
|
|
|
/*
|
|
|
|
Copyright (C) 2010 Paul Davis
|
|
|
|
|
|
|
|
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., 675 Mass Ave, Cambridge, MA 02139, USA.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef __pbd_semutils_h__
|
|
|
|
#define __pbd_semutils_h__
|
|
|
|
|
2015-12-02 07:38:59 -05:00
|
|
|
#if (defined PLATFORM_WINDOWS && !defined USE_PTW32_SEMAPHORE)
|
|
|
|
#define WINDOWS_SEMAPHORE 1
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef WINDOWS_SEMAPHORE
|
2013-07-11 12:46:05 -04:00
|
|
|
#include <windows.h>
|
|
|
|
#else
|
2015-12-02 07:38:59 -05:00
|
|
|
#include <pthread.h>
|
2010-11-16 14:47:38 -05:00
|
|
|
#include <semaphore.h>
|
2013-07-11 12:46:05 -04:00
|
|
|
#endif
|
2010-11-16 14:47:38 -05:00
|
|
|
|
2013-10-16 23:30:28 -04:00
|
|
|
#include "pbd/libpbd_visibility.h"
|
|
|
|
|
2010-11-16 14:47:38 -05:00
|
|
|
namespace PBD {
|
|
|
|
|
2015-12-02 14:18:48 -05:00
|
|
|
class LIBPBD_API Semaphore {
|
2010-11-16 14:47:38 -05:00
|
|
|
private:
|
2015-12-02 07:38:59 -05:00
|
|
|
#ifdef WINDOWS_SEMAPHORE
|
2013-07-11 12:46:05 -04:00
|
|
|
HANDLE _sem;
|
|
|
|
|
|
|
|
#elif __APPLE__
|
2010-11-16 14:47:38 -05:00
|
|
|
sem_t* _sem;
|
|
|
|
sem_t* ptr_to_sem() const { return _sem; }
|
|
|
|
#else
|
2010-11-16 15:01:01 -05:00
|
|
|
mutable sem_t _sem;
|
2010-11-16 14:47:38 -05:00
|
|
|
sem_t* ptr_to_sem() const { return &_sem; }
|
|
|
|
#endif
|
|
|
|
|
|
|
|
public:
|
2015-12-02 14:18:48 -05:00
|
|
|
Semaphore (const char* name, int val);
|
|
|
|
~Semaphore ();
|
2010-11-16 14:47:38 -05:00
|
|
|
|
2015-12-02 07:38:59 -05:00
|
|
|
#ifdef WINDOWS_SEMAPHORE
|
2013-07-11 12:46:05 -04:00
|
|
|
|
|
|
|
int signal ();
|
|
|
|
int wait ();
|
|
|
|
|
|
|
|
#else
|
2010-11-16 14:47:38 -05:00
|
|
|
int signal () { return sem_post (ptr_to_sem()); }
|
|
|
|
int wait () { return sem_wait (ptr_to_sem()); }
|
2013-07-11 12:46:05 -04:00
|
|
|
#endif
|
2010-11-16 14:47:38 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif /* __pbd_semutils_h__ */
|