void media::release(LinkedObject **nat, unsigned expires) { assert(nat != NULL); proxy *member; time_t expire = 0; if(!*nat) return; if(expires) { time(&expire); expire += expires; } lock.acquire(); linked_pointer<proxy> pp = *nat; while(is(pp)) { member = *pp; pp.next(); member->release(expires); member->enlist(&runlist); } lock.release(); *nat = NULL; }
kernel loadKernel(occa::device_v *dHandle, const std::string &kernelName){ infoID_t infoID; infoID.modelID = dHandle->modelID(); infoID.kernelName = kernelName; headerMutex.lock(); const infoHeader_t &h = headerMap[infoID]; headerMutex.unlock(); const std::string hFilename = fileDatabase::getFilename(h.fileID); FILE *inFD = fopen(hFilename.c_str(), "rb"); char *buffer = new char[h.contentBytes + 1]; buffer[h.contentBytes] = '\0'; fseek(inFD, h.contentOffset, SEEK_SET); ignoreResult( fread(buffer, sizeof(char), h.contentBytes, inFD) ); fclose(inFD); kernel k = kernel(dHandle->loadKernelFromLibrary(buffer, kernelName)); delete [] buffer; fclose(inFD); return k; }
void messages::update(const char *uid) { assert(uid == NULL || *uid != 0); linked_pointer<message> mp; LinkedObject *next; unsigned path; time_t now; if(!uid || !pending) return; path = NamedObject::keyindex(uid, keysize); msglock.lock(); time(&now); mp = msgs[path]; while(mp) { next = mp->getNext(); if(!stricmp(mp->user, uid)) { --pending; mp->delist(&msgs[path]); if(mp->expires < now) mp->enlist(&freelist); else mp->enlist(&sending); } mp = next; } msglock.unlock(); }
int messages::deliver(const char *to, const char *reply, const char *from, caddr_t text, size_t len, const char *msgtype, const char *digest) { message *msg; if(!msgtype) msgtype = "text/plain"; if(len > sizeof(msg->body)) return SIP_MESSAGE_TOO_LARGE; msglock.lock(); msg = static_cast<message *>(freelist); if(msg) freelist = msg->getNext(); msglock.unlock(); if(!msg) { ++allocated; msg = new message(); } msg->create(); String::set(msg->reply, sizeof(msg->reply), reply); String::set(msg->from, sizeof(msg->from), from); String::set(msg->type, sizeof(msg->type), msgtype); memset(msg->body, 0, sizeof(msg->body)); if(len) memcpy(msg->body, text, len); msg->msglen = len; if(!strchr(to, '@')) { String::set(msg->user, sizeof(msg->user), to); return deliver(msg); } return remote(to, msg, digest); }
bool messages::check(void) { shell::log(shell::INFO, "checking messages..."); msglock.lock(); msglock.release(); return true; }
int genDeviceID(){ deviceIDMutex.lock(); const int id = (currentDeviceID++); deviceIDMutex.unlock(); return id; }
OffloadHostTimerData* offload_timer_init(const char *file, int line) { static bool first_time = true; OffloadHostTimerData* timer_data = NULL; timer_data_mutex.lock(); { if (timer_enabled || (offload_report_level && offload_report_enabled)) { timer_data = (OffloadHostTimerData*) OFFLOAD_MALLOC(sizeof(OffloadHostTimerData), 0); memset(timer_data, 0, sizeof(OffloadHostTimerData)); timer_data->offload_number = OFFLOAD_DEBUG_INCR_OFLD_NUM() - 1; if (timer_data_head == 0) { timer_data_head = timer_data; timer_data_tail = timer_data; } else { timer_data_tail->next = timer_data; timer_data_tail = timer_data; } timer_data->file = file; timer_data->line = line; } } timer_data_mutex.unlock(); return timer_data; }
void messages::cleanup(void) { linked_pointer<message> mp; LinkedObject *msgnext; unsigned msgcount = 0; time_t now; if(!pending) return; while(msgcount < keysize) { msglock.lock(); time(&now); mp = msgs[msgcount]; while(mp) { msgnext = mp->getNext(); if(mp->expires < now) { mp->delist(&msgs[msgcount]); mp->enlist(&freelist); } mp = msgnext; } msglock.unlock(); ++msgcount; } }
void load(const std::string &filename){ //---[ Load file ]------ std::string sBuffer = readFile(filename); const char *buffer = sBuffer.c_str(); //---[ Read file ]------ const uint32_t *buffer32 = (const uint32_t*) buffer; const uint64_t *buffer64; const uint32_t headerCount = *(buffer32++); for(uint32_t i = 0; i < headerCount; ++i){ infoID_t infoID; const int mode_ = *(buffer32++); buffer64 = (const uint64_t*) buffer32; const uint64_t flagsOffset = *(buffer64++); const uint64_t flagsBytes = *(buffer64++); const uint64_t contentOffset = *(buffer64++); const uint64_t contentBytes = *(buffer64++); const uint64_t kernelNameOffset = *(buffer64++); const uint64_t kernelNameBytes = *(buffer64++); buffer32 = (const uint32_t*) buffer64; infoID.kernelName = std::string(buffer + kernelNameOffset, kernelNameBytes); deviceIdentifier identifier(mode_, buffer + flagsOffset, flagsBytes); infoID.modelID = deviceModelID(identifier); kernelMutex.lock(); kernelMap[infoID.kernelName].push_back(infoID.modelID); kernelMutex.unlock(); //---[ Input to header map ]---- headerMutex.lock(); infoHeader_t &h = headerMap[infoID]; h.fileID = fileDatabase::getFileID(filename); h.mode = mode_; h.flagsOffset = flagsOffset; h.flagsBytes = flagsBytes; h.contentOffset = contentOffset; h.contentBytes = contentBytes; h.kernelNameOffset = kernelNameOffset; h.kernelNameBytes = kernelNameBytes; headerMutex.unlock(); //============================== } }
size_t addToScratchPad(const std::string &s){ scratchMutex.lock(); size_t offset = scratchPad.size(); scratchPad += s; scratchMutex.unlock(); return offset; }
void media::thread::run(void) { fd_set session; socket_t max; shell::log(DEBUG1, "starting media thread"); running = true; socket_t so; media::proxy *mp; time_t now; while(running) { lock.acquire(); max = hiwater; memcpy(&session, &connections, sizeof(session)); lock.release(); select(max, &session, NULL, NULL, NULL); if(!running) break; time(&now); for(so = 0; so < max; ++so) { #ifdef _MSWINDOWS_ #else char buf[1]; if(so == control[0] && FD_ISSET(so, &session)) { if(::read(so, buf, 1) < 1) shell::log(shell::ERR, "media control failure"); continue; } #endif mp = NULL; if(!FD_ISSET(so, &session)) continue; lock.acquire(); mp = proxymap[so]; if(mp->so == INVALID_SOCKET) { proxymap[so] = NULL; mp = NULL; } if(mp && mp->expires && mp->expires < now) mp->release(0); else if(mp) mp->copy(); lock.release(); } } shell::log(DEBUG1, "stopping media thread"); running = true; }
std::string getFilename(const int id){ OCCA_CHECK((0 <= id) && (id < filesInDatabase), "File with ID [" << id << "] was not found"); mutex.lock(); std::string filename = itsMap[id]; mutex.unlock(); return filename; }
int deviceModelID(const occa::deviceIdentifier &id){ deviceModelMutex.lock(); deviceModelMapIterator it = deviceModelMap.find(id); int dID; if(it != deviceModelMap.end()) dID = it->second; else{ dID = deviceModelMap.size(); deviceModelMap[id] = dID; } deviceModelMutex.unlock(); return dID; }
int getFileID(const std::string &filename){ int id; mutex.lock(); stiMapIterator it = stiMap.find(filename); if(it == stiMap.end()){ id = (filesInDatabase++); stiMap[filename] = id; itsMap[id] = filename; } else id = (it->second); mutex.unlock(); return id; }
occa::kernelDatabase loadKernelDatabase(const std::string &kernelName){ kernelDatabase kdb(kernelName); kernelMutex.lock(); kernelMapIterator it = kernelMap.find(kernelName); if(it != kernelMap.end()){ std::vector<int> &ids = it->second; const int idCount = ids.size(); for(int i = 0; i < idCount; ++i) kdb.modelKernelIsAvailable(ids[i]); } kernelMutex.unlock(); return kdb; }
namespace fileDatabase { mutex_t mutex; itsMap_t itsMap; stiMap_t stiMap; int filesInDatabase = 0; int getFileID(const std::string &filename){ int id; mutex.lock(); stiMapIterator it = stiMap.find(filename); if(it == stiMap.end()){ id = (filesInDatabase++); stiMap[filename] = id; itsMap[id] = filename; } else id = (it->second); mutex.unlock(); return id; } std::string getFilename(const int id){ OCCA_CHECK((0 <= id) && (id < filesInDatabase), "File with ID [" << id << "] was not found"); mutex.lock(); std::string filename = itsMap[id]; mutex.unlock(); return filename; } };
void ClearInterruptFlag(uint32 flag) { intflags_mutex.lock(); InterruptFlags &= ~flag; intflags_mutex.unlock(); }
void SetInterruptFlag(uint32 flag) { intflags_mutex.lock(); InterruptFlags |= flag; intflags_mutex.unlock(); }
void media::sdp::check_media(char *buffer, size_t len) { char *cp, *ep, *sp; char tmp[128]; char mtype[32]; unsigned tport; unsigned count = 1; media::proxy *pp; if(strnicmp(buffer, "m=", 2)) return; cp = sp = strchr(buffer, ' '); if(!cp) return; while(isspace(*cp)) ++cp; tport = atoi(cp); if(!tport) return; ep = strchr(cp, '/'); if(ep) count = atoi(ep + 1); // at the moment we can only do rtp/rtcp pairs... if(count > 2) { result = NULL; return; } mediacount = count; count = align(count); ep = strchr(cp, ' '); if(!ep) ep = (char *)""; else while(isspace(*ep)) ++ep; mediaport = tport; mediacount = count; tport = 0; lock.acquire(); String::set(tmp, sizeof(tmp), ep); while(count--) { pp = media::get(this); if(!pp) { result = NULL; lock.release(); return; } if(!tport) tport = (pp->port / 2) * 2; } lock.release(); *sp = 0; String::set(mtype, sizeof(mtype), buffer); if(mediacount > 1) snprintf(buffer, len, "%s %u/%u %s", mtype, tport, mediacount, tmp); else snprintf(buffer, len, "%s %u %s", mtype, tport, tmp); mediacount = align(mediacount); }
namespace library { mutex_t headerMutex, kernelMutex; mutex_t deviceIDMutex, deviceModelMutex; mutex_t scratchMutex; headerMap_t headerMap; kernelMap_t kernelMap; deviceModelMap_t deviceModelMap; std::string scratchPad; int currentDeviceID = 0; size_t addToScratchPad(const std::string &s){ scratchMutex.lock(); size_t offset = scratchPad.size(); scratchPad += s; scratchMutex.unlock(); return offset; } void load(const std::string &filename){ //---[ Load file ]------ std::string sBuffer = readFile(filename); const char *buffer = sBuffer.c_str(); //---[ Read file ]------ const uint32_t *buffer32 = (const uint32_t*) buffer; const uint64_t *buffer64; const uint32_t headerCount = *(buffer32++); for(uint32_t i = 0; i < headerCount; ++i){ infoID_t infoID; const int mode_ = *(buffer32++); buffer64 = (const uint64_t*) buffer32; const uint64_t flagsOffset = *(buffer64++); const uint64_t flagsBytes = *(buffer64++); const uint64_t contentOffset = *(buffer64++); const uint64_t contentBytes = *(buffer64++); const uint64_t kernelNameOffset = *(buffer64++); const uint64_t kernelNameBytes = *(buffer64++); buffer32 = (const uint32_t*) buffer64; infoID.kernelName = std::string(buffer + kernelNameOffset, kernelNameBytes); deviceIdentifier identifier(mode_, buffer + flagsOffset, flagsBytes); infoID.modelID = deviceModelID(identifier); kernelMutex.lock(); kernelMap[infoID.kernelName].push_back(infoID.modelID); kernelMutex.unlock(); //---[ Input to header map ]---- headerMutex.lock(); infoHeader_t &h = headerMap[infoID]; h.fileID = fileDatabase::getFileID(filename); h.mode = mode_; h.flagsOffset = flagsOffset; h.flagsBytes = flagsBytes; h.contentOffset = contentOffset; h.contentBytes = contentBytes; h.kernelNameOffset = kernelNameOffset; h.kernelNameBytes = kernelNameBytes; headerMutex.unlock(); //============================== } } void save(const std::string &filename){ headerMutex.lock(); FILE *outFD = fopen(filename.c_str(), "wb"); uint32_t headerCount = headerMap.size(); if(headerCount == 0){ headerMutex.unlock(); return; } fwrite(&headerCount, sizeof(uint32_t), 1, outFD); cHeaderMapIterator it = headerMap.begin(); const uint64_t headerOffset = headerCount * (( sizeof(uint32_t)) + (6*sizeof(uint64_t))); uint64_t contentOffsets = sizeof(headerCount) + headerOffset; for(uint32_t i = 0; i < headerCount; ++i){ const infoHeader_t &h = it->second; fwrite(&(h.mode), sizeof(uint32_t), 1, outFD); fwrite(&(contentOffsets), sizeof(uint64_t), 1, outFD); fwrite(&(h.flagsBytes) , sizeof(uint64_t), 1, outFD); contentOffsets += h.flagsBytes; fwrite(&(contentOffsets), sizeof(uint64_t), 1, outFD); fwrite(&(h.contentBytes), sizeof(uint64_t), 1, outFD); contentOffsets += h.contentBytes; fwrite(&(contentOffsets) , sizeof(uint64_t), 1, outFD); fwrite(&(h.kernelNameBytes), sizeof(uint64_t), 1, outFD); contentOffsets += h.kernelNameBytes; ++it; } it = headerMap.begin(); for(uint32_t i = 0; i < headerCount; ++i){ const infoHeader_t &h = it->second; ++it; char *buffer = new char[std::max(h.flagsBytes, std::max(h.contentBytes, h.kernelNameBytes))]; if(0 <= h.fileID){ const std::string hFilename = fileDatabase::getFilename(h.fileID); FILE *inFD = fopen(hFilename.c_str(), "rb"); fseek(inFD, h.flagsOffset, SEEK_SET); ignoreResult( fread(buffer , sizeof(char), h.flagsBytes, inFD) ); fwrite(buffer, sizeof(char), h.flagsBytes, outFD); ignoreResult( fread(buffer , sizeof(char), h.contentBytes, inFD) ); fwrite(buffer, sizeof(char), h.contentBytes, outFD); ignoreResult( fread(buffer , sizeof(char), h.kernelNameBytes, inFD) ); fwrite(buffer, sizeof(char), h.kernelNameBytes, outFD); fclose(inFD); delete [] buffer; } else{ const char *c = scratchPad.c_str(); const char *c1 = c + h.flagsOffset; const char *c2 = c + h.contentOffset; const char *c3 = c + h.kernelNameOffset; fwrite(c1, sizeof(char), h.flagsBytes , outFD); fwrite(c2, sizeof(char), h.contentBytes , outFD); fwrite(c3, sizeof(char), h.kernelNameBytes, outFD); } } fclose(outFD); headerMutex.unlock(); } int genDeviceID(){ deviceIDMutex.lock(); const int id = (currentDeviceID++); deviceIDMutex.unlock(); return id; } int deviceModelID(occa::device &dev){ return deviceModelID(dev.getIdentifier()); } int deviceModelID(const occa::deviceIdentifier &id){ deviceModelMutex.lock(); deviceModelMapIterator it = deviceModelMap.find(id); int dID; if(it != deviceModelMap.end()) dID = it->second; else{ dID = deviceModelMap.size(); deviceModelMap[id] = dID; } deviceModelMutex.unlock(); return dID; } occa::kernelDatabase loadKernelDatabase(const std::string &kernelName){ kernelDatabase kdb(kernelName); kernelMutex.lock(); kernelMapIterator it = kernelMap.find(kernelName); if(it != kernelMap.end()){ std::vector<int> &ids = it->second; const int idCount = ids.size(); for(int i = 0; i < idCount; ++i) kdb.modelKernelIsAvailable(ids[i]); } kernelMutex.unlock(); return kdb; } kernel loadKernel(occa::device_v *dHandle, const std::string &kernelName){ infoID_t infoID; infoID.modelID = dHandle->modelID(); infoID.kernelName = kernelName; headerMutex.lock(); const infoHeader_t &h = headerMap[infoID]; headerMutex.unlock(); const std::string hFilename = fileDatabase::getFilename(h.fileID); FILE *inFD = fopen(hFilename.c_str(), "rb"); char *buffer = new char[h.contentBytes + 1]; buffer[h.contentBytes] = '\0'; fseek(inFD, h.contentOffset, SEEK_SET); ignoreResult( fread(buffer, sizeof(char), h.contentBytes, inFD) ); fclose(inFD); kernel k = kernel(dHandle->loadKernelFromLibrary(buffer, kernelName)); delete [] buffer; fclose(inFD); return k; } };
void save(const std::string &filename){ headerMutex.lock(); FILE *outFD = fopen(filename.c_str(), "wb"); uint32_t headerCount = headerMap.size(); if(headerCount == 0){ headerMutex.unlock(); return; } fwrite(&headerCount, sizeof(uint32_t), 1, outFD); cHeaderMapIterator it = headerMap.begin(); const uint64_t headerOffset = headerCount * (( sizeof(uint32_t)) + (6*sizeof(uint64_t))); uint64_t contentOffsets = sizeof(headerCount) + headerOffset; for(uint32_t i = 0; i < headerCount; ++i){ const infoHeader_t &h = it->second; fwrite(&(h.mode), sizeof(uint32_t), 1, outFD); fwrite(&(contentOffsets), sizeof(uint64_t), 1, outFD); fwrite(&(h.flagsBytes) , sizeof(uint64_t), 1, outFD); contentOffsets += h.flagsBytes; fwrite(&(contentOffsets), sizeof(uint64_t), 1, outFD); fwrite(&(h.contentBytes), sizeof(uint64_t), 1, outFD); contentOffsets += h.contentBytes; fwrite(&(contentOffsets) , sizeof(uint64_t), 1, outFD); fwrite(&(h.kernelNameBytes), sizeof(uint64_t), 1, outFD); contentOffsets += h.kernelNameBytes; ++it; } it = headerMap.begin(); for(uint32_t i = 0; i < headerCount; ++i){ const infoHeader_t &h = it->second; ++it; char *buffer = new char[std::max(h.flagsBytes, std::max(h.contentBytes, h.kernelNameBytes))]; if(0 <= h.fileID){ const std::string hFilename = fileDatabase::getFilename(h.fileID); FILE *inFD = fopen(hFilename.c_str(), "rb"); fseek(inFD, h.flagsOffset, SEEK_SET); ignoreResult( fread(buffer , sizeof(char), h.flagsBytes, inFD) ); fwrite(buffer, sizeof(char), h.flagsBytes, outFD); ignoreResult( fread(buffer , sizeof(char), h.contentBytes, inFD) ); fwrite(buffer, sizeof(char), h.contentBytes, outFD); ignoreResult( fread(buffer , sizeof(char), h.kernelNameBytes, inFD) ); fwrite(buffer, sizeof(char), h.kernelNameBytes, outFD); fclose(inFD); delete [] buffer; } else{ const char *c = scratchPad.c_str(); const char *c1 = c + h.flagsOffset; const char *c2 = c + h.contentOffset; const char *c3 = c + h.kernelNameOffset; fwrite(c1, sizeof(char), h.flagsBytes , outFD); fwrite(c2, sizeof(char), h.contentBytes , outFD); fwrite(c3, sizeof(char), h.kernelNameBytes, outFD); } } fclose(outFD); headerMutex.unlock(); }