<commit_before>/*
 * linuxWaitingSlot.cpp
 *
 *  Created on: 12. 10. 2017
 *      Author: ondra
 */


#include <fcntl.h>
#include <sys/socket.h>

#include <unistd.h>

#include "../exceptions.h"
#include "../mt.h"
#include "netEventDispatcher.h"
#include "../defer.h"

namespace simpleServer {

using ondra_shared::defer;

static LinuxEventDispatcher::Task empty_task([](AsyncState){},asyncOK);


LinuxEventDispatcher::LinuxEventDispatcher() {
	int fds[2];
	if (pipe2(fds, O_CLOEXEC)!=0) {
		int err = errno;
		throw SystemException(err,"Failed to call pipe2 (LinuxEventDispatcher)");
	}
	intrHandle = fds[1];
	intrWaitHandle = fds[0];


	addIntrWaitHandle();

	exitFlag = false;
}

LinuxEventDispatcher::~LinuxEventDispatcher() noexcept {
	close(intrHandle);
	close(intrWaitHandle);
}

LinuxEventDispatcher::Task LinuxEventDispatcher::runQueue() {
	std::lock_guard<std::mutex> _(queueLock);
	char b;
	::read(intrWaitHandle, &b, 1);

	if (!queue.empty()) {
		const RegReq &r = queue.front();
		if (r.ares.socket == 0 && r.ares.socket == 0) {
			return Task(r.extra.completionFn,asyncOK);
		} else if (r.extra.completionFn == nullptr) {
			return findAndCancel(r.ares);
		} else {
			addResource(r);
			queue.pop();
		}
	}
	return Task();
}

LinuxEventDispatcher::Task LinuxEventDispatcher::findAndCancel(const AsyncResource &res) {
	CompletionFn curFn = nullptr;
	int cnt = (int)fdmap.size();
	for (int i = 0; i < cnt; i++) {
		if (fdmap[i].fd == res.socket && fdmap[i].events == res.op) {
			if (curFn ==nullptr) curFn = fdextramap[i].completionFn;
			else {<commit_msg>ignore result of read operation<commit_after>/*
 * linuxWaitingSlot.cpp
 *
 *  Created on: 12. 10. 2017
 *      Author: ondra
 */


#include <fcntl.h>
#include <sys/socket.h>

#include <unistd.h>

#include "../exceptions.h"
#include "../mt.h"
#include "netEventDispatcher.h"
#include "../defer.h"

namespace simpleServer {

using ondra_shared::defer;

static LinuxEventDispatcher::Task empty_task([](AsyncState){},asyncOK);


LinuxEventDispatcher::LinuxEventDispatcher() {
	int fds[2];
	if (pipe2(fds, O_CLOEXEC)!=0) {
		int err = errno;
		throw SystemException(err,"Failed to call pipe2 (LinuxEventDispatcher)");
	}
	intrHandle = fds[1];
	intrWaitHandle = fds[0];


	addIntrWaitHandle();

	exitFlag = false;
}

LinuxEventDispatcher::~LinuxEventDispatcher() noexcept {
	close(intrHandle);
	close(intrWaitHandle);
}

template<typename T>
void ignore_variable(T) {}

LinuxEventDispatcher::Task LinuxEventDispatcher::runQueue() {
	std::lock_guard<std::mutex> _(queueLock);
	char b;
	ignore_variable(::read(intrWaitHandle, &b, 1));



	if (!queue.empty()) {
		const RegReq &r = queue.front();
		if (r.ares.socket == 0 && r.ares.socket == 0) {
			return Task(r.extra.completionFn,asyncOK);
		} else if (r.extra.completionFn == nullptr) {
			return findAndCancel(r.ares);
		} else {
			addResource(r);
			queue.pop();
		}
	}
	return Task();
}

LinuxEventDispatcher::Task LinuxEventDispatcher::findAndCancel(const AsyncResource &res) {
	CompletionFn curFn = nullptr;
	int cnt = (int)fdmap.size();
	for (int i = 0; i < cnt; i++) {
		if (fdmap[i].fd == res.socket && fdmap[i].events == res.op) {
			if (curFn ==nullptr) curFn = fdextramap[i].completionFn;
			else {
{'fraction_non_alphanumeric': 0.1642901425914445, 'fraction_numerical': 0.011159330440173589, 'mean_word_length': 4.671353251318102, 'pattern_counts': {'":': 0, '<': 14, '<?xml version=': 0, '>': 12, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1552747', 'n_tokens_mistral': 1265, 'n_tokens_neox': 1146, 'n_words': 242}