Path: blob/21.2-virgl/src/gallium/frontends/clover/api/event.cpp
4572 views
//1// Copyright 2012 Francisco Jerez2//3// Permission is hereby granted, free of charge, to any person obtaining a4// copy of this software and associated documentation files (the "Software"),5// to deal in the Software without restriction, including without limitation6// the rights to use, copy, modify, merge, publish, distribute, sublicense,7// and/or sell copies of the Software, and to permit persons to whom the8// Software is furnished to do so, subject to the following conditions:9//10// The above copyright notice and this permission notice shall be included in11// all copies or substantial portions of the Software.12//13// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR14// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,15// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL16// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR17// OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,18// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR19// OTHER DEALINGS IN THE SOFTWARE.20//2122#include "api/util.hpp"23#include "core/event.hpp"2425using namespace clover;2627CLOVER_API cl_event28clCreateUserEvent(cl_context d_ctx, cl_int *r_errcode) try {29auto &ctx = obj(d_ctx);3031ret_error(r_errcode, CL_SUCCESS);32return desc(new soft_event(ctx, {}, false));3334} catch (error &e) {35ret_error(r_errcode, e);36return NULL;37}3839CLOVER_API cl_int40clSetUserEventStatus(cl_event d_ev, cl_int status) try {41auto &sev = obj<soft_event>(d_ev);4243if (status > 0)44return CL_INVALID_VALUE;4546if (sev.status() <= 0)47return CL_INVALID_OPERATION;4849if (status)50sev.abort(status);51else52sev.trigger();5354return CL_SUCCESS;5556} catch (error &e) {57return e.get();58}5960CLOVER_API cl_int61clWaitForEvents(cl_uint num_evs, const cl_event *d_evs) try {62auto evs = objs(d_evs, num_evs);6364for (auto &ev : evs) {65if (ev.context() != evs.front().context())66throw error(CL_INVALID_CONTEXT);6768if (ev.status() < 0)69throw error(CL_EXEC_STATUS_ERROR_FOR_EVENTS_IN_WAIT_LIST);70}7172// Create a temporary soft event that depends on all the events in73// the wait list74auto sev = create<soft_event>(evs.front().context(), evs, true);7576// ...and wait on it.77sev().wait();7879return CL_SUCCESS;8081} catch (error &e) {82return e.get();83}8485CLOVER_API cl_int86clGetEventInfo(cl_event d_ev, cl_event_info param,87size_t size, void *r_buf, size_t *r_size) try {88property_buffer buf { r_buf, size, r_size };89auto &ev = obj(d_ev);9091switch (param) {92case CL_EVENT_COMMAND_QUEUE:93buf.as_scalar<cl_command_queue>() = desc(ev.queue());94break;9596case CL_EVENT_CONTEXT:97buf.as_scalar<cl_context>() = desc(ev.context());98break;99100case CL_EVENT_COMMAND_TYPE:101buf.as_scalar<cl_command_type>() = ev.command();102break;103104case CL_EVENT_COMMAND_EXECUTION_STATUS:105buf.as_scalar<cl_int>() = ev.status();106break;107108case CL_EVENT_REFERENCE_COUNT:109buf.as_scalar<cl_uint>() = ev.ref_count();110break;111112default:113throw error(CL_INVALID_VALUE);114}115116return CL_SUCCESS;117118} catch (error &e) {119return e.get();120}121122CLOVER_API cl_int123clSetEventCallback(cl_event d_ev, cl_int type,124void (CL_CALLBACK *pfn_notify)(cl_event, cl_int, void *),125void *user_data) try {126auto &ev = obj(d_ev);127128if (!pfn_notify ||129(type != CL_COMPLETE && type != CL_SUBMITTED && type != CL_RUNNING))130throw error(CL_INVALID_VALUE);131132// Create a temporary soft event that depends on ev, with133// pfn_notify as completion action.134create<soft_event>(ev.context(), ref_vector<event> { ev }, true,135[=, &ev](event &) {136ev.wait();137pfn_notify(desc(ev), ev.status(), user_data);138});139140return CL_SUCCESS;141142} catch (error &e) {143return e.get();144}145146CLOVER_API cl_int147clRetainEvent(cl_event d_ev) try {148obj(d_ev).retain();149return CL_SUCCESS;150151} catch (error &e) {152return e.get();153}154155CLOVER_API cl_int156clReleaseEvent(cl_event d_ev) try {157if (obj(d_ev).release())158delete pobj(d_ev);159160return CL_SUCCESS;161162} catch (error &e) {163return e.get();164}165166CLOVER_API cl_int167clEnqueueMarker(cl_command_queue d_q, cl_event *rd_ev) try {168auto &q = obj(d_q);169170if (!rd_ev)171throw error(CL_INVALID_VALUE);172173*rd_ev = desc(new hard_event(q, CL_COMMAND_MARKER, {}));174175return CL_SUCCESS;176177} catch (error &e) {178return e.get();179}180181CLOVER_API cl_int182clEnqueueMarkerWithWaitList(cl_command_queue d_q, cl_uint num_deps,183const cl_event *d_deps, cl_event *rd_ev) try {184auto &q = obj(d_q);185auto deps = objs<wait_list_tag>(d_deps, num_deps);186187for (auto &ev : deps) {188if (ev.context() != q.context())189throw error(CL_INVALID_CONTEXT);190}191192// Create a hard event that depends on the events in the wait list:193// previous commands in the same queue are implicitly serialized194// with respect to it -- hard events always are.195auto hev = create<hard_event>(q, CL_COMMAND_MARKER, deps);196197ret_object(rd_ev, hev);198return CL_SUCCESS;199200} catch (error &e) {201return e.get();202}203204CLOVER_API cl_int205clEnqueueBarrier(cl_command_queue d_q) try {206obj(d_q);207208// No need to do anything, q preserves data ordering strictly.209210return CL_SUCCESS;211212} catch (error &e) {213return e.get();214}215216CLOVER_API cl_int217clEnqueueBarrierWithWaitList(cl_command_queue d_q, cl_uint num_deps,218const cl_event *d_deps, cl_event *rd_ev) try {219auto &q = obj(d_q);220auto deps = objs<wait_list_tag>(d_deps, num_deps);221222for (auto &ev : deps) {223if (ev.context() != q.context())224throw error(CL_INVALID_CONTEXT);225}226227// Create a hard event that depends on the events in the wait list:228// subsequent commands in the same queue will be implicitly229// serialized with respect to it -- hard events always are.230auto hev = create<hard_event>(q, CL_COMMAND_BARRIER, deps);231232ret_object(rd_ev, hev);233return CL_SUCCESS;234235} catch (error &e) {236return e.get();237}238239CLOVER_API cl_int240clEnqueueWaitForEvents(cl_command_queue d_q, cl_uint num_evs,241const cl_event *d_evs) try {242// The wait list is mandatory for clEnqueueWaitForEvents().243objs(d_evs, num_evs);244245return clEnqueueBarrierWithWaitList(d_q, num_evs, d_evs, NULL);246247} catch (error &e) {248return e.get();249}250251CLOVER_API cl_int252clGetEventProfilingInfo(cl_event d_ev, cl_profiling_info param,253size_t size, void *r_buf, size_t *r_size) try {254property_buffer buf { r_buf, size, r_size };255hard_event &hev = dynamic_cast<hard_event &>(obj(d_ev));256257if (hev.status() != CL_COMPLETE)258throw error(CL_PROFILING_INFO_NOT_AVAILABLE);259260switch (param) {261case CL_PROFILING_COMMAND_QUEUED:262buf.as_scalar<cl_ulong>() = hev.time_queued();263break;264265case CL_PROFILING_COMMAND_SUBMIT:266buf.as_scalar<cl_ulong>() = hev.time_submit();267break;268269case CL_PROFILING_COMMAND_START:270buf.as_scalar<cl_ulong>() = hev.time_start();271break;272273case CL_PROFILING_COMMAND_END:274case CL_PROFILING_COMMAND_COMPLETE:275buf.as_scalar<cl_ulong>() = hev.time_end();276break;277278default:279throw error(CL_INVALID_VALUE);280}281282return CL_SUCCESS;283284} catch (std::bad_cast &e) {285return CL_PROFILING_INFO_NOT_AVAILABLE;286287} catch (lazy<cl_ulong>::undefined_error &e) {288return CL_PROFILING_INFO_NOT_AVAILABLE;289290} catch (error &e) {291return e.get();292}293294CLOVER_API cl_int295clFinish(cl_command_queue d_q) try {296auto &q = obj(d_q);297298// Create a temporary hard event -- it implicitly depends on all299// the previously queued hard events.300auto hev = create<hard_event>(q, 0, ref_vector<event> {});301302// And wait on it.303hev().wait();304305return CL_SUCCESS;306307} catch (error &e) {308return e.get();309}310311312