bool JdbcUtil::removeMemory(void) { setError(NULL); if (!getMemory(false)) { // If not found, it does not exist if (errorCode == ENOENT) return true; return false; } // deactivate the old memory in case some still has it attached if (attachMemory()) { sharedMemory->active = false; detachMemory(); } // Remove the memory if (shmctl(sharedMemoryId,IPC_RMID,NULL)==-1) { setPerror("Cannot remove shared memory"); return false; } sharedMemoryId = -1; return true; }
bool JdbcUtil::showStatus(void) { if (!attachMemory()) return false; int idx=0; int freeCount=0; printf("Process Status:\n"); for (idx=0; idx<MAX_PROCESSES; idx++) { if (sharedMemory->processTable[idx].processId) { char *pending; if (sharedMemory->processTable[idx].pending) pending = "Yes"; else pending = "No"; printf(" Pid: %u\n", sharedMemory->processTable[idx].processId); printf(" Cmd: %s\n",getCmdName(sharedMemory->processTable[idx].cmd)); printf(" Pending: %s\n",pending); printf(" Parameter: %s\n",sharedMemory->processTable[idx].parameter); } else freeCount++; } if (freeCount==MAX_PROCESSES) printf(" Process Table Empty.\n"); return true; }
bool JdbcUtil::removeProcess(int idx) { bool rc = true; if (!attachMemory()) return false; if (!lockSemaphore(SEM_MEMORY_ACCESS, true)) return false; sharedMemory->processTable[idx].processId = 0; if (!lockSemaphore(SEM_MEMORY_ACCESS, false)) return false; return rc; }
/** * @brief * * @return */ int MemoryInputV1::run() { SharedData sharedData; memset( &sharedData, 0, sizeof(sharedData) ); while( !mStop ) { Info( "Querying memory" ); if ( queryMemory( &sharedData ) && sharedData.valid ) break; Info( "Can't query shared memory" ); usleep( 500000 ); } //Info( "SHV: %d", sharedData.valid ); //mImageCount = 40; //mPixelFormat = sharedData.imageFormat; //mPixelFormat = PIX_FMT_UYVY422; //mPixelFormat = PIX_FMT_YUYV422; //mPixelFormat = PIX_FMT_RGB24; //mFrameRate = 15; ////mImageWidth = sharedData.imageWidth; //mImageWidth = 720; ////mImageHeight = sharedData.imageHeight; //mImageHeight = 576; attachMemory( mImageCount, mPixelFormat, mImageWidth, mImageHeight ); int lastWriteIndex = 0; while( !mStop ) { if ( !mSharedData || !mSharedData->valid ) { stop(); break; } if ( mSharedData->last_write_index != lastWriteIndex ) { const FeedFrame *frame = loadFrame(); //Info( "Sending frame %d", frame->id() ); lastWriteIndex = mSharedData->last_write_index; distributeFrame( FramePtr( frame ) ); //delete frame; mFrameCount++; } usleep( INTERFRAME_TIMEOUT ); } cleanup(); return( !ended() ); }
bool JdbcUtil::cleanup(void) { if (!attachMemory()) return false; bool cleanList[MAX_PROCESSES]; bool cleanTable = false; int idx=0; for (idx=0; idx<MAX_PROCESSES; idx++) { cleanList[idx] = false; if (sharedMemory->processTable[idx].processId) { if (kill(sharedMemory->processTable[idx].processId,0)==-1) { if (errno==ESRCH) { cleanList[idx] = true; cleanTable = true; } } } } if (cleanTable) { printf("Cleaning out the following processes:\n"); for (idx=0; idx<MAX_PROCESSES; idx++) { if (cleanList[idx]) { printf(" %u\n",sharedMemory->processTable[idx].processId); } } if (!lockSemaphore(SEM_MEMORY_ACCESS, true)) return false; for (idx=0; idx<MAX_PROCESSES; idx++) { if (cleanList[idx]) { sharedMemory->processTable[idx].processId = 0; } } if (!lockSemaphore(SEM_MEMORY_ACCESS, false)) return false; } return true; }
int JdbcUtil::addProcess(pid_t pid) { if (!attachMemory()) return -1; if (!lockSemaphore(SEM_MEMORY_ACCESS,true)) return -1; int rc = -1; thisProcessId = pid; int idx = findProcess(thisProcessId); if (idx>=0) { setError("Cannot add existing process to table"); } else { idx = findProcess(0); if (idx<0) { if (lockSemaphore(SEM_MEMORY_ACCESS, false)) { if (cleanup()) { if (lockSemaphore(SEM_MEMORY_ACCESS, true)) { idx = findProcess(0); if (idx<0) { setError("Too many processes"); } } } } } if (idx>=0) { sharedMemory->processTable[idx].processId = thisProcessId; sharedMemory->processTable[idx].cmd = JDBCUTIL_CMD_NOP; sharedMemory->processTable[idx].pending = false; rc = idx; memoryIdx = idx; } } if (!lockSemaphore(SEM_MEMORY_ACCESS, false)) rc = -1; return rc; }
bool JdbcUtil::setCmd(int cmd, const char *param1, const char *param2, bool force) { if (!attachMemory()) return false; if (param1 && (strlen(param1) >= sizeof(sharedMemory->processTable[0].parameter[0]))) { setError("First parameter is too long"); return false; } if (param2 && (strlen(param2) >= sizeof(sharedMemory->processTable[0].parameter[0]))) { setError("Second parameter is too long"); return false; } int idx=0; for (idx=0; idx<MAX_PROCESSES; idx++) { if (sharedMemory->processTable[idx].processId) { if (sharedMemory->processTable[idx].pending && !force) { printf(" Pid: %u - Cannot send command. A command is already pending.\n", sharedMemory->processTable[idx].processId); } else { sharedMemory->processTable[idx].cmd = cmd; if (param1) strcpy(sharedMemory->processTable[idx].parameter[0],param1); else sharedMemory->processTable[idx].parameter[0][0] = 0; if (param2) strcpy(sharedMemory->processTable[idx].parameter[1],param2); else sharedMemory->processTable[idx].parameter[1][0] = 0; sharedMemory->processTable[idx].pending = true; printf(" Pid: %u - Sent Command\n", sharedMemory->processTable[idx].processId); } } } return true; }
const struct JdbcUtilProcessTableStruct *JdbcUtil::getCmd(void) { if (!attachMemory()) return NULL; static struct JdbcUtilProcessTableStruct rc; rc.cmd = JDBCUTIL_CMD_NOP; if (sharedMemory->processTable[memoryIdx].processId!=thisProcessId) { setError("Process Id was removed from process table"); return NULL; } if (sharedMemory->processTable[memoryIdx].pending) { memcpy(&rc,sharedMemory->processTable+memoryIdx,sizeof(rc)); sharedMemory->processTable[memoryIdx].pending = false; } return &rc; }
bool JdbcUtil::memoryInit(void) { setError(NULL); if (!removeMemory()) return false; if (!removeSemaphores()) return false; if (!getMemory(true)) return false; if (!attachMemory()) return false; memset(sharedMemory,0,SHARED_MEMORY_SIZE); sharedMemory->active = true; if (!getSemaphores(true)) return false; if (semctl(semaphoreId, 0, SETVAL, 1) == -1) { int err = errno; removeMemory(); removeSemaphores(); setPerror(err,"Cannot initialize semaphore value"); return false; } return true; }
int getSharedMemory(char *fileName, int id) { int result = getShMemId(fileName, id, 0666); if (result < 0) return -1; return attachMemory(); }