Skip to content
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

Option to wait for init #8

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
56 changes: 51 additions & 5 deletions prof.c
Original file line number Diff line number Diff line change
Expand Up @@ -2507,9 +2507,10 @@ int utracy_client_disconnect(void) {

UTRACY_INTERNAL
#if defined(UTRACY_WINDOWS)
DWORD WINAPI utracy_server_thread_start(PVOID user) {
DWORD WINAPI utracy_server_thread_start(HANDLE profilerConnectedEventHandle) {
#elif defined(UTRACY_LINUX)
void *utracy_server_thread_start(void *user) {
(void)user; // TODO Linux startup blocking
#endif
(void) user;

Expand Down Expand Up @@ -2550,6 +2551,12 @@ void *utracy_server_thread_start(void *user) {
(void) utracy_client_disconnect();
continue;
}

#if defined(UTRACY_WINDOWS)
SetEvent(profilerConnectedEventHandle);
#else
// TODO Linux startup blocking
#endif
}
}

Expand All @@ -2558,7 +2565,13 @@ void *utracy_server_thread_start(void *user) {
if(1 == utracy.sock.connected) {
(void) utracy_client_disconnect();
}
} while(1);
} while (1);

#if defined(UTRACY_WINDOWS)
CloseHandle(profilerConnectedEventHandle);
#else
// TODO Linux startup blocking
#endif

#if defined(UTRACY_WINDOWS)
ExitThread(0);
Expand Down Expand Up @@ -2921,8 +2934,13 @@ char *UTRACY_WINDOWS_CDECL UTRACY_LINUX_CDECL init(int argc, char **argv) {
return "already initialized";
}

(void) UTRACY_MEMSET(&byond, 0, sizeof(byond));
(void) UTRACY_MEMSET(&utracy, 0, sizeof(utracy));
int block_start = 0;
if (argc > 0 && strcmp(argv[0], "block") == 0) {
block_start = 1;
}

(void)UTRACY_MEMSET(&byond, 0, sizeof(byond));
(void)UTRACY_MEMSET(&utracy, 0, sizeof(utracy));

utracy.info.init_begin = utracy_tsc();

Expand Down Expand Up @@ -3055,18 +3073,46 @@ char *UTRACY_WINDOWS_CDECL UTRACY_LINUX_CDECL init(int argc, char **argv) {
utracy.info.init_end = utracy_tsc();

#if defined(UTRACY_WINDOWS)
if(NULL == CreateThread(NULL, 0, utracy_server_thread_start, NULL, 0, NULL)) {
HANDLE profilerConnectedEvent = NULL;

if(block_start != 0) {
profilerConnectedEvent = CreateEvent(
NULL, // default security attributes
TRUE, // manual-reset event
FALSE, // initial state is nonsignaled
TEXT("ProfilerConnectedEvent")); // object name

if (profilerConnectedEvent == NULL) {
return "failed to allocate event handle";
}
}

if (NULL == CreateThread(NULL, 0, utracy_server_thread_start, profilerConnectedEvent, 0, NULL)) {
LOG_DEBUG_ERROR;
return "CreateThread failed";
}

if (block_start != 0) {
WaitForSingleObject(
profilerConnectedEvent,
INFINITE);
}

#elif defined(UTRACY_LINUX)
if (block_start != 0) {
// TODO Linux startup blocking
}

pthread_t thr;
if(0 != pthread_create(&thr, NULL, utracy_server_thread_start, NULL)) {
LOG_DEBUG_ERROR;
return "pthread_create failed";
}

if (block_start != 0) {
// TODO Linux startup blocking
}

#endif

initialized = 1;
Expand Down
3 changes: 2 additions & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ simply call `init` from `prof.dll` to begin collecting profile data and connect
if(UNIX) lib = "libprof.so"
else CRASH("unsupported platform")

var/init = call_ext(lib, "init")()
var/wait_for_profiler_attach = TRUE
var/init = wait_for_profiler_attach ? call_ext(lib, "init")() : call_ext(lib, "init")("block")
if("0" != init) CRASH("[lib] init error: [init]")

/world/New()
Expand Down