Example #1
0
void ShutdownThreading()
{
	if (g_pWorker)
	{
		g_pWorker->Stop(true);
		delete g_pWorker;
		g_pWorker = NULL;
	}

	g_QueueLock->Lock();
	while (!g_ThreadQueue.empty())
	{
		delete g_ThreadQueue.front();
		g_ThreadQueue.pop();
	}
	while (!g_FreeThreads.empty())
	{
		delete g_FreeThreads.front();
		g_FreeThreads.pop();
	}
	g_QueueLock->Unlock();
	g_QueueLock->DestroyThis();

	FreeHandleTable();
}
Example #2
0
void StartFrame()
{
	if (g_pWorker && (g_lasttime < gpGlobals->time))
	{
        g_lasttime = gpGlobals->time + 0.05f;
		g_QueueLock->Lock();
		size_t remaining = g_ThreadQueue.size();
		if (remaining)
		{
			MysqlThread *kmThread;
			do 
			{
				kmThread = g_ThreadQueue.front();
				g_ThreadQueue.pop();
				g_QueueLock->Unlock();
				kmThread->Execute();
				kmThread->Invalidate();
				g_FreeThreads.push(kmThread);
				g_QueueLock->Lock();
			} while (!g_ThreadQueue.empty());
		}

		g_QueueLock->Unlock();
	}

	RETURN_META(MRES_IGNORED);
}
Example #3
0
void OnPluginsUnloading()
{
	if (!g_pWorker)
	{
		return;
	}

	g_pWorker->Stop(false);
	delete g_pWorker;
	g_pWorker = NULL;

	g_QueueLock->Lock();
	size_t remaining = g_ThreadQueue.size();
	if (remaining)
	{
		MysqlThread *kmThread;
		do 
		{
			kmThread = g_ThreadQueue.front();
			g_ThreadQueue.pop();
			g_QueueLock->Unlock();
			kmThread->Execute();
			kmThread->Invalidate();
			g_FreeThreads.push(kmThread);
			g_QueueLock->Lock();
		} while (!g_ThreadQueue.empty());
	}

	g_QueueLock->Unlock();
}
Example #4
0
//Makes a new menu handle (-1 for failure)
//native csdm_makemenu(title[]);
static cell AMX_NATIVE_CALL menu_create(AMX *amx, cell *params)
{
	int len;
	char *title = get_amxstring(amx, params[1], 0, len);
	validate_menu_text(title);
	char *handler = get_amxstring(amx, params[2], 1, len);

	int func = registerSPForwardByName(amx, handler, FP_CELL, FP_CELL, FP_CELL, FP_DONE);
	
	if (func == -1)
	{
		LogError(amx, AMX_ERR_NOTFOUND, "Invalid function \"%s\"", handler);
		return 0;
	}

	Menu *pMenu = new Menu(title, amx, func);

	if (g_MenuFreeStack.empty())
	{
		g_NewMenus.append(pMenu);
		pMenu->thisId = (int)g_NewMenus.length() - 1;
	} else {
		int pos = g_MenuFreeStack.front();
		g_MenuFreeStack.pop();
		g_NewMenus[pos] = pMenu;
		pMenu->thisId = pos;
	}

	return pMenu->thisId;
}
Example #5
0
void OnAmxxDetach()
{
	while (!g_FreeTRs.empty())
	{
		delete g_FreeTRs.front();
		g_FreeTRs.pop();
	}
}
Example #6
0
// Check if the parenthesis in an expression are balanced
// PRE: str points to the expression to check
// POST: returns 1 if balanced, otherwise returns 0
bool CheckParens(const char* str) {
	CStack stack;		// Stack used for balance checking
	char temp[2];		// String used to push the parens on the stack

	// Loop through pushing lefts and popping/comparing rights
	temp[1]=0;
	for(int x=0;str[x];x++) {
		*temp=str[x];
		if(isLeftParen(*temp)) {
			stack.push(temp);
		} else if(isRightParen(*temp)) {
			if(stack.empty() || getRightParen(stack.pop()[0])!=*temp)
				return 0;
		}
	}

	// If the stack isn't empty, parens aren't balanced
	if(stack.empty())
		return 1;
	else
		return 0;
}
Example #7
0
static cell AMX_NATIVE_CALL create_tr2(AMX *amx, cell *params)
{
    TraceResult *tr;
    if (g_FreeTRs.empty())
    {
        tr = new TraceResult;
    } else {
        tr = g_FreeTRs.front();
        g_FreeTRs.pop();
    }
    memset(tr, 0, sizeof(TraceResult));
    return reinterpret_cast<cell>(tr);
}
Example #8
0
void ClearMenus()
{
	for (size_t i = 0; i < g_NewMenus.length(); i++)
	{
		delete g_NewMenus[i];
	}
	
	g_NewMenus.clear();
	while (!g_MenuFreeStack.empty())
	{
		g_MenuFreeStack.pop();
	}
}
Example #9
0
TimerInfo *TimerNatives::CreateTimerInfo()
{
	TimerInfo *pInfo;

	if (m_FreeTimers.empty())
	{
		pInfo = new TimerInfo;
	} else {
		pInfo = m_FreeTimers.front();
		m_FreeTimers.pop();
	}

	return pInfo;
}
Example #10
0
void OnAmxxDetach()
{
	while (!g_FreeTRs.empty())
	{
		delete g_FreeTRs.front();
		g_FreeTRs.pop();
	}

	while (!g_KVDWs.empty())
		delete g_KVDWs.popCopy();

	while (!g_FreeKVDWs.empty())
		delete g_FreeKVDWs.popCopy();
}
Example #11
0
void LoadExtraPluginsFromDir(const char *initialdir)
{
	CStack<ke::AString *> files;
	char path[255];
	BuildPluginFileList(initialdir, files);
	while (!files.empty())
	{
		ke::AString *pString = files.front();
		ke::SafeSprintf(path, sizeof(path), "%s/%s",
			initialdir,
			pString->chars());
		g_plugins.loadPluginsFromFile(path);
		delete pString;
		files.pop();
	}
}
Example #12
0
File: 7.cpp Project: song-syj/pro
int main()
{
	CStack<int> stk;
	int data[] = {2, 4, 5, 9 ,11};
	int size = sizeof(data)/ sizeof(int);
	for(int i = 0; i < size; i++)
		stk.appendTail(data[i]);
	while(!stk.empty()) {
		int data = stk.deleteTail();
		cout << data << " ";
	}	

	cout << endl;

	return 0;
}
Example #13
0
void LoadExtraPluginsFromDir(const char *initialdir)
{
	CStack<String *> files;
	char path[255];
	BuildPluginFileList(initialdir, files);
	while (!files.empty())
	{
		String *pString = files.front();
		UTIL_Format(path, sizeof(path)-1, "%s/%s", 
			initialdir,
			pString->c_str());
		g_plugins.loadPluginsFromFile(path);
		delete pString;
		files.pop();
	}
}
Example #14
0
//public QueryHandler(state, Handle:query, error[], errnum, data[], size)
//native SQL_ThreadQuery(Handle:cn_tuple, const handler[], const query[], const data[]="", dataSize=0);
static cell AMX_NATIVE_CALL SQL_ThreadQuery(AMX *amx, cell *params)
{
	if (!g_pWorker)
	{
		MF_LogError(amx, AMX_ERR_NATIVE, "Thread worker was unable to start.");
		return 0;
	}

	SQL_Connection *cn = (SQL_Connection *)GetHandle(params[1], Handle_Connection);
	if (!cn)
	{
		MF_LogError(amx, AMX_ERR_NATIVE, "Invalid info tuple handle: %d", params[1]);
		return 0;
	}

	int len;
	const char *handler = MF_GetAmxString(amx, params[2], 0, &len);
	int fwd = MF_RegisterSPForwardByName(amx, handler, FP_CELL, FP_CELL, FP_STRING, FP_CELL, FP_ARRAY, FP_CELL, FP_CELL, FP_DONE);
	if (fwd < 1)
	{
		MF_LogError(amx, AMX_ERR_NATIVE, "Function not found: %s", handler);
		return 0;
	}

	MysqlThread *kmThread;
	g_QueueLock->Lock();
	if (g_FreeThreads.empty())
	{
		kmThread = new MysqlThread();
	} 
	else 
	{
		kmThread = g_FreeThreads.front();
		g_FreeThreads.pop();
	}
	g_QueueLock->Unlock();

	kmThread->SetInfo(cn->host, cn->user, cn->pass, cn->db, cn->port, cn->max_timeout);
	kmThread->SetForward(fwd);
	kmThread->SetQuery(MF_GetAmxString(amx, params[3], 1, &len));
	kmThread->SetCellData(MF_GetAmxAddr(amx, params[4]), (ucell)params[5]);
	kmThread->SetCharacterSet(cn->charset);

	g_pWorker->MakeThread(kmThread);

	return 1;
}
Example #15
0
void OnAmxxDetach()
{
	ConfigManager->CloseGameConfigFile(CommonConfig);
	ConfigManager->CloseGameConfigFile(GamerulesConfig);

	while (!g_FreeTRs.empty())
	{
		delete g_FreeTRs.front();
		g_FreeTRs.pop();
	}

	while (!g_KVDWs.empty())
		delete g_KVDWs.popCopy();

	while (!g_FreeKVDWs.empty())
		delete g_FreeKVDWs.popCopy();
}
Example #16
0
void StartFrame() {
    if (g_Worker && g_QueueLock) {
        g_QueueLock->Lock();

        CurlThread* thread;
        while (!g_ThreadQueue.empty()) {
            thread = g_ThreadQueue.front();
            g_ThreadQueue.pop();
            g_QueueLock->Unlock();
            thread->Execute();
            delete thread;
            g_QueueLock->Lock();
        }

        g_QueueLock->Unlock();
    }

    RETURN_META(MRES_IGNORED);
}
Example #17
0
void ShutdownThreading() {
    if (g_Worker) {
        g_Worker->SetMaxThreadsPerFrame(8192);
        g_Worker->Stop(true);
    }

    g_QueueLock->Lock();

    CurlThread* thread;
    while (!g_ThreadQueue.empty()) {
        thread = g_ThreadQueue.front();
        g_ThreadQueue.pop();
        g_QueueLock->Unlock();
        delete thread;
        g_QueueLock->Lock();
    }

    g_QueueLock->Unlock();
}
Example #18
0
void OnPluginsUnloading() {
    if (!g_Worker) {
        return;
    }

    g_Worker->SetMaxThreadsPerFrame(8192);
    g_Worker->Stop(false);
    delete g_Worker;
    g_Worker = NULL;

    g_QueueLock->Lock();

    CurlThread* thread;
    while (!g_ThreadQueue.empty()) {
        thread = g_ThreadQueue.front();
        g_ThreadQueue.pop();
        g_QueueLock->Unlock();
        delete thread;
        g_QueueLock->Lock();
    }

    g_QueueLock->Unlock();
}
Example #19
0
MsgListenerWrapper *UsrMessageNatives::CreateListener(IPluginContext *pCtx)
{
	MsgWrapperList *pList;
	MsgListenerWrapper *pListener;
	IPlugin *pl = g_PluginSys.FindPluginByContext(pCtx->GetContext());

	if (m_FreeListeners.empty())
	{
		pListener = new MsgListenerWrapper;
	} else {
		pListener = m_FreeListeners.front();
		m_FreeListeners.pop();
	}

	if (!pl->GetProperty("MsgListeners", reinterpret_cast<void **>(&pList)))
	{
		pList = new List<MsgListenerWrapper *>;
		pl->SetProperty("MsgListeners", pList);
	}

	pList->push_back(pListener);

	return pListener;
}
Example #20
0
// Convert an infix expression to postfix
// PRE: expr is the infix expression to convert
//      output is a string for the output
// POST: output is the postfix expression, returns 1 on
//       success or 0 on error
bool SolveInfix(const char *input,char* output) {
	char op[2];					// "string" for the operator stack
	char expr[MAX_LENGTH+1];	// Modifible input string
	int length;					// The current length of the output string [+1]
	CStack opStack;				// Operation stack
	int x;						// Index variable for reading input
	bool foundError;			// 1 if error found, otherwise 0

	// 
	memcpy(expr,input,MAX_LENGTH*sizeof(char));
	InsertZeros(expr);

	// Initialize some variables
	op[1]=0;
	length=0;
	foundError=0;
	output[0]=0;

	// Read each character of the expression
	for(x=0;expr[x];x++) {
		*op=expr[x];

		// If we have a left paren, push it
		if(isLeftParen(*op)) {
			opStack.push(op);

		// If we have a number, push it
		} else if(isNumber(*op)) {
			do {
				output[length++]=expr[x++];
			} while(isNumber(expr[x]));
			output[length++]=' ';
			output[length]=0;
			--x;

		// If we have an operator, pop lower precedence operators and push it
		} else if(isOperator(*op)) {
			while(!opStack.empty()
				&& !isLeftParen(opStack.top()[0])
				&& isOperator(opStack.top()[0])<=isOperator(*op)) {
				if(!isOperator(opStack.top()[0])) foundError=1;
				output[length++]=opStack.pop()[0];
				output[length]=0;
			}
			opStack.push(op);

		// If we have a right paren, pop operators between the parens
		} else if(isRightParen(*op)) {
			for(;;) {
				if(opStack.empty()) break;
				*op=opStack.pop()[0];
				if(!*op || isLeftParen(*op)) break;
				if(!isOperator(*op)) {
					foundError=1;
					break;
				}
				output[length++]=*op;
			}
			output[length]=0;

		// If this happens, we have a syntax error (unless whitespace)
		} else {
			switch(*op) {
			case ' ':
			case '\t':
			case '\n':
				break;
			default:
				foundError=1;
				break;
			}
		}

		// No need to continue if we have errors
		if(foundError) break;
	}

	// Pop any remaining operations off the stack
	while(!opStack.empty()) {
		if(!isOperator(opStack.top()[0])) foundError=1;
		output[length++]=opStack.pop()[0];
		output[length]=0;
	}

	return !foundError;
}
Example #21
0
// Solve a postfix expression
// PRE: expr is the postfix expression to solve
// POST: answer is the answer, returns 1 if
//       successful, otherwise returns 0
bool SolvePostfix(const char* expr,double &answer) {
	int x,y;					// Index variables
	double right,left;			// Right and left parts of the value
	char str[MAX_LENGTH+1];		// String for number to string conversion
	char ch;					// Temporary char to hold expr[x] etc
	CStack stack;				// The stack to hold the values

	// Loop through the postfix expression
	for(x=0;x<signed(strlen(expr));x++) {
		ch=expr[x];

		// If we have an operator, set up left/right
		if(isOperator(ch)) {
			right=atof(stack.pop());
			if(stack.empty()) {
				break;
			} else {
				left=atof(stack.pop());
			}
		}

		// Do an operation or push a number
		switch(ch) {
		case '+':
			sprintf(str,"%lf",left+right);
			stack.push(str);
			break;
		case '-':
			sprintf(str,"%lf",left-right);
			stack.push(str);
			break;
		case '*':
			sprintf(str,"%lf",left*right);
			stack.push(str);
			break;
		case '/':
			if(!right)
				break;
			sprintf(str,"%lf",left/right);
			stack.push(str);
			break;
		case '^':
			sprintf(str,"%lf",pow(left,right));
			stack.push(str);
			break;
		case ' ':		// Skip white space and various delimiters
		case ',':
		case '\t':
		case '\n':
			break;
		default:	// Number (hopefully)
			for(y=0;y<=MAX_LENGTH && expr[x];y++,x++) {
				if(!isNumber(expr[x])) {
					--x;		// Revisit this character
					break;
				}
				str[y]=expr[x];
			}
			if(!y) {	// Bad input
				return 0;
			}
			str[y]=0;
			stack.push(str);
			break;
		}
	}

	// If the stack is empty, no answer!
	if(stack.empty())
		return 0;
	else
		answer=atof(stack.pop());

	// If the stack is not empty, too few operations
	if(!stack.empty())
		return 0;

	return 1;
}
Example #22
0
void CPluginMngr::CacheAndLoadModules(const char *plugin)
{
	size_t progsize;
	char *prog = ReadIntoOrFromCache(plugin, progsize);

	if (!prog)
		return;

	AMX_HEADER hdr;
	memcpy(&hdr, prog, sizeof(AMX_HEADER));

	uint16_t magic = hdr.magic;
	amx_Align16(&magic);

	if (magic != AMX_MAGIC)
	{
		return;
	}

	if (hdr.file_version < MIN_FILE_VERSION ||
		hdr.file_version > CUR_FILE_VERSION)
	{
		return;
	}
	if ((hdr.defsize != sizeof(AMX_FUNCSTUB)) &&
		(hdr.defsize != sizeof(AMX_FUNCSTUBNT)))
	{
		return;
	}

	amx_Align32((uint32_t*)&hdr.nametable);
	uint16_t *namelength=(uint16_t*)((unsigned char*)prog + (unsigned)hdr.nametable);
	amx_Align16(namelength);
	if (*namelength>sNAMEMAX)
	{
		return;
	}

	if (hdr.stp <= 0)
	{
		return;
	}

	AMX amx;
	memset(&amx, 0, sizeof(AMX));
	amx.base = (unsigned char *)prog;

	int num;
	char name[sNAMEMAX+1];

	num = amx_GetLibraries(&amx);
	for (int i=0; i<num; i++)
	{
		amx_GetLibrary(&amx, i, name, sNAMEMAX);
		if (stricmp(name, "Float")==0)
			continue;
		//awful backwards compat hack
		if (stricmp(name, "socket")==0)
			strcpy(name, "sockets");
		//we don't want to report failed modules here...
		LoadModule(name, PT_ANYTIME, true, true);
	}

	cell tag_id;
	amx_NumTags(&amx, &num);

	ke::Vector<LibDecoder *> expects;
	ke::Vector<LibDecoder *> defaults;
	CStack<LibDecoder *> delstack;
	for (int i=0; i<num; i++)
	{
		amx_GetTag(&amx, i, name, &tag_id);
		if (name[0] == '?')
		{
			LibDecoder *dc = new LibDecoder;
			delstack.push(dc);
			if (DecodeLibCmdString(name, dc))
			{
				if (dc->cmd == LibCmd_ForceLib)
				{
					RunLibCommand(dc);
				} else if ( (dc->cmd == LibCmd_ExpectClass) ||
							(dc->cmd == LibCmd_ExpectLib) )
				{
					expects.append(dc);
				} else if (dc->cmd == LibCmd_DefaultLib) {
					defaults.append(dc);
				}
			}
		}
	}

	for (size_t i=0; i<expects.length(); i++)
	{
		RunLibCommand(expects[i]);
	}
	for (size_t i=0; i<defaults.length(); i++)
	{
		RunLibCommand(defaults[i]);
	}

	expects.clear();
	defaults.clear();

	while (!delstack.empty())
	{
		delete delstack.front();
		delstack.pop();
	}

	return;
}