1
0
Fork 0

Use atomics

master
Gavin Howard 2 years ago
parent a5e025fa7b
commit 9e23434fd7
Signed by: gavin
GPG Key ID: C08038BDF280D33E
  1. 8
      Makefile
  2. 41
      test.c

@ -2,10 +2,10 @@ all: shmem shmem_affinity kernel kernel_affinity
run: run_shmem run_kernel run_shmem_affinity run_kernel_affinity
shmem:
$(CC) -DSHMEM $(CFLAGS) -lpthread -lrt -o shmem test.c
$(CC) -std=c11 -DSHMEM $(CFLAGS) -lpthread -lrt -o shmem test.c
shmem_affinity:
$(CC) -DSHMEM -DAFFINITY $(CFLAGS) -lpthread -lrt -o shmem_affinity test.c
$(CC) -std=c11 -DSHMEM -DAFFINITY $(CFLAGS) -lpthread -lrt -o shmem_affinity test.c
run_shmem: shmem
@./shmem
@ -14,10 +14,10 @@ run_shmem_affinity: shmem_affinity
@./shmem_affinity
kernel:
$(CC) -DKERNEL $(CFLAGS) -o kernel test.c
$(CC) -std=c11 -DKERNEL $(CFLAGS) -o kernel test.c
kernel_affinity:
$(CC) -DKERNEL -DAFFINITY $(CFLAGS) -o kernel_affinity test.c
$(CC) -std=c11 -DKERNEL -DAFFINITY $(CFLAGS) -o kernel_affinity test.c
run_kernel: kernel
@./kernel

@ -27,6 +27,12 @@
#error Must define one of SHMEM or KERNEL
#endif // SHMEM
#ifdef __STDC_NO_ATOMICS__
#error must have atomics
#endif // __STDC_NO_ATOMICS__
#include <stdatomic.h>
#else //KERNEL
#ifdef SHMEM
@ -51,11 +57,9 @@
#ifdef SHMEM
typedef struct shmem
{
pthread_mutex_t smutex;
pthread_mutex_t rmutex;
size_t size;
volatile sig_atomic_t sent;
volatile sig_atomic_t read;
atomic_uint sent;
atomic_uint read;
size_t a[ARRAY_SIZE / sizeof(size_t)];
} Shmem;
@ -87,7 +91,6 @@ void err(char* msg, int exit_code)
Shmem* shmem_create(void)
{
Shmem* s;
pthread_mutexattr_t attr;
// Our memory buffer will be readable and writable:
int protection = PROT_READ | PROT_WRITE;
@ -106,14 +109,6 @@ Shmem* shmem_create(void)
printf("s: %p, %p\n", s, s + sizeof(Shmem));
#endif
if (pthread_mutexattr_init(&attr)) err("could not init mutexattr", 1);
if (pthread_mutexattr_setpshared(&attr, 1)) err("could not set pshared", 1);
if (pthread_mutex_init(&s->smutex, &attr)) err("could not init mutex", 1);
if (pthread_mutex_init(&s->rmutex, &attr)) err("could not init mutex", 1);
if (pthread_mutexattr_destroy(&attr)) err("could not destroy mutexattr", 1);
s->size = ARRAY_SIZE;
s->sent = 0;
s->read = 0;
@ -123,8 +118,6 @@ Shmem* shmem_create(void)
void shmem_destroy(Shmem* s)
{
if (pthread_mutex_destroy(&s->smutex)) err("could not destroy mutex", 1);
if (pthread_mutex_destroy(&s->rmutex)) err("could not destroy mutex", 1);
if (munmap(s, sizeof(Shmem))) err("could not unmap memory", 1);
}
@ -134,15 +127,11 @@ sig_atomic_t get_val(bool read)
if (read)
{
if (pthread_mutex_lock(&s->rmutex)) err("mutex lock", 11);
val1 = s->read;
if (pthread_mutex_unlock(&s->rmutex)) err("mutex unlock", 11);
val1 = atomic_load(&s->read);
}
else
{
if (pthread_mutex_lock(&s->smutex)) err("mutex lock", 11);
val1 = s->sent;
if (pthread_mutex_unlock(&s->smutex)) err("mutex unlock", 11);
val1 = atomic_load(&s->sent);
}
return val1;
@ -150,20 +139,14 @@ sig_atomic_t get_val(bool read)
void set_val(sig_atomic_t val, bool read)
{
if (read)
{
if (pthread_mutex_lock(&s->rmutex)) err("mutex lock", 11);
s->read = val;
if (pthread_mutex_unlock(&s->rmutex)) err("mutex unlock", 11);
atomic_store(&s->read, val);
}
else
{
if (pthread_mutex_lock(&s->smutex)) err("mutex lock", 11);
s->sent = val;
if (pthread_mutex_unlock(&s->smutex)) err("mutex unlock", 11);
atomic_store(&s->sent, val);
}
}
#endif // SHMEM

Loading…
Cancel
Save