Beispiel #1
0
////////////////////////////////////////////////////////////
/// Check if the user is on the list
////////////////////////////////////////////////////////////
bool UserContains(struct UserData * User)
{
	if (User != NULL)
	{
		struct UserData * Iterator;

		Iterator = UserList.First;

		MutexLock(&User->MutexData);

		while (Iterator != NULL)
		{
			MutexLock(&Iterator->MutexData);

			if (Iterator == User)
			{
				MutexUnlock(&Iterator->MutexData);
				MutexUnlock(&User->MutexData);
				return true;
			}

			MutexUnlock(&Iterator->MutexData);

			Iterator = Iterator->Next;
		}
	
		MutexUnlock(&User->MutexData);
	}

	return false;
}
Beispiel #2
0
void* mpThread(void* _args)
{
    char *p = NULL;
    char buf[1024];
    char* args = _args ? strdup(_args) : 0;
    int n;
    int offset;
    void* data;
    for (;;)
    {
        if (data = plGetEntry(&playlist))
        {
            n = mpOpen(data, args);
        }
        else
        {
            if (!loopclip) break;
            n = mpOpen(loopclip, 0);
        }
        free(data);
        if (n) break;

        mpState = MP_PLAYING;
        MutexLock(&mpConsoleMutex);
        while (mpCommand("get_time_pos") <= 0) msleep(500);
        do
        {
            offset = 0;
            while (offset < sizeof(buf) - 1)
            {
                n = mpRead(buf + offset, sizeof(buf) - 1 - offset);
                if (n <= 0) break;
                offset += n;
                buf[offset] = 0;
                if (p = strstr(buf, "ANS_TIME_POSITION="))
                {
                    mpPos = atoi(p + 18);
                    break;
                }
            }
            // stop here when paused
            MutexUnlock(&mpConsoleMutex);
            do
            {
                msleep(500);
            }
            while (mpState == MP_PAUSED);
            MutexLock(&mpConsoleMutex);
        }
        while (mpCommand("get_time_pos") > 0);
        MutexUnlock(&mpConsoleMutex);
        ShellTerminate(&mpx);
        ShellClean(&mpx);
    }
    free(args);
    mpState = MP_IDLE;
    mpThreadHandle = 0;
    return 0;
}
Beispiel #3
0
////////////////////////////////////////////////////////////
/// Remove user from the list
////////////////////////////////////////////////////////////
void UserRemove(struct UserData * User)
{
	if (UserContains(User))
	{
		struct UserData * UserPtr;

		// Remove from the list
		MutexLock(&User->MutexData);

		// Update the previous node
		if (User->Previous == NULL)
		{
			MutexLock(&UserList.First->MutexData);
			UserPtr = UserList.First;
			UserList.First = User->Next;
		}
		else
		{
			MutexLock(&User->Previous->Next->MutexData);
			UserPtr = User->Previous->Next;
			User->Previous->Next = User->Next;
		}

		MutexUnlock(&UserPtr->MutexData);

		// Update the next node
		if (User->Next == NULL)
		{
			MutexLock(&UserList.Last->MutexData);
			UserPtr = UserList.Last;
			UserList.Last = User->Previous;
		}
		else
		{
			MutexLock(&User->Next->Previous->MutexData);
			UserPtr = User->Next->Previous;
			User->Next->Previous = User->Previous;
		}

		MutexUnlock(&UserPtr->MutexData);

		// Close the socket
		SocketClose(&User->Connection.Socket);

		// Unlock the mutex
		MutexUnlock(&User->MutexData);

		// Destroy the mutex
		MutexDestroy(&User->MutexData);

		// Delete it
		free(User);

		User = NULL;
	}
}
Beispiel #4
0
void* HeapAllocate(MemAllocHeap* heap, size_t size)
{
  bool thread_safe = 0 != (heap->m_Flags & HeapFlags::kThreadSafe);

  if (thread_safe)
  {
    MutexLock(&heap->m_Lock);
  }

  void* ptr = nullptr;
#if ENABLED(USE_DLMALLOC)
  ptr = mspace_malloc(heap->m_MemSpace, size);
#else
  ptr = malloc(size);
#endif
  if (!ptr)
  {
    Croak("out of memory allocating %d bytes", (int) size);
  }

  if (thread_safe)
  {
    MutexUnlock(&heap->m_Lock);
  }

  return ptr;
}
int MQTTDisconnect(MQTTClient *c)
{
    int rc = FAILURE;
    Timer timer;     // we might wait for incomplete incoming publishes to complete
    int len = 0;

#if defined(MQTT_TASK)
    MutexLock(&c->mutex);
#endif
    TimerInit(&timer);
    TimerCountdownMS(&timer, c->command_timeout_ms);

    len = MQTTSerialize_disconnect(c->buf, c->buf_size);

    if (len > 0) {
        rc = sendPacket(c, len, &timer);    // send the disconnect packet
    }

    MQTTCloseSession(c);

#if defined(MQTT_TASK)
    MutexUnlock(&c->mutex);
#endif
    return rc;
}
void MQTTRun(void *parm)
{
    Timer timer;
    MQTTClient *c = (MQTTClient *)parm;

    TimerInit(&timer);

    while (1) {
        TimerCountdownMS(&timer, CONFIG_MQTT_RECV_CYCLE); /* Don't wait too long if no traffic is incoming */

#if CONFIG_MQTT_RECV_CYCLE == 0     /* The smaller cycle, the greater throughput */
        esp_task_wdt_reset();
#endif

#if defined(MQTT_TASK)
        MutexLock(&c->mutex);
#endif

        int rc = cycle(c, &timer);

        if (rc == FAILURE) {
            ESP_LOGE(TAG, "MQTTRun cycle failed");
#if defined(MQTT_TASK)
            MutexUnlock(&c->mutex);
#endif
            vTaskDelete(NULL);
        }

#if defined(MQTT_TASK)
        MutexUnlock(&c->mutex);
#endif
    }
}
Beispiel #7
0
bool cRemote::PutMacro(eKeys Key)
// returns true it opens a menu
// returns false if it is a command that has no menu
{
  const cKeyMacro *km = KeyMacros.Get(Key);
  if (km) {
     keyMacroPlugin = km->Plugin();
     if(km->Command()) {
         dsyslog("Executing keymacros.conf-command: \"%s\"\n", km->Command());
         SystemExec(km->Command());
         return false;
     } else {
         cMutexLock MutexLock(&mutex);
         for (int i = km->NumKeys(); --i > 0; ) {
            if (!Put(km->Macro()[i], true))
               return true;
            }
         }
     }
  else if (Key == kTT)
      Skins.Message(mtError, tr("Teletext not available!"));
  else if (Key == kPiP)
      Skins.Message(mtError, tr("PiP not available!"));
  return true;
}
Beispiel #8
0
static THREAD_ENTRY _audioThread(void* context) {
	struct mPSP2AudioContext* audio = (struct mPSP2AudioContext*) context;
	uint32_t zeroBuffer[PSP2_SAMPLES] = {0};
	void* buffer = zeroBuffer;
	int audioPort = sceAudioOutOpenPort(SCE_AUDIO_OUT_PORT_TYPE_MAIN, PSP2_SAMPLES, 48000, SCE_AUDIO_OUT_MODE_STEREO);
	while (audio->running) {
		MutexLock(&audio->mutex);
		if (buffer != zeroBuffer) {
			// Can only happen in successive iterations
			audio->samples -= PSP2_SAMPLES;
			ConditionWake(&audio->cond);
		}
		if (audio->samples >= PSP2_SAMPLES) {
			buffer = &audio->buffer[audio->readOffset];
			audio->readOffset += PSP2_SAMPLES;
			if (audio->readOffset >= PSP2_AUDIO_BUFFER_SIZE) {
				audio->readOffset = 0;
			}
			// Don't mark samples as read until the next loop iteration to prevent
			// writing to the buffer while being read (see above)
		} else {
			buffer = zeroBuffer;
		}
		MutexUnlock(&audio->mutex);

		sceAudioOutOutput(audioPort, buffer);
	}
	sceAudioOutReleasePort(audioPort);
	return 0;
}
Beispiel #9
0
  void BuildQueueDestroy(BuildQueue* queue)
  {
    Log(kDebug, "destroying build queue");
    const BuildQueueConfig* config = &queue->m_Config;

    MutexLock(&queue->m_Lock);
    queue->m_QuitSignalled = true;
    MutexUnlock(&queue->m_Lock);

    CondBroadcast(&queue->m_WorkAvailable);

    for (int i = 0, thread_count = config->m_ThreadCount; i < thread_count; ++i)
    {
      if (i > 0)
      {
        Log(kDebug, "joining with build thread %d", i);
        ThreadJoin(queue->m_Threads[i]);
      }

      ThreadStateDestroy(&queue->m_ThreadState[i]);
    }

    // Deallocate storage.
    MemAllocHeap* heap = queue->m_Config.m_Heap;
    HeapFree(heap, queue->m_ExpensiveWaitList);
    HeapFree(heap, queue->m_Queue);

    CondDestroy(&queue->m_WorkAvailable);
    MutexDestroy(&queue->m_Lock);

    // Unblock all signals on the main thread.
    SignalHandlerSetCondition(nullptr);
    SignalBlockThread(false);
  }
