Skip to content

Add Subgraph tree record API #62

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Apr 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,26 @@ AGUniqueID AGSubgraphAddObserver(AGSubgraphRef cf_subgraph,
const void (*function)(const void * _Nullable context AG_SWIFT_CONTEXT) AG_SWIFT_CC(swift),
const void * _Nullable context);

AG_EXPORT
AG_REFINED_FOR_SWIFT
bool AGSubgraphShouldRecordTree(void) AG_SWIFT_NAME(getter:AGSubgraphRef.shouldRecordTree());

AG_EXPORT
AG_REFINED_FOR_SWIFT
void AGSubgraphSetShouldRecordTree(void) AG_SWIFT_NAME(AGSubgraphRef.setShouldRecordTree());

AG_EXPORT
AG_REFINED_FOR_SWIFT
void AGSubgraphBeginTreeElement(AGAttribute attribute, AGTypeID type, uint32_t flags);

AG_EXPORT
AG_REFINED_FOR_SWIFT
void AGSubgraphAddTreeValue(AGAttribute attribute, AGTypeID type, const char * key, uint32_t flags);

AG_EXPORT
AG_REFINED_FOR_SWIFT
void AGSubgraphEndTreeElement(AGAttribute attribute);

AG_EXTERN_C_END

AG_ASSUME_NONNULL_END
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -403,6 +403,12 @@ extension AttributeGraph.AGSubgraph {
public func forEach(_ flags: AGAttributeFlags, _ callback: (AGAttribute) -> Void)
}

extension AttributeGraph.AGSubgraph {
public static func beginTreeElement<Value>(value: AttributeGraph.Attribute<Value>, flags: Swift.UInt32)
public static func endTreeElement<Value>(value: AttributeGraph.Attribute<Value>)
public static func addTreeValue<Value>(_ value: AttributeGraph.Attribute<Value>, forKey key: Swift.UnsafePointer<Int8>, flags: Swift.UInt32)
}

// MARK: - Rule Related

// MARK: - Rule
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -403,6 +403,12 @@ extension AttributeGraph.AGSubgraph {
public func forEach(_ flags: AGAttributeFlags, _ callback: (AGAttribute) -> Void)
}

extension AttributeGraph.AGSubgraph {
public static func beginTreeElement<Value>(value: AttributeGraph.Attribute<Value>, flags: Swift.UInt32)
public static func endTreeElement<Value>(value: AttributeGraph.Attribute<Value>)
public static func addTreeValue<Value>(_ value: AttributeGraph.Attribute<Value>, forKey key: Swift.UnsafePointer<Int8>, flags: Swift.UInt32)
}

// MARK: - Rule Related

// MARK: - Rule
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,26 @@ AGUniqueID AGSubgraphAddObserver(AGSubgraphRef cf_subgraph,
const void (*function)(const void * _Nullable context AG_SWIFT_CONTEXT) AG_SWIFT_CC(swift),
const void * _Nullable context);

AG_EXPORT
AG_REFINED_FOR_SWIFT
bool AGSubgraphShouldRecordTree(void) AG_SWIFT_NAME(getter:AGSubgraphRef.shouldRecordTree());

AG_EXPORT
AG_REFINED_FOR_SWIFT
void AGSubgraphSetShouldRecordTree(void) AG_SWIFT_NAME(AGSubgraphRef.setShouldRecordTree());

AG_EXPORT
AG_REFINED_FOR_SWIFT
void AGSubgraphBeginTreeElement(AGAttribute attribute, AGTypeID type, uint32_t flags);

AG_EXPORT
AG_REFINED_FOR_SWIFT
void AGSubgraphAddTreeValue(AGAttribute attribute, AGTypeID type, const char * key, uint32_t flags);

AG_EXPORT
AG_REFINED_FOR_SWIFT
void AGSubgraphEndTreeElement(AGAttribute attribute);

AG_EXTERN_C_END

AG_ASSUME_NONNULL_END
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -403,6 +403,12 @@ extension AttributeGraph.AGSubgraph {
public func forEach(_ flags: AGAttributeFlags, _ callback: (AGAttribute) -> Void)
}

extension AttributeGraph.AGSubgraph {
public static func beginTreeElement<Value>(value: AttributeGraph.Attribute<Value>, flags: Swift.UInt32)
public static func endTreeElement<Value>(value: AttributeGraph.Attribute<Value>)
public static func addTreeValue<Value>(_ value: AttributeGraph.Attribute<Value>, forKey key: Swift.UnsafePointer<Int8>, flags: Swift.UInt32)
}

