
/* OpenWebSpider
 *
 *  Authors:     Stefano Alimonti AND Stefano Fantin
 *  Version:     0.8
 *  E-Mails:     shen139 [at] openwebspider (dot) org AND stefanofantinguz@yahoo.it
 *
 *
 * This file is part of OpenWebSpider
 *
 * 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 2 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, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 */


#ifndef __MYMUTEX
#define __MYMUTEX

/*
 * ORIGINAL FUNCTIONS
 *
void thrdBlock(int iHndl)
{
#ifdef WIN32
	WaitForSingleObject(hMutex[iHndl],INFINITE);
#else
	pthread_mutex_trylock(&hMutex[iHndl]);
#endif
return;
}

void thrdUnBlock(int iHndl)
{
#ifdef WIN32
	ReleaseMutex(hMutex[iHndl]);
#else
	pthread_mutex_unlock(&hMutex[iHndl]);
#endif
return;
}
 *
 */

void relinquish()
{
#ifdef WIN32
	Sleep(0);
#else
	sched_yield();
#endif
}

/*Initializes all MUTEXes*/
void init_mutex()
{
int i;
	for(i=0;i<MAXMUTEX;i++)
		hMutex[i]=0;
}

/*Un-Block all MUTEXes owned by the current thread*/
void UnBlockAll()
{
int i;
	for(i=0;i<MAXMUTEX;i++)
		thrdUnBlock(i);
		
}

void thrdBlock(int iHndl)
{
#ifdef WIN32
OWS_MUTEX_OWNER thrdNum=(OWS_MUTEX_OWNER)GetCurrentThreadId();
#else
OWS_MUTEX_OWNER thrdNum=(OWS_MUTEX_OWNER)(uintptr_t)pthread_self();
#endif
	
    if(iHndl<0 || iHndl>=MAXMUTEX || iHndl==NO_BLOCK)
        return;                 // ERROR

    if(hMutex[iHndl]==thrdNum)  // <----+
        return;                 //      +-------------+
    else if(hMutex[iHndl]==0)   //      |             |
        hMutex[iHndl]=thrdNum;  //      |             |
    else                        //      |             |
    {                           //      |             |
        while(hMutex[iHndl]!=0) //      |             |
            relinquish();       //      |             |
                                //      |             |
        hMutex[iHndl]=thrdNum;  //      |             |
        relinquish();           //      |             |
    }                           //      |             |
                                //      |             |
    thrdBlock(iHndl);           //--->Recursive check |
                                //                    |
return;	                        //<-------------------+
}

void thrdUnBlock(int iHndl)
{
#ifdef WIN32
OWS_MUTEX_OWNER thrdNum=(OWS_MUTEX_OWNER)GetCurrentThreadId();
#else
OWS_MUTEX_OWNER thrdNum=(OWS_MUTEX_OWNER)(uintptr_t)pthread_self();
#endif

	if(iHndl<0 || iHndl>=MAXMUTEX || iHndl==NO_BLOCK)
		return;                  // ERROR

	if(hMutex[iHndl]==thrdNum)
		hMutex[iHndl]=0;
return;
}

#endif

/*EOF*/
