<commit_before>/* FiberTaskingLib - A tasking library that uses fibers for efficient task switching
 *
 * This library was created as a proof of concept of the ideas presented by
 * <NAME> in his 2015 GDC Talk 'Parallelizing the Naughty Dog Engine Using Fibers'
 *
 * http://gdcvault.com/play/1022186/Parallelizing-the-Naughty-Dog-Engine
 *
 * FiberTaskingLib is the legal property of <NAME>
 * Copyright <NAME> 2015
 */

#pragma once

#include "fiber_tasking_lib/portability.h"

#include <cstddef>


#if defined(_MSC_VER)

// VC++ implementation
#define WIN32_LEAN_AND_MEAN
#include <windows.h>

namespace FiberTaskingLib {

typedef void (STDCALL *FiberStartRoutine)(void *arg);
typedef void * fiber_arg_t;
#define FIBER_START_FUNCTION(functionName) void STDCALL functionName(void *arg)
#define FIBER_START_FUNCTION_CLASS_IMPL(className, functionName) void className::functionName(void *arg)

typedef void * FiberId;

inline FiberId FTLConvertThreadToFiber() {
	return ConvertThreadToFiberEx(nullptr, FIBER_FLAG_FLOAT_SWITCH);
}
inline void FTLConvertFiberToThread() {
	ConvertFiberToThread();
}

inline FiberId FTLCreateFiber(size_t stackSize, FiberStartRoutine startRoutine, fiber_arg_t arg) {
	return CreateFiberEx(stackSize, 0, FIBER_FLAG_FLOAT_SWITCH, startRoutine, arg);
}
inline void FTLDeleteFiber(FiberId fiber) {
	DeleteFiber(fiber);
}

inline void FTLSwitchToFiber(FiberId currentFiber, FiberId destFiber) {
	SwitchToFiber(destFiber);
}

} // End of namespace FiberTaskingLib

#else
// Boost.Context implementation
#define BOOST_CONTEXT

#include <boost/context/fcontext.hpp>
#include <boost/context/fixedsize_stack.hpp>

namespace FiberTaskingLib {

typedef void (*FiberStartRoutine)(intptr_t arg);
typedef intptr_t fiber_arg_t;
#define FIBER_START_FUNCTION(functionName) void functionName(intptr_t arg)
#define FIBER_START_FUNCTION_CLASS_IMPL(className, functionName) void className::functionName(intptr_t arg)

class Fiber {
public:
	Fiber()
		: m_stack(nullptr) {
	}
	Fiber(std::size_t stackSize, FiberStartRoutine startRoutine, fiber_arg_t arg)
			: m_stack(new boost::context::fixedsize_stack(stackSize)),
			  m_stackContext(m_stack->allocate()),
			  m_arg(arg) {
		m_fiberContext = boost::context::make_fcontext(m_stackContext.sp, m_stackContext.size, startRoutine);
	}
	~Fiber() {
		if (m_stack) {
			m_stack->deallocate(m_stackContext);
		}
		delete m_stack;
	}

private:
	boost::context::fcontext_t m_fiberContext;
	boost::context::fixedsize_stack *m_stack;
	boost::context::stack_context m_stackContext;
	fiber_arg_t m_arg;

public:
	inline void SwitchToFiber(Fiber *fiber) {
		boost::context::jump_fcontext(&m_fiberContext, fiber->m_fiberContext, m_arg);
	}
};

typedef Fiber * FiberId;


inline FiberId FTLConvertThreadToFiber() {
	return new Fiber();
}
inline void FTLConvertFiberToThread() {<commit_msg>FIBER_TASKING_LIB: Pass the destination fiber arg rather than the source<commit_after>/* FiberTaskingLib - A tasking library that uses fibers for efficient task switching
 *
 * This library was created as a proof of concept of the ideas presented by
 * <NAME> in his 2015 GDC Talk 'Parallelizing the Naughty Dog Engine Using Fibers'
 *
 * http://gdcvault.com/play/1022186/Parallelizing-the-Naughty-Dog-Engine
 *
 * FiberTaskingLib is the legal property of <NAME>
 * Copyright <NAME> 2015
 */

#pragma once

#include "fiber_tasking_lib/portability.h"

#include <cstddef>


#if defined(_MSC_VER)

// VC++ implementation
#define WIN32_LEAN_AND_MEAN
#include <windows.h>

namespace FiberTaskingLib {

typedef void (STDCALL *FiberStartRoutine)(void *arg);
typedef void * fiber_arg_t;
#define FIBER_START_FUNCTION(functionName) void STDCALL functionName(void *arg)
#define FIBER_START_FUNCTION_CLASS_IMPL(className, functionName) void className::functionName(void *arg)

typedef void * FiberId;

inline FiberId FTLConvertThreadToFiber() {
	return ConvertThreadToFiberEx(nullptr, FIBER_FLAG_FLOAT_SWITCH);
}
inline void FTLConvertFiberToThread() {
	ConvertFiberToThread();
}

inline FiberId FTLCreateFiber(size_t stackSize, FiberStartRoutine startRoutine, fiber_arg_t arg) {
	return CreateFiberEx(stackSize, 0, FIBER_FLAG_FLOAT_SWITCH, startRoutine, arg);
}
inline void FTLDeleteFiber(FiberId fiber) {
	DeleteFiber(fiber);
}

inline void FTLSwitchToFiber(FiberId currentFiber, FiberId destFiber) {
	SwitchToFiber(destFiber);
}

} // End of namespace FiberTaskingLib

#else
// Boost.Context implementation
#define BOOST_CONTEXT

#include <boost/context/fcontext.hpp>
#include <boost/context/fixedsize_stack.hpp>

namespace FiberTaskingLib {

typedef void (*FiberStartRoutine)(intptr_t arg);
typedef intptr_t fiber_arg_t;
#define FIBER_START_FUNCTION(functionName) void functionName(intptr_t arg)
#define FIBER_START_FUNCTION_CLASS_IMPL(className, functionName) void className::functionName(intptr_t arg)

class Fiber {
public:
	Fiber()
		: m_stack(nullptr) {
	}
	Fiber(std::size_t stackSize, FiberStartRoutine startRoutine, fiber_arg_t arg)
			: m_stack(new boost::context::fixedsize_stack(stackSize)),
			  m_stackContext(m_stack->allocate()),
			  m_arg(arg) {
		m_fiberContext = boost::context::make_fcontext(m_stackContext.sp, m_stackContext.size, startRoutine);
	}
	~Fiber() {
		if (m_stack) {
			m_stack->deallocate(m_stackContext);
		}
		delete m_stack;
	}

private:
	boost::context::fcontext_t m_fiberContext;
	boost::context::fixedsize_stack *m_stack;
	boost::context::stack_context m_stackContext;
	fiber_arg_t m_arg;

public:
	inline void SwitchToFiber(Fiber *fiber) {
		boost::context::jump_fcontext(&m_fiberContext, fiber->m_fiberContext, fiber->m_arg, true);
	}
};

typedef Fiber * FiberId;


inline FiberId FTLConvertThreadToFiber() {
	return new Fiber();
}
inline void FTLConvertFiberToThread() {
{'fraction_non_alphanumeric': 0.09832635983263599, 'fraction_numerical': 0.006276150627615063, 'mean_word_length': 6.628989361702128, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 24, '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': '2002311', 'n_tokens_mistral': 2100, 'n_tokens_neox': 1968, 'n_words': 434}