Skip to content

Commit

Permalink
(vmware#500) Move hook-related global vars to task_system{} struct.
Browse files Browse the repository at this point in the history
This commit removes the dependency of task system structures on global
variables declared in task.c . The hook-related variables are now
moved as members of the task_system{} struct. This removes accessing
potentially stale values when task-system is destroyed and re-created.
Also, TASK_MAX_HOOKS is now decreased from 8 to 4. This change largely
has no functional impact, and is mostly a test-stabilization fix.
  • Loading branch information
gapisback committed Mar 14, 2023
1 parent 4b6e0b1 commit 2ea30f4
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 17 deletions.
26 changes: 10 additions & 16 deletions src/task.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,6 @@

#include "poison.h"

#define MAX_HOOKS (8)

int hook_init_done = 0;
static int num_hooks = 0;
static task_hook hooks[MAX_HOOKS];

const char *task_type_name[] = {"TASK_TYPE_INVALID",
"TASK_TYPE_MEMTABLE",
"TASK_TYPE_NORMAL"};
Expand Down Expand Up @@ -148,13 +142,13 @@ task_get_max_tid(task_system *ts)
****************************************/

static platform_status
task_register_hook(task_hook newhook)
task_register_hook(task_system *ts, task_hook newhook)
{
int my_hook_idx = __sync_fetch_and_add(&num_hooks, 1);
if (my_hook_idx >= MAX_HOOKS) {
int my_hook_idx = __sync_fetch_and_add(&ts->num_hooks, 1);
if (my_hook_idx >= TASK_MAX_HOOKS) {
return STATUS_LIMIT_EXCEEDED;
}
hooks[my_hook_idx] = newhook;
ts->hooks[my_hook_idx] = newhook;

return STATUS_OK;
}
Expand All @@ -171,19 +165,19 @@ task_system_io_register_thread(task_system *ts)
* __attribute__((constructor)) works.
*/
static void
register_standard_hooks(void)
register_standard_hooks(task_system *ts)
{
// hooks need to be initialized only once.
if (__sync_fetch_and_add(&hook_init_done, 1) == 0) {
task_register_hook(task_system_io_register_thread);
if (__sync_fetch_and_add(&ts->hook_init_done, 1) == 0) {
task_register_hook(ts, task_system_io_register_thread);
}
}

static void
task_run_thread_hooks(task_system *ts)
{
for (int i = 0; i < num_hooks; i++) {
hooks[i](ts);
for (int i = 0; i < ts->num_hooks; i++) {
ts->hooks[i](ts);
}
}

Expand Down Expand Up @@ -872,7 +866,7 @@ task_system_create(platform_heap_id hid,
task_init_tid_bitmask(&ts->tid_bitmask);

// task initialization
register_standard_hooks();
register_standard_hooks(ts);

// Ensure that the main thread gets registered and init'ed first before
// any background threads are created. (Those may grab their own tids.).
Expand Down
8 changes: 7 additions & 1 deletion src/task.h
Original file line number Diff line number Diff line change
Expand Up @@ -89,12 +89,14 @@ task_system_config_init(task_system_config *task_cfg,
uint64 scratch_size);


#define TASK_MAX_HOOKS (4)

/*
* ----------------------------------------------------------------------
* Splinter specific state that gets created during initialization in
* splinter_system_init(). Contains global state for splinter such as the
* init thread, init thread's scratch memory, thread_id counter and an array
* of all the threads, which acts like a map that is accessed by thread id
* of all the threads, which acts like a map that is accessed by thread ID
* to get the thread pointer.
*
* This structure is passed around like an opaque structure to all the
Expand All @@ -120,6 +122,10 @@ struct task_system {
void *thread_scratch[MAX_THREADS];
// task groups
task_group group[NUM_TASK_TYPES];

int hook_init_done;
int num_hooks;
task_hook hooks[TASK_MAX_HOOKS];
};

platform_status
Expand Down

0 comments on commit 2ea30f4

Please sign in to comment.