Logo
shared_memory-modify.hh
Go to the documentation of this file.
1 #pragma once
2 #include <pthread.h>
3 #include <sys/shm.h>
4 #include <sys/ipc.h>
5 #include <sys/types.h>
6 #include <sys/stat.h>
7 #include <sys/mman.h>
8 #include <fcntl.h>
9 #include <cstdlib>
10 
11 
12 #define DEFAULT_KEY 85549
13 
14 struct shm_buf{
15  uint64_t chunk_size;
16  char* buf;
17  uint32_t chunk_index;
18  uint32_t chunk_offset;
19  bool commit;
20 };
21 
22 struct semaphore {
23  pthread_mutex_t lock;
24  pthread_cond_t nonzero;
25  unsigned head;
26  unsigned tail;
27  unsigned queue_size;
28  unsigned count;
29 };
30 
31 typedef struct semaphore semaphore_t;
32 
34 semaphore_create(char *semaphore_name, unsigned qs)
35 {
36  int fd;
37  semaphore_t *semap;
38  pthread_mutexattr_t psharedm;
39  pthread_condattr_t psharedc;
40 
41  fd = open(semaphore_name, O_RDWR | O_CREAT, 0666);
42  if (fd < 0){
43  printf("%s : fd open failed\n", semaphore_name);
44  return (NULL);
45  }
46  (void) ftruncate(fd, sizeof(semaphore_t));
47  (void) pthread_mutexattr_init(&psharedm);
48  (void) pthread_mutexattr_setpshared(&psharedm,
49  PTHREAD_PROCESS_SHARED);
50  (void) pthread_condattr_init(&psharedc);
51  (void) pthread_condattr_setpshared(&psharedc,
52  PTHREAD_PROCESS_SHARED);
53  semap = (semaphore_t *) mmap(NULL, sizeof(semaphore_t),
54  PROT_READ | PROT_WRITE, MAP_SHARED,
55  fd, 0);
56 
57  close (fd);
58  (void) pthread_mutex_init(&semap->lock, &psharedm);
59  (void) pthread_cond_init(&semap->nonzero, &psharedc);
60  semap->head = 0;
61  semap->tail = 0;
62  semap->queue_size = qs;
63  semap->count = 0;
64  return (semap);
65 }
66 
68 semaphore_open(char *semaphore_name)
69 {
70  int fd;
71  semaphore_t *semap;
72 
73  fd = open(semaphore_name, O_RDWR, 0666);
74  if (fd < 0)
75  return (NULL);
76  semap = (semaphore_t *) mmap(NULL, sizeof(semaphore_t),
77  PROT_READ | PROT_WRITE, MAP_SHARED,
78  fd, 0);
79  close (fd);
80  return (semap);
81 }
82 
83 void
85 {
86  munmap((void *) semap, sizeof(semaphore_t));
87 }
void semaphore_close(semaphore_t *semap)
Definition: shared_memory-modify.hh:84
semaphore_t * semaphore_open(char *semaphore_name)
Definition: shared_memory-modify.hh:68
semaphore_t * semaphore_create(char *semaphore_name, unsigned qs)
Definition: shared_memory-modify.hh:34
Definition: shared_memory-modify.hh:22
unsigned queue_size
Definition: shared_memory-modify.hh:27
pthread_cond_t nonzero
Definition: shared_memory-modify.hh:24
pthread_mutex_t lock
Definition: shared_memory-modify.hh:23
unsigned tail
Definition: shared_memory-modify.hh:26
unsigned count
Definition: shared_memory-modify.hh:28
unsigned head
Definition: shared_memory-modify.hh:25
Definition: shared_memory-modify.hh:14
char * buf
Definition: shared_memory-modify.hh:16
uint32_t chunk_offset
Definition: shared_memory-modify.hh:18
uint32_t chunk_index
Definition: shared_memory-modify.hh:17
bool commit
Definition: shared_memory-modify.hh:19
uint64_t chunk_size
Definition: shared_memory-modify.hh:15