spb/forge Public MIT
Forge — LLM training from scratch in pure C++20 + Metal on Apple Silicon.
C++ 61.2%
C 23%
Python 7.6%
TeX 7.2%
CMake 1.1%
1//-------------------------------------------------------------------------------------------------------------------------------------------------------------2//3// Metal/MTLEvent.hpp4//5// Copyright 2020-2025 Apple Inc.6//7// Licensed under the Apache License, Version 2.0 (the "License");8// you may not use this file except in compliance with the License.9// You may obtain a copy of the License at10//11// http://www.apache.org/licenses/LICENSE-2.012//13// Unless required by applicable law or agreed to in writing, software14// distributed under the License is distributed on an "AS IS" BASIS,15// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.16// See the License for the specific language governing permissions and17// limitations under the License.18//19//-------------------------------------------------------------------------------------------------------------------------------------------------------------2021#pragma once2223#include "../Foundation/Foundation.hpp"24#include "MTLDefines.hpp"25#include "MTLHeaderBridge.hpp"26#include "MTLPrivate.hpp"27#include <cstdint>28#include <dispatch/dispatch.h>2930#include <cstdint>31#include <functional>3233namespace MTL34{35class Device;36class SharedEvent;37class SharedEventHandle;38class SharedEventListener;3940using SharedEventNotificationBlock = void (^)(SharedEvent* pEvent, std::uint64_t value);41using SharedEventNotificationFunction = std::function<void(SharedEvent* pEvent, std::uint64_t value)>;4243class Event : public NS::Referencing<Event>44{45public:46 Device* device() const;4748 NS::String* label() const;49 void setLabel(const NS::String* label);50};51class SharedEventListener : public NS::Referencing<SharedEventListener>52{53public:54 static SharedEventListener* alloc();5556 dispatch_queue_t dispatchQueue() const;5758 SharedEventListener* init();59 SharedEventListener* init(const dispatch_queue_t dispatchQueue);6061 static SharedEventListener* sharedListener();62};63class SharedEvent : public NS::Referencing<SharedEvent, Event>64{65public:66 SharedEventHandle* newSharedEventHandle();6768 void notifyListener(const MTL::SharedEventListener* listener, uint64_t value, const MTL::SharedEventNotificationBlock block);69 void notifyListener(const MTL::SharedEventListener* listener, uint64_t value, const MTL::SharedEventNotificationFunction& function);7071 void setSignaledValue(uint64_t signaledValue);72 uint64_t signaledValue() const;73 bool waitUntilSignaledValue(uint64_t value, uint64_t milliseconds);74};75class SharedEventHandle : public NS::SecureCoding<SharedEventHandle>76{77public:78 static SharedEventHandle* alloc();7980 SharedEventHandle* init();8182 NS::String* label() const;83};8485}86_MTL_INLINE MTL::Device* MTL::Event::device() const87{88 return Object::sendMessage<MTL::Device*>(this, _MTL_PRIVATE_SEL(device));89}9091_MTL_INLINE NS::String* MTL::Event::label() const92{93 return Object::sendMessage<NS::String*>(this, _MTL_PRIVATE_SEL(label));94}9596_MTL_INLINE void MTL::Event::setLabel(const NS::String* label)97{98 Object::sendMessage<void>(this, _MTL_PRIVATE_SEL(setLabel_), label);99}100101_MTL_INLINE MTL::SharedEventListener* MTL::SharedEventListener::alloc()102{103 return NS::Object::alloc<MTL::SharedEventListener>(_MTL_PRIVATE_CLS(MTLSharedEventListener));104}105106_MTL_INLINE dispatch_queue_t MTL::SharedEventListener::dispatchQueue() const107{108 return Object::sendMessage<dispatch_queue_t>(this, _MTL_PRIVATE_SEL(dispatchQueue));109}110111_MTL_INLINE MTL::SharedEventListener* MTL::SharedEventListener::init()112{113 return NS::Object::init<MTL::SharedEventListener>();114}115116_MTL_INLINE MTL::SharedEventListener* MTL::SharedEventListener::init(const dispatch_queue_t dispatchQueue)117{118 return Object::sendMessage<MTL::SharedEventListener*>(this, _MTL_PRIVATE_SEL(initWithDispatchQueue_), dispatchQueue);119}120121_MTL_INLINE MTL::SharedEventListener* MTL::SharedEventListener::sharedListener()122{123 return Object::sendMessage<MTL::SharedEventListener*>(_MTL_PRIVATE_CLS(MTLSharedEventListener), _MTL_PRIVATE_SEL(sharedListener));124}125126_MTL_INLINE MTL::SharedEventHandle* MTL::SharedEvent::newSharedEventHandle()127{128 return Object::sendMessage<MTL::SharedEventHandle*>(this, _MTL_PRIVATE_SEL(newSharedEventHandle));129}130131_MTL_INLINE void MTL::SharedEvent::notifyListener(const MTL::SharedEventListener* listener, uint64_t value, const MTL::SharedEventNotificationBlock block)132{133 Object::sendMessage<void>(this, _MTL_PRIVATE_SEL(notifyListener_atValue_block_), listener, value, block);134}135136_MTL_INLINE void MTL::SharedEvent::notifyListener(const MTL::SharedEventListener* listener, uint64_t value, const MTL::SharedEventNotificationFunction& function)137{138 __block MTL::SharedEventNotificationFunction callback = function;139 notifyListener(listener, value, ^void(SharedEvent* pEvent, std::uint64_t innerValue) { callback(pEvent, innerValue); });140}141142_MTL_INLINE void MTL::SharedEvent::setSignaledValue(uint64_t signaledValue)143{144 Object::sendMessage<void>(this, _MTL_PRIVATE_SEL(setSignaledValue_), signaledValue);145}146147_MTL_INLINE uint64_t MTL::SharedEvent::signaledValue() const148{149 return Object::sendMessage<uint64_t>(this, _MTL_PRIVATE_SEL(signaledValue));150}151152_MTL_INLINE bool MTL::SharedEvent::waitUntilSignaledValue(uint64_t value, uint64_t milliseconds)153{154 return Object::sendMessage<bool>(this, _MTL_PRIVATE_SEL(waitUntilSignaledValue_timeoutMS_), value, milliseconds);155}156157_MTL_INLINE MTL::SharedEventHandle* MTL::SharedEventHandle::alloc()158{159 return NS::Object::alloc<MTL::SharedEventHandle>(_MTL_PRIVATE_CLS(MTLSharedEventHandle));160}161162_MTL_INLINE MTL::SharedEventHandle* MTL::SharedEventHandle::init()163{164 return NS::Object::init<MTL::SharedEventHandle>();165}166167_MTL_INLINE NS::String* MTL::SharedEventHandle::label() const168{169 return Object::sendMessage<NS::String*>(this, _MTL_PRIVATE_SEL(label));170}171