/*
 * asmail is the AfterStep mailbox monitor
 * Copyright (c) 2002 Albert Dorofeev <albert@tigr.net>
 * For the updates see http://www.tigr.net/
 *
 * This software is distributed under GPL. For details see LICENSE file.
 */

#include <pthread.h>
#include "globals.h"

char config_file_name[MAX_INPUT_LENGTH+1] = "";
int flag_verbose = 0;
int flag_allow_insecure = 0;
struct x11_set_struct x11_set;

struct mbox_struct * mbox = NULL;

pthread_mutex_t update_lock;
pthread_cond_t update_cv;
int update_count = 0;

void signal_update() {
	pthread_mutex_lock(&update_lock);
	++update_count;
	pthread_mutex_unlock(&update_lock);
	/* pthread_cond_signal(&update_cv); */
	pthread_cond_broadcast(&update_cv);
}

pthread_mutex_t md5_lock;

pthread_mutex_t check_lock;
pthread_cond_t check_cv;
int check_count = 0;

void signal_check() {
	pthread_mutex_lock(&check_lock);
	++check_count;
	pthread_mutex_unlock(&check_lock);
	pthread_cond_broadcast(&check_cv);
}

void sleep_check( const int secs ) {
	time_t t;
	struct timespec tv;
	/* Can't find a function returning nanosecs? */
	time(&t);
	tv.tv_sec = t + secs;
	tv.tv_nsec = 0;
	pthread_mutex_lock(&check_lock);
	pthread_cond_timedwait(&check_cv, &check_lock, &tv);
	pthread_mutex_unlock(&check_lock);
}

pthread_mutex_t xkick_lock = PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t xkick_cv = PTHREAD_COND_INITIALIZER;
int xkick_count = 0;

void signal_xkick() {
	pthread_mutex_lock(&xkick_lock);
	++xkick_count;
	pthread_mutex_unlock(&xkick_lock);
	pthread_cond_broadcast(&xkick_cv);
}

int sleep_xkick( ) {
	int kicks = 0;
	pthread_mutex_lock(&xkick_lock);
	while ( xkick_count <= 0 ) {
		pthread_cond_wait(&xkick_cv, &xkick_lock);
	}
	kicks = xkick_count;
	xkick_count = 0; /* we need an update of the screen only once */
	pthread_mutex_unlock(&xkick_lock);
	return kicks;
}

