/**********************\*************************************************
 * 
 *  Copyright (C) 2006 Novell, Inc. All Rights Reserved.
 *
 *  This library is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU Lesser General Public
 *  License as published by the Free Software Foundation; version 2.1
 *  of the License.
 *
 *  This library 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
 *  Library Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public
 *  License along with this library; if not, Novell, Inc.
 * 
 *  To contact Novell about this file by physical or electronic mail, 
 *  you may find current contact information at www.novell.com.
 * 
 *  Author: Juan Carlos Luciani <jluciani@novell.com>
 *
 ***********************************************************************/

#ifndef _IPCINT_
#define _IPCINT_

//===[ Include files ]=====================================================

#include <iostream>
#include <string>
#include <list>
#include <map>
#include <vector>
using namespace std;

extern "C" {
#include <stdint.h>
#include <unistd.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <errno.h>
#include <string.h>
#include <stdlib.h>
#include <pthread.h>
#include <syslog.h>
#include <signal.h>
#include <sys/stat.h>
#include <sys/file.h>
#include <assert.h>     // Ensure that NDEBUG is defined for release builds!
#include <sys/ipc.h>
#include <casa_s_ipc.h>
}

//===[ External data ]=====================================================

extern int DebugLevel;
extern bool UseSyslog;
extern char appName[];
extern char *pAppName;
extern pthread_mutex_t interlockedMutex;

//===[ Macro definitions ]=================================================

//
// DbgTrace macro define
//
#define MAX_FORMAT_STRING_LEN 1024
#define DbgTrace(LEVEL, X, Y) {                                      \
   if (LEVEL == 0 || DebugLevel >= LEVEL) {                          \
      if (UseSyslog)                                                 \
         syslog(LOG_USER | LOG_INFO, X, Y);                          \
      else {                                                         \
         char *pFormatString = new char[MAX_FORMAT_STRING_LEN];      \
         if (pFormatString) {                                        \
            snprintf(pFormatString, MAX_FORMAT_STRING_LEN, X, Y);    \
            fprintf(stderr, "%s -%s", appName, pFormatString);       \
            delete[] pFormatString;                                  \
         }                                                           \
      }                                                              \
   }                                                                 \
}

//
// Interlocked Increment and Decrement macros
// 
// Well, kind of interlocked :-).
// 
__inline static unsigned long
InterlockedIncrement(unsigned long *pValue)
{
   unsigned long retVal;
   pthread_mutex_lock(&interlockedMutex);
   (*pValue) ++;
   retVal = *pValue;
   pthread_mutex_unlock(&interlockedMutex);
   return retVal;
}

__inline static unsigned long
InterlockedDecrement(unsigned long *pValue)
{
   unsigned long retVal;
   pthread_mutex_lock(&interlockedMutex);
   (*pValue) --;
   retVal = *pValue;
   pthread_mutex_unlock(&interlockedMutex);
   return retVal;
}

__inline static uint32_t
InterlockedIncrement(uint32_t *pValue)
{
   uint32_t retVal;
   pthread_mutex_lock(&interlockedMutex);
   (*pValue) ++;
   retVal = *pValue;
   pthread_mutex_unlock(&interlockedMutex);
   return retVal;
}

__inline static uint32_t
InterlockedDecrement(uint32_t *pValue)
{
   uint32_t retVal;
   pthread_mutex_lock(&interlockedMutex);
   (*pValue) --;
   retVal = *pValue;
   pthread_mutex_unlock(&interlockedMutex);
   return retVal;
}

//===[ Include files ]=====================================================

//===[ External prototypes ]===============================================

//===[ Manifest constants ]================================================

//===[ Type definitions ]==================================================

//===[ Function prototypes ]===============================================


#endif // _IPCINT_

//=========================================================================
//=========================================================================