<commit_before>//
// C++ Implementation: TaskManager
//
// Description: 
//
//
// Author: <NAME> <<EMAIL>>, (C) 2010
//
//

#include "TaskManager.h"
#include "Lockable.h"
#include <pthread.h>
#include <stack>
#include <stdio.h>
#include <assert.h>

#undef DEBUG

#ifndef RT_THREAD_COUNT
#define RT_THREAD_COUNT 2
#endif

#ifdef MACOSX

#include <dispatch/dispatch.h>

class Semaphore
{
public:
	Semaphore(unsigned inStartingValue=0) : mSema(dispatch_semaphore_create((long)inStartingValue)) {}
	~Semaphore() { mSema = NULL; }
	void wait() { dispatch_semaphore_wait(mSema, DISPATCH_TIME_FOREVER); }	// each thread will wait on this
	void post() { dispatch_semaphore_signal(mSema); }	// when done, each thread calls this
private:
	dispatch_semaphore_t	mSema;
};

#else

#include <semaphore.h>

class Semaphore
{
public:
	Semaphore(unsigned inStartingValue=0) { sem_init(&mSema, 0, inStartingValue); }
	~Semaphore() { sem_destroy(&mSema); }
	void wait() { sem_wait(&mSema); }	// each thread will wait on this
	void post() { sem_post(&mSema); }	// when done, each thread calls this
private:
	sem_t	mSema;
};

#endif

class Notifiable {
public:
	virtual void notify(int inIndex) = 0;
};

class Notifier {
public:
	Notifier(Notifiable *inTarget, int inIndex) : mTarget(inTarget), mIndex(inIndex) {}
	void notify() { mTarget->notify(mIndex); }
private:
	Notifiable	*mTarget;
	int 		mIndex;
};

class TaskThread : public Notifier
{
public:
	TaskThread(Notifiable *inTarget, int inIndex)
		: Notifier(inTarget, inIndex), mTask(NULL)
	{
		pthread_create(&mThread, NULL, sProcess, this);
	}
	~TaskThread() { start(NULL); pthread_join(mThread, NULL); }
	inline void start(Task *inTask);
protected:
	void run();
	static void *sProcess(void *inContext);
private:
	pthread_t	mThread;
	Semaphore	mSema;
	Task *		mTask;
};

inline void TaskThread::start(Task *inTask)
{
	mTask = inTask;
#ifdef DEBUG
	printf("TaskThread::start(%p): posting for Task %p\n", this, inTask);
#endif
	mSema.post();
}

void TaskThread::run()
{
#ifdef DEBUG
	printf("TaskThread %p running\n", this);
#endif
	bool done = false;
	do {
		mSema.wait();
#ifdef DEBUG
		printf("TaskThread %p woke up -- about to run mTask %p\n", this, mTask);
#endif
		if (mTask) {
			mTask->run();
			delete mTask;
		}
		else {
			done = true;
		}
		notify();
	}
	while (!done);
#ifdef DEBUG
	printf("TaskThread %p exiting\n", this);
#endif
}

void *TaskThread::sProcess(void *inContext)
{
	TaskThread *This = (TaskThread *) inContext;
	This->run();
	return NULL;
}

class ThreadPool : private Lockable, private Notifiable
{
public:<commit_msg>Setting thread scheduling and priority.<commit_after>//
// C++ Implementation: TaskManager
//
// Description: 
//
//
// Author: <NAME> <<EMAIL>>, (C) 2010
//
//

#include "TaskManager.h"
#include "Lockable.h"
#include <pthread.h>
#include <stack>
#include <stdio.h>
#include <assert.h>

#undef DEBUG

#ifndef RT_THREAD_COUNT
#define RT_THREAD_COUNT 2
#endif

#ifdef MACOSX

#include <dispatch/dispatch.h>

class Semaphore
{
public:
	Semaphore(unsigned inStartingValue=0) : mSema(dispatch_semaphore_create((long)inStartingValue)) {}
	~Semaphore() { mSema = NULL; }
	void wait() { dispatch_semaphore_wait(mSema, DISPATCH_TIME_FOREVER); }	// each thread will wait on this
	void post() { dispatch_semaphore_signal(mSema); }	// when done, each thread calls this
private:
	dispatch_semaphore_t	mSema;
};

#else

#include <semaphore.h>

class Semaphore
{
public:
	Semaphore(unsigned inStartingValue=0) { sem_init(&mSema, 0, inStartingValue); }
	~Semaphore() { sem_destroy(&mSema); }
	void wait() { sem_wait(&mSema); }	// each thread will wait on this
	void post() { sem_post(&mSema); }	// when done, each thread calls this
private:
	sem_t	mSema;
};

#endif

class Notifiable {
public:
	virtual void notify(int inIndex) = 0;
};

class Notifier {
public:
	Notifier(Notifiable *inTarget, int inIndex) : mTarget(inTarget), mIndex(inIndex) {}
	void notify() { mTarget->notify(mIndex); }
private:
	Notifiable	*mTarget;
	int 		mIndex;
};

class TaskThread : public Notifier
{
public:
	TaskThread(Notifiable *inTarget, int inIndex)
		: Notifier(inTarget, inIndex), mTask(NULL)
	{
        pthread_attr_t attr;
        pthread_attr_init(&attr);
        pthread_attr_setschedpolicy(&attr, SCHED_RR);
		pthread_create(&mThread, &attr, sProcess, this);
        pthread_attr_destroy(&attr);
	}
	~TaskThread() { start(NULL); pthread_join(mThread, NULL); }
	inline void start(Task *inTask);
protected:
	void run();
	static void *sProcess(void *inContext);
private:
	pthread_t	mThread;
	Semaphore	mSema;
	Task *		mTask;
};

inline void TaskThread::start(Task *inTask)
{
	mTask = inTask;
#ifdef DEBUG
	printf("TaskThread::start(%p): posting for Task %p\n", this, inTask);
#endif
	mSema.post();
}

void TaskThread::run()
{
#ifdef DEBUG
	printf("TaskThread %p running\n", this);
#endif
	bool done = false;
	do {
		mSema.wait();
#ifdef DEBUG
		printf("TaskThread %p woke up -- about to run mTask %p\n", this, mTask);
#endif
		if (mTask) {
			mTask->run();
			delete mTask;
		}
		else {
			done = true;
		}
		notify();
	}
	while (!done);
#ifdef DEBUG
	printf("TaskThread %p exiting\n", this);
#endif
}

void *TaskThread::sProcess(void *inContext)
{
	TaskThread *This = (TaskThread *) inContext;
    if (setpriority(PRIO_PROCESS, 0, -20) != 0) {
        perror("TaskThread::sProcess: setpriority() failed.");
    }
	This->run();
	return NULL;
}

class ThreadPool : private Lockable, private Notifiable
{
public:
{'fraction_non_alphanumeric': 0.14646649578908824, 'fraction_numerical': 0.004027828634199927, 'mean_word_length': 4.696558915537017, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 27, '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': '5870419', 'n_tokens_mistral': 2093, 'n_tokens_neox': 2028, 'n_words': 503}