1/**************************************************************************
2 *
3 * Copyright 2019 Red Hat.
4 * All Rights Reserved.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included
14 * in all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
17 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 * SOFTWARE.
23 *
24 **************************************************************************/
25
26/* This is a compute shader specific thread pool.
27 * It allows the queuing of a number of tasks per work item.
28 * The item is added to the work queue once, but it must execute
29 * number of iterations times. This saves storing a bunch of queue
30 * structs with just unique indexes in them.
31 * It also supports a local memory support struct to be passed from
32 * outside the thread exec function.
33 */
34#ifndef LP_CS_QUEUE
35#define LP_CS_QUEUE
36
37#include "pipe/p_compiler.h"
38
39#include "util/u_thread.h"
40#include "util/list.h"
41
42#include "lp_limits.h"
43
44struct lp_cs_tpool {
45   mtx_t m;
46   cnd_t new_work;
47
48   thrd_t threads[LP_MAX_THREADS];
49   unsigned num_threads;
50   struct list_head workqueue;
51   bool shutdown;
52};
53
54struct lp_cs_local_mem {
55   unsigned local_size;
56   void *local_mem_ptr;
57};
58
59typedef void (*lp_cs_tpool_task_func)(void *data, int iter_idx, struct lp_cs_local_mem *lmem);
60
61struct lp_cs_tpool_task {
62   lp_cs_tpool_task_func work;
63   void *data;
64   struct list_head list;
65   cnd_t finish;
66   unsigned iter_total;
67   unsigned iter_start;
68   unsigned iter_finished;
69   unsigned iter_per_thread;
70   unsigned iter_remainder;
71};
72
73struct lp_cs_tpool *lp_cs_tpool_create(unsigned num_threads);
74void lp_cs_tpool_destroy(struct lp_cs_tpool *);
75
76struct lp_cs_tpool_task *lp_cs_tpool_queue_task(struct lp_cs_tpool *,
77                                                lp_cs_tpool_task_func func,
78                                                void *data, int num_iters);
79
80void lp_cs_tpool_wait_for_task(struct lp_cs_tpool *pool,
81                            struct lp_cs_tpool_task **task);
82
83#endif /* LP_BIN_QUEUE */
84