[3/7] util/queue: move thread creation into a separate function

Submitted by Marek Olšák on Feb. 25, 2019, 6:27 p.m.

Details

Message ID 20190225182752.1804-3-maraeo@gmail.com
State New
Headers show
Series "Series without cover letter" ( rev: 1 ) in Mesa

Not browsing as part of any series.

Commit Message

Marek Olšák Feb. 25, 2019, 6:27 p.m.
From: Marek Olšák <marek.olsak@amd.com>

Reviewed-by: Ian Romanick <ian.d.romanick@intel.com>
---
 src/util/u_queue.c | 56 ++++++++++++++++++++++++++--------------------
 1 file changed, 32 insertions(+), 24 deletions(-)

Patch hide | download patch | download mbox

diff --git a/src/util/u_queue.c b/src/util/u_queue.c
index 3812c824b6d..48c5c79552d 100644
--- a/src/util/u_queue.c
+++ b/src/util/u_queue.c
@@ -298,20 +298,51 @@  util_queue_thread_func(void *input)
          util_queue_fence_signal(queue->jobs[i].fence);
          queue->jobs[i].job = NULL;
       }
    }
    queue->read_idx = queue->write_idx;
    queue->num_queued = 0;
    mtx_unlock(&queue->lock);
    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,
                 unsigned max_jobs,
                 unsigned num_threads,
                 unsigned flags)
 {
    unsigned i;
 
    /* Form the thread name from process_name and name, limited to 13
@@ -357,53 +388,30 @@  util_queue_init(struct util_queue *queue,
    queue->num_queued = 0;
    cnd_init(&queue->has_queued_cond);
    cnd_init(&queue->has_space_cond);
 
    queue->threads = (thrd_t*) calloc(num_threads, sizeof(thrd_t));
    if (!queue->threads)
       goto fail;
 
    /* 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;
          } else {
             /* at least one thread created, so use it */
             queue->num_threads = i;
             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);
    return true;
 
 fail:
    free(queue->threads);
 
    if (queue->jobs) {
       cnd_destroy(&queue->has_space_cond);