// MARK: - Rule Related

// MARK: - Rule
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -403,6 +403,12 @@ extension AttributeGraph.AGSubgraph {
public func forEach(_ flags: AGAttributeFlags, _ callback: (AGAttribute) -> Void)
}

extension AttributeGraph.AGSubgraph {
public static func beginTreeElement<Value>(value: AttributeGraph.Attribute<Value>, flags: Swift.UInt32)
public static func endTreeElement<Value>(value: AttributeGraph.Attribute<Value>)
public static func addTreeValue<Value>(_ value: AttributeGraph.Attribute<Value>, forKey key: Swift.UnsafePointer<Int8>, flags: Swift.UInt32)
}

// MARK: - Rule Related

// MARK: - Rule
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,26 @@ AGUniqueID AGSubgraphAddObserver(AGSubgraphRef cf_subgraph,
const void (*function)(const void * _Nullable context AG_SWIFT_CONTEXT) AG_SWIFT_CC(swift),
const void * _Nullable context);

AG_EXPORT
AG_REFINED_FOR_SWIFT
bool AGSubgraphShouldRecordTree(void) AG_SWIFT_NAME(getter:AGSubgraphRef.shouldRecordTree());

AG_EXPORT
AG_REFINED_FOR_SWIFT
void AGSubgraphSetShouldRecordTree(void) AG_SWIFT_NAME(AGSubgraphRef.setShouldRecordTree());

AG_EXPORT
AG_REFINED_FOR_SWIFT
void AGSubgraphBeginTreeElement(AGAttribute attribute, AGTypeID type, uint32_t flags);

AG_EXPORT
AG_REFINED_FOR_SWIFT
void AGSubgraphAddTreeValue(AGAttribute attribute, AGTypeID type, const char * key, uint32_t flags);

AG_EXPORT
AG_REFINED_FOR_SWIFT
void AGSubgraphEndTreeElement(AGAttribute attribute);

AG_EXTERN_C_END

AG_ASSUME_NONNULL_END
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -403,6 +403,12 @@ extension AttributeGraph.AGSubgraph {
public func forEach(_ flags: AGAttributeFlags, _ callback: (AGAttribute) -> Void)
}

extension AttributeGraph.AGSubgraph {
public static func beginTreeElement<Value>(value: AttributeGraph.Attribute<Value>, flags: Swift.UInt32)
public static func endTreeElement<Value>(value: AttributeGraph.Attribute<Value>)
public static func addTreeValue<Value>(_ value: AttributeGraph.Attribute<Value>, forKey key: Swift.UnsafePointer<Int8>, flags: Swift.UInt32)
}

// MARK: - Rule Related

// MARK: - Rule
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -403,6 +403,12 @@ extension AttributeGraph.AGSubgraph {
public func forEach(_ flags: AGAttributeFlags, _ callback: (AGAttribute) -> Void)
}

extension AttributeGraph.AGSubgraph {
public static func beginTreeElement<Value>(value: AttributeGraph.Attribute<Value>, flags: Swift.UInt32)
public static func endTreeElement<Value>(value: AttributeGraph.Attribute<Value>)
public static func addTreeValue<Value>(_ value: AttributeGraph.Attribute<Value>, forKey key: Swift.UnsafePointer<Int8>, flags: Swift.UInt32)
}

// MARK: - Rule Related

// MARK: - Rule
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -403,6 +403,12 @@ extension AttributeGraph.AGSubgraph {
public func forEach(_ flags: AGAttributeFlags, _ callback: (AGAttribute) -> Void)
}

extension AttributeGraph.AGSubgraph {
public static func beginTreeElement<Value>(value: AttributeGraph.Attribute<Value>, flags: Swift.UInt32)
public static func endTreeElement<Value>(value: AttributeGraph.Attribute<Value>)
public static func addTreeValue<Value>(_ value: AttributeGraph.Attribute<Value>, forKey key: Swift.UnsafePointer<Int8>, flags: Swift.UInt32)
}

// MARK: - Rule Related

// MARK: - Rule
Expand Down
20 changes: 20 additions & 0 deletions AG/Sources/Headers/AGSubgraph.h
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,26 @@ AGUniqueID AGSubgraphAddObserver(AGSubgraphRef cf_subgraph,
const void (*function)(const void * _Nullable context AG_SWIFT_CONTEXT) AG_SWIFT_CC(swift),
const void * _Nullable context);

