MCPcopy Create free account
hub / github.com/MegEngine/MegEngine / callback

Method callback

src/core/impl/comp_node/comp_node.cpp:316–400  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

314//! environments that do not provide large alignment support.
315#if defined(MGB_MAX_SECTION_ALIGNMENT) && MGB_MAX_SECTION_ALIGNMENT < 64
316struct comp_node_detail::DepedentObjList::StaticInfo {
317#else
318// use a large alignment to avoid cache line pollution
319struct alignas(64) comp_node_detail::DepedentObjList::StaticInfo {
320#endif
321 Spinlock lock;
322 DepedentObjList* head;
323};
324comp_node_detail::DepedentObjList::StaticInfo
325 comp_node_detail::DepedentObjList::sm_info;
326
327class comp_node_detail::DepedentObjList::Sentinel final
328 : public comp_node_detail::DepedentObjList {
329 std::shared_ptr<void> callback() override { return {}; }
330
331public:
332 Sentinel() { init_list(); }
333
334 void init_list() {
335 sm_info.head = this;
336 m_next = m_prev = this;
337 }
338
339 static Sentinel* get() {
340 // no need to delete; use static storage to avoid its dtor being invoked
341 static std::aligned_storage_t<sizeof(Sentinel), alignof(Sentinel)> storage;
342 static Sentinel* ptr = new (&storage) Sentinel{};
343 return ptr;
344 }
345};
346
347void comp_node_detail::DepedentObjList::add(DepedentObjList* ptr) {
348 MGB_LOCK_GUARD(sm_info.lock);
349 // if this becomes slow (which I do not think is likely to happen), we can
350 // try a lock-free list implementation
351 Sentinel::get();
352 auto a = sm_info.head, b = a->m_next;
353 // insert and delete from head, so items added last can be deleted first
354 link(a, ptr);
355 link(ptr, b);
356}
357
358void comp_node_detail::DepedentObjList::remove(DepedentObjList* ptr) {
359 if (ptr->m_prev) {
360 MGB_LOCK_GUARD(sm_info.lock);
361 link(ptr->m_prev, ptr->m_next);
362 }
363}
364
365void comp_node_detail::DepedentObjList::invoke_callback_and_clean() {
366 SmallVector<std::shared_ptr<void>> refholds;
367 {
368 MGB_LOCK_GUARD(sm_info.lock);
369 auto st = Sentinel::get();
370 for (DepedentObjList *i = st->m_next, *inext; i != st; i = inext) {
371 inext = i->m_next;
372 i->m_prev = i->m_next = nullptr;
373 auto ref = i->callback();

Calls

no outgoing calls

Tested by

no test coverage detected