示例#1
0
static void
s_once_proc(void) {
  lagopus_result_t r;

  if ((r = lagopus_mutex_create(&s_lck)) != LAGOPUS_RESULT_OK) {
    lagopus_perror(r);
    lagopus_exit_fatal("Can't initilize a mutex.\n");
  }
  if ((r = lagopus_cond_create(&s_cond)) !=  LAGOPUS_RESULT_OK) {
    lagopus_perror(r);
    lagopus_exit_fatal("Can't initilize a cond.\n");
  }

  (void)pthread_atfork(NULL, NULL, s_child_at_fork);
}
示例#2
0
static lagopus_result_t
snmp_initialize(void) {
  lagopus_result_t ret = LAGOPUS_RESULT_ANY_FAILURES;

  ret = lagopus_rwlock_create(&(conf.s_lck));
  if (ret != LAGOPUS_RESULT_OK) {
    lagopus_perror(ret);
    lagopus_exit_fatal("Can't initialize the configurator lock.\n");
    free((void *)(conf.s_lck));
  } else {
    if (ret == LAGOPUS_RESULT_OK) {

      s_wlock_snmp_conf(&conf);
      {
        conf.ping_interval = DEFAULT_SNMPMGR_AGENTX_PING_INTERVAL_SEC;
        conf.agentx_sock = strdup(DEFAULT_SNMPMGR_AGENTX_SOCKET);
        conf.is_enabled = false;
      }
      s_unlock_snmp_conf(&conf);

      return LAGOPUS_RESULT_OK;
    }
  }
  return ret;
}
示例#3
0
static void
snmpmgr_thread_finally_called(
  const lagopus_thread_t *thd, bool is_canceled,
  void *arg) {
  bool is_valid = false;
  lagopus_result_t ret;
  (void) is_canceled;
  (void) arg;

  if (*thd != snmpmgr) {
    lagopus_exit_fatal("other threads worked, argument is :%p, snmpmgr is :%p\n",
                       *thd, snmpmgr);
  }

  ret = lagopus_thread_is_valid(&snmpmgr, &is_valid);
  if (ret != LAGOPUS_RESULT_OK || is_valid == false) {
    lagopus_perror(ret);
    return;
  }

  (void)lagopus_mutex_unlock(&snmp_lock);
  (void)lagopus_mutex_unlock(&snmp_state_lock);

  (void)lagopus_mutex_lock(&snmp_state_lock);
  state = SNMPMGR_NONE;
  (void)lagopus_mutex_unlock(&snmp_state_lock);

  global_state_cancel_janitor();
}
示例#4
0
static void
initialize_internal(void) {
  lagopus_result_t ret = LAGOPUS_RESULT_ANY_FAILURES;

  /* create hashmap */
  ret = lagopus_hashmap_create(&bridgeq_table,
                               LAGOPUS_HASHMAP_TYPE_ONE_WORD,
                               bridgeq_freeup_proc);
  if (ret != LAGOPUS_RESULT_OK) {
    lagopus_exit_fatal("Can't create bridgeq table.\n");
  }

  ret = lagopus_mutex_create(&lock);
  if (ret != LAGOPUS_RESULT_OK) {
    lagopus_exit_fatal("Can't create lock.\n");
  }
}
示例#5
0
文件: numa.c 项目: roccen/lagopus
void
lagopus_free_on_cpu(void *p) {
  if (likely(s_free_proc != NULL)) {
    return s_free_proc(p);
  } else {
    lagopus_exit_fatal("The NUMA aware allocator not initialized??\n");
  }
}
示例#6
0
文件: numa.c 项目: roccen/lagopus
void *
lagopus_malloc_on_cpu(size_t sz, int cpu) {
  if (likely(s_alloc_proc != NULL)) {
    return s_alloc_proc(sz, cpu);
  } else {
    lagopus_exit_fatal("The NUMA aware allocator not initialized??\n");
  }
}
示例#7
0
static void
s_once_proc(void) {
  lagopus_result_t r;

  if ((r = lagopus_hashmap_create(&s_ps_name_tbl, LAGOPUS_HASHMAP_TYPE_STRING,
                                  NULL)) != LAGOPUS_RESULT_OK) {
    lagopus_perror(r);
    lagopus_exit_fatal("can't initialize the name - pipeline stage table.\n");
  }

  if ((r = lagopus_hashmap_create(&s_ps_obj_tbl, LAGOPUS_HASHMAP_TYPE_ONE_WORD,
                                  NULL)) != LAGOPUS_RESULT_OK) {
    lagopus_perror(r);
    lagopus_exit_fatal("can't initialize the pipeline stage table.\n");
  }

  (void)pthread_atfork(NULL, NULL, s_child_at_fork);
}
示例#8
0
static void
initialize_internal(void) {
  lagopus_result_t ret = LAGOPUS_RESULT_ANY_FAILURES;

  ret = datastore_load_config();
  if (ret != LAGOPUS_RESULT_OK) {
    lagopus_perror(ret);
    lagopus_exit_fatal("can't load the configuration parameters.\n");
  }
}
示例#9
0
static void
s_once_proc(void) {
  lagopus_result_t r;
  s_n_modules = 0;
  (void)memset((void *)s_modules, sizeof(s_modules), 0);

  if ((r = lagopus_mutex_create(&s_lck)) != LAGOPUS_RESULT_OK) {
    lagopus_perror(r);
    lagopus_exit_fatal("can't initialize a mutex.\n");
  }
}
示例#10
0
static void
initialize_internal(void) {
  lagopus_result_t ret;

  ret = lagopus_mutex_create(&lock);
  if (ret != LAGOPUS_RESULT_OK) {
    lagopus_exit_fatal("session_tls_init:lagopus_mutex_create");
  }

  SSL_library_init();
  SSL_load_error_strings();
}
示例#11
0
static lagopus_result_t
snmpmgr_thread_loop(const lagopus_thread_t *selfptr, void *arg) {
  lagopus_result_t ret = LAGOPUS_RESULT_ANY_FAILURES;
  lagopus_chrono_t interval = DEFAULT_SNMPMGR_LOOP_INTERVAL_NSEC;
  global_state_t global_state;
  shutdown_grace_level_t l;
  (void)selfptr;
  (void)arg;

  /* open the session to AgentX master agent here. */
  init_snmp(SNMP_TYPE);

  /* wait all modules start */
  if ((ret = global_state_wait_for(GLOBAL_STATE_STARTED,
                                   &global_state, &l,
                                   -1 /* forever! */)) !=
      LAGOPUS_RESULT_OK) {
    lagopus_perror(ret);
  } else {
    if (global_state != GLOBAL_STATE_STARTED) {
      lagopus_exit_fatal("must not happen. die!\n");
    }
  }

  /* all modules have started, then send a coldStart trap */
  (void)send_coldStart_trap();

  lagopus_msg_info("SNMP manager started (as a thread).\n");

  /* main loop */
  while (keep_running) {
    (void)lagopus_mutex_lock(&snmp_lock);
    ret = internal_snmpmgr_poll(interval);
    (void)lagopus_mutex_unlock(&snmp_lock);
    if (ret != LAGOPUS_RESULT_OK && ret != LAGOPUS_RESULT_TIMEDOUT) {
      lagopus_msg_warning("failed to poll SNMP AgentX request: %s",
                          lagopus_error_get_string(ret));
    }
    check_status_and_send_traps();
  }

  /* stop SNMP */
  snmp_shutdown(SNMP_TYPE);

  (void)lagopus_mutex_lock(&snmp_state_lock);
  if (state != SNMPMGR_RUNNABLE) {
    state = SNMPMGR_NONE;
  }
  (void)lagopus_mutex_unlock(&snmp_state_lock);

  return LAGOPUS_RESULT_OK;
}
示例#12
0
static void
s_once_proc(void) {
  lagopus_result_t r;

  lagopus_msg_debug(5, "called.\n");

  if ((r = lagopus_mutex_create(&s_lck)) != LAGOPUS_RESULT_OK) {
    lagopus_perror(r);
    lagopus_exit_fatal("can't initialize a mutex.\n");
  }

  if ((r = lagopus_module_register(MY_MOD_NAME,
                                   dummy_module_initialize, NULL,
                                   dummy_module_start,
                                   dummy_module_shutdown,
                                   dummy_module_stop,
                                   dummy_module_finalize,
                                   dummy_module_usage)) != LAGOPUS_RESULT_OK) {
    lagopus_perror(r);
    lagopus_exit_fatal("can't register the %s module.\n", MY_MOD_NAME);
  }
}
示例#13
0
static void
initialize_internal(void) {
  lagopus_result_t ret;

  ret = lagopus_mutex_create(&lock);
  if (ret != LAGOPUS_RESULT_OK) {
    lagopus_exit_fatal("channel_mgr_initialize:lagopus_mutex_create");
  }

  ret = lagopus_hashmap_create(&main_table, LAGOPUS_HASHMAP_TYPE_STRING,
                               channel_entry_free);
  if (ret != LAGOPUS_RESULT_OK) {
    lagopus_exit_fatal("channel_mgr_initialize:lagopus_hashmap_create");
  }

  ret = lagopus_hashmap_create(&dp_table, LAGOPUS_HASHMAP_TYPE_ONE_WORD,
                               channel_list_entry_free);
  if (ret != LAGOPUS_RESULT_OK) {
    lagopus_exit_fatal("channel_mgr_initialize:lagopus_hashmap_create");
  }

}
示例#14
0
static void
s_once_proc(void) {
  lagopus_result_t r;

  if ((r = lagopus_hashmap_create(&s_tbl, LAGOPUS_HASHMAP_TYPE_STRING,
                                  NULL)) != LAGOPUS_RESULT_OK) {
    lagopus_perror(r);
    lagopus_exit_fatal("can't initialize an object table.\n");
  }

  if ((r = datastore_register_table(MY_COMMAND_NAME,
                                    &s_tbl,
                                    s_update,
                                    NULL,
                                    NULL,
                                    NULL,
                                    NULL,
                                    NULL)) != LAGOPUS_RESULT_OK) {
    lagopus_perror(r);
    lagopus_exit_fatal("can't register an object table for \"%s\".\n",
                       MY_COMMAND_NAME);
  }

  if ((r = datastore_create_interp(&s_interp)) != LAGOPUS_RESULT_OK) {
    lagopus_perror(r);
    lagopus_exit_fatal("can't create the datastore interpretor.\n");
  }

  if ((r = datastore_interp_register_command(&s_interp, NULL,
           MY_COMMAND_NAME, s_parse)) !=
      LAGOPUS_RESULT_OK) {
    lagopus_perror(r);
    lagopus_exit_fatal("can't register an object command \"%s\".\n",
                       MY_COMMAND_NAME);
  }

  (void)pthread_atfork(NULL, NULL, s_child_at_fork);
}
示例#15
0
文件: thread.c 项目: tidatida/lagopus
lagopus_result_t
dataplane_initialize(int argc,
                     const char *const argv[],
                     __UNUSED void *extarg,
                     lagopus_thread_t **thdptr) {
#ifdef HAVE_DPDK
  static struct dataplane_arg dparg;
#endif /* HAVE_DPDK */
  static struct dataplane_arg sockarg;
  static struct dataplane_arg timerarg;
  lagopus_result_t nb_ports;

  nb_ports = lagopus_dataplane_init(argc, argv);
  if (nb_ports < 0) {
    lagopus_msg_fatal("lagopus_dataplane_init failed\n");
    return nb_ports;
  }
  dp_api_init();
#ifdef HAVE_DPDK
  if (rawsocket_only_mode != true) {
    dparg.threadptr = &dpdk_thread;
    dparg.lock = &dpdk_lock;
    lagopus_thread_create(&dpdk_thread, dpdk_thread_loop,
                          dp_finalproc, dp_freeproc, "dataplane", &dparg);

    if (lagopus_mutex_create(&dpdk_lock) != LAGOPUS_RESULT_OK) {
      lagopus_exit_fatal("lagopus_mutex_create");
    }
  }
#endif /* HAVE_DPDK */

  lagopus_meter_init();
  lagopus_register_action_hook = lagopus_set_action_function;
  lagopus_register_instruction_hook = lagopus_set_instruction_function;
  flowinfo_init();

  timerarg.threadptr = &timer_thread;
  timerarg.lock = &timer_lock;
  timerarg.running = NULL;
  timerthread_initialize(&timerarg);

  sockarg.threadptr = &sock_thread;
  sockarg.lock = &sock_lock;
  sockarg.running = NULL;
  sockthread_initialize(&sockarg);

  *thdptr = &sock_thread;

  return LAGOPUS_RESULT_OK;
}
示例#16
0
static void
s_once_proc(void) {
  lagopus_result_t r;

  if ((r = lagopus_mutex_create(&s_sched_lck)) != LAGOPUS_RESULT_OK) {
    lagopus_perror(r);
    lagopus_exit_fatal("can't initialize the callout scheduler main mutex.\n");
  }

  if ((r = lagopus_mutex_create(&s_lck)) != LAGOPUS_RESULT_OK) {
    lagopus_perror(r);
    lagopus_exit_fatal("can't initialize the callout global mutex.\n");
  }

  if ((r = lagopus_cond_create(&s_sched_cnd)) != LAGOPUS_RESULT_OK) {
    lagopus_perror(r);
    lagopus_exit_fatal("can't initialize the callout cond.\n");
  }

  if ((r = lagopus_hashmap_create(&s_tsk_tbl,
                                  LAGOPUS_HASHMAP_TYPE_ONE_WORD,
                                  NULL)) != LAGOPUS_RESULT_OK) {
    lagopus_perror(r);
    lagopus_exit_fatal("can't initialize the callout table.\n");
  }

  if ((r = lagopus_bbq_create(&s_urgent_tsk_q, lagopus_callout_task_t,
                              CALLOUT_TASK_MAX, s_task_freeup)) != 
      LAGOPUS_RESULT_OK) {
    lagopus_perror(r);
    lagopus_exit_fatal("can't initialize the callout urgent tasks queue.\n");
  }

  if ((r = lagopus_bbq_create(&s_idle_tsk_q, lagopus_callout_task_t,
                              CALLOUT_TASK_MAX, s_task_freeup)) !=
      LAGOPUS_RESULT_OK) {
    lagopus_perror(r);
    lagopus_exit_fatal("can't initialize the callout idle tasks queue.\n");
  }

  if ((r = lagopus_mutex_create_recursive(&s_q_lck)) != LAGOPUS_RESULT_OK) {
    lagopus_perror(r);
    lagopus_exit_fatal("can't initialize the callout task queue mutex.\n");
  }

  TAILQ_INIT(&s_chrono_tsk_q);
}
示例#17
0
文件: thread.c 项目: tidatida/lagopus
void
dp_finalproc(const lagopus_thread_t *t, bool is_canceled, void *arg) {
  bool is_valid = false;
  struct dataplane_arg *dparg;
  lagopus_result_t ret;

  (void) is_canceled;
  dparg = arg;

  lagopus_msg_info("finalproc in %p\n", *dparg->threadptr);
  if (*t != *dparg->threadptr) {
    lagopus_exit_fatal("other threads worked t:%p, my_thread:%p\n",
                       *t, *dparg->threadptr);
  }

  ret = lagopus_thread_is_valid(dparg->threadptr, &is_valid);
  if (ret != LAGOPUS_RESULT_OK || is_valid == false) {
    lagopus_perror(ret);
    return;
  }
}
示例#18
0
文件: thread.c 项目: tidatida/lagopus
void
dp_freeproc(const lagopus_thread_t *t, void *arg) {
  bool is_valid = false;
  struct dataplane_arg *dparg;
  lagopus_result_t ret;

  dparg = arg;

  lagopus_msg_info("freeproc in\n");
  if (*t != *dparg->threadptr) {
    lagopus_exit_fatal("other threads worked %p, %p", *t, *dparg->threadptr);
  }

  ret = lagopus_thread_is_valid(dparg->threadptr, &is_valid);
  if (ret != LAGOPUS_RESULT_OK || is_valid == false) {
    lagopus_perror(ret);
    return;
  }
  /* ADD delete ports? */

  lagopus_mutex_destroy(dparg->lock);
  *dparg->lock = NULL;
}
示例#19
0
文件: numa.c 项目: roccen/lagopus
static inline void
s_init_numa_thingies(void) {
  numa_set_strict(1);

  s_n_cpus = (int64_t)sysconf(_SC_NPROCESSORS_CONF);
  if (s_n_cpus > 0) {
    s_numa_nodes = (unsigned int *)malloc(sizeof(int) * (size_t)s_n_cpus);
    if (s_numa_nodes != NULL) {
      int i;
      lagopus_result_t r;

      (void)memset((void *)s_numa_nodes, 0, sizeof(int) * (size_t)s_n_cpus);

      for (i = 0; i < (int)s_n_cpus; i++) {
        s_numa_nodes[i] = (unsigned int)numa_node_of_cpu(i);
        if (s_min_numa_node > s_numa_nodes[i]) {
          s_min_numa_node = s_numa_nodes[i];
        }
        if (s_max_numa_node < s_numa_nodes[i]) {
          s_max_numa_node = s_numa_nodes[i];
        }
      }

#ifndef DO_NUMA_EVNE_ONE_NODE
      if (s_max_numa_node > s_min_numa_node) {
        r = lagopus_hashmap_create(&s_tbl,
                                   LAGOPUS_HASHMAP_TYPE_ONE_WORD, NULL);
        if (r == LAGOPUS_RESULT_OK) {
          s_is_numa = true;

          s_alloc_proc = s_numa_alloc;
          s_free_proc = s_numa_free;
          
          lagopus_msg_debug(5, "The NUMA aware memory allocator is "
                            "initialized.\n");
        } else {
          lagopus_perror(r);
          lagopus_exit_fatal("can't initialize the "
                             "NUMA memory allocation table.\n");
        }
      } else {
        s_alloc_proc = s_uma_alloc;
        s_free_proc = s_uma_free;

        lagopus_msg_debug(5, "There is only one NUMA node on this machine. "
                          "No NUMA aware memory allocation is enabled.\n");
      }
#else
      r = lagopus_hashmap_create(&s_tbl,
                                 LAGOPUS_HASHMAP_TYPE_ONE_WORD, NULL);
      if (r == LAGOPUS_RESULT_OK) {
        s_is_numa = true;

        s_alloc_proc = s_numa_alloc;
        s_free_proc = s_numa_free;
          
        lagopus_msg_debug(5, "The NUMA aware memory allocator is "
                          "initialized.\n");
      } else {
        lagopus_perror(r);
        lagopus_exit_fatal("can't initialize the "
                           "NUMA memory allocation table.\n");
      }
#endif /* ! DO_NUMA_EVNE_ONE_NODE */
    }
  }
}
示例#20
0
lagopus_result_t
lagopus_pipeline_stage_wait(const lagopus_pipeline_stage_t *sptr,
                            lagopus_chrono_t nsec) {
  lagopus_result_t ret = LAGOPUS_RESULT_ANY_FAILURES;

  if (sptr != NULL && *sptr != NULL) {
    lagopus_pipeline_stage_t ps = *sptr;

    if (s_is_stage(ps) == true) {

      s_lock_stage(ps);
      {
        if (ps->m_status == STAGE_STATE_STARTED) {
          size_t n_shutdown;
          size_t n_canceled;

          ret = s_wait_stage(ps, ps->m_n_workers, nsec, false);

          s_final_lock_stage(ps);
          {
            n_shutdown = ps->m_n_shutdown_workers;
            n_canceled = ps->m_n_canceled_workers;
          }
          s_final_unlock_stage(ps);

          if (ret == LAGOPUS_RESULT_OK) {
            if (n_shutdown == ps->m_n_workers) {
              if (n_canceled > 0LL) {
                ps->m_status = STAGE_STATE_CANCELED;
              } else {
                ps->m_status = STAGE_STATE_SHUTDOWN;
              }

              if (ps->m_final_proc != NULL) {
                (ps->m_final_proc)(&ps,
                                   (n_canceled > 0) ? true : false);
              }

              if (ps->m_shutdown_proc != NULL) {
                (void)(ps->m_shutdown_proc)(&ps, ps->m_sg_lvl);
              }

            } else {
              lagopus_exit_fatal("must not happen, waiting for all the worker "
                                 "exit succeeded but the number of the exited "
                                 "workers and the number of succeeded API "
                                 "calls differ on stage '%s', "
                                 "workers " PFSZ(u) ", "
                                 "shutdown " PFSZ(u) ", "
                                 "cancel " PFSZ(u) "\n",
                                 ps->m_name,
                                 ps->m_n_workers, n_shutdown, n_canceled);
            }

            ps->m_do_loop = false;
          }

        } else {
          ret = LAGOPUS_RESULT_INVALID_STATE_TRANSITION;
        }
      }
      s_unlock_stage(ps);

    } else {
      ret = LAGOPUS_RESULT_INVALID_OBJECT;
    }
  } else {
    ret = LAGOPUS_RESULT_INVALID_ARGS;
  }

  return ret;
}
示例#21
0
int
main(int argc, const char *const argv[]) {
  test_thread_record ttr;
  test_thread_t tt;
  bool is_canceled;
  int ret = 1;

  (void)argc;
  (void)argv;

  fprintf(stderr, "Pthread wrapper check: start\n\n");

  /*
   * Heap object
   */
  fprintf(stderr, "Heap object, regular: start\n");
  tt = NULL;
  if (test_thread_create(&tt, 100) != true) {
    lagopus_exit_fatal("Can't create a test thread object.\n");
  }
  if (lagopus_thread_start((lagopus_thread_t *)&tt, false) !=
      LAGOPUS_RESULT_OK) {
    lagopus_exit_fatal("Can't start a thread.\n");
  }
  lagopus_chrono_nanosleep(5LL * 1000LL * 1000LL * 1000LL, NULL);
  test_thread_request_stop(tt);
  if (lagopus_thread_wait((lagopus_thread_t *)&tt, -1) !=
      LAGOPUS_RESULT_OK) {
    lagopus_exit_fatal("Can't wait the thread.\n");
  }
  lagopus_thread_destroy((lagopus_thread_t *)&tt);
  fprintf(stderr, "Heap object, regular: end\n\n");

  /*
   * Stack object
   */
  fprintf(stderr, "Stack object, regular: start\n");
  tt = &ttr;
  if (test_thread_create(&tt, 100) != true) {
    lagopus_exit_fatal("Can't initialize a test thread object.\n");
  }
  if (lagopus_thread_start((lagopus_thread_t *)&tt, false) !=
      LAGOPUS_RESULT_OK) {
    lagopus_exit_fatal("Can't start a thread.\n");
  }
  lagopus_chrono_nanosleep(5LL * 1000LL * 1000LL * 1000LL, NULL);
  test_thread_request_stop(tt);
  if (lagopus_thread_wait((lagopus_thread_t *)&tt, -1) !=
      LAGOPUS_RESULT_OK) {
    lagopus_exit_fatal("Can't wait the thread.\n");
  }
  lagopus_thread_destroy((lagopus_thread_t *)&tt);
  fprintf(stderr, "Stack object, regular: end\n\n");

  /*
   * Cancel
   */
  fprintf(stderr, "Stack object, cancel: start\n");
  tt = &ttr;
  if (test_thread_create(&tt, 100) != true) {
    lagopus_exit_fatal("Can't initialize a test thread object.\n");
  }
  if (lagopus_thread_start((lagopus_thread_t *)&tt, false) !=
      LAGOPUS_RESULT_OK) {
    lagopus_exit_fatal("Can't start a thread.\n");
  }
  lagopus_chrono_nanosleep(5LL * 1000LL * 1000LL * 1000LL, NULL);
  if (lagopus_thread_cancel((lagopus_thread_t *)&tt) != LAGOPUS_RESULT_OK) {
    lagopus_exit_fatal("Can't cancel the thread.\n");
  }
  if (lagopus_thread_wait((lagopus_thread_t *)&tt, -1) !=
      LAGOPUS_RESULT_OK) {
    lagopus_exit_fatal("Can't wait the thread.\n");
  }
  if (lagopus_thread_is_canceled((lagopus_thread_t *)&tt, &is_canceled) !=
      LAGOPUS_RESULT_OK) {
    lagopus_exit_fatal("The returned value must be \"canceled\".\n");
  }
  lagopus_thread_destroy((lagopus_thread_t *)&tt);
  fprintf(stderr, "Stack object, cancel: end\n\n");

  /*
   * Heap object auto deletion
   */
  fprintf(stderr, "Heap object, auto deletion: start\n");
  s_is_deleted = false;
  tt = NULL;
  if (test_thread_create(&tt, 100) != true) {
    lagopus_exit_fatal("Can't initialize a test thread object.\n");
  }
  if (lagopus_thread_start((lagopus_thread_t *)&tt, true) !=
      LAGOPUS_RESULT_OK) {
    lagopus_exit_fatal("Can't start a thread.\n");
  }
  lagopus_chrono_nanosleep(5LL * 1000LL * 1000LL * 1000LL, NULL);
  test_thread_request_stop(tt);
  lagopus_chrono_nanosleep(2LL * 1000LL * 1000LL * 1000LL, NULL);
  if (s_is_deleted == false) {
    lagopus_exit_fatal("The thread object must be freed and "
                       "the flag must be true.\n");
  }
  fprintf(stderr, "Heap object, auto deletion: end\n\n");

  /*
   * Stack object auto deletion
   */
  fprintf(stderr, "Stack object, auto deletion: start\n");
  s_is_deleted = false;
  tt = &ttr;
  if (test_thread_create(&tt, 100) != true) {
    lagopus_exit_fatal("Can't initialize a test thread object.\n");
  }
  if (lagopus_thread_start((lagopus_thread_t *)&tt, true) !=
      LAGOPUS_RESULT_OK) {
    lagopus_exit_fatal("Can't start a thread.\n");
  }
  lagopus_chrono_nanosleep(5LL * 1000LL * 1000LL * 1000LL, NULL);
  test_thread_request_stop(tt);
  lagopus_chrono_nanosleep(2LL * 1000LL * 1000LL * 1000LL, NULL);
  if (s_is_deleted == false) {
    lagopus_exit_fatal("The thread object must be freed and "
                       "the flag must be true.\n");
  }
  fprintf(stderr, "Stack object, auto deletion: end\n\n");

  /*
   * Heap object cancel, auto deletion
   */
  fprintf(stderr, "Heap object, cancel, auto deletion: start\n");
  s_is_deleted = false;
  tt = NULL;
  if (test_thread_create(&tt, 100) != true) {
    lagopus_exit_fatal("Can't initialize a test thread object.\n");
  }
  if (lagopus_thread_start((lagopus_thread_t *)&tt, true) !=
      LAGOPUS_RESULT_OK) {
    lagopus_exit_fatal("Can't start a thread.\n");
  }
  lagopus_chrono_nanosleep(5LL * 1000LL * 1000LL * 1000LL, NULL);
  if (lagopus_thread_cancel((lagopus_thread_t *)&tt) != LAGOPUS_RESULT_OK) {
    lagopus_exit_fatal("Can't cancel the thread.\n");
  }
  lagopus_chrono_nanosleep(2LL * 1000LL * 1000LL * 1000LL, NULL);
  if (s_is_deleted == false) {
    lagopus_exit_fatal("The thread object must be freed and "
                       "the flag must be true.\n");
  }
  fprintf(stderr, "Heap object, cancel, auto deletion: end\n\n");

  /*
   * Stack object cancel, auto deletion
   */
  fprintf(stderr, "Stack object, cancel, auto deletion: start\n");
  s_is_deleted = false;
  tt = &ttr;
  if (test_thread_create(&tt, 100) != true) {
    lagopus_exit_fatal("Can't initialize a test thread object.\n");
  }
  if (lagopus_thread_start((lagopus_thread_t *)&tt, true) !=
      LAGOPUS_RESULT_OK) {
    lagopus_exit_fatal("Can't start a thread.\n");
  }
  lagopus_chrono_nanosleep(5LL * 1000LL * 1000LL * 1000LL, NULL);
  if (lagopus_thread_cancel((lagopus_thread_t *)&tt) != LAGOPUS_RESULT_OK) {
    lagopus_exit_fatal("Can't cancel the thread.\n");
  }
  lagopus_chrono_nanosleep(2LL * 1000LL * 1000LL * 1000LL, NULL);
  if (s_is_deleted == false) {
    lagopus_exit_fatal("The thread object must be freed and "
                       "the flag must be true.\n");
  }
  fprintf(stderr, "Stack object, cancel, auto deletion: end\n\n");

  /*
   * Timed wait
   */
  fprintf(stderr, "Timed wait: start\n");
  tt = &ttr;
  if (test_thread_create(&tt, 100) != true) {
    lagopus_exit_fatal("Can't initialize a test thread object.\n");
  }
  if (lagopus_thread_start((lagopus_thread_t *)&tt, false) !=
      LAGOPUS_RESULT_OK) {
    lagopus_exit_fatal("Can't start a thread.\n");
  }
  if (lagopus_thread_wait((lagopus_thread_t *)&tt,
                          5LL * 1000LL * 1000LL * 1000LL) !=
      LAGOPUS_RESULT_TIMEDOUT) {
    lagopus_exit_fatal("Must be timed out.\n");
  }
  test_thread_request_stop(tt);
  if (lagopus_thread_wait((lagopus_thread_t *)&tt, -1) !=
      LAGOPUS_RESULT_OK) {
    lagopus_exit_fatal("Can't wait the thread.\n");
  }
  lagopus_thread_destroy((lagopus_thread_t *)&tt);
  fprintf(stderr, "Timed wait: end\n\n");

  /*
   * Force destroy
   */
  fprintf(stderr, "Force destroy: start\n");
  s_is_deleted = false;
  tt = NULL;
  if (test_thread_create(&tt, 100) != true) {
    lagopus_exit_fatal("Can't create a test thread object.\n");
  }
  if (lagopus_thread_start((lagopus_thread_t *)&tt, false) !=
      LAGOPUS_RESULT_OK) {
    lagopus_exit_fatal("Can't start a thread.\n");
  }
  lagopus_thread_destroy((lagopus_thread_t *)&tt);
  if (s_is_deleted == false) {
    lagopus_exit_fatal("The thread object must be freed and "
                       "the flag must be true.\n");
  }
  fprintf(stderr, "Force destroy: end\n\n");

  /*
   * Force destroy, not started
   */
  fprintf(stderr, "Force destroy, not started: start\n");
  s_is_deleted = false;
  tt = NULL;
  if (test_thread_create(&tt, 100) != true) {
    lagopus_exit_fatal("Can't create a test thread object.\n");
  }
  lagopus_thread_destroy((lagopus_thread_t *)&tt);
  if (s_is_deleted == false) {
    lagopus_exit_fatal("The thread object must be freed and "
                       "the flag must be true.\n");
  }
  fprintf(stderr, "Force destroy, not started: end\n\n");

  ret = 0;
  fprintf(stderr, "Pthread wrapper check: end\n");

  return ret;
}
示例#22
0
文件: check3.c 项目: AkiraSuu/lagopus
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;
}