AG_EXPORT
AG_REFINED_FOR_SWIFT
bool AGSubgraphShouldRecordTree(void) AG_SWIFT_NAME(getter:AGSubgraphRef.shouldRecordTree());

AG_EXPORT
AG_REFINED_FOR_SWIFT
void AGSubgraphSetShouldRecordTree(void) AG_SWIFT_NAME(AGSubgraphRef.setShouldRecordTree());

AG_EXPORT
AG_REFINED_FOR_SWIFT
void AGSubgraphBeginTreeElement(AGAttribute attribute, AGTypeID type, uint32_t flags);

AG_EXPORT
AG_REFINED_FOR_SWIFT
void AGSubgraphAddTreeValue(AGAttribute attribute, AGTypeID type, const char * key, uint32_t flags);

AG_EXPORT
AG_REFINED_FOR_SWIFT
void AGSubgraphEndTreeElement(AGAttribute attribute);

AG_EXTERN_C_END

AG_ASSUME_NONNULL_END
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -399,6 +399,12 @@ extension AttributeGraph.AGSubgraph {
public func forEach(_ flags: AGAttributeFlags, _ callback: (AGAttribute) -> Void)
}

extension AttributeGraph.AGSubgraph {
public static func beginTreeElement<Value>(value: AttributeGraph.Attribute<Value>, flags: Swift.UInt32)
public static func endTreeElement<Value>(value: AttributeGraph.Attribute<Value>)
public static func addTreeValue<Value>(_ value: AttributeGraph.Attribute<Value>, forKey key: Swift.UnsafePointer<Int8>, flags: Swift.UInt32)
}

// MARK: - Rule Related

