summaryrefslogtreecommitdiff
path: root/src/api/api_event.cpp
blob: 6e114b2fac88651bc2235feb7bc161ed4f371fa2 (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
#include <CL/cl.h>

#include <core/commandqueue.h>
#include <core/events.h>
#include <core/context.h>

// Event Object APIs
cl_int
clWaitForEvents(cl_uint             num_events,
                const cl_event *    event_list)
{
    if (!num_events || !event_list)
        return CL_INVALID_VALUE;

    // Check the events in the list
    cl_context global_ctx = 0;

    for (cl_uint i=0; i<num_events; ++i)
    {
        if (!event_list[i]->isA(Coal::Object::T_Event))
            return CL_INVALID_EVENT;

        if (event_list[i]->status() < 0)
            return CL_EXEC_STATUS_ERROR_FOR_EVENTS_IN_WAIT_LIST;

        cl_context evt_ctx = (cl_context)event_list[i]->parent()->parent();

        if (global_ctx == 0)
            global_ctx = evt_ctx;
        else if (global_ctx != evt_ctx)
            return CL_INVALID_CONTEXT;
    }

    // Wait for the events
    for (cl_uint i=0; i<num_events; ++i)
    {
        event_list[i]->waitForStatus(Coal::Event::Complete);
    }

    return CL_SUCCESS;
}

cl_int
clGetEventInfo(cl_event         event,
               cl_event_info    param_name,
               size_t           param_value_size,
               void *           param_value,
               size_t *         param_value_size_ret)
{
    if (!event->isA(Coal::Object::T_Event))
        return CL_INVALID_EVENT;

    return event->info(param_name, param_value_size, param_value,
                       param_value_size_ret);
}

cl_int
clSetEventCallback(cl_event     event,
                   cl_int       command_exec_callback_type,
                   void         (CL_CALLBACK *pfn_event_notify)(cl_event event,
                                                                cl_int exec_status,
                                                                void *user_data),
                   void *user_data)
{
    if (!event->isA(Coal::Object::T_Event))
        return CL_INVALID_EVENT;

    if (!pfn_event_notify || command_exec_callback_type != CL_COMPLETE)
        return CL_INVALID_VALUE;

    event->setCallback(command_exec_callback_type, pfn_event_notify, user_data);

    return CL_SUCCESS;
}

cl_int
clRetainEvent(cl_event event)
{
    if (!event->isA(Coal::Object::T_Event))
        return CL_INVALID_EVENT;

    event->reference();

    return CL_SUCCESS;
}

cl_int
clReleaseEvent(cl_event event)
{
    if (!event->isA(Coal::Object::T_Event))
        return CL_INVALID_EVENT;

    if (event->dereference())
    {
        event->freeDeviceData();
        delete event;
    }

    return CL_SUCCESS;
}

cl_event
clCreateUserEvent(cl_context    context,
                  cl_int *      errcode_ret)
{
    cl_int dummy_errcode;

    if (!errcode_ret)
        errcode_ret = &dummy_errcode;

    if (!context->isA(Coal::Object::T_Context))
    {
        *errcode_ret = CL_INVALID_CONTEXT;
        return 0;
    }

    *errcode_ret = CL_SUCCESS;

    Coal::UserEvent *command = new Coal::UserEvent(
        (Coal::Context *)context, errcode_ret
    );

    if (*errcode_ret != CL_SUCCESS)
    {
        delete command;
        return 0;
    }

    return (cl_event)command;
}

cl_int
clSetUserEventStatus(cl_event   event,
                     cl_int     execution_status)
{
    Coal::Event *command = (Coal::Event *)event;

    if (!command->isA(Coal::Object::T_Event) ||
        command->type() != Coal::Event::User)
        return CL_INVALID_EVENT;

    if (execution_status != CL_COMPLETE)
        return CL_INVALID_VALUE;

    if (command->status() != CL_SUBMITTED)
        return CL_INVALID_OPERATION;

    command->setStatus((Coal::Event::Status)execution_status);

    return CL_SUCCESS;
}