summaryrefslogtreecommitdiffstats
path: root/src/gallium/state_trackers/nine/threadpool.c
blob: cc62fd25799b479e81f9ea266566f5cd692f237c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
/*
 * Copyright © 2012 Intel Corporation
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice (including the next
 * paragraph) shall be included in all copies or substantial portions of the
 * Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
 * IN THE SOFTWARE.
 */

#include "swapchain9.h"
#include "surface9.h"
#include "device9.h"

#include "nine_helpers.h"
#include "nine_pipe.h"
#include "nine_dump.h"

#include "util/u_inlines.h"
#include "util/u_surface.h"
#include "hud/hud_context.h"
#include "state_tracker/drm_driver.h"

#include "os/os_thread.h"
#include "threadpool.h"

static void *
threadpool_worker(void *data)
{
    struct threadpool *pool = data;

    pthread_mutex_lock(&pool->m);

    while (!pool->shutdown) {
        struct threadpool_task *task;

        /* Block (dropping the lock) until new work arrives for us. */
        while (!pool->workqueue && !pool->shutdown)
            pthread_cond_wait(&pool->new_work, &pool->m);

        if (pool->shutdown) {
            pthread_mutex_unlock(&pool->m);
            return NULL;
        }

        /* Pull the first task from the list.  We don't free it -- it now lacks
         * a reference other than the worker creator's, whose responsibility it
         * is to call threadpool_wait_for_work() to free it.
         */
        task = pool->workqueue;
        pool->workqueue = task->next;

        /* Call the task's work func. */
        pthread_mutex_unlock(&pool->m);
        task->work(task->data);
        pthread_mutex_lock(&pool->m);
        task->finished = TRUE;
        pthread_cond_broadcast(&task->finish);
    }

    pthread_mutex_unlock(&pool->m);

    return NULL;
}

struct threadpool *
_mesa_threadpool_create(struct NineSwapChain9 *swapchain)
{
    struct threadpool *pool = calloc(1, sizeof(*pool));

    if (!pool)
        return NULL;

    pthread_mutex_init(&pool->m, NULL);
    pthread_cond_init(&pool->new_work, NULL);

    pool->wthread = NineSwapChain9_CreateThread(swapchain, threadpool_worker, pool);
    if (!pool->wthread) {
        /* using pthread as fallback */
        pthread_create(&pool->pthread, NULL, threadpool_worker, pool);
    }
    return pool;
}

void
_mesa_threadpool_destroy(struct NineSwapChain9 *swapchain, struct threadpool *pool)
{
    if (!pool)
        return;

    pthread_mutex_lock(&pool->m);
    pool->shutdown = TRUE;
    pthread_cond_broadcast(&pool->new_work);
    pthread_mutex_unlock(&pool->m);

    if (pool->wthread) {
        NineSwapChain9_WaitForThread(swapchain, pool->wthread);
    } else {
        pthread_join(pool->pthread, NULL);
    }

    pthread_cond_destroy(&pool->new_work);
    pthread_mutex_destroy(&pool->m);
    free(pool);
}

/**
 * Queues a request for the work function to be asynchronously executed by the
 * thread pool.
 *
 * The work func will get the "data" argument as its parameter -- any
 * communication between the caller and the work function will occur through
 * that.
 *
 * If there is an error, the work function is called immediately and NULL is
 * returned.
 */
struct threadpool_task *
_mesa_threadpool_queue_task(struct threadpool *pool,
                            threadpool_task_func work, void *data)
{
    struct threadpool_task *task, *previous;

    if (!pool) {
        work(data);
        return NULL;
    }

    task = calloc(1, sizeof(*task));
    if (!task) {
        work(data);
        return NULL;
    }

    task->work = work;
    task->data = data;
    task->next = NULL;
    pthread_cond_init(&task->finish, NULL);

    pthread_mutex_lock(&pool->m);

    if (!pool->workqueue) {
        pool->workqueue = task;
    } else {
        previous = pool->workqueue;
        while (previous && previous->next)
            previous = previous->next;

        previous->next = task;
    }
    pthread_cond_signal(&pool->new_work);
    pthread_mutex_unlock(&pool->m);

    return task;
}

/**
 * Blocks on the completion of the given task and frees the task.
 */
void
_mesa_threadpool_wait_for_task(struct threadpool *pool,
                               struct threadpool_task **task_handle)
{
    struct threadpool_task *task = *task_handle;

    if (!pool || !task)
        return;

    pthread_mutex_lock(&pool->m);
    while (!task->finished)
        pthread_cond_wait(&task->finish, &pool->m);
    pthread_mutex_unlock(&pool->m);

    pthread_cond_destroy(&task->finish);
    free(task);
    *task_handle = NULL;
}