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

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

#include "internal.h"

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

//
// Platform Mutex structure
//
typedef struct _PlatformMutex
{
   pthread_mutex_t   mutex;

} PlatformMutex, *PPlatformMutex;

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

//===[ Global variables ]==================================================

//
// Module synchronization mutex
// 
pthread_mutex_t   g_hModuleMutex = PTHREAD_MUTEX_INITIALIZER;


//++=======================================================================
HANDLE
PlatAllocMutex(void)
//
//  Arguments: 
//
//  Returns:   
//
//  Abstract:  
//
//  Notes:
//
//  Environment:
//
// L2
//=======================================================================--
{
   PlatformMutex        *pPlatMutex;
   pthread_mutexattr_t  mutexAttr = {PTHREAD_MUTEX_RECURSIVE};

   DbgTrace(2, "-PlatAllocMutex- Start\n", 0);

   // Allocate space for our mutex structure
   pPlatMutex = malloc(sizeof(*pPlatMutex));
   if (pPlatMutex)
   {
      // Finish initializing the mutex
      pthread_mutex_init(&pPlatMutex->mutex, &mutexAttr);
   }
   else
   {
      DbgTrace(0, "-PlatAllocMutex- Memory allocation failure\n", 0);
   }

   DbgTrace(2, "-PlatAllocMutex- End, retHandle = %0X\n", (unsigned int) pPlatMutex);

   return (HANDLE) pPlatMutex;
}


//++=======================================================================
void
PlatDestroyMutex(HANDLE hMutex)
//
//  Arguments: 
//
//  Returns:   
//
//  Abstract:  
//
//  Notes:
//
//  Environment:
//
// L2
//=======================================================================--
{
   PlatformMutex  *pPlatMutex = (PlatformMutex*) hMutex;

   DbgTrace(2, "-PlatDestroyMutex- Start\n", 0);

   // Free the resources associated with the mutex
   pthread_mutex_destroy(&pPlatMutex->mutex);
   free(pPlatMutex);

   DbgTrace(2, "-PlatDestroyMutex- End\n", 0);
}


//++=======================================================================
void
PlatAcquireMutex(HANDLE hMutex)
//
//  Arguments: 
//
//  Returns:   
//
//  Abstract:  
//
//  Notes:
//
//  Environment:
//
// L2
//=======================================================================--
{
   PlatformMutex  *pPlatMutex = (PlatformMutex*) hMutex;

   DbgTrace(2, "-PlatAcquireMutex- Start\n", 0);

   // Acquire the mutex
   pthread_mutex_lock(&pPlatMutex->mutex);

   DbgTrace(2, "-PlatAcquireMutex- End\n", 0);
}


//++=======================================================================
void
PlatReleaseMutex(HANDLE hMutex)
//
//  Arguments: 
//
//  Returns:   
//
//  Abstract:  
//
//  Notes:
//
//  Environment:
//
// L2
//=======================================================================--
{
   PlatformMutex  *pPlatMutex = (PlatformMutex*) hMutex;

   DbgTrace(2, "-PlatReleaseMutex- Start\n", 0);

   // Release the mutex
   pthread_mutex_unlock(&pPlatMutex->mutex);

   DbgTrace(2, "-PlatRelease- End\n", 0);
}


//++=======================================================================
//++=======================================================================
//++=======================================================================