mergerfs/libfuse/lib/node.cpp

93 lines
1.3 KiB
C++
Raw Normal View History

#include "node.hpp"
2025-10-27 20:58:06 -05:00
#include <mutex>
2025-10-27 21:39:29 -05:00
#include <vector>
2025-10-27 20:58:06 -05:00
2025-10-25 18:21:18 -05:00
#include <cstddef>
#include <cstdlib>
2025-10-27 22:11:05 -05:00
#include "fmt/core.h"
#include <unistd.h>
2025-10-25 18:21:18 -05:00
struct stack_t
{
2025-10-25 18:21:18 -05:00
stack_t *next;
};
2025-10-27 21:40:22 -05:00
struct StackInfo
2025-10-27 21:39:29 -05:00
{
2025-10-27 21:46:04 -05:00
stack_t *stack;
bool *should_gc;
2025-10-27 21:39:29 -05:00
};
2025-10-27 21:00:38 -05:00
thread_local static stack_t *g_stack = NULL;
thread_local bool g_should_gc = false;
thread_local bool g_initialized = false;
2025-10-27 21:37:59 -05:00
static std::mutex g_mutex;
2025-10-27 21:40:18 -05:00
static std::vector<StackInfo> g_all_stacks;
2025-10-27 21:52:52 -05:00
node_t*
node_alloc()
{
2025-10-27 21:53:19 -05:00
if(g_initialized == false)
2025-10-27 21:41:56 -05:00
{
std::lock_guard<std::mutex> guard(g_mutex);
2025-10-27 21:50:10 -05:00
g_all_stacks.push_back({g_stack,&g_should_gc});
2025-10-27 21:50:28 -05:00
g_initialized = true;
2025-10-27 21:41:56 -05:00
}
2025-10-27 22:07:08 -05:00
if(g_should_gc)
{
int i;
stack_t *s;
i = 0;
s = g_stack;
while(s)
{
i++;
s = s->next;
}
2025-10-27 22:32:28 -05:00
fmt::print("{} {}\n",
2025-10-27 22:10:26 -05:00
gettid(),
i);
2025-10-27 22:07:08 -05:00
g_should_gc = false;
}
2025-10-25 18:21:18 -05:00
if(g_stack == NULL)
return (node_t*)calloc(1,sizeof(node_t));
2025-10-25 18:21:18 -05:00
node_t *node;
2025-10-25 18:21:18 -05:00
node = (node_t*)g_stack;
g_stack = g_stack->next;
return node;
}
void
2025-10-25 18:21:18 -05:00
node_free(node_t *node_)
{
2025-10-25 18:21:18 -05:00
stack_t *stack;
2025-10-25 18:21:18 -05:00
stack = (stack_t*)node_;
stack->next = g_stack;
g_stack = stack;
}
2025-10-27 22:12:51 -05:00
void
node_gc()
{
std::lock_guard<std::mutex> _(g_mutex);
for(auto &info : g_all_stacks)
{
*info.should_gc = true;
}
}