Ejemplo n.º 1
0
bool CCondvar::CImpl::MWait(CMutex* _mutex, unsigned msec)
{
	int _generation;
	{
		CRAII<CMutex> _block(FMutex);
		++FWaitNum;
		_generation = FWaitGenNum;
	}
	_mutex->MUnlock();
	DWORD ret;
	for (HANG_INIT;;HANG_CHECK)
	{
		ret = ::WaitForSingleObject(FSignalEvent, msec);
		VLOG(2) << "Ret = " << ret;

		CRAII<CMutex> _block(FMutex);
		{
			bool _wait_done=FReleaseCount>0 &&FWaitGenNum!=_generation;
			LOG_IF(DFATAL,((ret != WAIT_TIMEOUT) && (ret!= NO_ERROR)))<<" Error in WaitForSingleObject "<<ret;
			if(_wait_done ||ret == WAIT_TIMEOUT)
			{
				VLOG(2)<<"The condvar is unlocked. Number of Release "<<FReleaseCount//
						<<"; current 'generation' "<<FWaitGenNum//
						<<"; Gen="<<_generation;
				--FWaitNum;
				--FReleaseCount;
				if(FReleaseCount == 0)
					ResetEvent(FSignalEvent);
				break;
			}
			else
			{
				VLOG(1)<<"Does not for me. Number of Release "<<FReleaseCount	//
				<<"; current 'generation' "<<FWaitGenNum//
				<<"; Gen="<<_generation<<"; ret="<<ret<<"; msec="<<msec;
			}
		}
	}
	_mutex->MLock();
/*	bool _last;
	{
		CRAII<CMutex> _block(FMutex);
		--FWaitNum;
		--FReleaseCount;
		_last = FReleaseCount == 0;
	}
	if (_last)
		ResetEvent(FSignalEvent);*/
	return ret != WAIT_TIMEOUT;
}
Ejemplo n.º 2
0
Error StreamPeerWinsock::_poll_connection(bool p_block) const {

	ERR_FAIL_COND_V(status != STATUS_CONNECTING || sockfd == INVALID_SOCKET, FAILED);

	if (p_block) {

		_block(sockfd, false, true);
	};

	struct sockaddr_in their_addr;
	set_addr_in(their_addr, peer_host, peer_port);

	if (::connect(sockfd, (struct sockaddr *)&their_addr,sizeof(struct sockaddr)) == SOCKET_ERROR) {

		int err = WSAGetLastError();
		if (err == WSAEISCONN) {
			status = STATUS_CONNECTED;
			return OK;
		};

		return OK;
	} else {

		status = STATUS_CONNECTED;
		return OK;
	};

	return OK;
};
Ejemplo n.º 3
0
Error StreamPeerWinsock::write(const uint8_t* p_data,int p_bytes, int &r_sent, bool p_block) {

	if (status == STATUS_NONE || status == STATUS_ERROR) {

		return FAILED;
	};

	if (status != STATUS_CONNECTED) {

		if (_poll_connection(p_block) != OK) {

			return FAILED;
		};

		if (status != STATUS_CONNECTED) {
			r_sent = 0;
			return OK;
		};
	};

	int data_to_send = p_bytes;
	const uint8_t *offset = p_data;
	if (sockfd == -1) return FAILED;
	errno = 0;
	int total_sent = 0;

	while (data_to_send) {

		int sent_amount = send(sockfd, (const char*)offset, data_to_send, 0);
		//printf("Sent TCP data of %d bytes, errno %d\n", sent_amount, errno);

		if (sent_amount == -1) {

			if (WSAGetLastError() != WSAEWOULDBLOCK) {

				perror("shit?");
				disconnect();
				ERR_PRINT("Server disconnected!\n");
				return FAILED;
			};

			if (!p_block) {
				r_sent = total_sent;
				return OK;
			};

			_block(sockfd, false, true);
		} else {

			data_to_send -= sent_amount;
			offset += sent_amount;
			total_sent += sent_amount;
		};
	}

	r_sent = total_sent;

	return OK;
};
Ejemplo n.º 4
0
inline oid_t dense_bin_fun::value (oid_t i, oid_t j) const
{
    POMAGMA_ASSERT_RANGE_(5, i, item_dim());
    POMAGMA_ASSERT_RANGE_(5, j, item_dim());

    const oid_t * block = _block(i / ITEMS_PER_BLOCK, j / ITEMS_PER_BLOCK);
    return _block2value(block, i & BLOCK_POS_MASK, j & BLOCK_POS_MASK);
}
Ejemplo n.º 5
0
Error StreamPeerWinsock::read(uint8_t* p_buffer, int p_bytes,int &r_received, bool p_block) {

	if (!is_connected()) {

		return FAILED;
	};

	if (status != STATUS_CONNECTED) {

		if (_poll_connection(p_block) != OK) {

			return FAILED;
		};

		if (status != STATUS_CONNECTED) {
			r_received = 0;
			return OK;
		};
	};

	int to_read = p_bytes;
	int total_read = 0;
	errno = 0;

	while (to_read) {

		int read = recv(sockfd, (char*)p_buffer + total_read, to_read, 0);

		if (read == -1) {

			if (WSAGetLastError() != WSAEWOULDBLOCK) {

				perror("shit?");
				disconnect();
				ERR_PRINT("Server disconnected!\n");
				return FAILED;
			};

			if (!p_block) {

				r_received = total_read;
				return OK;
			};
			_block(sockfd, true, false);
		} else if (read == 0) {
			disconnect();
			return ERR_FILE_EOF;
		} else {

			to_read -= read;
			total_read += read;
		};
	};

	r_received = total_read;

	return OK;
};
Ejemplo n.º 6
0
std::ostream &RaptorQ__v1::Impl::Operation_Block::print (std::ostream &os) const
{
	os << static_cast<uint8_t> (Operation_type::BLOCK);
	// X is a square matrix
	os << static_cast<uint16_t> (_block.cols());
	for (uint16_t row = 0; row < _block.cols(); ++row) {
		for (uint16_t col = 0; col < _block.cols(); ++col)
			os << _block (row, col);
	}
	return os;
}
Ejemplo n.º 7
0
bool CCondvar::CImpl::MSignal()
{
	CRAII<CMutex> _block(FMutex);
	if (FWaitNum > FReleaseCount)
	{
		VLOG(1) << "Do signal...";
		SetEvent(FSignalEvent);
		++FReleaseCount;
		++FWaitGenNum;
		return true;
	}
	VLOG(1) << "There is not expecting thread.";
	return false;
}
Ejemplo n.º 8
0
bool CCondvar::CImpl::MBroadcast(void)
{
	CRAII<CMutex> _block(FMutex);
	if (FWaitNum > 0)
	{
		VLOG(1) << "Do broadcast...";
		SetEvent(FSignalEvent);
		FReleaseCount = FWaitNum;
		++FWaitGenNum;
		return true;
	}
	VLOG(1) << "There is not expecting thread.";
	return false;
}
Ejemplo n.º 9
0
void sthread_t::sleep(timeout_in_ms timeout, const char *reason)
{
    reason = (reason && *reason) ? reason : "sleep";

    /* FRJ: even though we could just use the posix sleep() call,
       we'll stick to the sthreads way and block on a cond
       var. That way the sthreads debug stuff will keep
       working. Besides, we're here to waste time, right?
    */
    CRITICAL_SECTION(cs, _wait_lock);
    _sleeping = true;
    (void) _block(timeout, reason, this); // W_IGNORE
    _sleeping = false;
}
Ejemplo n.º 10
0
void CanvasItem::_propagate_visibility_changed(bool p_visible) {

	notification(NOTIFICATION_VISIBILITY_CHANGED);

	if (p_visible)
		update(); //todo optimize
	else
		emit_signal(SceneStringNames::get_singleton()->hide);
	_block();

	for (int i = 0; i < get_child_count(); i++) {

		CanvasItem *c = Object::cast_to<CanvasItem>(get_child(i));

		if (c && c->visible) //should the toplevels stop propagation? i think so but..
			c->_propagate_visibility_changed(p_visible);
	}

	_unblock();
}
Ejemplo n.º 11
0
void IMPL_CLASS::MDisconnect()
{
	VLOG(2) << "Disconnect client " << FMyInfo->FInfo.FId;
	NSHARE::CRAII<CMutex> _block(FConnectMutex);
	if(!MIsConnected() || !MIsOpen())
	{
		LOG(INFO) << "It's not connected."<< MIsOpen();
		return;
	}

	event_info_t _info;
	_info.FEventType = event_info_t::E_DISCONECT;
	_info.FConnect.FClientOffset =static_cast<CSharedAllocator::offset_t>( FSharedMemory.MGetAllocator()->MOffset(
			FMyInfo));
	MInvokeEvent(&_info);

	LOG_IF(DFATAL,FEventDone) << "The event handler is not working";

	if (!FEventDone)
	{
		double const _time = NSHARE::get_time();
		VLOG(2) << "Wait for disconnected:" << _time;
		for (; FIsConnected && ((NSHARE::get_time() - _time) <= 1.);
				NSHARE::usleep(1000))
		{

		}
		VLOG(2) << "End Wait for disconnected:" << NSHARE::get_time()
		<< " FIsConnected=" << FIsConnected;
	}
	LOG_IF(ERROR,FIsConnected) << "The event \"Disconnected\" is not handled";
	if (FIsConnected)
	{
		CHECK_NOTNULL(FServerInfo);
		if (!FEventDone)
		MStopEventHandlerForce();
		MEventDisconnected(FServerInfo->FInfo.FId.MGetId(), 0);
	}
	VLOG(2) << "The client is disconnected successfully.";
}
Ejemplo n.º 12
0
void CCCallBlock::execute()
{
    if (_block) _block();
}
Ejemplo n.º 13
0
	void base_channel::wait()
	{
		_block();
	}
