Exemple #1
0
static inline bool
s_check_get_thread_id(lagopus_thread_t *thd_ptr,
                      lagopus_result_t require_ret) {
  bool result = false;
  lagopus_result_t ret;
  pthread_t tid;
  int i;
  for (i = 0; i < GET_TID_RETRY; i++) {
    ret = lagopus_thread_get_pthread_id(thd_ptr, &tid);
    if (ret == require_ret) {
      break;
    }
  }
  if (ret == require_ret) {
    if (ret == LAGOPUS_RESULT_OK) {
      TEST_ASSERT_NOT_EQUAL(tid, LAGOPUS_INVALID_THREAD);
    }
    result = true;
  } else {
    if (require_ret == LAGOPUS_RESULT_OK
        && ret == LAGOPUS_RESULT_ALREADY_HALTED) {
      lagopus_msg_warning(
        "test requires LAGOPUS_RESULT_OK, but result is LAGOPUS_ALREADY_HALTED. "
        "Modify value of WAIT_NSEC and MAIN_SLEEP_USEC of thread_test.c, please try again.\n");
      result = true;
    } else {
      lagopus_perror(ret);
      TEST_FAIL_MESSAGE("get_thread_id failed");
      result = false;
    }
  }
  return result;
}
Exemple #2
0
static lagopus_result_t
main_proc_long(const lagopus_thread_t *selfptr, void *arg) {
  lagopus_result_t ret = LAGOPUS_RESULT_ANY_FAILURES;
  pthread_t tid = LAGOPUS_INVALID_THREAD;
  (void)arg;
  ret = lagopus_thread_get_pthread_id(selfptr, &tid);
  TEST_ASSERT_EQUAL(LAGOPUS_RESULT_OK, ret);
  sleep(1000);
  return LAGOPUS_RESULT_OK;
}
Exemple #3
0
int
main(int argc, char const *const argv[]) {
  lagopus_result_t ret = LAGOPUS_RESULT_ANY_FAILURES;
  int i;
  int n = 10000;
  lagopus_thread_t thread = NULL;
  pthread_t tid = LAGOPUS_INVALID_THREAD;

  lagopus_log_set_debug_level(10);

  if (argc > 1) {
    int tmp;
    if (lagopus_str_parse_int32(argv[1], &tmp) == LAGOPUS_RESULT_OK &&
        tmp > 0) {
      n = tmp;
    }
  }

  /* create, start, wait, destroy */
  for (i = 0; i < n; i++) {
    thread = NULL;

    if ((ret = lagopus_thread_create(
                 (lagopus_thread_t *)&thread,
                 (lagopus_thread_main_proc_t)s_main_proc,
                 (lagopus_thread_finalize_proc_t)s_finalize_proc,
                 (lagopus_thread_freeup_proc_t)s_freeup_proc,
                 (const char *) "thread",
                 (void *) NULL)) != LAGOPUS_RESULT_OK) {
      lagopus_perror(ret);
      lagopus_exit_fatal("Can't create a thread.\n");
    } else {
      fprintf(stderr, "create a thread [%d]\n", i);
      if ((ret = lagopus_thread_get_pthread_id(&thread, &tid))
          != LAGOPUS_RESULT_NOT_STARTED) {
        lagopus_perror(ret);
        goto done;
      }
    }

    if ((ret = lagopus_thread_start(&thread, false)) != LAGOPUS_RESULT_OK) {
      lagopus_perror(ret);
      lagopus_exit_fatal("Can't start the thread.\n");
    } else {
      ret = lagopus_thread_get_pthread_id(&thread, &tid);
      if (ret == LAGOPUS_RESULT_OK ||
          ret == LAGOPUS_RESULT_ALREADY_HALTED) {
        fprintf(stderr, " start thread:  %lu\n", tid);
      } else {
        lagopus_perror(ret);
        goto done;
      }
    }

    s_check_cancelled(&thread, false);

    if ((ret = lagopus_thread_wait(&thread, 1000LL * 1000LL * 1000LL)) !=
        LAGOPUS_RESULT_OK) {
      lagopus_perror(ret);
      lagopus_msg_error("Can't wait the thread.\n");
    } else {
      if ((ret = lagopus_thread_get_pthread_id(&thread, &tid))
          == LAGOPUS_RESULT_OK) {
        lagopus_msg_error(
          "Thread has been completed, but I can get thread ID, %lu\n",
          tid);
      } else if (ret != LAGOPUS_RESULT_ALREADY_HALTED) {
        lagopus_perror(ret);
        goto done;
      }
    }

    s_check_cancelled(&thread, false);

    lagopus_thread_destroy(&thread);
    fprintf(stderr, "destroy the thread [%d]\n", i);

    fprintf(stderr, "\n");
  }

  /* create, start, cancel, destroy */
  for (i = 0; i < n; i++) {
    thread = NULL;

    if ((ret = lagopus_thread_create(
                 (lagopus_thread_t *)&thread,
                 (lagopus_thread_main_proc_t)s_main_proc_with_sleep,
                 (lagopus_thread_finalize_proc_t)s_finalize_proc,
                 (lagopus_thread_freeup_proc_t)s_freeup_proc,
                 (const char *) "thread",
                 (void *) NULL)) != LAGOPUS_RESULT_OK) {
      lagopus_perror(ret);
      lagopus_exit_fatal("Can't create a thread.\n");
    } else {
      fprintf(stderr, "create a thread [%d]\n", i);

      if ((ret = lagopus_thread_get_pthread_id(&thread, &tid))
          != LAGOPUS_RESULT_NOT_STARTED) {
        lagopus_perror(ret);
        goto done;
      }
    }

    if ((ret = lagopus_thread_start(&thread, false)) != LAGOPUS_RESULT_OK) {
      lagopus_perror(ret);
      lagopus_exit_fatal("Can't start the thread.\n");
    } else {
      ret = lagopus_thread_get_pthread_id(&thread, &tid);
      if (ret == LAGOPUS_RESULT_OK ||
          ret == LAGOPUS_RESULT_ALREADY_HALTED) {
        fprintf(stderr, " start thread:  %lu\n", tid);
      } else {
        lagopus_perror(ret);
        goto done;
      }
    }

    s_check_cancelled(&thread, false);

    if ((ret = lagopus_thread_cancel(&thread)) !=
        LAGOPUS_RESULT_OK) {
      lagopus_perror(ret);
      lagopus_msg_error("Can't cancel the thread.\n");
    } else {
      if ((ret = lagopus_thread_get_pthread_id(&thread, &tid))
          == LAGOPUS_RESULT_OK) {
        lagopus_msg_error(
          "Thread has been canceled, but I can get thread ID, %lu\n",
          tid);
      } else if (ret != LAGOPUS_RESULT_NOT_STARTED &&
                 ret != LAGOPUS_RESULT_ALREADY_HALTED) {
        lagopus_perror(ret);
        goto done;
      }
    }

    s_check_cancelled(&thread, true);

    lagopus_thread_destroy(&thread);
    fprintf(stderr, "destroy the thread [%d]\n", i);

    fprintf(stderr, "\n");
  }

  /* create, start, destroy */
  for (i = 0; i < n; i++) {
    thread = NULL;

    if ((ret = lagopus_thread_create(
                 (lagopus_thread_t *)&thread,
                 (lagopus_thread_main_proc_t)s_main_proc_with_sleep,
                 (lagopus_thread_finalize_proc_t)s_finalize_proc,
                 (lagopus_thread_freeup_proc_t)s_freeup_proc,
                 (const char *) "thread",
                 (void *) NULL)) != LAGOPUS_RESULT_OK) {
      lagopus_perror(ret);
      lagopus_exit_fatal("Can't create a thread.\n");
    } else {
      fprintf(stderr, "create a thread [%d]\n", i);

      if ((ret = lagopus_thread_get_pthread_id(&thread, &tid))
          != LAGOPUS_RESULT_NOT_STARTED) {
        lagopus_perror(ret);
        goto done;
      }
    }

    if ((ret = lagopus_thread_start(&thread, false)) != LAGOPUS_RESULT_OK) {
      lagopus_perror(ret);
      lagopus_exit_fatal("Can't start the thread.\n");
    } else {
      ret = lagopus_thread_get_pthread_id(&thread, &tid);
      if (ret == LAGOPUS_RESULT_OK ||
          ret == LAGOPUS_RESULT_ALREADY_HALTED) {
        fprintf(stderr, " start thread:  %lu\n", tid);
      } else {
        lagopus_perror(ret);
        goto done;
      }
    }

    lagopus_thread_destroy(&thread);
    fprintf(stderr, "destroy the thread [%d]\n", i);

    fprintf(stderr, "\n");
  }

done:
  return (ret == LAGOPUS_RESULT_OK) ? 0 : 1;
}