Skip to content

Instantly share code, notes, and snippets.

@sophiawisdom
Last active April 25, 2020 17:14
Show Gist options
  • Save sophiawisdom/1833115ce6f3d18421babcf1d3e9255c to your computer and use it in GitHub Desktop.
Save sophiawisdom/1833115ce6f3d18421babcf1d3e9255c to your computer and use it in GitHub Desktop.
namesCount before is 10
There are 2 refs for mach_thread_self and 2 refs for task_self_trap()
There are 1002 refs for mach_thread_self and 1002 refs for task_self_trap()
namesCount after is 10
new thread started
numKRefs for new thread is 2
sophiawisdom@Sophias-MacBook-Pro tweaks %
#include <mach/mach_traps.h>
#include <mach/mach_types.h>
#include <mach/port.h>
#include <pthread.h>
#define nil ((void *)0)
void test_newthread(void *arg) {
printf("new thread started\n");
int k = mach_thread_self();
int numKRefs;
mach_port_get_refs(mach_task_self(), k, MACH_PORT_RIGHT_SEND, &numKRefs);
printf("numKRefs for new thread is %d\n", numKRefs);
}
int main() {
mach_port_name_array_t names;
int namesCount;
mach_port_type_array_t types;
int typesCount;
mach_port_names(mach_task_self(), &names, &namesCount, &types, &typesCount);
printf("namesCount before is %d\n", namesCount);
int k = mach_thread_self();
int j = task_self_trap();
int numKRefs;
int numJRefs;
mach_port_get_refs(mach_task_self(), k, MACH_PORT_RIGHT_SEND, &numKRefs);
mach_port_get_refs(mach_task_self(), k, MACH_PORT_RIGHT_SEND, &numJRefs);
printf("There are %d refs for mach_thread_self and %d refs for task_self_trap()\n", numKRefs, numJRefs);
for (int i = 0; i < 1000; i++) {
k = mach_thread_self();
j = task_self_trap();
}
mach_port_get_refs(mach_task_self(), k, MACH_PORT_RIGHT_SEND, &numKRefs);
mach_port_get_refs(mach_task_self(), k, MACH_PORT_RIGHT_SEND, &numJRefs);
printf("There are %d refs for mach_thread_self and %d refs for task_self_trap()\n", numKRefs, numJRefs);
int newCount;
mach_port_names(mach_task_self(), &names, &newCount, &types, &typesCount);
printf("namesCount after is %d\n", namesCount);
pthread_t testThread;
pthread_create(&testThread, nil, test_newthread, nil);
usleep(1000000);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment