Commit patch to not break on spaces.
[bowtie.git] / shmem.h
1 /*
2  * shmem.h
3  *
4  *  Created on: Aug 13, 2009
5  *      Author: Ben Langmead
6  */
7
8 #ifndef SHMEM_H_
9 #define SHMEM_H_
10
11 #ifdef BOWTIE_SHARED_MEM
12
13 #include <string>
14 #include <sys/shm.h>
15 #include <unistd.h>
16 #include <sys/shm.h>
17 #include <errno.h>
18 #include <stdint.h>
19 #include <stdexcept>
20 #include "str_util.h"
21
22 extern void notifySharedMem(void *mem, size_t len);
23
24 extern void waitSharedMem(void *mem, size_t len);
25
26 #define ALLOC_SHARED_U8 allocSharedMem<uint8_t>
27 #define ALLOC_SHARED_U32 allocSharedMem<uint32_t>
28 #define FREE_SHARED shmdt
29 #define NOTIFY_SHARED notifySharedMem
30 #define WAIT_SHARED waitSharedMem
31
32 #define SHMEM_UNINIT  0xafba4242
33 #define SHMEM_INIT    0xffaa6161
34
35 /**
36  * Tries to allocate a shared-memory chunk for a given file of a given size.
37  */
38 template <typename T>
39 bool allocSharedMem(std::string fname,
40                     size_t len,
41                     T ** dst,
42                     const char *memName,
43                     bool verbose)
44 {
45         using namespace std;
46         int shmid = -1;
47         // Calculate key given string
48         key_t key = (key_t)hash_string(fname);
49         shmid_ds ds;
50         int ret;
51         // Reserve 4 bytes at the end for silly synchronization
52         size_t shmemLen = len + 4;
53         if(verbose) {
54                 cerr << "Reading " << len << "+4 bytes into shared memory for " << memName << endl;
55         }
56         T *ptr = NULL;
57         while(true) {
58                 // Create the shrared-memory block
59                 if((shmid = shmget(key, shmemLen, IPC_CREAT | 0666)) < 0) {
60                         if(errno == ENOMEM) {
61                                 cerr << "Out of memory allocating shared area " << memName << endl;
62                         } else if(errno == EACCES) {
63                                 cerr << "EACCES" << endl;
64                         } else if(errno == EEXIST) {
65                                 cerr << "EEXIST" << endl;
66                         } else if(errno == EINVAL) {
67                                 cerr << "Warning: shared-memory chunk's segment size doesn't match expected size (" << (shmemLen) << ")" << endl
68                                          << "Deleteing old shared memory block and trying again." << endl;
69                                 shmid = shmget(key, 0, 0);
70                                 if((ret = shmctl(shmid, IPC_RMID, &ds)) < 0) {
71                                         cerr << "shmctl returned " << ret
72                                                  << " for IPC_RMID, errno is " << errno
73                                                  << ", shmid is " << shmid << endl;
74                                         throw 1;
75                                 } else {
76                                         cerr << "Deleted shared mem chunk with shmid " << shmid << endl;
77                                 }
78                                 continue;
79                         } else if(errno == ENOENT) {
80                                 cerr << "ENOENT" << endl;
81                         } else if(errno == ENOSPC) {
82                                 cerr << "ENOSPC" << endl;
83                         } else {
84                                 cerr << "shmget returned " << shmid << " for and errno is " << errno << endl;
85                         }
86                         throw 1;
87                 }
88                 ptr = (T*)shmat(shmid, 0, 0);
89                 if(ptr == (void*)-1) {
90                         cerr << "Failed to attach " << memName << " to shared memory with shmat()." << endl;
91                         throw 1;
92                 }
93                 if(ptr == NULL) {
94                         cerr << memName << " pointer returned by shmat() was NULL." << endl;
95                         throw 1;
96                 }
97                 // Did I create it, or did I just attach to one created by
98                 // another process?
99                 if((ret = shmctl(shmid, IPC_STAT, &ds)) < 0) {
100                         cerr << "shmctl returned " << ret << " for IPC_STAT and errno is " << errno << endl;
101                         throw 1;
102                 }
103                 if(ds.shm_segsz != shmemLen) {
104                         cerr << "Warning: shared-memory chunk's segment size (" << ds.shm_segsz
105                                  << ") doesn't match expected size (" << shmemLen << ")" << endl
106                                  << "Deleteing old shared memory block and trying again." << endl;
107                         if((ret = shmctl(shmid, IPC_RMID, &ds)) < 0) {
108                                 cerr << "shmctl returned " << ret << " for IPC_RMID and errno is " << errno << endl;
109                                 throw 1;
110                         }
111                 } else {
112                         break;
113                 }
114         } // while(true)
115         *dst = ptr;
116         bool initid = (((volatile uint32_t*)((char*)ptr + len))[0] == SHMEM_INIT);
117         if(ds.shm_cpid == getpid() && !initid) {
118                 if(verbose) {
119                         cerr << "  I (pid = " << getpid() << ") created the "
120                              << "shared memory for " << memName << endl;
121                 }
122                 // Set this value just off the end of the chunk to
123                 // indicate that the data hasn't been read yet.
124                 ((volatile uint32_t*)((char*)ptr + len))[0] = SHMEM_UNINIT;
125                 return true;
126         } else {
127                 if(verbose) {
128                         cerr << "  I (pid = " << getpid()
129                              << ") did not create the shared memory for "
130                              << memName << ".  Pid " << ds.shm_cpid << " did." << endl;
131                 }
132                 return false;
133         }
134 }
135
136 #else
137
138 #define ALLOC_SHARED_U8(...) 0
139 #define ALLOC_SHARED_U32(...) 0
140 #define FREE_SHARED(...)
141 #define NOTIFY_SHARED(...)
142 #define WAIT_SHARED(...)
143
144 #endif /*BOWTIE_SHARED_MEM*/
145
146 #endif /* SHMEM_H_ */