/*
    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/5];
#endif
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;
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;
int Verbose;
double Sim_Score;
struct Thread_I *Actions_t_info;
int Stats_Handled;

#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);
		//if(ret != 0)
		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