/*
    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"

pthread_mutex_t Mutex1 = PTHREAD_MUTEX_INITIALIZER;
pthread_mutex_t Mutex2 = PTHREAD_MUTEX_INITIALIZER;
pthread_mutex_t Mutex3 = PTHREAD_MUTEX_INITIALIZER;
pthread_barrier_t Barrier1;
pthread_barrier_t Barrier2;
pthread_barrier_t Barrier3;
pthread_mutex_t Region_Mutexes[HEIGHT/5];
unsigned char Region_Handled[HEIGHT/5];
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;
//uint32_t Thread_Step;
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;
//struct Thread_I *Opengl_t_info;
int Stats_Handled;
//unsigned char Region_Offsets[5] = {0, 1, 2, 3, 4}; //Initialized

#ifndef HAVE_PTHREAD_BARRIER_INIT
int pthread_barrier_init(pthread_barrier_t *barrier, const pthread_barrierattr_t *attr, unsigned int count){
	if(count == 0){
		errno = EINVAL;
		return -1;
	}
	if(pthread_mutex_init(&barrier->mutex, 0) < 0){
		return -1;
	}
	if(pthread_cond_init(&barrier->cond, 0) < 0){
		pthread_mutex_destroy(&barrier->mutex);
		return -1;
	}
	barrier->tripCount = count;
	barrier->count = 0;
	return 0;
}
#endif

#ifndef HAVE_PTHREAD_BARRIER_DESTROY
int pthread_barrier_destroy(pthread_barrier_t *barrier){
	pthread_cond_destroy(&barrier->cond);
	pthread_mutex_destroy(&barrier->mutex);
	return 0;
}
#endif

#ifndef HAVE_PTHREAD_BARRIER_WAIT
int pthread_barrier_wait(pthread_barrier_t *barrier){
	pthread_mutex_lock(&barrier->mutex);
	++(barrier->count);
	if(barrier->count >= barrier->tripCount){
		barrier->count = 0;
		pthread_cond_broadcast(&barrier->cond);
		pthread_mutex_unlock(&barrier->mutex);
		return 1;
	}else{
		pthread_cond_wait(&barrier->cond, &barrier->mutex);
		pthread_mutex_unlock(&barrier->mutex);
		return 0;
	}
}
#endif