summaryrefslogtreecommitdiff
path: root/src/cl_command_queue_enqueue.c
blob: 1848d50c0d3e04a8bb03d065ab8951b725abfb2b (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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
/*
 * Copyright © 2012 Intel Corporation
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library. If not, see <http://www.gnu.org/licenses/>.
 *
 * Author: He Junyan <junyan.he@intel.com>
 */

#include "cl_command_queue.h"
#include "cl_event_new.h"
#include "cl_alloc.h"
#include <stdio.h>

static void *worker_thread_function(void *Arg)
{
  cl_command_queue_enqueue_worker worker = (cl_command_queue_enqueue_worker)Arg;
  cl_command_queue queue = worker->queue;
  cl_event e;
  cl_uint cookie = -1;
  list_head *pos;
  list_head *n;
  list_head ready_list;
  cl_int exec_status;

  CL_OBJECT_LOCK(queue);

  while (1) {
    /* Must have locked here. */

    if (worker->quit == CL_TRUE) {
      CL_OBJECT_UNLOCK(queue);
      return NULL;
    }

    if (list_empty(&worker->enqueued_events)) {
      CL_OBJECT_WAIT_ON_COND(queue);
      continue;
    }

    /* The cookie will change when event status change or something happend to
       this command queue. If we already checked the event list and do not find
       anything to exec, we need to wait the cookie update, to avoid loop for ever. */
    if (cookie == worker->cookie) {
      CL_OBJECT_WAIT_ON_COND(queue);
      continue;
    }

    /* Here we hold lock to check event status, to avoid missing the status notify*/
    list_init(&ready_list);
    list_for_each_safe(pos, n, &worker->enqueued_events)
    {
      e = list_entry(pos, _cl_event, enqueue_node);
      if (cl_event_is_ready(e) <= CL_COMPLETE) {
        list_del(&e->enqueue_node);
        list_add_tail(&e->enqueue_node, &ready_list);
      }
    }

    if (list_empty(&ready_list)) { /* Nothing to do, just wait. */
      cookie = worker->cookie;
      continue;
    }

    /* Notify waiters, we change the event list. */
    CL_OBJECT_NOTIFY_COND(queue);

    worker->in_exec_status = CL_QUEUED;
    CL_OBJECT_UNLOCK(queue);

    /* Do the really job without lock.*/
    exec_status = CL_SUBMITTED;
    list_for_each_safe(pos, n, &ready_list)
    {
      e = list_entry(pos, _cl_event, enqueue_node);
      cl_event_exec(e, exec_status);
    }

    /* Notify all waiting for flush. */
    CL_OBJECT_LOCK(queue);
    worker->in_exec_status = CL_SUBMITTED;
    CL_OBJECT_NOTIFY_COND(queue);
    CL_OBJECT_UNLOCK(queue);

    for (exec_status = CL_RUNNING; exec_status >= CL_COMPLETE; exec_status--) {
      list_for_each_safe(pos, n, &ready_list)
      {
        e = list_entry(pos, _cl_event, enqueue_node);
        cl_event_exec(e, exec_status);
      }
    }

    /* Clear and delete all the events. */
    list_for_each_safe(pos, n, &ready_list)
    {
      e = list_entry(pos, _cl_event, enqueue_node);
      list_del(&e->enqueue_node);
      cl_event_delete(e);
    }

    CL_OBJECT_LOCK(queue);
    worker->in_exec_status = CL_COMPLETE;

    /* Notify finish waiters, we have done all the ready event. */
    CL_OBJECT_NOTIFY_COND(queue);
  }
}

LOCAL void
cl_command_queue_notify(cl_command_queue queue)
{
  if (CL_OBJECT_GET_REF(queue) < 1) {
    return;
  }

  assert(CL_OBJECT_IS_COMMAND_QUEUE(queue));
  CL_OBJECT_LOCK(queue);
  queue->worker.cookie++;
  CL_OBJECT_NOTIFY_COND(queue);
  CL_OBJECT_UNLOCK(queue);
}

LOCAL void
cl_command_queue_enqueue_event(cl_command_queue queue, cl_event event)
{
  CL_OBJECT_INC_REF(event);
  assert(CL_OBJECT_IS_COMMAND_QUEUE(queue));
  CL_OBJECT_LOCK(queue);
  assert(queue->worker.quit == CL_FALSE);
  assert(list_empty(&event->enqueue_node));
  list_add_tail(&event->enqueue_node, &queue->worker.enqueued_events);
  queue->worker.cookie++;
  CL_OBJECT_NOTIFY_COND(queue);
  CL_OBJECT_UNLOCK(queue);
}

LOCAL cl_int
cl_command_queue_init_enqueue(cl_command_queue queue)
{
  cl_command_queue_enqueue_worker worker = &queue->worker;
  worker->queue = queue;
  worker->quit = CL_FALSE;
  worker->in_exec_status = CL_COMPLETE;
  worker->cookie = 8;
  list_init(&worker->enqueued_events);

  if (pthread_create(&worker->tid, NULL, worker_thread_function, worker)) {
    DEBUGP(DL_ERROR, "Can not create worker thread for queue %p...\n", queue);
    return CL_OUT_OF_RESOURCES;
  }

  return CL_SUCCESS;
}

LOCAL void
cl_command_queue_destroy_enqueue(cl_command_queue queue)
{
  cl_command_queue_enqueue_worker worker = &queue->worker;

  assert(worker->queue == queue);
  assert(worker->quit == CL_FALSE);

  CL_OBJECT_LOCK(queue);
  worker->quit = 1;
  CL_OBJECT_NOTIFY_COND(queue);
  CL_OBJECT_UNLOCK(queue);

  pthread_join(worker->tid, NULL);

  /* We will wait for finish before destroy the command queue. */
  if (!list_empty(&worker->enqueued_events)) {
    DEBUGP(DL_WARNING, "There are still some enqueued works in the queue %p when this queue is destroyed,"
                       " this may cause very serious problems.\n",
           queue);
    assert(0);
  }
}

/* Note: Must call this function with queue's lock. */
LOCAL cl_event *
cl_command_queue_record_in_queue_events(cl_command_queue queue, cl_uint *list_num)
{
  int event_num = 0;
  list_head *pos;
  cl_command_queue_enqueue_worker worker = &queue->worker;
  cl_event *enqueued_list = NULL;
  int i;
  cl_event tmp_e = NULL;

  list_for_each(pos, &worker->enqueued_events)
  {
    event_num++;
  }
  assert(event_num > 0);

  enqueued_list = CL_CALLOC(event_num, sizeof(cl_event));
  assert(enqueued_list);

  i = 0;
  list_for_each(pos, &worker->enqueued_events)
  {
    tmp_e = list_entry(pos, _cl_event, enqueue_node);
    cl_event_add_ref(tmp_e); // Add ref temp avoid delete.
    enqueued_list[i] = tmp_e;
    i++;
  }
  assert(i == event_num);

  *list_num = event_num;
  return enqueued_list;
}

LOCAL cl_int
cl_command_queue_wait_flush(cl_command_queue queue)
{
  cl_command_queue_enqueue_worker worker = &queue->worker;
  cl_event *enqueued_list = NULL;
  cl_uint enqueued_num = 0;
  int i;

  CL_OBJECT_LOCK(queue);

  if (worker->quit) { // already destroy the queue?
    CL_OBJECT_UNLOCK(queue);
    return CL_INVALID_COMMAND_QUEUE;
  }

  if (!list_empty(&worker->enqueued_events)) {
    enqueued_list = cl_command_queue_record_in_queue_events(queue, &enqueued_num);
    assert(enqueued_num > 0);
    assert(enqueued_list);
  }

  while (worker->in_exec_status == CL_QUEUED) {
    CL_OBJECT_WAIT_ON_COND(queue);

    if (worker->quit) { // already destroy the queue?
      CL_OBJECT_UNLOCK(queue);
      return CL_INVALID_COMMAND_QUEUE;
    }
  }

  CL_OBJECT_UNLOCK(queue);

  /* Wait all event enter submitted status. */
  for (i = 0; i < enqueued_num; i++) {
    CL_OBJECT_LOCK(enqueued_list[i]);
    while (enqueued_list[i]->status > CL_SUBMITTED) {
      CL_OBJECT_WAIT_ON_COND(enqueued_list[i]);
    }
    CL_OBJECT_UNLOCK(enqueued_list[i]);
  }

  for (i = 0; i < enqueued_num; i++) {
    cl_event_delete(enqueued_list[i]);
  }
  if (enqueued_list)
    CL_FREE(enqueued_list);

  return CL_SUCCESS;
}

LOCAL cl_int
cl_command_queue_wait_finish(cl_command_queue queue)
{
  cl_command_queue_enqueue_worker worker = &queue->worker;
  cl_event *enqueued_list = NULL;
  cl_uint enqueued_num = 0;
  int i;

  CL_OBJECT_LOCK(queue);

  if (worker->quit) { // already destroy the queue?
    CL_OBJECT_UNLOCK(queue);
    return CL_INVALID_COMMAND_QUEUE;
  }

  if (!list_empty(&worker->enqueued_events)) {
    enqueued_list = cl_command_queue_record_in_queue_events(queue, &enqueued_num);
    assert(enqueued_num > 0);
    assert(enqueued_list);
  }

  while (worker->in_exec_status > CL_COMPLETE) {
    CL_OBJECT_WAIT_ON_COND(queue);

    if (worker->quit) { // already destroy the queue?
      CL_OBJECT_UNLOCK(queue);
      return CL_INVALID_COMMAND_QUEUE;
    }
  }

  CL_OBJECT_UNLOCK(queue);

  /* Wait all event enter submitted status. */
  for (i = 0; i < enqueued_num; i++) {
    CL_OBJECT_LOCK(enqueued_list[i]);
    while (enqueued_list[i]->status > CL_COMPLETE) {
      CL_OBJECT_WAIT_ON_COND(enqueued_list[i]);
    }
    CL_OBJECT_UNLOCK(enqueued_list[i]);
  }

  for (i = 0; i < enqueued_num; i++) {
    cl_event_delete(enqueued_list[i]);
  }
  if (enqueued_list)
    CL_FREE(enqueued_list);

  return CL_SUCCESS;
}