<commit_before>#ifndef __BLADEPSGI_MAIN_HEADER__
#define __BLADEPSGI_MAIN_HEADER__

// This will probably do for now
#define Assert(cond) assert(cond)

#include <atomic>
#include <array>
#include <cassert>
#include <cstddef>
#include <cstdarg>
#include <cstdlib>
#include <cstdio>
#include <cstring>
#include <exception>
#include <fcntl.h>
#include <memory>
#include <stdexcept>
#include <vector>
#include <signal.h>
#include <string>
#include <sys/errno.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <sys/shm.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>

#include "make_unique.hpp"
using std::unique_ptr;


typedef int WorkerNo;

class SyscallException {
public:
	SyscallException(const char *syscall, int s_errno);
	SyscallException(const char *syscall, const char *fmt, ...);
	~SyscallException();

	const char *syscall() const;
	const char *strerror() const;

private:
	const char *syscall_;

	// Only one of these is set
	int s_errno_;
	const char *strerror_;
};

class BPSGISemaphore {
public:
	BPSGISemaphore(void *ptr, std::string name, int64_t value);

	int64_t Read();
	bool TryAcquire();
	void Release();

	std::string name() const { return name_; }

private:
	std::atomic<int_fast64_t> *ptr_;
	std::string name_;
	int64_t init_value_;
};

class BPSGIAtomicInt64 {
public:
	BPSGIAtomicInt64(void *ptr, std::string name, int64_t value);

	std::string name() const { return name_; }

private:
	std::atomic<int64_t> *ptr_;
	std::string name_;
};

class BPSGISharedMemory {
public:

public:
	BPSGISharedMemory(void *shared_memory_segment, size_t shmem_size);

	void LockAllocations();
	void *AllocateUserShmem(size_t size);
	BPSGISemaphore *NewSemaphore(std::string name, int64_t value);
	int64_t *NewAtomicInt64(std::string name, int64_t value);

	std::atomic<int_fast8_t> *WorkerArraySegment() const;
	int_fast8_t GetWorkerStatus(WorkerNo workerno) const;
	void GetAllWorkerStatuses(int nworkers, char *out) const;

	int_fast64_t IncreaseRequestCounter();
	bool SetShouldExitImmediately();
	bool ShouldExitImmediately() const;

private:
	char   *shared_memory_segment_;
	size_t	shmem_size_;
	bool	locked_;
	size_t	next_user_available_offset_;

	std::vector<unique_ptr<BPSGISemaphore>> semaphores_;<commit_msg>Friends FeelsAmazingMan<commit_after>#ifndef __BLADEPSGI_MAIN_HEADER__
#define __BLADEPSGI_MAIN_HEADER__

// This will probably do for now
#define Assert(cond) assert(cond)

#include <atomic>
#include <array>
#include <cassert>
#include <cstddef>
#include <cstdarg>
#include <cstdlib>
#include <cstdio>
#include <cstring>
#include <exception>
#include <fcntl.h>
#include <memory>
#include <stdexcept>
#include <vector>
#include <signal.h>
#include <string>
#include <sys/errno.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <sys/shm.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>

#include "make_unique.hpp"
using std::unique_ptr;


typedef int WorkerNo;

class SyscallException {
public:
	SyscallException(const char *syscall, int s_errno);
	SyscallException(const char *syscall, const char *fmt, ...);
	~SyscallException();

	const char *syscall() const;
	const char *strerror() const;

private:
	const char *syscall_;

	// Only one of these is set
	int s_errno_;
	const char *strerror_;
};

class BPSGISemaphore {
public:
	BPSGISemaphore(void *ptr, std::string name, int64_t value);

	int64_t Read();
	bool TryAcquire();
	void Release();

	std::string name() const { return name_; }

private:
	std::atomic<int_fast64_t> *ptr_;
	std::string name_;
	int64_t init_value_;
};

class BPSGIAtomicInt64 {
public:
	BPSGIAtomicInt64(void *ptr, std::string name, int64_t value);

	std::string name() const { return name_; }

private:
	std::atomic<int64_t> *ptr_;
	std::string name_;
};

class BPSGISharedMemory {
	friend class BPSGIMainApplication;

public:
	BPSGISharedMemory(void *shared_memory_segment, size_t shmem_size);

	void *AllocateUserShmem(size_t size);
	BPSGISemaphore *NewSemaphore(std::string name, int64_t value);
	int64_t *NewAtomicInt64(std::string name, int64_t value);

	std::atomic<int_fast8_t> *WorkerArraySegment() const;
	int_fast8_t GetWorkerStatus(WorkerNo workerno) const;
	void GetAllWorkerStatuses(int nworkers, char *out) const;

	int_fast64_t IncreaseRequestCounter();
	bool SetShouldExitImmediately();
	bool ShouldExitImmediately() const;

protected:
	void LockAllocations();

private:
	char   *shared_memory_segment_;
	size_t	shmem_size_;
	bool	locked_;
	size_t	next_user_available_offset_;

	std::vector<unique_ptr<BPSGISemaphore>> semaphores_;
{'fraction_non_alphanumeric': 0.1210456942003515, 'fraction_numerical': 0.012302284710017574, 'mean_word_length': 6.500823723228995, 'pattern_counts': {'":': 0, '<': 57, '<?xml version=': 0, '>': 57, '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': '1602024', 'n_tokens_mistral': 1769, 'n_tokens_neox': 1658, 'n_words': 407}