Skip to content

Commit

Permalink
Merge branch 'fix-multi-init-intrum' into 'master'
Browse files Browse the repository at this point in the history
Fix nested init instrumentation

Closes #20

See merge request nos-v/nos-v!98
  • Loading branch information
dave96 committed Feb 13, 2024
2 parents 1ecf7d0 + 3d12f27 commit 6a63fd4
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 8 deletions.
2 changes: 1 addition & 1 deletion configure.ac
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

# nOS-V Version and Copyright

m4_define([nosv_version], [2.1.1])
m4_define([nosv_version], [2.1.2])
m4_define([nosv_license], ["GPL3"])
m4_define([nosv_copyright], ["2021-2023 Barcelona Supercomputing Center (BSC)"])

Expand Down
14 changes: 12 additions & 2 deletions src/bootstrap.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
#include "system/tasks.h"

__internal int32_t rt_refcount = 0;
__internal thread_local int32_t th_refcount = 0;
__internal bool rt_initialized = false;
__internal pthread_mutex_t rt_mutex = PTHREAD_MUTEX_INITIALIZER;

Expand All @@ -48,8 +49,12 @@ static int nosv_init_impl(void)
task_affinity_init();

// Mark as initialized
assert(th_refcount == 0);
th_refcount++;
rt_initialized = true;
} else {
} else if (++th_refcount == 1 && !nosv_self()) {
// Emit instrumentation if the current thread is the first time
// that this thread calls nosv_init and it is not attached.
assert(rt_initialized);

instr_thread_init();
Expand Down Expand Up @@ -82,9 +87,14 @@ static int nosv_shutdown_impl(void)
locality_shutdown();
config_free();

assert(th_refcount == 1);
th_refcount--;

instr_thread_end();
instr_proc_fini();
} else {
} else if (--th_refcount == 0 && !nosv_self()) {
// Emit instrumentation if the current thread is the last thread
// calling nosv_shutdown and it is not attached.
instr_thread_end();
}
return NOSV_SUCCESS;
Expand Down
64 changes: 59 additions & 5 deletions tests/basic/init-multicall.c
Original file line number Diff line number Diff line change
@@ -1,24 +1,78 @@
/*
This file is part of nOS-V and is licensed under the terms contained in the COPYING file.
Copyright (C) 2023 Barcelona Supercomputing Center (BSC)
Copyright (C) 2023-2024 Barcelona Supercomputing Center (BSC)
*/

#include "test.h"

#include <nosv.h>
#include <string.h>

int main() {
test_t test;
test_t test;

test_init(&test, 8);
void *thread(void *arg)
{
int err;
nosv_task_t task;
intptr_t attach = (intptr_t) arg;

int err = nosv_init();
if (attach)
CHECK(nosv_attach(&task, NULL, "thread", NOSV_ATTACH_NONE));

err = nosv_init();
test_check(&test, !err, "Valid nosv_init (1): initialize");

err = nosv_init();
test_check(&test, !err, "Valid nosv_init (1): initialize");

err = nosv_shutdown();
test_check(&test, !err, "Valid nosv_shutdown (1)");

err = nosv_shutdown();
test_check(&test, !err, "Valid nosv_shutdown (1)");

if (attach)
CHECK(nosv_detach(NOSV_DETACH_NONE));

return NULL;
}

int main()
{
int err;
pthread_t pthread;

test_init(&test, 16);

err = nosv_init();
test_check(&test, !err, "Valid nosv_init (1): initialize");

err = nosv_init();
test_check(&test, !err, "Valid nosv_init (2)");

err = pthread_create(&pthread, NULL, thread, NULL);
if (err) {
fprintf(stderr, "Error: pthread_create: %s\n", strerror(err));
return 1;
}
err = pthread_join(pthread, NULL);
if (err) {
fprintf(stderr, "Error: pthread_join: %s\n", strerror(err));
return 1;
}

err = pthread_create(&pthread, NULL, thread, (void *) 1);
if (err) {
fprintf(stderr, "Error: pthread_create: %s\n", strerror(err));
return 1;
}
err = pthread_join(pthread, NULL);
if (err) {
fprintf(stderr, "Error: pthread_join: %s\n", strerror(err));
return 1;
}

err = nosv_shutdown();
test_check(&test, !err, "Valid nosv_shutdown (1)");

Expand Down

0 comments on commit 6a63fd4

Please sign in to comment.