Beispiel #10
0
void cDvbTuner::Action(void)
{
  cTimeMs Timer;
  bool LostLock = false;
  fe_status_t Status = (fe_status_t)0;
  while (Running()) {
        fe_status_t NewStatus;
        if (GetFrontendStatus(NewStatus, 10))
           Status = NewStatus;
        cMutexLock MutexLock(&mutex);
        switch (tunerStatus) {
          case tsIdle:
               break;
          case tsSet:
               tunerStatus = SetFrontend() ? tsTuned : tsIdle;
               Timer.Set(tuneTimeout);
               continue;
          case tsTuned:
               if (Timer.TimedOut()) {
                  tunerStatus = tsSet;
                  diseqcCommands = NULL;
                  if (time(NULL) - lastTimeoutReport > 60) { // let's not get too many of these
                     isyslog("frontend %d timed out while tuning to channel %d, tp %d", cardIndex, channel.Number(), channel.Transponder());
                     lastTimeoutReport = time(NULL);
                     }
                  continue;
                  }
          case tsLocked:
               if (Status & FE_REINIT) {
                  tunerStatus = tsSet;
                  diseqcCommands = NULL;
                  isyslog("frontend %d was reinitialized", cardIndex);
                  lastTimeoutReport = 0;
                  continue;
                  }
               else if (Status & FE_HAS_LOCK) {
                  if (LostLock) {
                     isyslog("frontend %d regained lock on channel %d, tp %d", cardIndex, channel.Number(), channel.Transponder());
                     LostLock = false;
                     }
                  tunerStatus = tsLocked;
                  locked.Broadcast();
                  lastTimeoutReport = 0;
                  }
               else if (tunerStatus == tsLocked) {
                  LostLock = true;
                  isyslog("frontend %d lost lock on channel %d, tp %d", cardIndex, channel.Number(), channel.Transponder());
                  tunerStatus = tsTuned;
                  Timer.Set(lockTimeout);
                  lastTimeoutReport = 0;
                  continue;
                  }
          }

        if (ciHandler)
           ciHandler->Process();
        if (tunerStatus != tsTuned)
           newSet.TimedWait(mutex, 1000);
        }
}
Beispiel #11
0
void* HeapReallocate(MemAllocHeap *heap, void *ptr, size_t size)
{
  bool thread_safe = 0 != (heap->m_Flags & HeapFlags::kThreadSafe);

  if (thread_safe)
  {
    MutexLock(&heap->m_Lock);
  }

  void *new_ptr;
#if ENABLED(USE_DLMALLOC)
  new_ptr = mspace_realloc(heap->m_MemSpace, ptr, size);
#else
  new_ptr = realloc(ptr, size);
#endif
  if (!new_ptr && size > 0)
  {
    Croak("out of memory reallocating %d bytes at %p", (int) size, ptr);
  }

  if (thread_safe)
  {
    MutexUnlock(&heap->m_Lock);
  }

  return new_ptr;
}
Beispiel #12
0
bool cRemote::Put(eKeys Key, bool AtFront)
{
  if (Key != kNone) {
     cMutexLock MutexLock(&mutex);
     if (in != out && (keys[out] & k_Repeat) && (Key & k_Release))
        Clear();
     int d = out - in;
     if (d <= 0)
        d = MaxKeys + d;
     if (d - 1 > 0) {
        if (AtFront) {
           if (--out < 0)
              out = MaxKeys - 1;
           keys[out] = Key;
           }
        else {
           keys[in] = Key;
           if (++in >= MaxKeys)
              in = 0;
           }
        keyPressed.Broadcast();
        return true;
        }
     return false;
     }
  return true; // only a real key shall report an overflow!
}
Beispiel #13
0
void UvdState::lock()
{
    if (m_isRealtimeMode)
    {
        MutexLock(&m_lock);
    }
}
Beispiel #14
0
////////////////////////////////////////////////////////////
/// Destroy all users
////////////////////////////////////////////////////////////
bool UsersDestroy()
{
	struct UserData * Iterator = UserList.First;

	// Remove all users
	while (Iterator != NULL)
	{
		struct UserData * TempUser = Iterator;
		
		Iterator = Iterator->Next;

		// Close the socket
		MutexLock(&TempUser->MutexData);
		
		SocketClose(&TempUser->Connection.Socket);

		MutexUnlock(&TempUser->MutexData);

		// Destroy the mutex
		MutexDestroy(&TempUser->MutexData);

		// Free it
		free(TempUser);

		TempUser = NULL;
	}

	return true;
}
Beispiel #15
0
/**
 *	Call this function when a writer task needs access to the data
 *	protected by the given rwlock.
 */
