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)); }
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++; }
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); }
//-------------------------------------------------------------------- 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; }
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; }
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++; }
void PdBase::closePatch(Patch& patch) { if(!patch.isValid()) { return; } _LOCK(); libpd_closefile(patch.handle()); _UNLOCK(); patch.clear(); }
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(); }
//---------------------------------------------------------- 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; }
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; }
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(); }
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(); }
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; }
//-------------------------------------------------------------------- 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); }
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)); }
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; }
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; }
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; }
//---------------------------------------------------------- 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)); } }
//---------------------------------------------------------- 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(); }
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); }
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; }
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; }
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; }
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; }
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; }
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; }
void PdBase::clearSearchPath() { _LOCK(); libpd_clear_search_path(); _UNLOCK(); }
//-------------------------------------------------------------------- void PdBase::addToSearchPath(const std::string& path) { _LOCK(); libpd_add_to_search_path(path.c_str()); _UNLOCK(); }
void PdBase::clear() { _LOCK(); PdContext::instance().clear(); _UNLOCK(); unsubscribeAll(); }