Skip to content

Commit

Permalink
proccontrol: move thread sync to linux_process, and count neonatal
Browse files Browse the repository at this point in the history
  • Loading branch information
cuviper committed Nov 10, 2016
1 parent f1fa9ef commit 897f0c4
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 35 deletions.
3 changes: 2 additions & 1 deletion proccontrol/src/int_process.h
Expand Up @@ -273,7 +273,8 @@ class int_process

bool attachThreads(bool &found_new_threads);
bool attachThreads();
bool attachThreadsSync();
virtual bool plat_attachThreadsSync();

virtual async_ret_t post_attach(bool wasDetached, std::set<response::ptr> &aresps);
async_ret_t initializeAddressSpace(std::set<response::ptr> &async_responses);

Expand Down
38 changes: 38 additions & 0 deletions proccontrol/src/linux.C
Expand Up @@ -1110,6 +1110,44 @@ bool linux_process::plat_attach(bool, bool &)
return true;
}

// Attach any new threads and synchronize, until there are no new threads
bool linux_process::plat_attachThreadsSync()
{
while (true) {
bool found_new_threads = false;

ProcPool()->condvar()->lock();
bool result = attachThreads(found_new_threads);
if (found_new_threads)
ProcPool()->condvar()->broadcast();
ProcPool()->condvar()->unlock();

if (!result) {
pthrd_printf("Failed to attach to threads in %d\n", pid);
setLastError(err_internal, "Could not get threads during attach\n");
return false;
}

if (!found_new_threads)
return true;

while (Counter::processCount(Counter::NeonatalThreads, this) > 0) {
bool proc_exited = false;
pthrd_printf("Waiting for neonatal threads in process %d\n", pid);
result = waitAndHandleForProc(true, this, proc_exited);
if (!result) {
perr_printf("Internal error calling waitAndHandleForProc on %d\n", getPid());
return false;
}
if (proc_exited) {
perr_printf("Process exited while waiting for user thread stop, erroring\n");
setLastError(err_exited, "Process exited while thread being stopped.\n");
return false;
}
}
}
}

bool linux_process::plat_attachWillTriggerStop() {
char procName[64];
char cmd[256];
Expand Down
1 change: 1 addition & 0 deletions proccontrol/src/linux.h
Expand Up @@ -111,6 +111,7 @@ class linux_process : public sysv_process, public unix_process, public thread_db
virtual bool plat_create();
virtual bool plat_create_int();
virtual bool plat_attach(bool allStopped, bool &);
virtual bool plat_attachThreadsSync();
virtual bool plat_attachWillTriggerStop();
virtual bool plat_forked();
virtual bool plat_execed();
Expand Down
43 changes: 9 additions & 34 deletions proccontrol/src/process.C
Expand Up @@ -257,41 +257,16 @@ bool int_process::attachThreads()
return attachThreads(found_new_threads);
}

// Attach any new threads and synchronize, until there are no new threads
bool int_process::attachThreadsSync()
bool int_process::plat_attachThreadsSync()
{
while (true) {
bool found_new_threads = false;

ProcPool()->condvar()->lock();
bool result = attachThreads(found_new_threads);
if (found_new_threads)
ProcPool()->condvar()->broadcast();
ProcPool()->condvar()->unlock();

if (!result) {
pthrd_printf("Failed to attach to threads in %d\n", pid);
setLastError(err_internal, "Could not get threads during attach\n");
return false;
}

if (!found_new_threads)
return true;

pthrd_printf("Wait again for attach from process %d\n", pid);
bool proc_exited = false;
result = waitAndHandleForProc(true, this, proc_exited);
if (!result) {
perr_printf("Internal error calling waitAndHandleForProc on %d\n", getPid());
setLastError(err_internal, "Error while calling waitAndHandleForProc for attached threads\n");
return false;
}
if (proc_exited) {
perr_printf("Process exited while waiting for user thread stop, erroring\n");
setLastError(err_exited, "Process exited while thread being stopped.\n");
return false;
}
// By default, platforms just call the idempotent attachThreads().
// Some platforms may override, e.g. Linux should sync with all threads.
if (!attachThreads()) {
pthrd_printf("Failed to attach to threads in %d\n", pid);
setLastError(err_internal, "Could not get threads during attach\n");
return false;
}
return true;
}

bool int_process::attach(int_processSet *ps, bool reattach)
Expand Down Expand Up @@ -488,7 +463,7 @@ bool int_process::attach(int_processSet *ps, bool reattach)
int_process *proc = *i;
if (proc->getState() == errorstate)
continue;
bool result = proc->attachThreadsSync();
bool result = proc->plat_attachThreadsSync();
if (!result) {
pthrd_printf("Failed to attach to threads in %d--now an error\n", proc->pid);
procs.erase(i++);
Expand Down

0 comments on commit 897f0c4

Please sign in to comment.