Code::Blocks  SVN r11506
ipc.h
Go to the documentation of this file.
1 /*
2  * This file is part of the Code::Blocks IDE and licensed under the GNU General Public License, version 3
3  * http://www.gnu.org/licenses/gpl-3.0.html
4  */
5 
6 #ifndef IPC_H
7 #define IPC_H
8 
9 
10 #include "sdk.h"
11 
12 #include <wx/wx.h>
13 
14 
15 #ifdef __WIN32__
16 
17  #define WIN32_LEAN_AND_MEAN
18  #define NOGDI
19  #include <windows.h>
20  typedef HANDLE shm_handle_t;
21  typedef HANDLE semaphore_t;
22 
23 #else
24 
25  #include <fcntl.h>
26  #include <errno.h>
27  #include <sys/types.h>
28  #include <sys/sem.h>
29  #include <sys/ipc.h>
30  #include <sys/shm.h>
31 
32  #if defined(__APPLE__) && defined(__MACH__)
33  typedef int shm_handle_t;
34  typedef mach_port_t semaphore_t;
35  #else
36  typedef int shm_handle_t;
37  typedef int semaphore_t;
38  #endif
39 
40 #endif
41 
42 static const int ipc_buf_size = 1024*64;
43 
44 
46 {
48 
49  union
50  {
51  semaphore_t semid;
52  semaphore_t sem[2];
53  };
54 
55  void* shared;
56  bool ok;
57  bool server;
58 
59 public:
60 
61  enum rw_t{ reader, writer };
62 
63  SharedMemory();
64  ~SharedMemory();
65 
66  bool OK() const { return ok; };
67 
68  void* BasePointer() const { return shared; };
69  size_t Size() const { return ipc_buf_size; };
70 
71  bool Server() const { return server; };
72  bool Client() const { return !server; };
73 
74 
75  /*
76  * Lock(reader) locks "as reader", not "the reader semaphore", i.e. it
77  * 1. locks the reader semaphore
78  * 2. locks the writer mutex, so the shared memory cannot be written while we read it
79  *
80  * Lock(writer) locks "as writer", this is equivalent to locking "the writer mutex"
81  *
82  * Unlock(reader) unlocks "as reader", i.e. it actually unlocks the writer mutex
83  * which the caller is still holding, so another process can write to the shared memory area again.
84  * It does not release the reader semaphore, since it should block on it on the next iteration.
85  *
86  * Unlock(writer) locks "as writer", i.e. it
87  * 1. unlocks the reader semaphore, waking up the Server thread
88  * 2. unlocks the writer mutex, so the Server thread can acquire it and prevent other processes from writing
89  */
90  bool Lock(rw_t rw);
91  void Unlock(rw_t rw);
92 };
93 
94 
95 class IPC : public wxThread
96 {
97  volatile bool is_shutdown;
99 
100 public:
101  IPC() : is_shutdown(false) {};
102 
103  virtual ExitCode Entry();
104 
105  bool Server() const { return shm.Server(); };
106 
107  void Shutdown();
108 
109  void Send(const wxString& value);
110 };
111 
112 
113 /*
114  * expemplary code for app.cpp, specifically CodeBlocksApp::ParseCmdLine >>>>>>>>>>>>>>>
115  *
116  *
117 IPC *ipc = new IPC; // don't delete
118 
119 if (ipc->Server())
120 {
121  ipc->Run();
122 }
123 else
124 {
125  // parser is the wxCmdLineParser
126  wxString item;
127  wxString buf;
128 
129  static const unsigned int max_size = ipc_buf_size / sizeof(wxChar);
130 
131  buf.Alloc(4096);
132 
133  int count = parser.GetParamCount();
134 
135  for (int i = 0; i < count; ++i)
136  {
137  item = parser.GetParam(i);
138  item.append(_T('\n'));
139 
140  if (buf.length() + item.length() + 1) >= max_size)
141  {
142  buf.append(_T('\0'));
143  ipc->Send(buf);
144  buf.Empty();
145  }
146  buf.append(item);
147  }
148 
149  if (buf.length())
150  {
151  buf.append(_T('\0'));
152  ipc->Send(buf);
153  }
154 }
155  *
156  *
157  * <<<<<<<<<<<<<<< expemplary code
158  */
159 
160 
161 
162 
163 #endif
volatile bool is_shutdown
Definition: ipc.h:97
IPC()
Definition: ipc.h:101
bool Lock(rw_t rw)
Definition: ipc.cpp:105
bool ok
Definition: ipc.h:56
static const int ipc_buf_size
Definition: ipc.h:42
HANDLE shm_handle_t
Definition: ipc.h:20
size_t Size() const
Definition: ipc.h:69
~SharedMemory()
Definition: ipc.cpp:97
bool Server() const
Definition: ipc.h:71
Definition: ipc.h:95
shm_handle_t handle
Definition: ipc.h:47
SharedMemory shm
Definition: ipc.h:98
SharedMemory()
Definition: ipc.cpp:75
bool Client() const
Definition: ipc.h:72
void * BasePointer() const
Definition: ipc.h:68
bool OK() const
Definition: ipc.h:66
void Unlock(rw_t rw)
Definition: ipc.cpp:120
void * shared
Definition: ipc.h:55
bool server
Definition: ipc.h:57
bool Server() const
Definition: ipc.h:105
void * ExitCode
HANDLE semaphore_t
Definition: ipc.h:21