Source

blake / blake_pendingloop.c

Full commit
/*
Copyright (C) 2001-2007 Daybo Logic.
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
1. Redistributions of source code must retain the above copyright
   notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
   notice, this list of conditions and the following disclaimer in the
   documentation and/or other materials provided with the distribution.
3. Neither the name of the project nor the names of its contributors
   may be used to endorse or promote products derived from this software
   without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
SUCH DAMAGE.
*/
/*
  %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  % Blake pending loop.                               22nd August 2001  %
  % The pending loop is the catalyst for receiving network data and any %
  % other callbacks Blake must execute.  Library users are expected to  %
  % call this in a timer to event loop (preferred) or call once and have%
  % the loop continue to to run until the loop is cancelled.  It is     %
  % illegal to run more than one catalyst per process.                  %
  %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
*/

#define BLAKE_SOURCE

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include <stdio.h>
#include <time.h>

#ifdef HAVE_LIMITS_H
# include <limits.h>
#endif

#ifdef HAVE_STDDEF_H
# include <stddef.h>
#endif

#ifdef BLAKE_HDRSTOP
# pragma hdrstop
#endif /*BLAKE_HDRSTOP*/

#ifdef HAVE_DLSTDINC_H
# include "dlstdinc.h"
#endif

#ifdef HAVE_DPCRTLMM_H
# include "dpcrtlmm.h"
#endif

#include "blake_threads.h"
#include "blake.h"
#include "blake_main.h"
#include "blake_sockets.h"
#include "blake_pendingloop.h"
/*-------------------------------------------------------------------------*/
static const char *const rcsid =
  "$Id: blake_pendingloop.c,v 0.9 2007/01/18 16:18:00 ddrp Exp $";

static bool inited;

static struct {                   /* Protected data, aquire mutant first */
  #ifdef BLAKE_THREADS
  dtlock_t lock;
  #endif /*BLAKE_THREADS*/
  bool runForever; /* Flag indicating loop runs without returning */
  bool running; /* Flag for mutual exclusion of the catalyst */
} data;

static bool CheckRunForever(void);
static void SetRunForever(bool NewState);
static bool CheckRunning(void);
static void SetRunning(bool NewState);
/*-------------------------------------------------------------------------*/
void blake_int_pendingloop_NCI()
{
    blake_Unused(rcsid);
}
/*-------------------------------------------------------------------------*/
bool blake_int_PendingLoopIsInited()
{
  return inited;
}
/*-------------------------------------------------------------------------*/
bool blake_int_PendingLoopInit()
{
  if ( !inited ) {
    daybothreads_CreateLock(&data.lock);
    inited = true;
    return inited;
  }
  return false; /* Already inited */
}
/*-------------------------------------------------------------------------*/
void blake_int_PendingLoopCleanup()
{
  if ( inited ) {
    blake_CancelPendingLoop(); /* Ensure it's cancelled */
    /* Wait until the cancellation has happened */
    inited = false;
    while ( CheckRunning() ); /* Bit of a hack I must admit */
    daybothreads_DestroyLock(&data.lock); /* Safe to go */
  }
}
/*-------------------------------------------------------------------------*/
BLAKEAPI(void) blake_PendingLoop(blake_bool_t RunForever)
{
  if ( !inited ) return; /* Safety */
  if ( CheckRunning() ) return; /* Illegal to run twice */
  SetRunning(true);
  SetRunForever(RunForever);

  do {
    network_ExecuteConnectionCallback();
    network_ExecuteIncomingCallbacks();
  } while ( CheckRunForever() && CheckRunning() );

  SetRunning(false);
  return;
}
/*-------------------------------------------------------------------------*/
BLAKEAPI(void) blake_CancelPendingLoop(void)
{
  if ( inited ) {
    daybothreads_EnterLock(&data.lock);
    data.runForever = false;
    daybothreads_LeaveLock(&data.lock);
  }
  return;
}
/*-------------------------------------------------------------------------*/
static bool CheckRunForever()
{
  bool ret;

  daybothreads_EnterLock(&data.lock);
  ret = data.runForever;
  daybothreads_LeaveLock(&data.lock);

  return ret;
}
/*-------------------------------------------------------------------------*/
static void SetRunForever(bool NewState)
{
  daybothreads_EnterLock(&data.lock);
  data.runForever = NewState;
  daybothreads_LeaveLock(&data.lock);
}
/*-------------------------------------------------------------------------*/
static bool CheckRunning()
{
  bool ret;

  daybothreads_EnterLock(&data.lock);
  ret = data.running;
  daybothreads_LeaveLock(&data.lock);

  return ret;
}
/*-------------------------------------------------------------------------*/
static void SetRunning(bool NewState)
{
  daybothreads_EnterLock(&data.lock);
  data.running = NewState;
  daybothreads_LeaveLock(&data.lock);
}
/*-------------------------------------------------------------------------*/