Ejemplo n.º 14
0
bool IMPL_CLASS::MConnect(double aTime)
{
	if (!MIsOpen())
	{
		LOG(ERROR)<<"The shared client is not opened.";
		return false;
	}
	CHECK_NOTNULL(FMyInfo);
	CHECK_NOTNULL(FEv.FEvents);
	CHECK_NOTNULL(FServerInfo);
	NSHARE::CRAII<CMutex> _block(FConnectMutex);
	if (MIsConnected())
	{
		LOG(ERROR)<<"connected already";
		return true;
	}
	VLOG(2) << "Connect "<<FMyInfo->FInfo.FId<<" Time=" << aTime;
	event_info_t _info;
	_info.FEventType = event_info_t::E_CONNECT;
	_info.FConnect.FClientOffset = static_cast<CSharedAllocator::offset_t>(FSharedMemory.MGetAllocator()->MOffset(FMyInfo));
	MInvokeEvent(&_info);

	_info.FEventType = event_info_t::E_NO;
	double  _time=NSHARE::get_time();
	bool _is_not_timeout=false;
	bool _is_try_again=false;
	do
	{
		for(;(NSHARE::get_time()-_time)<aTime && MIsOpen();)
		{
			_is_not_timeout=MWaitForEvent(FEv,&_info, aTime);
			VLOG_IF(2,_is_not_timeout)<<"Event recv="<<_info;
			if(!_is_not_timeout)
			{
				VLOG(2)<<"Event connecting is not received";
				continue;
			}
			if(_info.FEventType == event_info_t::E_CONNECTED)
				break;
			else
			{
				LOG(WARNING)<<"Receive unknown event "<<_info;
			}
		}

		if (_is_not_timeout)
		{
			if(_info.FEventType == event_info_t::E_CONNECTED)
			{
				LOG(INFO)<<"Connected";
				MEventConnected(_info.FIdFrom.MGetId(),_info.FConnect.FClientOffset);
				break;
			}
			else
				CHECK(false);
		}
		else if(MRemoveEvent(&_info,FEv))
		{
			LOG(ERROR)<<"Cannot connect";
			CHECK_EQ(FMyInfo->FInfo.FId.FUniqueID,0);
			break;
		}else
		{
			CHECK_NE(_info.FEventType, event_info_t::E_CONNECTED);
			if(_is_try_again)
			{
				DCHECK_EQ(FMyInfo->FInfo.FId.FUniqueID,0);
				if(FMyInfo->FInfo.FId.FUniqueID==0)
					return false;
				break;
			}
			_is_try_again=true;
			LOG(ERROR)<<"The server is handling the connection";
			 _time=NSHARE::get_time();
			 continue;
		}
	}while(!FIsConnected && MIsOpen());

	return FIsConnected;
}