util/queue: move thread creation into a separate function
authorMarek Olšák <marek.olsak@amd.com>
Tue, 7 Aug 2018 00:15:43 +0000 (20:15 -0400)
committerMarek Olšák <marek.olsak@amd.com>
Mon, 1 Apr 2019 16:37:52 +0000 (12:37 -0400)
Reviewed-by: Ian Romanick <ian.d.romanick@intel.com>
src/util/u_queue.c

index 3812c824b6dab14a8586434dc12b6f10faddbfea..48c5c79552d1b6159a7c3e7532172005803b33d0 100644 (file)
@@ -305,6 +305,37 @@ util_queue_thread_func(void *input)
    return 0;
 }
 
+static bool
+util_queue_create_thread(struct util_queue *queue, unsigned index)
+{
+   struct thread_input *input =
+      (struct thread_input *) malloc(sizeof(struct thread_input));
+   input->queue = queue;
+   input->thread_index = index;
+
+   queue->threads[index] = u_thread_create(util_queue_thread_func, input);
+
+   if (!queue->threads[index]) {
+      free(input);
+      return false;
+   }
+
+   if (queue->flags & UTIL_QUEUE_INIT_USE_MINIMUM_PRIORITY) {
+#if defined(__linux__) && defined(SCHED_IDLE)
+      struct sched_param sched_param = {0};
+
+      /* The nice() function can only set a maximum of 19.
+       * SCHED_IDLE is the same as nice = 20.
+       *
+       * Note that Linux only allows decreasing the priority. The original
+       * priority can't be restored.
+       */
+      pthread_setschedparam(queue->threads[index], SCHED_IDLE, &sched_param);
+#endif
+   }
+   return true;
+}
+
 bool
 util_queue_init(struct util_queue *queue,
                 const char *name,
@@ -364,16 +395,7 @@ util_queue_init(struct util_queue *queue,
 
    /* start threads */
    for (i = 0; i < num_threads; i++) {
-      struct thread_input *input =
-         (struct thread_input *) malloc(sizeof(struct thread_input));
-      input->queue = queue;
-      input->thread_index = i;
-
-      queue->threads[i] = u_thread_create(util_queue_thread_func, input);
-
-      if (!queue->threads[i]) {
-         free(input);
-
+      if (!util_queue_create_thread(queue, i)) {
          if (i == 0) {
             /* no threads created, fail */
             goto fail;
@@ -383,20 +405,6 @@ util_queue_init(struct util_queue *queue,
             break;
          }
       }
-
-      if (flags & UTIL_QUEUE_INIT_USE_MINIMUM_PRIORITY) {
-   #if defined(__linux__) && defined(SCHED_IDLE)
-         struct sched_param sched_param = {0};
-
-         /* The nice() function can only set a maximum of 19.
-          * SCHED_IDLE is the same as nice = 20.
-          *
-          * Note that Linux only allows decreasing the priority. The original
-          * priority can't be restored.
-          */
-         pthread_setschedparam(queue->threads[i], SCHED_IDLE, &sched_param);
-   #endif
-      }
    }
 
    add_to_atexit_list(queue);