Esempio n. 1
0
static int test_mutex_timed_thread_func(void* arg)
{
  int ret;
  struct timespec ts;
  struct TestMutexTimedData* data = (struct TestMutexTimedData*) arg;

  ret = mtx_timedlock(&(data->mutex), &(data->timeout));
  assert (ret == thrd_timedout);

  timespec_get(&ts, TIME_UTC);
  ret = timespec_compare(&ts, &(data->start));
  assert (ret >= 0);
  ret = timespec_compare(&ts, &(data->timeout));
  assert (ret >= 0);
  ret = timespec_compare(&ts, &(data->end));
  assert (ret < 0);

  ret = mtx_lock(&(data->mutex));
  assert (ret == thrd_success);

  timespec_get(&ts, TIME_UTC);
  ret = timespec_compare(&ts, &(data->end));
  assert (ret >= 0);

  ret = timespec_compare(&ts, &(data->upper));
  assert (ret < 0);

  mtx_unlock(&(data->mutex));

  return 0;
}
Esempio n. 2
0
main()
{
    thrd_t thrd1;
    printf("%d\n", mtx_init(&mtx1, mtx_plain));
    thrd_create(&thrd1, thread1, 0);
    thrd_join(thrd1, NULL);
    mtx_destroy(&mtx1);

    printf("\n%d\n", mtx_init(&mtx1, mtx_plain | mtx_recursive));
    thrd_create(&thrd1, thread1, 0);
    thrd_join(thrd1, NULL);
    mtx_destroy(&mtx1);

    mtx_init(&mtx1, mtx_try);
    thrd_create(&thrd1, thread3, 0); 
    Sleep(500);
    printf("%d\n", mtx_trylock(&mtx1));
    xtime xt;
    xtime_get(&xt, TIME_UTC);
    xt.nsec += 200000000;
    printf("%d\n", mtx_timedlock(&mtx1, &xt));
    xt.nsec += 500000000;
    printf("%d\n", mtx_timedlock(&mtx1, &xt));
    mtx_unlock(&mtx1);
    printf("%d\n", mtx_trylock(&mtx1));
    mtx_unlock(&mtx1);
    
    thrd_create(&thrd1, thread4, 0);
    thrd_detach(thrd1);
    thrd_create(&thrd1, thread5, 0);
    thrd_join(thrd1, NULL);
        
    thrd_create(&thrd1, thread2, 0);
    thrd_detach(thrd1);
    thrd_create(&thrd1, thread2, (void *)1);
    thrd_detach(thrd1);
    Sleep(4000);

    thrd_create(&thrd1, thread2, (void *)2);
    thrd_detach(thrd1);
    thrd_create(&thrd1, thread2, (void *)3);
    thrd_detach(thrd1);
    Sleep(4000);
    
    mtx_destroy(&mtx1);

}
int func(void * thrd)
{
    struct timespec ts;
    timespec_get( &ts, TIME_UTC);        // The current time;
    ts.tv_sec += 3;                      // 3 seconds from now.

    printf("%s waiting ...\n", (char*)thrd);
    int res = mtx_timedlock(&mtx, &ts);
    switch(res)
    {
        case thrd_success:
           puts("Obtained mutex\n... releasing ...");
           mtx_unlock(&mtx);   break;
        case thrd_timedout:
           puts("Timed out.");   break;
        default:
           puts("mtx_timedlock: error.");
    };
    return res;
}
Esempio n. 4
0
int     _RTL_FUNC mtx_trylock(mtx_t *mtx)
{
    return mtx_timedlock(mtx, 0);
}
Esempio n. 5
0
int     _RTL_FUNC mtx_lock(mtx_t *mtx)
{
    return mtx_timedlock(mtx, (void *)-1);
}