//-----------------------------------------------------------------------------
// Project     : VST SDK
//
// Category    : Examples
// Filename    : public.sdk/source/common/threadchecker_mac.mm
// Created by  : Steinberg, 01/2019
// Description : macOS thread checker
//
//-----------------------------------------------------------------------------
// This file is part of a Steinberg SDK. It is subject to the license terms
// in the LICENSE file found in the top-level directory of this distribution
// and at www.steinberg.net/sdklicenses.
// No part of the SDK, including this file, may be copied, modified, propagated,
// or distributed except according to the terms contained in the LICENSE file.
//-----------------------------------------------------------------------------

#include "threadchecker.h"

#if SMTG_OS_MACOS

#include <exception>
#include <pthread.h>
#include <Foundation/Foundation.h>

//------------------------------------------------------------------------
namespace Steinberg {
namespace Vst {

//------------------------------------------------------------------------
class MacThreadChecker : public ThreadChecker
{
public:
	bool test (const char* failmessage = nullptr, bool exit = false) override
	{
		if (threadID == pthread_self ())
			return true;
		if (failmessage)
			NSLog (@"%s", failmessage);
		if (exit)
			std::terminate ();
		return false;
	}

	pthread_t threadID {pthread_self ()};
};

//------------------------------------------------------------------------
std::unique_ptr<ThreadChecker> ThreadChecker::create ()
{
	return std::unique_ptr<ThreadChecker> (new MacThreadChecker);
}

//------------------------------------------------------------------------
} // namespace Vst
} // namespace Steinberg

#endif // SMTG_OS_MACOS
