Skip to content
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
9 changes: 9 additions & 0 deletions dmtcp/include/mcmini/common/shm_config.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ extern "C" {

#include <stddef.h>
#include <stdint.h>
#include <pthread.h>

#include "mcmini/defines.h"
#include "mcmini/real_world/mailbox/runner_mailbox.h"
Expand All @@ -15,6 +16,14 @@ extern "C" {
const extern size_t shm_size;
void mc_get_shm_handle_name(char *dst, size_t sz);

// Declare the global variable and the mutex
extern volatile void* global_shm_start;
extern pthread_mutex_t shm_start_mutex;

// Function declarations
void set_global_shm_start(void* addr);
volatile void* get_global_shm_start();

struct mcmini_shm_file {
struct template_process_t tpt;
struct rec_list rec_list;
Expand Down
23 changes: 23 additions & 0 deletions dmtcp/src/common/shm_config.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,26 @@ void mc_get_shm_handle_name(char *dst, size_t sz) {
snprintf(dst, sz, "/mcmini-%s-%lu", getenv("USER"), (long)getppid());
dst[sz - 1] = '\0';
}

// Define the global variable and initialize the mutex
volatile void* global_shm_start = NULL;
pthread_mutex_t shm_start_mutex = PTHREAD_MUTEX_INITIALIZER;

// Function to set the global shared memory start address
void set_global_shm_start(void* addr) {
pthread_mutex_lock(&shm_start_mutex);
global_shm_start = addr;
pthread_mutex_unlock(&shm_start_mutex);
}

// Function to get the global shared memory start address
volatile void* get_global_shm_start() {
// Use a thread-local variable to cache the value
static __thread volatile void* local_shm_start = NULL;

if (local_shm_start == NULL) {
local_shm_start = global_shm_start;
}
Comment on lines +34 to +36
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This needs to be wrapped in a pthread_mutex_lock()/pthread_mutex_unlock(), but otherwise it's good

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When I say this, I meant only line 35 to be precise since local_shm_start is thread_local.


return local_shm_start;
}
4 changes: 1 addition & 3 deletions dmtcp/src/lib/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@
#include "dmtcp.h"
#include "mcmini/mcmini.h"

volatile void *global_shm_start = NULL;

void mc_allocate_shared_memory_region(const char *shm_name) {
int fd = shm_open(shm_name, O_RDWR, S_IRUSR | S_IWUSR);
if (fd == -1) {
Expand All @@ -43,7 +41,7 @@ void mc_allocate_shared_memory_region(const char *shm_name) {
}
fsync(fd);
close(fd);
global_shm_start = gshms;
set_global_shm_start(gshms);
}

void mc_deallocate_shared_memory_region(void) {
Expand Down
2 changes: 1 addition & 1 deletion dmtcp/src/lib/wrappers.c
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ volatile runner_mailbox *thread_get_mailbox() {
assert(!is_checkpoint_thread());
assert(tid_self != RID_INVALID);
assert(tid_self != RID_CHECKPOINT_THREAD);
return &((volatile struct mcmini_shm_file *)(global_shm_start))
return &((volatile struct mcmini_shm_file *)(get_global_shm_start()))
->mailboxes[tid_self];
}

Expand Down