UInt32
rwl_writer_start( TRWLock *rwlock )
{
	/*
         *	First we check if destroy has not been called.
         *	In a propper design this test should never fail.
         */
	if ( rwlock->delflag ) return 0;

	rwlock->wcount++;

        /*
	 *	We will wait for all readers to finish.
         *	A writer must have exclusive access.
         */
        while ( rwlock->rcount )
        {
                ThreadDelay( 10 );
        }

        /*
         *	Lock mutex for exclusive access and set the writer in progress
         *	flag.
         */
        rwlock->wflag = 1;
        MutexLock( rwlock->datalock );

        /*
         *	Start writing
         */
        return 1;
}
Beispiel #16
0
void cControl::Shutdown(void)
{
  cMutexLock MutexLock(&mutex);
  cControl *c = control; // avoids recursions
  control = NULL;
  delete c;
}
Beispiel #17
0
void cSatipSectionStatistics::AddSectionStatistic(long bytesP, long callsP)
{
  debug16("%s (%ld, %ld)", __PRETTY_FUNCTION__, bytesP, callsP);
  cMutexLock MutexLock(&mutexM);
  filteredDataM += bytesP;
  numberOfCallsM += callsP;
}
Beispiel #18
0
	bool WorkerThread::Pop( TaskBase *& outTask )
	{
		// lock this thread's task vector
		//boost::mutex::scoped_lock this_lock( TaskMutex_ );
		MutexLock(TaskMutex_);

		// if the task vector is empty, return false
		if( Tasks_.empty() )
		{
			MutexUnlock(TaskMutex_);
			return false;
		}

		// get a pointer to the last task in the vector
		TaskBase * t = Tasks_.back();

		// if the task can slice itself into a new task (e.g. several iterations of a loop)
		if( t->Slice( outTask ) )
		{
			// mark the task's completion flag as busy
			t->Completion_->Set( true );

			MutexUnlock(TaskMutex_);
			return true; // return the love
		}

		// assign the outTask to the last task
		outTask = t;

		// pop the task out of the vector
		Tasks_.pop_back();

		MutexUnlock(TaskMutex_);
		return true;// return the love
	}
