~ubuntu-branches/ubuntu/hardy/codeblocks/hardy-backports

« back to all changes in this revision

Viewing changes to src/src/ipc.h

  • Committer: Bazaar Package Importer
  • Author(s): Michael Casadevall
  • Date: 2008-07-17 04:39:23 UTC
  • Revision ID: james.westby@ubuntu.com-20080717043923-gmsy5cwkdjswghkm
Tags: upstream-8.02
ImportĀ upstreamĀ versionĀ 8.02

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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
 
 
45
 
 
46
class SharedMemory
 
47
{
 
48
        shm_handle_t handle;
 
49
 
 
50
        union
 
51
        {
 
52
                semaphore_t semid;
 
53
                semaphore_t sem[2];
 
54
        };
 
55
 
 
56
        void* shared;
 
57
        bool ok;
 
58
        bool server;
 
59
 
 
60
public:
 
61
 
 
62
        enum rw_t{ reader, writer };
 
63
 
 
64
        SharedMemory();
 
65
        ~SharedMemory();
 
66
 
 
67
        bool OK() const { return ok; };
 
68
 
 
69
        void* BasePointer() const { return shared; };
 
70
        size_t Size() const { return ipc_buf_size; };
 
71
 
 
72
        bool Server() const { return server; };
 
73
        bool Client() const { return !server; };
 
74
 
 
75
 
 
76
    /*
 
77
     * Lock(reader) locks "as reader", not "the reader semaphore", i.e. it
 
78
     *   1. locks the reader semaphore
 
79
     *   2. locks the writer mutex, so the shared memory cannot be written while we read it
 
80
     *
 
81
     * Lock(writer) locks "as writer", this is equivalent to locking "the writer mutex"
 
82
     *
 
83
     * Unlock(reader) unlocks "as reader", i.e. it actually unlocks the writer mutex
 
84
     *   which the caller is still holding, so another process can write to the shared memory area again.
 
85
     *   It does not release the reader semaphore, since it should block on it on the next iteration.
 
86
     *
 
87
     * Unlock(writer) locks "as writer", i.e. it
 
88
     *   1. unlocks the reader semaphore, waking up the Server thread
 
89
     *   2. unlocks the writer mutex, so the Server thread can acquire it and prevent other processes from writing
 
90
     */
 
91
        bool Lock(rw_t rw);
 
92
        void Unlock(rw_t rw);
 
93
};
 
94
 
 
95
 
 
96
 
 
97
 
 
98
 
 
99
class IPC : public wxThread
 
100
{
 
101
        volatile bool is_shutdown;
 
102
        SharedMemory  shm;
 
103
 
 
104
public:
 
105
        IPC() : is_shutdown(false) {};
 
106
 
 
107
        virtual ExitCode Entry();
 
108
 
 
109
        bool Server() const { return shm.Server(); };
 
110
 
 
111
        void Shutdown();
 
112
 
 
113
        void Send(const wxString& value);
 
114
};
 
115
 
 
116
 
 
117
 
 
118
 
 
119
 
 
120
 
 
121
 
 
122
 
 
123
 
 
124
/*
 
125
 *  expemplary code for app.cpp, specifically CodeBlocksApp::ParseCmdLine  >>>>>>>>>>>>>>>
 
126
 *
 
127
 *
 
128
IPC *ipc = new IPC; // don't delete
 
129
 
 
130
if(ipc->Server())
 
131
{
 
132
        ipc->Run();
 
133
}
 
134
else
 
135
{
 
136
        // parser is the wxCmdLineParser
 
137
        wxString item;
 
138
        wxString buf;
 
139
 
 
140
        static const unsigned int max_size = ipc_buf_size / sizeof(wxChar);
 
141
 
 
142
        buf.Alloc(4096);
 
143
 
 
144
        int count = parser.GetParamCount();
 
145
 
 
146
        for (int i = 0; i < count; ++i)
 
147
        {
 
148
                item = parser.GetParam(i);
 
149
                item.append(_T('\n'));
 
150
 
 
151
                if(buf.length() + item.length() + 1) >= max_size)
 
152
                {
 
153
                        buf.append(_T('\0'));
 
154
                        ipc->Send(buf);
 
155
                        buf.Empty();
 
156
                }
 
157
                buf.append(item);
 
158
        }
 
159
 
 
160
        if(buf.length())
 
161
        {
 
162
                buf.append(_T('\0'));
 
163
                ipc->Send(buf);
 
164
        }
 
165
}
 
166
 *
 
167
 *
 
168
 *  <<<<<<<<<<<<<<< expemplary code
 
169
 */
 
170
 
 
171
 
 
172
 
 
173
 
 
174
#endif