Skip to content

Instantly share code, notes, and snippets.

static atomic_int executed_count;
void my_dispatch_async(dispatch_queue_t queue, dispatch_block_t block) {
dispatch_block_t wrapped_block = ^{
++executed_count;
block();
};
struct Block_layout *old_block_layout = (__bridge struct Block_layout *)block;
if (old_block_layout->invoke == dispatch_block_special_invoke()) {
wrapped_block = dispatch_block_create(DISPATCH_BLOCK_INHERIT_QOS_CLASS, wrapped_block);
typedef void (*dispatch_f)(void*, ...);
dispatch_f dispatch_block_special_invoke()
{
static dispatch_once_t onceToken;
static dispatch_f f;
dispatch_once(&onceToken, ^{
f = (__bridge struct Block_layout *)dispatch_block_create(DISPATCH_BLOCK_INHERIT_QOS_CLASS, ^{})->invoke;
});
return f;
}
dispatch_block_t
_dispatch_block_create(dispatch_block_flags_t flags, voucher_t voucher,
pthread_priority_t pri, dispatch_block_t block)
{
struct dispatch_block_private_data_s dbpds(flags, voucher, pri, block);
return reinterpret_cast<dispatch_block_t>(_dispatch_Block_copy(^{
// Capture stack object: invokes copy constructor (17094902)
(void)dbpds;
_dispatch_block_invoke_direct(&dbpds);
}));
// ...
struct Block_descriptor_1 {
unsigned long int reserved;
unsigned long int size;
};
// ...
struct Block_layout {
void *isa;
volatile int32_t flags; // contains ref count
int32_t reserved;
dispatch_block_t block = dispatch_block_create_with_qos_class(DISPATCH_BLOCK_ENFORCE_QOS_CLASS, QOS_USER_INITIATED, 0, old_block);
static atomic_int executed_count;
void my_dispatch_async(dispatch_queue_t queue, dispatch_block_t block) {
dispatch_async(queue, ^{
++executed_count;
block();
});
}
static co_decl_task(ab_t, _coroutine_a, (const int a, const int b),
static co_decl_task(ab_t, _coroutine_a, (const int a, const int b), private(
int i;
)) {
printf("param a %d\n", CO_P(a));
printf("param b %d\n", CO_P(b));
CO_V(i) = 2;
printf("%d\n", CO_V(i));
co_yield((ab_t){
.a = CO_V(i)
});
static void g(task_t* const task)
{
printf("start task %p\n", task);
taskyield(task);
printf("back to task %p to finish\n", task);
}
static void f(task_t* const task)
{
printf("create a new task to resume %p\n", task);
create a new task to resume 0x288d010
start task 0x289d410
done task 0x288d010
back to task 0x289d410 to finish