コード例 #1
0
ファイル: SDL_syscond.c プロジェクト: yeKcim/warmux
/* Wait on the condition variable for at most 'ms' milliseconds.
   The mutex must be locked before entering this function!
   The mutex is unlocked during the wait, and locked again after the wait.

Typical use:

Thread A:
 SDL_LockMutex(lock);
 while ( ! condition ) {
   SDL_CondWait(cond);
 }
 SDL_UnlockMutex(lock);

Thread B:
 SDL_LockMutex(lock);
 ...
 condition = true;
 ...
 SDL_UnlockMutex(lock);
 */
int SDL_CondWaitTimeout(SDL_cond *cond, SDL_mutex *mutex, Uint32 ms)
{
 int retval;
 pth_event_t ev;
 int sec;

 if ( ! cond ) {
   SDL_SetError("Passed a NULL condition variable");
   return -1;
 }

 retval = 0;

 sec = ms/1000;
 ev = pth_event(PTH_EVENT_TIME, pth_timeout(sec,(ms-sec*1000)*1000));

 if ( pth_cond_await(&(cond->condpth_p), &(mutex->mutexpth_p), ev) != 0 ) {
   SDL_SetError("pth_cond_await() failed");
   retval = -1;
 }

    pth_event_free(ev, PTH_FREE_ALL);

 return retval;
}
コード例 #2
0
ファイル: b5-pth.c プロジェクト: alepharchives/cpc
int
main()
{
    pth_t thread;
    int rc;
    int i, j, s;

    arg = -1;
    res = 0;
    pth_init();
    thread = pth_spawn(PTH_ATTR_DEFAULT, thread_routine, NULL);
    
    for(i = 0; i < 100; i++) {
        s = 0;
        for(j = 0; j < 10000; j++) {
            pth_mutex_acquire(&mutex, FALSE, NULL);
            res = -1;
            arg = j;
            pth_mutex_release(&mutex);
            pth_cond_notify(&c1, FALSE);
            pth_mutex_acquire(&mutex, FALSE, NULL);
            if(res < 0)
                pth_cond_await(&c2, &mutex, NULL);
            s += res;
            arg = -1;
            pth_mutex_release(&mutex);
        }
    }
    printf("%d\n", s);
    return 0;
}
コード例 #3
0
ファイル: test_philo.c プロジェクト: Distrotech/pth
static void pickup(unsigned int k)
{
    pth_mutex_acquire(&(tab->mutex), FALSE, NULL);
    (tab->status)[k] = hungry;
    printstate();
    if (!test(k))
        pth_cond_await(&((tab->condition)[k]), &(tab->mutex), NULL);
    printstate();
    pth_mutex_release(&(tab->mutex));
    return;
}
コード例 #4
0
ファイル: b5-pth.c プロジェクト: alepharchives/cpc
void *
thread_routine(void *dummy)
{
    while(1) {
        pth_mutex_acquire(&mutex, FALSE, NULL);
        if(arg < 0)
            pth_cond_await(&c1, &mutex, NULL);
        res = arg;
        arg = -1;
        pth_mutex_release(&mutex);
        pth_cond_notify(&c2, FALSE);
    }
}
コード例 #5
0
ファイル: SDL_syscond.c プロジェクト: yeKcim/warmux
/* Wait on the condition variable forever */
int SDL_CondWait(SDL_cond *cond, SDL_mutex *mutex)
{
 int retval;

 if ( ! cond ) {
   SDL_SetError("Passed a NULL condition variable");
   return -1;
 }

 retval = 0;
 if ( pth_cond_await(&(cond->condpth_p), &(mutex->mutexpth_p), NULL) != 0 ) {
   SDL_SetError("pth_cond_await() failed");
   retval = -1;
 }
 return retval;
}
コード例 #6
0
ファイル: groupcache.cpp プロジェクト: Yazracor/knxd-snapshot
Array < eibaddr_t > GroupCache::LastUpdates (uint16_t start, uint8_t Timeout,
					     uint16_t & end, pth_event_t stop)
{
  Array < eibaddr_t > a;
  pth_event_t timeout = pth_event (PTH_EVENT_RTIME, pth_time (Timeout, 0));

  do
    {
      if (pos < 0x100)
	{
	  if (pos < start && start < ((pos - 0x100) & 0xffff))
	    start = (pos - 0x100) & 0xffff;
	}
      else
	{
	  if (start < ((pos - 0x100) & 0xffff) || start > pos)
	    start = (pos - 0x100) & 0xffff;
	}
      TRACEPRINTF (t, 8, this, "LastUpdates start: %d pos: %d", start, pos);
      while (start != pos && !updates[start & 0xff])
	start++;
      if (start != pos)
	{
	  while (start != pos)
	    {
	      if (updates[start & 0xff])
		a.add (updates[start & 0xff]);
	      start++;
	    }
	  end = pos;
	  pth_event_free (timeout, PTH_FREE_THIS);
	  return a;
	}
      if (pth_event_status (timeout) == PTH_STATUS_OCCURRED)
	{
	  end = pos;
	  pth_event_free (timeout, PTH_FREE_THIS);
	  return a;
	}
      pth_event_concat (timeout, stop, NULL);
      pth_mutex_acquire (&mutex, 0, 0);
      pth_cond_await (&cond, &mutex, timeout);
      pth_mutex_release (&mutex);
      pth_event_isolate (timeout);
    }
  while (1);
}
コード例 #7
0
ファイル: cond.c プロジェクト: Distrotech/libprelude
int
glthread_cond_timedwait_multithreaded (gl_cond_t *cond,
                                       gl_lock_t *lock,
                                       struct timespec *abstime)
{
  int ret, status;
  pth_event_t ev;

