<commit_before>#include <stdio.h>
#include <algorithm> // for std::swap
#include "allocore/system/al_Config.h"
#include "allocore/system/al_Thread.hpp"

#ifdef AL_WINDOWS
	#define USE_THREADEX
#else
	#define USE_PTHREAD
#endif

namespace al {

#ifdef USE_PTHREAD
#include <pthread.h>

//typedef pthread_t ThreadHandle;
//typedef void * (*ThreadFunction)(void *);
//#define THREAD_FUNCTION(name) void * name(void * user)

struct Thread::Impl{
	Impl()
	:	mHandle(0)
	{ //printf("Thread::Impl(): %p\n", this);
		pthread_attr_init(&mAttr);

		// threads are not required to be joinable by default, so make it so
		pthread_attr_setdetachstate(&mAttr, PTHREAD_CREATE_JOINABLE);
	}

	~Impl(){ //printf("Thread::~Impl(): %p\n", this);
		pthread_attr_destroy(&mAttr);
	}


	bool start(ThreadFunction& func){
		if(mHandle) return false;
		//return 0 == pthread_create(&mHandle, NULL, cThreadFunc, &func);
		return 0 == pthread_create(&mHandle, &mAttr, cThreadFunc, &func);
	}

	bool start(std::function<void(void)>& func){
		if(mHandle) return false;
		struct F{
			static void * cFunc(void * user){
				(*((std::function<void(void)> *)user))();
				return NULL;
			}
		};
		return 0 == pthread_create(&mHandle, &mAttr, F::cFunc, &func);
	}

	bool join(double timeout){
		if(pthread_join(mHandle, NULL) == 0){
			mHandle = 0;
			return true;
		}
		return false;
	}

	void priority(int v){
		struct sched_param param;
		if(v >= 1 && v <= 99){
			param.sched_priority = v;
			//pthread_setschedparam(mHandle, SCHED_FIFO, &param);
			// FIFO and RR (round-robin) are for real-time scheduling
			pthread_attr_setschedpolicy(&mAttr, SCHED_FIFO);
			//pthread_attr_setschedpolicy(&mAttr, SCHED_RR);
			pthread_attr_setschedparam(&mAttr, &param);
		}
		else{
			param.sched_priority = 0;
			//pthread_setschedparam(mHandle, SCHED_OTHER, &param);
			pthread_attr_setschedpolicy(&mAttr, SCHED_OTHER);
			pthread_attr_setschedparam(&mAttr, &param);
		}
	}

//	bool cancel(){
//		return 0 == pthread_cancel(mHandle);
//	}
//
//	void testCancel(){
//		pthread_testcancel();
//	}

	void * handle(){ return &mHandle; }

	pthread_t mHandle;
	pthread_attr_t mAttr;

	static void * cThreadFunc(void * user){
		ThreadFunction& tfunc = *((ThreadFunction*)user);
		tfunc();
		return NULL;
	}
};


void * Thread::current(){
	// pthread_t pthread_self(void);
	static pthread_t r;
	r = pthread_self();
	return (void*)(&r);
}


#elif defined(USE_THREADEX)

#define WIN32_MEAN_AND_LEAN
#include <windows.h>
#include <process.h>

class Thread::Impl{<commit_msg>Thread: cleanup pthread impl<commit_after>#include <stdio.h>
#include <algorithm> // for std::swap
#include "allocore/system/al_Config.h"
#include "allocore/system/al_Thread.hpp"

#ifdef AL_WINDOWS
	#define USE_THREADEX
#else
	#define USE_PTHREAD
#endif

namespace al {

#ifdef USE_PTHREAD
#include <pthread.h>

struct Thread::Impl{
	Impl()
	:	mHandle(0)
	{ //printf("Thread::Impl(): %p\n", this);
		pthread_attr_init(&mAttr);

		// threads are not required to be joinable by default, so make it so
		pthread_attr_setdetachstate(&mAttr, PTHREAD_CREATE_JOINABLE);
	}

	~Impl(){ //printf("Thread::~Impl(): %p\n", this);
		pthread_attr_destroy(&mAttr);
	}

	bool start(std::function<void(void)>& func){
		if(mHandle) return false;
		struct F{
			static void * cFunc(void * user){
				(*((std::function<void(void)> *)user))();
				return NULL;
			}
		};
		return 0 == pthread_create(&mHandle, &mAttr, F::cFunc, &func);
	}

	bool join(double timeout){
		if(pthread_join(mHandle, NULL) == 0){
			mHandle = 0;
			return true;
		}
		return false;
	}

	void priority(int v){
		struct sched_param param;
		if(v >= 1 && v <= 99){
			param.sched_priority = v;
			//pthread_setschedparam(mHandle, SCHED_FIFO, &param);
			// FIFO and RR (round-robin) are for real-time scheduling
			pthread_attr_setschedpolicy(&mAttr, SCHED_FIFO);
			//pthread_attr_setschedpolicy(&mAttr, SCHED_RR);
			pthread_attr_setschedparam(&mAttr, &param);
		}
		else{
			param.sched_priority = 0;
			//pthread_setschedparam(mHandle, SCHED_OTHER, &param);
			pthread_attr_setschedpolicy(&mAttr, SCHED_OTHER);
			pthread_attr_setschedparam(&mAttr, &param);
		}
	}

//	bool cancel(){
//		return 0 == pthread_cancel(mHandle);
//	}
//
//	void testCancel(){
//		pthread_testcancel();
//	}

	void * handle(){ return &mHandle; }

	pthread_t mHandle;
	pthread_attr_t mAttr;
};


void * Thread::current(){
	// pthread_t pthread_self(void);
	static pthread_t r;
	r = pthread_self();
	return (void*)(&r);
}


#elif defined(USE_THREADEX)

#define WIN32_MEAN_AND_LEAN
#include <windows.h>
#include <process.h>

class Thread::Impl{
{'fraction_non_alphanumeric': 0.156671012603216, 'fraction_numerical': 0.005215123859191656, 'mean_word_length': 4.532451923076923, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 19, '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': '396988', 'n_tokens_mistral': 1888, 'n_tokens_neox': 1721, 'n_words': 365}