📄 mymutex.h
字号:
/* OpenWebSpider * * Authors: Stefano Alimonti AND Stefano Fantin * Version: 0.7 * 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]);#endifreturn;}void thrdUnBlock(int iHndl){#ifdef WIN32 ReleaseMutex(hMutex[iHndl]);#else pthread_mutex_unlock(&hMutex[iHndl]);#endifreturn;} * */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 WIN32unsigned long thrdNum=GetCurrentThreadId();#elseunsigned long thrdNum=pthread_self();#endif if(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 WIN32unsigned long thrdNum=GetCurrentThreadId();#elseunsigned long thrdNum=pthread_self();#endif if(iHndl>MAXMUTEX || iHndl==NO_BLOCK) return; // ERROR if(hMutex[iHndl]==thrdNum) hMutex[iHndl]=0;return;}#endif/*EOF*/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -