Exemple #1
0
void
__flockfile_internal(FILE *fp, int internal)
{

	if (__isthreaded == 0)
		return;

	mutex_lock(&_LOCK(fp));
	
	if (_LOCKOWNER(fp) == thr_self()) {
		_LOCKCOUNT(fp)++;
		if (internal)
			_LOCKINTERNAL(fp)++;
	} else {
		/* danger! cond_wait() is a cancellation point. */
		int oldstate;
		thr_setcancelstate(PTHREAD_CANCEL_DISABLE, &oldstate);
		while (_LOCKOWNER(fp) != NULL)
			cond_wait(&_LOCKCOND(fp), &_LOCK(fp));
		thr_setcancelstate(oldstate, NULL);
		_LOCKOWNER(fp) = thr_self();
		_LOCKCOUNT(fp) = 1;
		if (internal)
			_LOCKINTERNAL(fp) = 1;
	}

	if (_LOCKINTERNAL(fp) == 1)
		/* stash cancellation state and disable */
		thr_setcancelstate(PTHREAD_CANCEL_DISABLE,
		    &_LOCKCANCELSTATE(fp));

	mutex_unlock(&_LOCK(fp));
}
Exemple #2
0
void PdBase::addSymbol(const std::string& symbol) {

    PdContext& context = PdContext::instance();

    if(!context.bMsgInProgress) {
        cerr << "Pd: Can not add symbol, message not in progress" << endl;;
        return;
    }

    if(context.msgType != MSG) {
        cerr << "Pd: Can not add symbol, midi byte stream in progress" << endl;;
        return;
    }

    if(context.curMsgLen+1 >= context.maxMsgLen) {
        cerr << "Pd: Can not add symbol, max message len of " << context.maxMsgLen << " reached" << endl;
        return;
    }

    _LOCK();
    libpd_add_symbol(symbol.c_str());
    _UNLOCK();
    
    context.curMsgLen++;
}
Exemple #3
0
void PdBase::sendMessage(const std::string& dest, const std::string& msg, const List& list) {

    PdContext& context = PdContext::instance();

    if(context.bMsgInProgress) {
        cerr << "Pd: Can not send message, message in progress" << endl;
        return;
    }

    _LOCK();
    libpd_start_message(list.len());
    _UNLOCK();

    context.bMsgInProgress = true;

    // step through list
    for(int i = 0; i < list.len(); ++i) {
        if(list.isFloat(i))
            addFloat(list.getFloat(i));
        else if(list.isSymbol(i))
            addSymbol(list.getSymbol(i));
    }

    finishMessage(dest, msg);
}
Exemple #4
0
//--------------------------------------------------------------------
bool PdBase::init(const int numInChannels, const int numOutChannels, const int sampleRate, bool queued) {
    clear();
    _LOCK();
    bool ret = PdContext::instance().init(numInChannels, numOutChannels, sampleRate, queued);
    _UNLOCK();
    return ret;
}
Exemple #5
0
bool JudgeDB::uploadCompileResult(
				int nRunID,
				int nUserID,
				int nGameID,
				bool bCompileRes,
				const char* sCompileError)
{
	CAutoLock _LOCK(getLock());

	std::string sql_query;
	BaseFunc::strFormat(sql_query,
		JUDGE_CONFIG::SQL_UPLOAD_COMPILE_RESULT,
		nRunID,
		nUserID,
		nGameID,
		bCompileRes,
		sCompileError);

	CDBObject* p_judge_db = getDBObj();
	if( !p_judge_db->Query(sql_query.c_str()) )
	{
		ADD_LOG("DBError %s, %d: %s", __FUNCTION__, __LINE__, p_judge_db->Error());
		assert(false);
		return false;
	}

	return true;
}
Exemple #6
0
void PdBase::addFloat(const float num) {

    PdContext& context = PdContext::instance();

    if(!context.bMsgInProgress) {
        cerr << "Pd: Can not add float, message not in progress" << endl;
        return;
    }

    if(context.msgType != MSG) {
        cerr << "Pd: Can not add float, midi byte stream in progress" << endl;
        return;
    }

    if(context.curMsgLen+1 >= context.maxMsgLen) {
        cerr << "Pd: Can not add float, max message len of " << context.maxMsgLen << " reached" << endl;
        return;
    }

    _LOCK();
    libpd_add_float(num);
    _UNLOCK();
    
    context.curMsgLen++;
}
Exemple #7
0
void PdBase::closePatch(Patch& patch) {
    if(!patch.isValid()) {
        return;
    }
    _LOCK();
    libpd_closefile(patch.handle());
    _UNLOCK();
    patch.clear();
}
Exemple #8
0
void PdBase::closePatch(const std::string& patch) {
    // [; pd-name menuclose 1(
    string patchname = (string) "pd-"+patch;
    _LOCK();
    libpd_start_message(PdContext::instance().maxMsgLen);
    libpd_add_float(1.0f);
    libpd_finish_message(patchname.c_str(), "menuclose");
    _UNLOCK();
}
Exemple #9
0
//----------------------------------------------------------
int PdBase::arraySize(const std::string& arrayName) {
    _LOCK();
    int len = libpd_arraysize(arrayName.c_str());;
    _UNLOCK();
    if(len < 0) {
        cerr << "Pd: Cannot get size of unknown array \"" << arrayName << "\"" << endl;
        return 0;
    }
    return len;
}
Exemple #10
0
bool PdBase::readArray(const std::string& arrayName, std::vector<float>& dest, int readLen, int offset) {

    _LOCK();
    int arrayLen = libpd_arraysize(arrayName.c_str());
    _UNLOCK();
    if(arrayLen < 0) {
        cerr << "Pd: Cannot read unknown array \"" << arrayName << "\"" << endl;
        return false;
    }

    // full array len?
    if(readLen < 0) {
        readLen = arrayLen;
    }
    // check read len
    else if(readLen > arrayLen) {
        cerr << "Pd: Given read len " << readLen << " > len "
             << arrayLen << " of array \"" << arrayName << "\"" << endl;
        return false;
    }

    // check offset
    if(offset+readLen > arrayLen) {
        cerr << "Pd: Given read len and offset > len " << readLen
             << " of array \"" << arrayName << "\"" << endl;
        return false;
    }

    // resize if necessary
    if(dest.size() != readLen) {
        dest.resize(readLen, 0);
    }

    _LOCK();
    if(libpd_read_array(&dest[0], arrayName.c_str(), offset, readLen) < 0) {
        cerr << "Pd: libpd_read_array failed for array \""
             << arrayName << "\"" << endl;
        _UNLOCK();
        return false;
    }
    _UNLOCK();
    return true;
}
Exemple #11
0
void PdBase::unsubscribeAll(){
    map<string,void*>& sources = PdContext::instance().sources;
    map<string,void*>::iterator iter;
    _LOCK();
    for(iter = sources.begin(); iter != sources.end(); ++iter) {
        libpd_unbind(iter->second);
    }
    _UNLOCK();
    sources.clear();
}
Exemple #12
0
void PdBase::clearArray(const std::string& arrayName, int value) {

    _LOCK();
    int arrayLen = libpd_arraysize(arrayName.c_str());
    _UNLOCK();
    if(arrayLen < 0) {
        cerr << "Pd: Cannot clear unknown array \"" << arrayName << "\"" << endl;
        return;
    }

    std::vector<float> array;
    array.resize(arrayLen, value);

    _LOCK();
    if(libpd_write_array(arrayName.c_str(), 0, &array[0], arrayLen) < 0) {
        cerr << "Pd: libpd_write_array failed while clearing array \""
             << arrayName << "\"" << endl;
    }
    _UNLOCK();
}
Exemple #13
0
bool JudgeDB::getEscapeString( std::string& sDstString, const std::string& sSrcString )
{
	CAutoLock _LOCK(getLock());
	
	CDBObject* p_judge_db = getDBObj();
	sDstString.clear();
	sDstString.resize(sSrcString.length() * 2 + 1);
	int _len = p_judge_db->getEscapeString(
		const_cast<char*>(sDstString.c_str()),
		sSrcString.c_str(), sSrcString.length());
	sDstString.resize(_len);
	return true;
}
Exemple #14
0
//--------------------------------------------------------------------
Patch PdBase::openPatch(const std::string& patch, const std::string& path) {
    _LOCK();
    // [; pd open file folder(
    void* handle = libpd_openfile(patch.c_str(), path.c_str());
    if(handle == NULL) {
        return Patch(); // return empty Patch
    }
    int dollarZero = libpd_getdollarzero(handle);
    _UNLOCK();

    
    return Patch(handle, dollarZero, patch, path);
}
Exemple #15
0
void
__funlockfile_internal(FILE *fp, int internal)
{

	if (__isthreaded == 0)
		return;

	mutex_lock(&_LOCK(fp));

	if (internal) {
		_LOCKINTERNAL(fp)--;
		if (_LOCKINTERNAL(fp) == 0)
			thr_setcancelstate(_LOCKCANCELSTATE(fp), NULL);
	}

	_LOCKCOUNT(fp)--;
	if (_LOCKCOUNT(fp) == 0) {
		_LOCKOWNER(fp) = NULL;
		cond_signal(&_LOCKCOND(fp));
	}

	mutex_unlock(&_LOCK(fp));
}
Exemple #16
0
int
ftrylockfile(FILE *fp)
{
	int retval;

	if (__isthreaded == 0)
		return 0;

	retval = 0;
	mutex_lock(&_LOCK(fp));
		
	if (_LOCKOWNER(fp) == thr_self()) {
		_LOCKCOUNT(fp)++;
	} else if (_LOCKOWNER(fp) == NULL) {
		_LOCKOWNER(fp) = thr_self();
		_LOCKCOUNT(fp) = 1;
	} else
		retval = -1;

	mutex_unlock(&_LOCK(fp));

	return retval;
}
Exemple #17
0
bool PdBase::writeArray(const std::string& arrayName, std::vector<float>& source, int writeLen, int offset) {

    _LOCK();
    int arrayLen = libpd_arraysize(arrayName.c_str());
    _UNLOCK();
    if(arrayLen < 0) {
        cerr << "Pd: Cannot write to unknown array \"" << arrayName << "\"" << endl;
        return false;
    }

    // full array len?
    if(writeLen < 0) {
        writeLen = arrayLen;
    }
    // check write len
    else if(writeLen > arrayLen) {
        cerr << "Pd: Given write len " << writeLen << " > len " << arrayLen
             << " of array \"" << arrayName << "\"" << endl;
        return false;
    }

    // check offset
    if(offset+writeLen > arrayLen) {
        cerr << "Pd: Given write len and offset > len " << writeLen
             << " of array \"" << arrayName << "\"" << endl;
        return false;
    }

    _LOCK();
    if(libpd_write_array(arrayName.c_str(), offset, &source[0], writeLen) < 0) {
        cerr << "Pd: libpd_write_array failed for array \"" << arrayName << "\"" << endl;
        _UNLOCK();
        return false;
    }
    _UNLOCK();
    return true;
}
Exemple #18
0
bool JudgeDB::saveSourceFile(const char* sCodePath, int nRunID)
{
	CAutoLock _LOCK(getLock());

	std::string sql_query;
	BaseFunc::strFormat(sql_query,
		JUDGE_CONFIG::SQL_FETCH_SUBMITION_CODE,
		nRunID
		);

	CDBObject* p_judge_db = getDBObj();
	if( !p_judge_db->Query(sql_query.c_str()) )
	{
		ADD_LOG("DBError %s, %d: %s", __FUNCTION__, __LINE__, p_judge_db->Error());
		assert(false);
		return false;
	}

	CDBObject::PQUERY_RESULT sql_res = NULL;
	sql_res = p_judge_db->StoreResult();
	if (sql_res == NULL)
		return false;

	bool b_ret = false;
	char** res_body = NULL;
	res_body = p_judge_db->FetchRow(sql_res);
	if( res_body == NULL)
		goto end;

	b_ret = true;
	FILE* p_file = fopen(sCodePath, "wb");
	if (p_file == NULL)
	{
		ADD_LOG("%s:%d Save File Fault!", __FUNCTION__, __LINE__);
		b_ret = false;
		goto end;
	}

	if( -1 == fputs(res_body[1], p_file))
		b_ret = false;

	fclose(p_file);

end:
	p_judge_db->FreeResult(sql_res);
	p_judge_db->ClearResult();

	return b_ret;
}
Exemple #19
0
//----------------------------------------------------------
void PdBase::subscribe(const std::string& source) {

    if(exists(source)) {
        cerr << "Pd: unsubscribe: ignoring duplicate source" << endl;
        return;
    }

    _LOCK();
    void* pointer = libpd_bind(source.c_str());
    _UNLOCK();
    if(pointer != NULL) {
        map<string,void*>& sources = PdContext::instance().sources;
        sources.insert(pair<string,void*>(source, pointer));
    }
}
Exemple #20
0
//----------------------------------------------------------
void PdBase::startMessage() {

    PdContext& context = PdContext::instance();

    if(context.bMsgInProgress) {
        cerr << "Pd: Can not start message, message in progress" << endl;
        return;
    }

    _LOCK();
    if(libpd_start_message(context.maxMsgLen) == 0) {
        context.bMsgInProgress = true;
        context.msgType = MSG;
    }
    _UNLOCK();
}
Exemple #21
0
void PdBase::unsubscribe(const std::string& source) {

    map<string,void*>& sources = PdContext::instance().sources;

    map<string,void*>::iterator iter;
    iter = sources.find(source);
    if(iter == sources.end()) {
        cerr << "Pd: unsubscribe: ignoring unknown source" << endl;
        return;
    }

    _LOCK();
    libpd_unbind(iter->second);
    _UNLOCK();
    sources.erase(iter);
}
Exemple #22
0
bool JudgeDB::uploadMatchResult(int nMatchID,
								const char* sMatchResult)
{
	CAutoLock _LOCK(getLock());

	std::string sql_query;
	BaseFunc::strFormat(sql_query,
		JUDGE_CONFIG::SQL_UPLOAD_JUDGE_RESULT,
		nMatchID,
		sMatchResult);

	CDBObject* p_judge_db = getDBObj();
	if( !p_judge_db->Query(sql_query.c_str()) )
	{
		ADD_LOG("DBError %s, %d: %s", __FUNCTION__, __LINE__, p_judge_db->Error());
		assert(false);
		return false;
	}

	return true;
}
Exemple #23
0
void PdBase::finishMessage(const std::string& dest, const std::string& msg) {

    PdContext& context = PdContext::instance();

    if(!context.bMsgInProgress) {
        cerr << "Pd: Can not finish message, message not in progress" << endl;
        return;
    }

    if(context.msgType != MSG) {
        cerr << "Pd: Can not finish message, midi byte stream in progress" << endl;
        return;
    }

    _LOCK();
    libpd_finish_message(dest.c_str(), msg.c_str());
    _UNLOCK();

    context.bMsgInProgress = false;
    context.curMsgLen = 0;
}
Exemple #24
0
bool JudgeDB::fetchNewMatch(MatchInfo* miBody)
{
	CAutoLock _LOCK(getLock());

	const char* sql_query = JUDGE_CONFIG::SQL_FETCH_NEW_MATCH;

	CDBObject* p_judge_db = getDBObj();
	if( !p_judge_db->Query(sql_query) )
	{
		ADD_LOG("DBError %s, %d: %s", __FUNCTION__, __LINE__, p_judge_db->Error());
		assert(false);
		return false;
	}

	CDBObject::PQUERY_RESULT sql_res = NULL;
	sql_res = p_judge_db->StoreResult();
	if (sql_res == NULL)
		return false;

	bool b_ret = false;
	char** res_body = NULL;
	res_body = p_judge_db->FetchRow(sql_res);
	if( res_body == NULL)
		goto end;

	b_ret = true;
	memset(miBody, 0, sizeof(MatchInfo));
	miBody->MatchID		= BaseFunc::strToInteger(res_body[0]);
	miBody->GameID		= BaseFunc::strToInteger(res_body[1]);
	miBody->PalyerCnt	= BaseFunc::strToInteger(res_body[2]);
	
	for (int i=0; i<miBody->PalyerCnt; ++i)
		miBody->Players[i]	= BaseFunc::strToInteger(res_body[i+3]);

end:
	p_judge_db->FreeResult(sql_res);
	p_judge_db->ClearResult();

	return b_ret;
}
Exemple #25
0
bool JudgeDB::getJudgerExecInfo(int nGameID, std::string& sPath, std::string& sMd5, std::string& sConfig)
{
	CAutoLock _LOCK(getLock());

	std::string sql_query;
	BaseFunc::strFormat(sql_query,
		JUDGE_CONFIG::SQL_GET_JUDGER_EXEC_INFO,
		nGameID
		);

	CDBObject* p_judge_db = getDBObj();
	if( !p_judge_db->Query(sql_query.c_str()) )
	{
		ADD_LOG("DBError %s, %d: %s", __FUNCTION__, __LINE__, p_judge_db->Error());
		assert(false);
		return false;
	}

	CDBObject::PQUERY_RESULT sql_res = NULL;
	sql_res = p_judge_db->StoreResult();
	if (sql_res == NULL)
		return false;

	bool b_ret = false;
	char** res_body = NULL;
	res_body = p_judge_db->FetchRow(sql_res);
	if( res_body == NULL)
		goto end;

	b_ret = true;
	sPath	= res_body[1];
	sMd5	= res_body[2];
	sConfig	= res_body[3];

end:
	p_judge_db->FreeResult(sql_res);
	p_judge_db->ClearResult();

	return b_ret;
}
Exemple #26
0
bool JudgeDB::fetchNewSubmit(SubmitionInfo* siBody)
{
	CAutoLock _LOCK(getLock());

	const char* sql_query = JUDGE_CONFIG::SQL_FETCH_NEW_SUBMITION;

	CDBObject* p_judge_db = getDBObj();
	if( !p_judge_db->Query(sql_query) )
	{
		ADD_LOG("DBError %s, %d: %s", __FUNCTION__, __LINE__, p_judge_db->Error());
		assert(false);
		return false;
	}

	CDBObject::PQUERY_RESULT sql_res = NULL;
	sql_res = p_judge_db->StoreResult();
	if (sql_res == NULL)
		return false;

	bool b_ret = false;
	char** res_body = NULL;
	res_body = p_judge_db->FetchRow(sql_res);
	if( res_body == NULL)
		goto end;

	b_ret = true;
	memset(siBody, 0, sizeof(SubmitionInfo));
	siBody->RunID		= BaseFunc::strToInteger(res_body[0]);
	siBody->GameID		= BaseFunc::strToInteger(res_body[1]);
	siBody->UserID		= BaseFunc::strToInteger(res_body[2]);
	siBody->LanguageID	= (CodeLanguage)BaseFunc::strToInteger(res_body[3]);

end:
	p_judge_db->FreeResult(sql_res);
	p_judge_db->ClearResult();

	return b_ret;
}
Exemple #27
0
bool JudgeDB::storePlayerExecFile(int nRunID,
								const char* sFilePath,
								const char* sFileMd5 )
{
	CAutoLock _LOCK(getLock());

	std::string sql_query;
	BaseFunc::strFormat(sql_query,
		JUDGE_CONFIG::SQL_STORE_PLAYER_EXEC_FILE,
		nRunID,
		sFilePath,
		sFileMd5);

	CDBObject* p_judge_db = getDBObj();
	if( !p_judge_db->Query(sql_query.c_str()) )
	{
		ADD_LOG("DBError %s, %d: %s", __FUNCTION__, __LINE__, p_judge_db->Error());
		assert(false);
		return false;
	}

	return true;
}
Exemple #28
0
void PdBase::clearSearchPath() {
    _LOCK();
    libpd_clear_search_path();
    _UNLOCK();
}
Exemple #29
0
//--------------------------------------------------------------------
void PdBase::addToSearchPath(const std::string& path) {
    _LOCK();
    libpd_add_to_search_path(path.c_str());
    _UNLOCK();
}
Exemple #30
0
void PdBase::clear() {
    _LOCK();
    PdContext::instance().clear();
    _UNLOCK();
    unsubscribeAll();
}