diff mbox

[1/2] linux-generic: add pthread_create_single api

Message ID 1406722571-23556-1-git-send-email-santosh.shukla@linaro.org
State New
Headers show

Commit Message

Santosh Shukla July 30, 2014, 12:16 p.m. UTC
odp_linux_pthread_create_single api stripped down
version of existing odp_linux_pthread_create api,

It avoid complete thread_tbl flushing, only specific
thread_tbl index get flushed.

pthread_create_single api will
- only create one thread,
- thread can pin to core_num
- thread can update to any thread_tbl[] index.

Signed-off-by: santosh shukla <santosh.shukla@linaro.org>
---
 include/helper/odp_linux.h         |   16 ++++++++++++
 platform/linux-generic/odp_linux.c |   48 ++++++++++++++++++++++++++++++++++++
 2 files changed, 64 insertions(+)

Comments

Santosh Shukla July 30, 2014, 12:39 p.m. UTC | #1
On 30 July 2014 17:46, Santosh Shukla <santosh.shukla@linaro.org> wrote:
> odp_linux_pthread_create_single api stripped down
> version of existing odp_linux_pthread_create api,
>
> It avoid complete thread_tbl flushing, only specific
> thread_tbl index get flushed.
>
> pthread_create_single api will
> - only create one thread,
> - thread can pin to core_num
> - thread can update to any thread_tbl[] index.
>
> Signed-off-by: santosh shukla <santosh.shukla@linaro.org>
> ---
>  include/helper/odp_linux.h         |   16 ++++++++++++
>  platform/linux-generic/odp_linux.c |   48 ++++++++++++++++++++++++++++++++++++
>  2 files changed, 64 insertions(+)
>
> diff --git a/include/helper/odp_linux.h b/include/helper/odp_linux.h
> index 3076c22..834e553 100644
> --- a/include/helper/odp_linux.h
> +++ b/include/helper/odp_linux.h
> @@ -48,6 +48,22 @@ void odp_linux_pthread_create(odp_linux_pthread_t *thread_tbl,
>                               int num, int first_core,
>                               void *(*start_routine) (void *), void *arg);
>
> +/**
> + * Create and launch a single pthread
> + *
> + * Create, pin and launch thread to specific core_number
> + *
> + * @param thread_tbl    Thread table
> + * @param thd_tbl_idx   Thread table index to update thread info
> + * @param core_num     Core number to pin thread with
> + * @param start_routine Thread start function
> + * @param arg           Thread argument
> + */
> +void odp_linux_pthread_create_single(odp_linux_pthread_t *thread_tbl,
> +                                       int thd_tbl_idx,
> +                                       int first_core,
> +                                       void *(*start_routine) (void *),
> +                                       void *arg);
>
>  /**
>   * Waits pthreads to exit
> diff --git a/platform/linux-generic/odp_linux.c b/platform/linux-generic/odp_linux.c
> index 6e2b448..bebff51 100644
> --- a/platform/linux-generic/odp_linux.c
> +++ b/platform/linux-generic/odp_linux.c
> @@ -85,6 +85,54 @@ void odp_linux_pthread_create(odp_linux_pthread_t *thread_tbl, int num,
>  }
>
>
> +void odp_linux_pthread_create_single(odp_linux_pthread_t *thread_tbl,
> +                                    int thd_tbl_idx,
> +                                    int first_core,
> +                                    void *(*start_routine) (void *), void *arg)
> +{
> +       cpu_set_t cpu_set;
> +       odp_start_args_t *start_args;
> +       int core_count;
> +       int cpu;
> +
> +       core_count = odp_sys_core_count();
> +
> +       assert((first_core >= 0) && (first_core < core_count));
> +       assert((thd_tbl_idx >= 0) && (thd_tbl_idx <= core_count));

Bahhh, just realized that this check  "(thd_tbl_idx <= core_count));"
IS wrong, it should be something like
 (thd_tbl_idx <= MAX_THD_TBL_IDX_SIZE));

Maxim, Pl. ignore this patch although i like to know review feedback
on this patch, as Its a new api proposal in odp-linux-generic library.

Ankit, It won't stop your timer test functionality so pl. use this
patch for testing and let me know your feedback.

> +
> +       /* check thread_tbl idx is free for use? */
> +       if (thread_tbl[thd_tbl_idx].thread != 0) {
> +               ODP_ERR("thread_tbl idx[%d] used\n", thd_tbl_idx);
> +               return;
> +       }
> +
> +       /* flush that tbl_idx only */
> +       memset(thread_tbl+thd_tbl_idx, 0, sizeof(odp_linux_pthread_t));
> +
> +       /* now create a thread,  affine to first_core */
> +       pthread_attr_init(&thread_tbl[thd_tbl_idx].attr);
> +
> +       CPU_ZERO(&cpu_set);
> +
> +       cpu = first_core % core_count;
> +       CPU_SET(cpu, &cpu_set);
> +
> +       pthread_attr_setaffinity_np(&thread_tbl[thd_tbl_idx].attr,
> +                                   sizeof(cpu_set_t), &cpu_set);
> +
> +       start_args = malloc(sizeof(odp_start_args_t));
> +       memset(start_args, 0, sizeof(odp_start_args_t));
> +       start_args->start_routine = start_routine;
> +       start_args->arg           = arg;
> +
> +       start_args->thr_id        = odp_thread_create(cpu);
> +
> +       pthread_create(&thread_tbl[thd_tbl_idx].thread,
> +                      &thread_tbl[thd_tbl_idx].attr,
> +                      odp_run_start_routine, start_args);
> +}
> +
> +
>  void odp_linux_pthread_join(odp_linux_pthread_t *thread_tbl, int num)
>  {
>         int i;
> --
> 1.7.9.5
>
Maxim Uvarov July 31, 2014, 12:17 p.m. UTC | #2
On 07/30/2014 04:39 PM, Santosh Shukla wrote:
> Maxim, Pl. ignore this patch although i like to know review feedback
> on this patch, as Its a new api proposal in odp-linux-generic library.
OK.

Maxim.
diff mbox

Patch

diff --git a/include/helper/odp_linux.h b/include/helper/odp_linux.h
index 3076c22..834e553 100644
--- a/include/helper/odp_linux.h
+++ b/include/helper/odp_linux.h
@@ -48,6 +48,22 @@  void odp_linux_pthread_create(odp_linux_pthread_t *thread_tbl,
 			      int num, int first_core,
 			      void *(*start_routine) (void *), void *arg);
 
+/**
+ * Create and launch a single pthread
+ *
+ * Create, pin and launch thread to specific core_number
+ *
+ * @param thread_tbl    Thread table
+ * @param thd_tbl_idx   Thread table index to update thread info
+ * @param core_num	Core number to pin thread with
+ * @param start_routine Thread start function
+ * @param arg           Thread argument
+ */
+void odp_linux_pthread_create_single(odp_linux_pthread_t *thread_tbl,
+					int thd_tbl_idx,
+					int first_core,
+					void *(*start_routine) (void *),
+					void *arg);
 
 /**
  * Waits pthreads to exit
diff --git a/platform/linux-generic/odp_linux.c b/platform/linux-generic/odp_linux.c
index 6e2b448..bebff51 100644
--- a/platform/linux-generic/odp_linux.c
+++ b/platform/linux-generic/odp_linux.c
@@ -85,6 +85,54 @@  void odp_linux_pthread_create(odp_linux_pthread_t *thread_tbl, int num,
 }
 
 
+void odp_linux_pthread_create_single(odp_linux_pthread_t *thread_tbl,
+				     int thd_tbl_idx,
+				     int first_core,
+				     void *(*start_routine) (void *), void *arg)
+{
+	cpu_set_t cpu_set;
+	odp_start_args_t *start_args;
+	int core_count;
+	int cpu;
+
+	core_count = odp_sys_core_count();
+
+	assert((first_core >= 0) && (first_core < core_count));
+	assert((thd_tbl_idx >= 0) && (thd_tbl_idx <= core_count));
+
+	/* check thread_tbl idx is free for use? */
+	if (thread_tbl[thd_tbl_idx].thread != 0) {
+		ODP_ERR("thread_tbl idx[%d] used\n", thd_tbl_idx);
+		return;
+	}
+
+	/* flush that tbl_idx only */
+	memset(thread_tbl+thd_tbl_idx, 0, sizeof(odp_linux_pthread_t));
+
+	/* now create a thread,  affine to first_core */
+	pthread_attr_init(&thread_tbl[thd_tbl_idx].attr);
+
+	CPU_ZERO(&cpu_set);
+
+	cpu = first_core % core_count;
+	CPU_SET(cpu, &cpu_set);
+
+	pthread_attr_setaffinity_np(&thread_tbl[thd_tbl_idx].attr,
+				    sizeof(cpu_set_t), &cpu_set);
+
+	start_args = malloc(sizeof(odp_start_args_t));
+	memset(start_args, 0, sizeof(odp_start_args_t));
+	start_args->start_routine = start_routine;
+	start_args->arg           = arg;
+
+	start_args->thr_id        = odp_thread_create(cpu);
+
+	pthread_create(&thread_tbl[thd_tbl_idx].thread,
+		       &thread_tbl[thd_tbl_idx].attr,
+		       odp_run_start_routine, start_args);
+}
+
+
 void odp_linux_pthread_join(odp_linux_pthread_t *thread_tbl, int num)
 {
 	int i;