17f565845a1f3ec871cccee88e2acf6be2e429797Axel Davy/*
27f565845a1f3ec871cccee88e2acf6be2e429797Axel Davy * Copyright © 2012 Intel Corporation
37f565845a1f3ec871cccee88e2acf6be2e429797Axel Davy *
47f565845a1f3ec871cccee88e2acf6be2e429797Axel Davy * Permission is hereby granted, free of charge, to any person obtaining a
57f565845a1f3ec871cccee88e2acf6be2e429797Axel Davy * copy of this software and associated documentation files (the "Software"),
67f565845a1f3ec871cccee88e2acf6be2e429797Axel Davy * to deal in the Software without restriction, including without limitation
77f565845a1f3ec871cccee88e2acf6be2e429797Axel Davy * the rights to use, copy, modify, merge, publish, distribute, sublicense,
87f565845a1f3ec871cccee88e2acf6be2e429797Axel Davy * and/or sell copies of the Software, and to permit persons to whom the
97f565845a1f3ec871cccee88e2acf6be2e429797Axel Davy * Software is furnished to do so, subject to the following conditions:
107f565845a1f3ec871cccee88e2acf6be2e429797Axel Davy *
117f565845a1f3ec871cccee88e2acf6be2e429797Axel Davy * The above copyright notice and this permission notice (including the next
127f565845a1f3ec871cccee88e2acf6be2e429797Axel Davy * paragraph) shall be included in all copies or substantial portions of the
137f565845a1f3ec871cccee88e2acf6be2e429797Axel Davy * Software.
147f565845a1f3ec871cccee88e2acf6be2e429797Axel Davy *
157f565845a1f3ec871cccee88e2acf6be2e429797Axel Davy * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
167f565845a1f3ec871cccee88e2acf6be2e429797Axel Davy * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
177f565845a1f3ec871cccee88e2acf6be2e429797Axel Davy * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
187f565845a1f3ec871cccee88e2acf6be2e429797Axel Davy * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
197f565845a1f3ec871cccee88e2acf6be2e429797Axel Davy * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
207f565845a1f3ec871cccee88e2acf6be2e429797Axel Davy * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
217f565845a1f3ec871cccee88e2acf6be2e429797Axel Davy * IN THE SOFTWARE.
227f565845a1f3ec871cccee88e2acf6be2e429797Axel Davy */
237f565845a1f3ec871cccee88e2acf6be2e429797Axel Davy
247f565845a1f3ec871cccee88e2acf6be2e429797Axel Davy#ifndef _THREADPOOL_H_
257f565845a1f3ec871cccee88e2acf6be2e429797Axel Davy#define _THREADPOOL_H_
267f565845a1f3ec871cccee88e2acf6be2e429797Axel Davy
27f21b7d1e5c21b749ae7c19d3dc80dc4e14e4bb77Wang He#include <pthread.h>
28f21b7d1e5c21b749ae7c19d3dc80dc4e14e4bb77Wang He
29f3fa7e3068512dde55c3f4d7c2b599ab1d7fe67fPatrick Rudolphstruct NineSwapChain9;
30f3fa7e3068512dde55c3f4d7c2b599ab1d7fe67fPatrick Rudolph
317f565845a1f3ec871cccee88e2acf6be2e429797Axel Davy#define MAXTHREADS 1
327f565845a1f3ec871cccee88e2acf6be2e429797Axel Davy
337f565845a1f3ec871cccee88e2acf6be2e429797Axel Davystruct threadpool {
347f565845a1f3ec871cccee88e2acf6be2e429797Axel Davy    pthread_mutex_t m;
357f565845a1f3ec871cccee88e2acf6be2e429797Axel Davy    pthread_cond_t new_work;
367f565845a1f3ec871cccee88e2acf6be2e429797Axel Davy
37f3fa7e3068512dde55c3f4d7c2b599ab1d7fe67fPatrick Rudolph    HANDLE wthread;
38f3fa7e3068512dde55c3f4d7c2b599ab1d7fe67fPatrick Rudolph    pthread_t pthread;
397f565845a1f3ec871cccee88e2acf6be2e429797Axel Davy    struct threadpool_task *workqueue;
407f565845a1f3ec871cccee88e2acf6be2e429797Axel Davy    BOOL shutdown;
417f565845a1f3ec871cccee88e2acf6be2e429797Axel Davy};
427f565845a1f3ec871cccee88e2acf6be2e429797Axel Davy
437f565845a1f3ec871cccee88e2acf6be2e429797Axel Davytypedef void (*threadpool_task_func)(void *data);
447f565845a1f3ec871cccee88e2acf6be2e429797Axel Davy
457f565845a1f3ec871cccee88e2acf6be2e429797Axel Davystruct threadpool_task {
467f565845a1f3ec871cccee88e2acf6be2e429797Axel Davy    threadpool_task_func work;
477f565845a1f3ec871cccee88e2acf6be2e429797Axel Davy    void *data;
487f565845a1f3ec871cccee88e2acf6be2e429797Axel Davy    struct threadpool_task *next;
497f565845a1f3ec871cccee88e2acf6be2e429797Axel Davy    pthread_cond_t finish;
507f565845a1f3ec871cccee88e2acf6be2e429797Axel Davy    BOOL finished;
517f565845a1f3ec871cccee88e2acf6be2e429797Axel Davy};
527f565845a1f3ec871cccee88e2acf6be2e429797Axel Davy
53f3fa7e3068512dde55c3f4d7c2b599ab1d7fe67fPatrick Rudolphstruct threadpool *_mesa_threadpool_create(struct NineSwapChain9 *swapchain);
54f3fa7e3068512dde55c3f4d7c2b599ab1d7fe67fPatrick Rudolphvoid _mesa_threadpool_destroy(struct NineSwapChain9 *swapchain, struct threadpool *pool);
557f565845a1f3ec871cccee88e2acf6be2e429797Axel Davystruct threadpool_task *_mesa_threadpool_queue_task(struct threadpool *pool,
567f565845a1f3ec871cccee88e2acf6be2e429797Axel Davy                                                    threadpool_task_func func,
577f565845a1f3ec871cccee88e2acf6be2e429797Axel Davy                                                    void *data);
587f565845a1f3ec871cccee88e2acf6be2e429797Axel Davyvoid _mesa_threadpool_wait_for_task(struct threadpool *pool,
597f565845a1f3ec871cccee88e2acf6be2e429797Axel Davy                                    struct threadpool_task **task);
60f3fa7e3068512dde55c3f4d7c2b599ab1d7fe67fPatrick Rudolph#endif
61