示例#1
0
Error
IOS_DestroySempahore(SemaphoreId id)
{
   auto semaphore = getSemaphore(id);
   if (!semaphore) {
      return Error::Invalid;
   }

   semaphore->count = 0;
   semaphore->maxCount = 0;

   // Add semaphore to the free semaphore linked list.
   auto index = static_cast<int16_t>(semaphore - phys_addrof(sData->semaphores));
   auto prevSemaphoreIndex = sData->lastFreeSemaphoreIndex;

   if (prevSemaphoreIndex >= 0) {
      auto prevSemaphore = phys_addrof(sData->semaphores[sData->lastFreeSemaphoreIndex]);
      prevSemaphore->nextFreeSemaphoreIndex = index;
   }

   semaphore->prevFreeSemaphoreIndex = prevSemaphoreIndex;
   semaphore->nextFreeSemaphoreIndex = int16_t { -1 };

   sData->lastFreeSemaphoreIndex = index;

   if (sData->firstFreeSemaphoreIndex < 0) {
      sData->firstFreeSemaphoreIndex = index;
   }

   internal::wakeupAllThreads(phys_addrof(semaphore->waitThreadQueue),
                              Error::Intr);
   internal::reschedule();
   return Error::Invalid;
}
示例#2
0
Error
IOS_WaitSemaphore(SemaphoreId id,
                  BOOL tryWait)
{
   auto semaphore = getSemaphore(id);
   if (!semaphore) {
      return Error::Invalid;
   }

   if (semaphore->count <= 0 && tryWait) {
      return Error::SemUnavailable;
   }

   while (semaphore->count <= 0) {
      internal::sleepThread(phys_addrof(semaphore->waitThreadQueue));
      internal::reschedule();

      auto thread = internal::getCurrentThread();
      if (thread->context.queueWaitResult != Error::OK) {
         return thread->context.queueWaitResult;
      }
   }

   semaphore->count -= 1;
   return Error::OK;
}
示例#3
0
文件: SharedMem.cpp 项目: aopui/gnash
bool
SharedMem::attach()
{
#if !ENABLE_SHARED_MEM
# error "You need SYSV Shared memory support to use this option"
#endif
    
    // Don't try to attach twice.
    if (_addr) return true;
    
    _shmkey = rcfile.getLCShmKey();

    // Check rcfile for key; if there isn't one, use the Adobe key.
    if (_shmkey == 0) {
        log_debug("No shared memory key specified in rcfile. Using default for communication with other players");
        _shmkey = 0xdd3adabd;
    }
    
    log_debug("Using shared memory key %s",
            boost::io::group(std::hex, std::showbase, _shmkey));

    if (!getSemaphore()) {
        return false;
    }

    // Note that at this point semval is of indeterminate value: for the
    // process that created the semaphore, the value is 1, but for another
    // process the value would depend on whether there is an existing lock.
    
    Lock lock(*this);

    // Then attach shared memory. See if it exists.
    _shmid = ::shmget(_shmkey, _size, 0600);

    // If not create it.
    if (_shmid < 0) {
        _shmid = ::shmget(_shmkey, _size, IPC_CREAT | 0660);
    }

    if (_shmid < 0) {
        log_error(_("Unable to get shared memory segment!"));
        return false;
    }

    _addr = static_cast<iterator>(::shmat(_shmid, nullptr, 0));

    if (!_addr) {
        log_error(_("Unable to attach shared memory: %s"),
                std::strerror(errno));
        return false;
    }

    assert(_addr);
    return true;
}
示例#4
0
void consume(int *memAddr, int semID)
{
    while(1) {
        getSemaphore(semID);
        if(memAddr[FILLINDEX] > 0) {
            getNumber(memAddr);
        }
        releaseSemaphore(semID);
        sleep(1);
    }
}
示例#5
0
void produce(int *memoryAddr, int semID)
{
    while(1) {
        getSemaphore(semID);
        if(memoryAddr[FILLINDEX] < ARRAYSIZE) {
            putNumber(memoryAddr);
        }
        releaseSemaphore(semID);
        sleep(1);
    }
}
示例#6
0
Error
IOS_SignalSempahore(SemaphoreId id)
{
   auto semaphore = getSemaphore(id);
   if (!semaphore) {
      return Error::Invalid;
   }

   if (semaphore->count < semaphore->maxCount) {
      semaphore->count += 1;
   }

   internal::wakeupOneThread(phys_addrof(semaphore->waitThreadQueue), Error::OK);
   internal::reschedule();
   return Error::OK;
}
void initIPC() {
	if (data == NULL) getSharedMemory(DIRECTORY_NUMERADOR_CLIENTES, ID_NUMERADOR_CLIENTES);
	if (semaphoreId == 0) getSemaphore(DIRECTORY_NUMERADOR_CLIENTES, ID_NUMERADOR_CLIENTES, 1);
}
示例#8
0
int sched::sched_sleep(int sleeptime)
{
	// Timers:
	timeval currTime;
	timeval sleepStartTime;
	int prevTimeSlept = 0;
	int currTimeSlept = 0;
	
	// Figure out how long to sleep:
	//int time2sleep = sleeptime;	
	/*if( sleeptime != -1 )
	{
		time2sleep = sleeptime - timeSlept;
	}*/
	prevTimeSlept = timeSlept;
	
	myLogger.lw(SPAM,"SLEEP: Taking a nap for sleeptime: %d  - timeslept: %d = %d seconds. Play = %d.", sleeptime, timeSlept, sleeptime - timeSlept, (int) play );
	
	// Get time:
	gettimeofday(&currTime, NULL);
	sleepStartTime = currTime;
	do
	{
		// Checking for signal:
		if(*stop)
			return -1;
		
		myLogger.lw(SPAM,"SLEEP: Play = %d, Sleeping for %d more seconds.", (int) play, sleeptime - timeSlept );
		
		// Kick pdog:
		//myLogger.lw(SPAM,"SLEEP: Kicking pdog.");
		if ((setSemaphore(semid,SCHED_KICK_SEM,1)) == -1)
		{
			myLogger.lw(ERROR,"SLEEP: Unable to set semaphore!");
			if ((semid = getSemaphoreID(KICK_PATH,NUM_KICK)) == -1)
	        {
	            myLogger.lw(ERROR,"SLEEP: Unable to grab semaphores (%s)", KICK_PATH);
	        }
		}

		// Sleep:
 		usleep(1000000);
 		
 		// See if we are in pause or play mode:
 		play = (bool) getSemaphore(sched_semid,1);
 		setGV(playFname, (int) play);
		
		// Calculate amount of time slept so far if we are not paused:
		if( play )
		{
			// Get the current time:
			gettimeofday(&currTime, NULL);
			currTimeSlept = (currTime.tv_sec - sleepStartTime.tv_sec);
			timeSlept = prevTimeSlept + currTimeSlept;
			setGV(timeSleptFname, timeSlept);
			setSemaphore(sched_semid,4,timeSlept);
		}
		else
		{
			// Store previous time:
			prevTimeSlept = timeSlept;
			
			// Reset the sleep start time until we are in play mode again:
			gettimeofday(&sleepStartTime, NULL);
		}
		
	} while( (timeSlept < sleeptime) || (sleeptime == -1) || !play );
	
	// Reset time slept to zero:
	timeSlept = 0;
	setGV(timeSleptFname, timeSlept);
	
	return 0;
}
示例#9
0
void CommandWrapper::execute(message* msg)
{	
	// Lets prevent seggies...
	msg->rsp.length = 0;
				
	// Subsystem or Process??
	if(msg->cmd.proc >= TOT_PROC) // Subsystem Block
	{
		msg->err = 0;
		msg->qid = myqid;   // make sure the message come back to by queue
		msg->mtype = mytid; // unique message type for each process / thread
		
		if(msg->cmd.type == STATUS || msg->cmd.type == GET || msg->cmd.type == SET || msg->cmd.type == DATA)
		{
			// cout << msg->toString() << endl;
			if( msgQueueWrapper.msgQueueSend(ssmqid, msg) == -1 )
			{
				msg->rsp.ret = ERR_MSGQ_SEND;
				return;
			}
			
			if( msgQueueWrapper.msgQueueReceive(myqid, msg->mtype, msg) == -1 )
			{
				msg->rsp.ret = ERR_MSGQ_RECV;
				return;
			}
			// cout << msg->toString() << endl;
		}
		else if(msg->cmd.type == STOP || msg->cmd.type == START) // Run start/stop script
		{
			char action[1024];

			// Read in config file:
			configmap myConfig;
			myConfig.readfile("/home/conf/commandlist.xml");
		
			// Form action string:
			sprintf(action,"cmd_%d_%d",(unsigned int)msg->cmd.proc,(unsigned int)msg->cmd.type);

			// Execute action string in config file:
			msg->rsp.ret = system(myConfig[action].c_str())/256;
		
			// Is error?
			if(msg->rsp.ret >= 0xF0)
				msg->rsp.ret = msg->rsp.ret | 0xFFFFFF00;
		}
		else if(msg->cmd.type == SHUTDOWN)
		{
			 system("/home/scripts/rctest.sh; shutdown -P now"); // disable code from starting up again next boot.. and shutdown
		}
		else
		{
			msg->rsp.ret = ERR_INV_CMD_NAME;
		}
	}
	else                        // Process Block
	{
		// Cmd Line or Sem Execute??
		if(msg->cmd.type == STOP || msg->cmd.type == START || msg->cmd.type == STATUS)
		{
			char action[1024];
		
			// Read in config file:
			configmap myConfig;
			myConfig.readfile("/home/conf/commandlist.xml");

			// Check process number:
			if(msg->cmd.proc >= TOT_PROC)
			{
				msg->rsp.ret = ERR_INV_PROC_NAME;
				return;
			}
		
			// Form action string:
			sprintf(action,"cmd_%d_%d",(unsigned int)msg->cmd.proc,(unsigned int)msg->cmd.type);

			// Execute action string in config file:
			msg->rsp.ret = system(myConfig[action].c_str())/256;
		
			// Is error?
			if(msg->rsp.ret >= 0xF0)
				msg->rsp.ret = msg->rsp.ret | 0xFFFFFF00;
		}
		else if(msg->cmd.type == GET || msg->cmd.type == SET)
		{
			int semid;
			const char* path;
			int num_of_sem;
			int ret;
		
			// Get SEMID Vars:
			switch(msg->cmd.proc)
			{
				case ALL:
					msg->rsp.ret = ERR_CMD_DNE;
					return;
				
				case STPRO:
					num_of_sem  = NUM_STPRO;
					path = STPRO_PATH;
					break;
				
				case STCH:
					num_of_sem  = NUM_STCH;
					path = STCH_PATH;
					break;
				
				case HSKPR:
					num_of_sem  = NUM_HSKPR;
					path = HSKPR_PATH;
					break;
				
				case PDOG:
					num_of_sem  = NUM_PDOG;
					path = PDOG_PATH;
					break;
				
				case DCOL:
					num_of_sem  = NUM_DCOL;
					path = DCOL_PATH;
					break;
					
				case STIMG:
					num_of_sem  = NUM_STIMG;
					path = STIMG_PATH;
					break;
				
				case SCHED:
					num_of_sem  = NUM_SCHED;
					path = SCHED_PATH;
					break;
				
				default:
					msg->rsp.ret = ERR_INV_PROC_NAME;
					return;
			}
			
			// Get SEMID:
			if( (semid = initSemaphores(path,num_of_sem)) == -1 )
			{
				msg->rsp.ret = ERR_SEM_INIT;
				return;
			}
		
			// Check sem #:
			if( msg->cmd.arg1 >= num_of_sem )
			{
				msg->rsp.ret = ERR_SEM_DNE;
				return;
			}
			
			if( msg->cmd.type == GET )
			{
				if( (ret = getSemaphore(semid, msg->cmd.arg1)) <= -1 )
				{
					msg->rsp.ret = ERR_SEM_GET;
					return;
				}
				msg->rsp.ret = ret;
			}
			else
			{
				// Send command:
				if( setSemaphore(semid, msg->cmd.arg1, msg->cmd.arg2) <= -1 )
				{
					msg->rsp.ret = ERR_SEM_SET;
					return;
				}
				else
				{
					// Increment command count:
					int cmd_cnt;
					if( ( cmd_cnt = getSemaphore(semid, 0) ) <= -1 )
					{
						msg->rsp.ret = ERR_SEM_GET;
						return;
					}
					if( setSemaphore(semid, 0, cmd_cnt + 1) <= -1 )
					{
						msg->rsp.ret = ERR_SEM_SET;
						return;
					}
					msg->rsp.ret = 1;
				}
			}
		}
		else
		{
			msg->rsp.ret = ERR_INV_CMD_NAME;
			return;
		} 	
	}
}
示例#10
0
void initIPC() {
	if (data == NULL) getSharedMemory(DIRECTORY_NUM, ID_SHMEM_NUMERADOR);
	if (semaphoreId == 0) getSemaphore(DIRECTORY_NUM, ID_SEMCONTROL_NUMERADOR, 1);
}