272
|
1 #include <libkern/OSAtomic.h>
|
|
2
|
278
|
3 #include "../context.h"
|
272
|
4
|
|
5 static void start_worker(Worker* worker);
|
|
6
|
277
|
7 Worker* createCPUWorker(struct Context* context, int id, Queue* queue, enum Code next) {
|
272
|
8 struct Worker* worker = new Worker();
|
|
9 struct CPUWorker* cpuWorker = new CPUWorker();
|
|
10 worker->worker = (union Data*)cpuWorker;
|
|
11 worker->tasks = queue;
|
|
12 cpuWorker->id = id;
|
|
13 worker->taskReceive = C_taskReceiveWorker;
|
|
14 worker->shutdown = C_shutdownWorker;
|
|
15 pthread_create(&worker->worker->CPUWorker.thread, NULL, (void*)&start_worker, worker);
|
|
16 return worker;
|
|
17 }
|
|
18
|
|
19 static void start_worker(Worker* worker) {
|
|
20 CPUWorker* cpuWorker = (CPUWorker*)worker->worker;
|
|
21 cpuWorker->context = NEW(struct Context);
|
|
22 initContext(cpuWorker->context);
|
|
23 Gearef(cpuWorker->context, Worker)->worker = (union Data*)worker;
|
|
24 goto meta(cpuWorker->context, C_taskReceiveWorker);
|
|
25 }
|
|
26
|
|
27 __code taskReceiveWorker(struct Worker* worker,struct Queue* queue) {
|
|
28 queue->queue = (union Data*)worker->tasks;
|
|
29 queue->next = C_getTask;
|
|
30 goto meta(context, worker->tasks->take);
|
|
31 }
|
|
32
|
|
33 __code taskReceiveWorker_stub(struct Context* context) {
|
|
34 CPUWorker* cpuWorker = (CPUWorker *)GearImpl(context, Worker, worker);
|
|
35 pthread_cond_wait(&cpuWorker->cond, &cpuWorker->mutex);
|
|
36 goto taskReceiveWorker(context, &Gearef(context, Worker)->worker->Worker, Gearef(context, Queue));
|
|
37 }
|
|
38
|
|
39 __code getTask(struct Worker* worker, struct Context* task) {
|
|
40 if (!task)
|
|
41 return; // end thread
|
|
42 task->worker = worker;
|
|
43 context->next = C_taskReceiveWorker; // set CG after task exec
|
|
44 goto meta(task, task->next);
|
|
45 }
|
|
46
|
|
47 __code getTask_stub(struct Context* context) {
|
|
48 Worker* worker = &Gearef(context,Worker)->worker->Worker;
|
|
49 struct Context* task = &Gearef(context, Queue)->data->Context;
|
|
50 goto getTask(context, worker, task);
|
|
51 }
|
|
52
|
|
53 #ifdef USE_CUDA
|
|
54 __code twiceGpu() {
|
|
55 cuMemcpyHtoDAsync(context,context,context,context->stream);
|
|
56 cuLaunchkanel();
|
|
57 cuMemcpyDtoHAsync();
|
|
58 }
|
|
59 #endif
|
|
60
|
|
61 __code shutdownWorker(struct CPUWorker* worker) {
|
|
62 }
|
|
63
|