void mssleep(long ms)
{
  struct timespec ts;
  ts.tv_sec=ms/1000;
  ts.tv_nsec=(ms % 1000)*1000000;
  thrd_sleep(&ts, NULL);
}
Beispiel #2
0
static void test_mutex_timed(void)
{
  struct TestMutexTimedData data;
  thrd_t thread;
  struct timespec interval = { 0, };
  struct timespec start;
  struct timespec end;

  interval.tv_sec = 0;
  interval.tv_nsec = (NSECS_PER_SECOND / 10) * 2;

  mtx_init(&(data.mutex), mtx_timed);
  mtx_lock(&(data.mutex));

  timespec_get(&(data.start), TIME_UTC);
  data.timeout = data.start;
  timespec_add_nsec(&(data.timeout), NSECS_PER_SECOND / 10);
  data.end = data.timeout;
  timespec_add_nsec(&(data.end), NSECS_PER_SECOND / 10);
  data.upper = data.end;
  timespec_add_nsec(&(data.upper), NSECS_PER_SECOND / 10);

  thrd_create(&thread, test_mutex_timed_thread_func, &data);

  timespec_get(&start, TIME_UTC);
  assert (thrd_sleep(&interval, &interval) == 0);
  timespec_get(&end, TIME_UTC);
  mtx_unlock(&(data.mutex));

  thrd_join(thread, NULL);
}
Beispiel #3
0
static void thread_sleep(unsigned long _sec, unsigned long _nanosec)
{
    struct xtime xt = {0, 0};
    xtime_get(&xt, TIME_UTC);
    xt.sec += _sec;
    xt.nsec += _nanosec;
    thrd_sleep(&xt);
}
Beispiel #4
0
Datei: main.c Projekt: wuwx/simba
int test_sleep(struct harness_t *harness_p)
{
    BTASSERT(thrd_sleep(0.001) == 0);
    BTASSERT(thrd_sleep_ms(1) == 0);
    BTASSERT(thrd_sleep_us(1000) == 0);

    return (0);
}
Beispiel #5
0
void tfunc(void *arg)
{
	int num = (long)arg;
	xtime dur;

	printf("hello from thread %d\n", num);

	dur.sec = 4;
	dur.nsec = 0;
	thrd_sleep(&dur);

	printf("thread %d done\n", num);
}
Beispiel #6
0
void writer(void)  // Writes positive values; ends with a negative value.
{
    const int N = 20;                  // Number of data values to write.
    for( int n = 0; n <= N; ++n)
    {
        int d = n < N ? 10+n : -1;     // Prepare data or end marker.
        // When no readers are busy, lock the semaphore (count = -1):
        while( atomic_fetch_sub(&count,MAX_READERS+1) != MAX_READERS)
            atomic_fetch_add(&count, MAX_READERS+1);

        printf("Writer is writing %d\n", d),     // Critical section.
        data = d;
        atomic_fetch_add(&count, MAX_READERS+1); // Release the
                                                 // semaphores.
        thrd_sleep(&ms,NULL);          // Simulate data production.
    }
}
Beispiel #7
0
void reader(int* idPtr)
{
    int id = *idPtr;
    while(1)
    {
        // Check semaphore; decrement and read if count > 0.
        while( atomic_fetch_sub(&count, 1) <= 0)
        {  atomic_fetch_add(&count, 1); thrd_yield(); }

        if( data > 0)                  // Read valid data.
            printf("Reader %d is reading %d\n", id, data);
        if( data < 0)                  // End marker: stop looping.
            break;

        atomic_fetch_add(&count, 1);   // Release our reader slot.
        thrd_sleep(&ms,NULL);          // Simulate data processing.
    }
}
Beispiel #8
0
static void test_sleep(void)
{
  struct timespec ts;
  struct timespec interval;
  struct timespec end_ts;

  interval.tv_sec = 0;
  interval.tv_nsec = NSECS_PER_SECOND / 10;

  /* Calculate current time + 100ms */
  timespec_get(&ts, TIME_UTC);
  timespec_add_nsec(&ts, NSECS_PER_SECOND / 10);

  /* Sleep... */
  thrd_sleep(&interval, NULL);

  timespec_get(&end_ts, TIME_UTC);

  assert(timespec_compare(&ts, &end_ts) <= 0);
}
Beispiel #9
0
int main()
{
    int res, attempt;
    char remote_host_ip[] = STRINGIFY(REMOTE_HOST_IP);
    struct inet_ip_addr_t remote_host_ip_address;
    struct time_t round_trip_time, timeout;

    sys_start();

    if (inet_aton(remote_host_ip, &remote_host_ip_address) != 0) {
        std_printf(FSTR("Bad ip address '%s'.\r\n"), remote_host_ip);
        return (-1);
    }

    timeout.seconds = 3;
    timeout.nanoseconds = 0;
    attempt = 1;

    /* Ping the remote host once every second. */
    while (1) {
        res = ping_host_by_ip_address(&remote_host_ip_address,
                                      &timeout,
                                      &round_trip_time);

        if (res == 0) {
            std_printf(FSTR("Successfully pinged '%s' (#%d).\r\n"),
                       remote_host_ip,
                       attempt);
        } else {
            std_printf(FSTR("Failed to ping '%s' (#%d).\r\n"),
                       remote_host_ip,
                       attempt);
        }

        attempt++;
        thrd_sleep(1);
    }

    return (0);
}
Beispiel #10
0
Datei: main.c Projekt: wuwx/simba
static int test_ramp(struct harness_t *harness_p)
{
    int i;
    struct dac_driver_t dac;

    BTASSERT(dac_init(&dac,
                      &dac_0_dev,
                      &pin_0_dev,
                      &pin_1_dev,
                      1) == 0);

    for (i = 0; i < AMPLITUDE_MAX + 1; i++) {
        samples[0] = i;
        samples[1] = (AMPLITUDE_MAX - i);
        std_printf(FSTR("samples[0]: %5d, samples[1]: %5d\r\n"),
                   samples[0],
                   samples[1]);
        BTASSERT(dac_convert(&dac, samples, 2) == 0);
        thrd_sleep(10.0f / (AMPLITUDE_MAX + 1));
    }

    return (0);
}
Beispiel #11
0
int main()
{
    sys_start();

    /* Configure and start the the WiFi module as a Station and
       SoftAP. */
    esp_wifi_set_op_mode(esp_wifi_op_mode_station_softap_t);

    if (esp_wifi_softap_init("Simba", NULL) != 0) {
        std_printf(FSTR("Failed to configure the Soft AP.\r\n"));
    }

    if (esp_wifi_station_init("Qvist2", "maxierik", NULL) != 0) {
        std_printf(FSTR("Failed to configure the Station.\r\n"));
    }

    while (1) {
        esp_wifi_print(sys_get_stdout());
        thrd_sleep(2);
    }

    return (0);
}
Beispiel #12
0
/* This is the main program (i.e. the main thread) */
int main(void)
{
  /* Initialization... */
  mtx_init(&gMutex, mtx_plain);
  cnd_init(&gCond);

  /* Test 1: thread arguments & return values */
  printf("PART I: Thread arguments & return values\n");
  {
    thrd_t t1, t2, t3, t4;
    int id1, id2, id3, id4, ret1, ret2, ret3, ret4;

    /* Start a bunch of child threads */
    id1 = 1;
    thrd_create(&t1, ThreadIDs, (void*)&id1);
    thrd_join(t1, &ret1);
    printf(" Thread 1 returned %d.\n", ret1);
    id2 = 2;
    thrd_create(&t2, ThreadIDs, (void*)&id2);
    thrd_join(t2, &ret2);
    printf(" Thread 2 returned %d.\n", ret2);
    id3 = 3;
    thrd_create(&t3, ThreadIDs, (void*)&id3);
    thrd_join(t3, &ret3);
    printf(" Thread 3 returned %d.\n", ret3);
    id4 = 4;
    thrd_create(&t4, ThreadIDs, (void*)&id4);
    thrd_join(t4, &ret4);
    printf(" Thread 4 returned %d.\n", ret4);
  }

  /* Test 2: compile time thread local storage */
  printf("PART II: Compile time thread local storage\n");
#ifndef NO_CT_TLS
  {
    thrd_t t1;

    /* Clear the TLS variable (it should keep this value after all threads are
       finished). */
    gLocalVar = 1;
    printf(" Main gLocalVar is %d.\n", gLocalVar);

    /* Start a child thread that modifies gLocalVar */
    thrd_create(&t1, ThreadTLS, NULL);
    thrd_join(t1, NULL);

    /* Check if the TLS variable has changed */
    if(gLocalVar == 1)
      printf(" Main gLocalID was not changed by the child thread - OK!\n");
    else
      printf(" Main gLocalID was changed by the child thread - FAIL!\n");
  }
#else
  printf(" Compile time TLS is not supported on this platform...\n");
#endif

  /* Test 3: mutex locking */
  printf("PART III: Mutex locking (100 threads x 10000 iterations)\n");
  {
    thrd_t t[100];
    int i;

    /* Clear the global counter. */
    gCount = 0;

    /* Start a bunch of child threads */
    for (i = 0; i < 100; ++ i)
    {
      thrd_create(&t[i], ThreadLock, NULL);
    }

    /* Wait for the threads to finish */
    for (i = 0; i < 100; ++ i)
    {
      thrd_join(t[i], NULL);
    }

    /* Check the global count */
    printf(" gCount = %d\n", gCount);
  }

  /* Test 4: condition variable */
  printf("PART IV: Condition variable (40 + 1 threads)\n");
  {
    thrd_t t1, t[40];
    int i;

    /* Set the global counter to the number of threads to run. */
    gCount = 40;

    /* Start the waiting thread (it will wait for gCount to reach zero). */
    thrd_create(&t1, ThreadCondition2, NULL);

    /* Start a bunch of child threads (these will decrease gCount by 1 when they
       finish) */
    for (i = 0; i < 40; ++ i)
    {
      thrd_create(&t[i], ThreadCondition1, NULL);
    }

    /* Wait for the waiting thread to finish */
    thrd_join(t1, NULL);

    /* Wait for the other threads to finish */
    for (i = 0; i < 40; ++ i)
    {
      thrd_join(t[i], NULL);
    }
  }

  /* Test 5: yield */
  printf("PART V: Yield (40 + 1 threads)\n");
  {
    thrd_t t[40];
    int i;

    /* Start a bunch of child threads */
    for (i = 0; i < 40; ++ i)
    {
      thrd_create(&t[i], ThreadYield, NULL);
    }

    /* Yield... */
    thrd_yield();

    /* Wait for the threads to finish */
    for (i = 0; i < 40; ++ i)
    {
      thrd_join(t[i], NULL);
    }
  }

  /* Test 6: Sleep */
  printf("PART VI: Sleep (10 x 100 ms)\n");
  {
    int i;
    struct timespec ts;

    printf(" Sleeping");
    fflush(stdout);
    for (i = 0; i < 10; ++ i)
    {
      /* Calculate current time + 100ms */
      clock_gettime(TIME_UTC, &ts);
      ts.tv_nsec += 100000000;
      if (ts.tv_nsec >= 1000000000)
      {
        ts.tv_sec++;
        ts.tv_nsec -= 1000000000;
      }

      /* Sleep... */
      thrd_sleep(&ts, NULL);

      printf(".");
      fflush(stdout);
    }
    printf("\n");
  }

  /* Test 7: Time */
  printf("PART VII: Current time (UTC), three times\n");
  {
    struct timespec ts;
    clock_gettime(TIME_UTC, &ts);
    printf(" Time = %ld.%09ld\n", (long)ts.tv_sec, ts.tv_nsec);
    clock_gettime(TIME_UTC, &ts);
    printf(" Time = %ld.%09ld\n", (long)ts.tv_sec, ts.tv_nsec);
    clock_gettime(TIME_UTC, &ts);
    printf(" Time = %ld.%09ld\n", (long)ts.tv_sec, ts.tv_nsec);
  }

  /* FIXME: Implement some more tests for the TinyCThread API... */

  /* Finalization... */
  mtx_destroy(&gMutex);
  cnd_destroy(&gCond);

  return 0;
}
Beispiel #13
0
static int outputHandlerThread(void *stateArg) {
	if (stateArg == NULL) {
		return (thrd_error);
	}
	flowOutputState state = stateArg;
	switch (state->mode) {
		case OF_OUT_UART:
			thrd_set_name(SUBSYSTEM_UART);
			break;
		case OF_OUT_FILE:
			thrd_set_name(SUBSYSTEM_FILE);
			break;
		case OF_OUT_BOTH:
			thrd_set_name("FLOW_OUTPUT");
			break;
		case OF_OUT_NONE:
			break;
	}

	struct timespec sleepTime = { .tv_sec = 0, .tv_nsec = 500000 };

	// Wait until the buffer is initialized
	while (!atomic_load_explicit(&state->running, memory_order_relaxed)) {
		thrd_sleep(&sleepTime, NULL);
	}

	// Main thread loop
	while (atomic_load_explicit(&state->running, memory_order_relaxed)) {
		FlowEventPacket packet = getPacketFromTransferBuffer(state->buffer);
		if (packet == NULL) { // no data: sleep for a while
			thrd_sleep(&sleepTime, NULL);
		}
		else {
			if (state->mode == OF_OUT_UART || state->mode == OF_OUT_BOTH) {
				if (!sendFlowEventPacketUart(packet)) {
					caerLog(CAER_LOG_ALERT, SUBSYSTEM_UART, "A flow packet was not sent.");
				}
			}
			if (state->mode == OF_OUT_FILE || state->mode == OF_OUT_BOTH) {
				if (!writeFlowEventPacketFile(state,packet)) {
					caerLog(CAER_LOG_ALERT, SUBSYSTEM_FILE, "A flow packet was not written.");
				}
			}
			flowEventPacketFree(packet);
		}
	}

	// If shutdown: empty buffer before closing thread
	FlowEventPacket packet;
	while ((packet = getPacketFromTransferBuffer(state->buffer)) != NULL) {
		if (state->mode == OF_OUT_UART || state->mode == OF_OUT_BOTH) {
			if (!sendFlowEventPacketUart(packet)) {
				caerLog(CAER_LOG_ALERT, SUBSYSTEM_UART, "A flow packet was not sent.");
			}
		}
		if (state->mode == OF_OUT_FILE || state->mode == OF_OUT_BOTH) {
			if (!writeFlowEventPacketFile(state,packet)) {
				caerLog(CAER_LOG_ALERT, SUBSYSTEM_FILE, "A flow packet was not written.");
			}
		}
		flowEventPacketFree(packet);
	}

	return (thrd_success);
}