//------------------------------------------------------------------------------
// Lamp : Open source game middleware
// Copyright (C) 2004  Junpei Ohtani ( Email : junpee@users.sourceforge.jp )
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// This library 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
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
//------------------------------------------------------------------------------

/** @file
 * ウェイトセット実装
 * @author Junpee
 */

#include "LampBasic.h"
#include "Core/Thread/WaitSet.h"
#include "Core/Thread/CriticalSection.h"
#include "Core/Thread/Thread.h"

namespace Lamp{

//------------------------------------------------------------------------------
// コンストラクタ
WaitSet::WaitSet() : deleteLockObject_(true){
	lockObject_ = new CriticalSection();
}
//------------------------------------------------------------------------------
// コンストラクタ
WaitSet::WaitSet(LockObject* lockObject, bool deleteLockObject) :
	lockObject_(lockObject), deleteLockObject_(deleteLockObject){
	Assert(lockObject_ != NULL);
}
//------------------------------------------------------------------------------
// デストラクタ
WaitSet::~WaitSet(){
	Assert(waitingThreads_.getCount() == 0);
	if(deleteLockObject_){ delete lockObject_; }
}
//------------------------------------------------------------------------------
// 待機
void WaitSet::wait(const Thread* thread){
	if(!isLockedByCurrentThread()){
		ErrorOut("WaitSet::wait() "
			"wait()するためにはWaitSetに対するlock()が必要です");
	}
	HANDLE threadHandle = thread->getThreadHandle();
	waitingThreads_.pushBack(threadHandle);
	unlock();
	if(::SuspendThread(threadHandle) == 0xFFFFFFFF){
		ErrorOut("WaitSet::wait() SuspendThread()に失敗しました");
	}
	lock();
}
//------------------------------------------------------------------------------
// 待機
void WaitSet::wait(){
	if(!isLockedByCurrentThread()){
		ErrorOut("WaitSet::wait() "
			"wait()するためにはWaitSetに対するlock()が必要です");
	}
	// カレントスレッドの擬似ハンドルをグローバルなハンドルに変換
	HANDLE threadHandle;
	if(::DuplicateHandle(::GetCurrentProcess(), ::GetCurrentThread(),
		::GetCurrentProcess(), &threadHandle,
		0, false, DUPLICATE_SAME_ACCESS) == 0){
		ErrorOut("WaitSet::wait() DuplicateHandle()に失敗しました");
	}
	waitingThreads_.pushBack(threadHandle);
	unlock();
	if(::SuspendThread(threadHandle) == 0xFFFFFFFF){
		ErrorOut("WaitSet::wait() SuspendThread()に失敗しました");
	}
	lock();
	// ハンドルを複製したので閉じておく
	::CloseHandle(threadHandle);
}
//------------------------------------------------------------------------------
// 待機解除
void WaitSet::notify(){
	if(!isLockedByCurrentThread()){
		ErrorOut("WaitSet::notify() "
			"notify()するためにはWaitSetに対するlock()が必要です");
	}
	if(waitingThreads_.getCount() == 0){ return; }
	HANDLE threadHandle = waitingThreads_.popFront();
	if(::ResumeThread(threadHandle) == 0xFFFFFFFF){
		ErrorOut("WaitSet::notify() ResumeThread()に失敗しました");
	}
}
//------------------------------------------------------------------------------
// 待機全解除
void WaitSet::notifyAll(){
	if(!isLockedByCurrentThread()){
		ErrorOut("WaitSet::notifyAll() "
			"notifyAll()するためにはWaitSetに対するlock()が必要です");
	}
	int threadCount = waitingThreads_.getCount();
	if(threadCount == 0){ return; }
	for(int i = 0; i < threadCount; i++){
		HANDLE threadHandle = waitingThreads_.get(i);
		if(::ResumeThread(threadHandle) == 0xFFFFFFFF){
			ErrorOut("WaitSet::notifyAll() ResumeThread()に失敗しました");
		}
	}
	waitingThreads_.clear();
}
//------------------------------------------------------------------------------
} // End of namespace Lamp
//------------------------------------------------------------------------------