Beispiel #19
0
void GBASyncLockAudio(struct GBASync* sync) {
	if (!sync) {
		return;
	}

	MutexLock(&sync->audioBufferMutex);
}
MUTEX * avtExecutionManager::FindMutex( const MUTEX_ID id )
{
    if (tPool == NULL)
        return NULL;

    std::map<MUTEX_ID, MUTEX *>::iterator it;
    MUTEX *lock;

    MutexLock( &mutexMapLock );

    it = mutexMap.find( id );
    if( it == mutexMap.end() )
    {
        // Not found, create it.
        lock = new MUTEX;
        MutexInit( lock );

        mutexMap.insert( std::pair<MUTEX_ID, MUTEX *>(id, lock) );
    }
    else
    {
        lock = it->second;
    }

    MutexUnlock( &mutexMapLock );

    return( lock );
}
void avtExecutionManager::MutexLock( const MUTEX_ID stringID )
{
    if (tPool == NULL)
        return;

    MutexLock( FindMutex(stringID) );
}
Beispiel #22
0
void cControl::Launch(cControl *Control)
{
  cMutexLock MutexLock(&mutex);
  cControl *c = control; // keeps control from pointing to uninitialized memory
  control = Control;
  delete c;
}
Beispiel #23
0
bool GBASIOLockstepNodeUnload(struct GBASIODriver* driver) {
    struct GBASIOLockstepNode* node = (struct GBASIOLockstepNode*) driver;
    MutexLock(&node->p->mutex);
    --node->p->loaded;
    ConditionWake(&node->p->barrier);
    MutexUnlock(&node->p->mutex);
    return true;
}
Beispiel #24
0
void Client::crash() {

	MutexLock lock = MutexLock( &mutex );

	printf( "Client %d: crash\n", id );

	crashed = true;
}
Beispiel #25
0
int __ww_mutex_lock(struct ww_mutex *lock, struct ww_acquire_ctx *ctx)
{
    MutexLock(&lock->base);
    ww_mutex_lock_acquired(lock, ctx);
    lock->ctx = ctx;

    return 0;
}
Beispiel #26
0
void Client::report( int vms ) {

	printf( "Client %d: report[%d]\n", id, vms );

	MutexLock lock = MutexLock( &mutex );

	allocator->report( vms );
}
Beispiel #27
0
void KLogToScreenDisable(void)
{
	MutexLock (&dbg_mutex);

	klog_to_screen = FALSE;

	MutexUnlock (&dbg_mutex);
}
Beispiel #28
0
void cSatipBufferStatistics::AddBufferStatistic(long bytesP, long usedP)
{
  debug16("%s (%ld, %ld)", __PRETTY_FUNCTION__, bytesP, usedP);
  cMutexLock MutexLock(&mutexM);
  dataBytesM += bytesP;
  if (usedP > usedSpaceM)
     usedSpaceM = usedP;
}
Beispiel #29
0
void cRemote::Clear(void)
{
  cMutexLock MutexLock(&mutex);
  in = out = 0;
  if (learning) {
     free(unknownCode);
     unknownCode = NULL;
     }
}
Beispiel #30
0
static void _changeVideoSync(struct GBASync* sync, bool frameOn) {
	// Make sure the video thread can process events while the GBA thread is paused
	MutexLock(&sync->videoFrameMutex);
	if (frameOn != sync->videoFrameOn) {
		sync->videoFrameOn = frameOn;
		ConditionWake(&sync->videoFrameAvailableCond);
	}
	MutexUnlock(&sync->videoFrameMutex);
}