// MARK: - Rule
Expand Down
22 changes: 15 additions & 7 deletions Sources/OpenGraph/Graph/OGSubgraph.swift
Original file line number Diff line number Diff line change
Expand Up @@ -30,17 +30,25 @@ extension OGSubgraph {
public func forEach(_ flags: OGAttributeFlags, _ callback: (OGAttribute) -> Void) {
OGSubgraph.apply(self, flags: flags, callback: callback)
}

/*public*/ static func beginTreeElement<A>(value _: Attribute<A>, flags _: UInt32) {
fatalError("TODO")
}

extension OGSubgraph {
public static func beginTreeElement<Value>(value: Attribute<Value>, flags: UInt32) {
if shouldRecordTree {
__OGSubgraphBeginTreeElement(value.identifier, OGTypeID(Value.self), flags)
}
}

/*public*/ static func addTreeValue<A>(_: Attribute<A>, forKey _: UnsafePointer<Int8>, flags _: UInt32) {
fatalError("TODO")
public static func addTreeValue<Value>(_ value: Attribute<Value>, forKey key: UnsafePointer<Int8>, flags: UInt32) {
if shouldRecordTree {
__OGSubgraphAddTreeValue(value.identifier, OGTypeID(Value.self), key, flags)
}
}

/*public*/ static func endTreeElement<A>(value _: Attribute<A>) {
fatalError("TODO")
public static func endTreeElement<Value>(value: Attribute<Value>) {
if shouldRecordTree {
__OGSubgraphEndTreeElement(value.identifier)
}
}
}

Expand Down
73 changes: 62 additions & 11 deletions Sources/_OpenGraph/Graph/OGSubgraph.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,13 @@
#include "OGSubgraph.h"
#include "OGGraph.h"
#include "Subgraph.hpp"
#include "OGGraphContext.h"
#include "../Util/assert.hpp"
#include "../Util/env.hpp"
#include <pthread.h>
#include "OGGraphContext.h"
#if !OG_TARGET_OS_WASI
#include <dispatch/dispatch.h>
#endif

namespace {
CFRuntimeClass &subgraph_type_id() {
Expand Down Expand Up @@ -68,33 +72,34 @@ OGSubgraphRef OGSubgraphCreate2(OGGraphRef cf_graph, OGAttribute attribute) {
}

_Nullable OGSubgraphRef OGSubgraphGetCurrent() {
OG::Subgraph* subgraph = (OG::Subgraph*)pthread_getspecific(OG::Subgraph::current_key());
OG::Subgraph *subgraph = OG::Subgraph::get_current();
if (subgraph == nullptr) {
return nullptr;
}
return subgraph->to_cf();
}

void OGSubgraphSetCurrent(_Nullable OGSubgraphRef cf_subgraph) {
OG::Subgraph* oldSubgraph = (OG::Subgraph*)pthread_getspecific(OG::Subgraph::current_key());
OG::Subgraph *old_subgraph = OG::Subgraph::get_current();
if (cf_subgraph == nullptr) {
pthread_setspecific(OG::Subgraph::current_key(), nullptr);
OG::Subgraph::set_current(nullptr);
} else {
pthread_setspecific(OG::Subgraph::current_key(), cf_subgraph->subgraph);
if (cf_subgraph->subgraph != nullptr) {
OG::Subgraph *subgraph = cf_subgraph->subgraph;
OG::Subgraph::set_current(subgraph);
if (subgraph != nullptr) {
CFRetain(cf_subgraph);
}
}
if (oldSubgraph != nullptr) {
OGSubgraphRef cf_oldSubgraph = oldSubgraph->to_cf();
if (cf_oldSubgraph) {
CFRelease(cf_oldSubgraph);
if (old_subgraph != nullptr) {
OGSubgraphRef old_cf_Subgraph = old_subgraph->to_cf();
if (old_cf_Subgraph) {
CFRelease(old_cf_Subgraph);
}
}
}

OGGraphContextRef OGSubgraphGetCurrentGraphContext() {
OG::Subgraph* subgraph = (OG::Subgraph*)pthread_getspecific(OG::Subgraph::current_key());
OG::Subgraph *subgraph = OG::Subgraph::get_current();
if (subgraph == nullptr) {
return nullptr;
}
Expand Down Expand Up @@ -162,3 +167,49 @@ OGUniqueID OGSubgraphAddObserver(OGSubgraphRef cf_subgraph,
}
return subgraph->add_observer(OG::ClosureFunction<void>(function, context));
}

#if !OG_TARGET_OS_WASI
static bool should_record_tree;
static dispatch_once_t should_record_tree_once;

void init_should_record_tree(void *) {
should_record_tree = OG::get_env("OG_TREE") != 0;
}
#endif

bool OGSubgraphShouldRecordTree() {
#if !OG_TARGET_OS_WASI
dispatch_once_f(&should_record_tree_once, NULL, init_should_record_tree);
return should_record_tree;
#else
return false;
#endif
}

void OGSubgraphSetShouldRecordTree() {
#if !OG_TARGET_OS_WASI
dispatch_once_f(&should_record_tree_once, NULL, init_should_record_tree);
should_record_tree = true;
#endif
}

void OGSubgraphBeginTreeElement(OGAttribute attribute, OGTypeID type, uint32_t flags) {
OG::Subgraph * subgraph = OG::Subgraph::get_current();
if (subgraph) {
subgraph->begin_tree(attribute, type, flags);
}
}

void OGSubgraphAddTreeValue(OGAttribute attribute, OGTypeID type, const char * key, uint32_t flags) {
OG::Subgraph * subgraph = OG::Subgraph::get_current();
if (subgraph) {
subgraph->add_tree_value(attribute, type, key, flags);
}
}

void OGSubgraphEndTreeElement(OGAttribute attribute) {
OG::Subgraph * subgraph = OG::Subgraph::get_current();
if (subgraph) {
subgraph->end_tree(attribute);
}
}
20 changes: 20 additions & 0 deletions Sources/_OpenGraph/Graph/OGSubgraph.h
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,26 @@ OGUniqueID OGSubgraphAddObserver(OGSubgraphRef cf_subgraph,
const void (*function)(const void * _Nullable context OG_SWIFT_CONTEXT) OG_SWIFT_CC(swift),
const void * _Nullable context);

OG_EXPORT
OG_REFINED_FOR_SWIFT
bool OGSubgraphShouldRecordTree(void) OG_SWIFT_NAME(getter:OGSubgraphRef.shouldRecordTree());

OG_EXPORT
OG_REFINED_FOR_SWIFT
void OGSubgraphSetShouldRecordTree(void) OG_SWIFT_NAME(OGSubgraphRef.setShouldRecordTree());

OG_EXPORT
OG_REFINED_FOR_SWIFT
void OGSubgraphBeginTreeElement(OGAttribute attribute, OGTypeID type, uint32_t flags);

OG_EXPORT
OG_REFINED_FOR_SWIFT
void OGSubgraphAddTreeValue(OGAttribute attribute, OGTypeID type, const char * key, uint32_t flags);

OG_EXPORT
OG_REFINED_FOR_SWIFT
void OGSubgraphEndTreeElement(OGAttribute attribute);

OG_EXTERN_C_END

OG_ASSUME_NONNULL_END
Expand Down
Loading