  ev = pth_event (PTH_EVENT_TIME, pth_time (abstime->tv_sec, abstime->tv_nsec / 1000));
  ret = pth_cond_await (cond, lock, ev);

  status = pth_event_status (ev);
  pth_event_free (ev, PTH_FREE_THIS);

  if (status == PTH_STATUS_OCCURRED)
    return ETIMEDOUT;

  return ret;
}
コード例 #8
0
ファイル: thr_pth.c プロジェクト: cptaffe/openldap
int 
ldap_pvt_thread_cond_wait( ldap_pvt_thread_cond_t *cond, 
	ldap_pvt_thread_mutex_t *mutex )
{
	return( pth_cond_await( cond, mutex, NULL ) ? 0 : errno );
}
コード例 #9
0
ファイル: groupcache.cpp プロジェクト: Yazracor/knxd-snapshot
GroupCacheEntry
  GroupCache::Read (eibaddr_t addr, unsigned Timeout, uint16_t age)
{
  TRACEPRINTF (t, 4, this, "GroupCacheRead %s %d %d",
	       FormatGroupAddr (addr)(), Timeout, age);
  bool rm = false;
  GroupCacheEntry *c;
  if (!enable)
    {
      GroupCacheEntry f;
      f.src = 0;
      f.dst = 0;
      TRACEPRINTF (t, 4, this, "GroupCache not enabled");
      return f;
    }

  c = find (addr);
  if (c && age && c->recvtime + age < time (0))
    rm = true;

  if (c && !rm)
    {
      TRACEPRINTF (t, 4, this, "GroupCache found: %s",
		   FormatEIBAddr (c->src)());
      return *c;
    }

  if (!Timeout)
    {
      GroupCacheEntry f;
      f.src = 0;
      f.dst = addr;
      TRACEPRINTF (t, 4, this, "GroupCache no entry");
      return f;
    }

  A_GroupValue_Read_PDU apdu;
  T_DATA_XXX_REQ_PDU tpdu;
  L_Data_PDU *l;
  pth_event_t timeout = pth_event (PTH_EVENT_RTIME, pth_time (Timeout, 0));

  tpdu.data = apdu.ToPacket ();
  l = new L_Data_PDU (FakeL2);
  l->data = tpdu.ToPacket ();
  l->source = 0;
  l->dest = addr;
  l->AddrType = GroupAddress;
  layer3->send_L_Data (l);

  do
    {
      c = find (addr);
      rm = false;
      if (c && age && c->recvtime + age < time (0))
	rm = true;

      if (c && !rm)
	{
	  TRACEPRINTF (t, 4, this, "GroupCache found: %s",
		       FormatEIBAddr (c->src)());
	  pth_event_free (timeout, PTH_FREE_THIS);
	  return *c;
	}

      if (pth_event_status (timeout) == PTH_STATUS_OCCURRED && c)
	{
	  GroupCacheEntry gc;
	  gc.src = 0;
	  gc.dst = addr;
	  TRACEPRINTF (t, 4, this, "GroupCache reread timeout");
	  pth_event_free (timeout, PTH_FREE_THIS);
	  return gc;
	}

      if (pth_event_status (timeout) == PTH_STATUS_OCCURRED)
	{
	  c = new GroupCacheEntry;
	  c->src = 0;
	  c->dst = addr;
	  c->recvtime = time (0);
	  add (c);
	  TRACEPRINTF (t, 4, this, "GroupCache timeout");
	  pth_event_free (timeout, PTH_FREE_THIS);
	  return *c;
	}

      pth_mutex_acquire (&mutex, 0, 0);
      pth_cond_await (&cond, &mutex, timeout);
      pth_mutex_release (&mutex);
    }
  while (1);
}
コード例 #10
0
ファイル: main.cpp プロジェクト: dizel3d/disser
int main(int argc, char *argv[])
{
	if (argc < 5) {
		return 1;
	}

	auto processNum = 4;
	auto threadNum = atoi(argv[4]);
	auto dtime = atol(argv[3]) * 1000;
	auto isPth = std::string(argv[2]) == "pth";
	auto taskNum = atoi(argv[1]);
	long cycles = 0;

	switch(taskNum) {
	case 1: task = task1; break;
	case 2: task = task2; break;
	case 3: task = task3; break;
	}

	time_start();

	for (auto i = 0; i < processNum; ++i) {
		if (fork() != 0) {
			continue;
		}

		if (isPth) {
			pth_init();

			pth_attr_t attr = pth_attr_new();
			pth_attr_set(attr, PTH_ATTR_NAME, "task");
			pth_attr_set(attr, PTH_ATTR_STACK_SIZE, 64*1024);
			pth_attr_set(attr, PTH_ATTR_JOINABLE, true);

			pth_mutex_t mutex;
			pth_mutex_init(&mutex);
			pth_cond_init(&pthCond);

			while (time_stop() < dtime) {
				for (auto i = workingNum; i < threadNum; ++i) {
					++workingNum;
					pth_spawn(attr, task, &isPth);
				}

				int rc;
				if ((rc = pth_mutex_acquire(&mutex, FALSE, NULL)) != 0) {
					std::cout << "pthread_mutex_lock " << rc << " " << strerror(rc) << std::endl;
					return 3;
				}
				if (workingNum == threadNum) {
					if ((rc = pth_cond_await(&pthCond, &mutex, NULL)) != 0) {
						std::cout << "pthread_cond_wait " << rc << " " << strerror(rc) << std::endl;
						return 3;
					}
				}
				if ((rc = pth_mutex_release(&mutex)) != 0) {
					std::cout << "pthread_mutex_unlock " << rc << " " << strerror(rc) << std::endl;
					return 3;
				}
				cycles += threadNum - workingNum;
			}
		}
		else {
			pthread_attr_t attr;
			pthread_attr_setstacksize(&attr, 64*1024);

			pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
			pthread_mutexattr_t mutexattr;
			pthread_mutex_init(&mutex, &mutexattr);
			pthread_condattr_t condattr;
			pthread_cond_init(&cond, &condattr);

			pthread_t pid;
			while (time_stop() < dtime) {
				for (auto i = workingNum; i < threadNum; ++i) {
					++workingNum;
					if (pthread_create(&pid, NULL, task, &isPth) == -1) {
						return 2;
					}
				}

				int rc;
				if ((rc = pthread_mutex_lock(&mutex)) != 0) {
					std::cout << "pthread_mutex_lock " << rc << " " << strerror(rc) << std::endl;
					return 3;
				}
				if (workingNum == threadNum) {
					if ((rc = pthread_cond_wait(&cond, &mutex)) != 0) {
						std::cout << "pthread_cond_wait " << rc << " " << strerror(rc) << std::endl;
						return 3;
					}
				}
				if ((rc = pthread_mutex_unlock(&mutex)) != 0) {
					std::cout << "pthread_mutex_unlock " << rc << " " << strerror(rc) << std::endl;
					return 3;
				}
				//pthread_join(pids.front(), NULL);
				//pids.pop_front();
				cycles += threadNum - workingNum;
			}
		}

		std::cout << cycles << std::endl;

		return 0;
	}
	for (auto i = 0; i < processNum; ++i) {
		wait(NULL);
	}

	return 0;
}
コード例 #11
0
ファイル: FrSimThread.cpp プロジェクト: cainex/frsimulation
void FrSimThreadCondVar::wait()
{
	pth_cond_await(&cond, &mutex, NULL);
}