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