/*
    Living Realms is a primate evolution simulator.
    Copyright (C) 2011  Sterling Pickens

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

#include "Globals.h"

#ifndef LRSTATS_COMPILE
pthread_mutex_t Mutex1 = PTHREAD_MUTEX_INITIALIZER;
pthread_mutex_t Mutex2 = PTHREAD_MUTEX_INITIALIZER;
pthread_mutex_t Mutex3 = PTHREAD_MUTEX_INITIALIZER;
#ifndef WITHOUT_HOST_PT_BARRIER
pthread_barrier_t Barrier1;
pthread_barrier_t Barrier2;
pthread_barrier_t Barrier3;
#else
lrpthread_barrier_t Barrier1;
lrpthread_barrier_t Barrier2;
lrpthread_barrier_t Barrier3;
#endif
pthread_mutex_t Region_Mutexes[HEIGHT/REGION_SIZE];
#endif
unsigned char Region_Handled[HEIGHT/REGION_SIZE];
unsigned char **Alive;
unsigned char **Fought;
unsigned char **Mated;
double Backup_Freq_Runtime;
double Year_Runtime;
uint32_t Backup_Freq;
char *filename_backup_load;
char *filename_backup_save;
char *filename_dem;
uint32_t Threads;
struct timeval Year_Starttime,Year_Endtime;
struct Species ***life;
struct Stats *stats;
struct Stats_T stats_t;
struct image_attr *ia;
char *life_speed_up_X;
char *life_speed_up_Y;
char **thread_status;
int No_Exit;
struct Thread_I *Actions_t_info;
int Stats_Handled;

void SET_ONE(unsigned char **array, size_t src_y, size_t src_x){
	size_t byte_x = src_x>>3;
	unsigned char bit = src_x % 8;
	unsigned char cur = array[src_y][byte_x];
	unsigned char b0 = (bit == 0) ? 1   : (((cur<<7) & 128) >= 128);
	unsigned char b1 = (bit == 1) ? 2   : (((cur<<6) & 128) >= 128)<<1;
	unsigned char b2 = (bit == 2) ? 4   : (((cur<<5) & 128) >= 128)<<2;
	unsigned char b3 = (bit == 3) ? 8   : (((cur<<4) & 128) >= 128)<<3;
	unsigned char b4 = (bit == 4) ? 16  : (((cur<<3) & 128) >= 128)<<4;
	unsigned char b5 = (bit == 5) ? 32  : (((cur<<2) & 128) >= 128)<<5;
	unsigned char b6 = (bit == 6) ? 64  : (((cur<<1) & 128) >= 128)<<6;
	unsigned char b7 = (bit == 7) ? 128 : (((cur) & 128)  >= 128)<<7;

	array[src_y][byte_x] = b0+b1+b2+b3+b4+b5+b6+b7;
}

char IS_SET(unsigned char **array, size_t src_y, size_t src_x){
	unsigned char bit = src_x % 8;
	unsigned char cur = array[src_y][(src_x>>3)];
	cur <<= (7-bit);
	cur &= 128;

	if(cur >= 128)
		return 1;
	else
		return 0;
}

#if ( defined(WITHOUT_HOST_PT_BARRIER) && !defined(LRSTATS_COMPILE) )

int lrpthread_barrier_init(lrpthread_barrier_t *barrier, const lrpthread_barrierattr_t *attr, unsigned int count){
	//return 0 for success, non-zero for error
	int ret = -1;
	if(count == 0)
		return ret;
	ret = pthread_mutex_init(&barrier->mutex, 0);
	if(ret != 0)
		return ret;
    ret = pthread_cond_init(&barrier->cond, 0);
	if(ret != 0){
		//pthread_cond_init failed
		(void)pthread_mutex_destroy(&barrier->mutex);
		return ret;
	}
	barrier->tripCount = count;
	barrier->count = 0;
	return 0;
}

int lrpthread_barrier_destroy(lrpthread_barrier_t *barrier){
	//return 0 for success, non-zero for error
	int ret = 0;
    ret = pthread_cond_destroy(&barrier->cond);
	if(ret != 0)
		return ret;
    ret = pthread_mutex_destroy(&barrier->mutex);
    return ret;
}

int lrpthread_barrier_wait(lrpthread_barrier_t *barrier){
	//by standard:
	//# define PTHREAD_BARRIER_SERIAL_THREAD -1
	//returned for single arbitrary thread, and 0 for others
	//here:
	//return 0 for success, non-zero for error
	int ret = 0;
	ret = pthread_mutex_lock(&barrier->mutex);
	if(ret != 0)
		return ret;
	barrier->count++;
	if(barrier->count != barrier->tripCount){
		ret = pthread_cond_wait(&barrier->cond, &barrier->mutex);
		if(ret != 0)
			return ret;
		ret = pthread_mutex_unlock(&barrier->mutex);
		return ret;
	}
	barrier->count = 0;
	ret = pthread_cond_broadcast(&barrier->cond);
	if(ret != 0){
		(void)pthread_mutex_unlock(&barrier->mutex);
		return ret;
	}
	ret = pthread_mutex_unlock(&barrier->mutex);
	return ret;
}

#endif