1 #ifndef MULTIPROCESSING_H
2 #define MULTIPROCESSING_H
3 
4 #define PY_SSIZE_T_CLEAN
5 
6 #include "Python.h"
7 #include "structmember.h"
8 #include "pythread.h"
9 
10 /*
11  * Platform includes and definitions
12  */
13 
14 #ifdef MS_WINDOWS
15 #  define WIN32_LEAN_AND_MEAN
16 #  include <windows.h>
17 #  include <winsock2.h>
18 #  include <process.h>               /* getpid() */
19 #  ifdef Py_DEBUG
20 #    include <crtdbg.h>
21 #  endif
22 #  define SEM_HANDLE HANDLE
23 #  define SEM_VALUE_MAX LONG_MAX
24 #else
25 #  include <fcntl.h>                 /* O_CREAT and O_EXCL */
26 #  include <netinet/in.h>
27 #  include <sys/socket.h>
28 #  include <sys/uio.h>
29 #  include <arpa/inet.h>             /* htonl() and ntohl() */
30 #  if defined(HAVE_SEM_OPEN) && !defined(POSIX_SEMAPHORES_NOT_ENABLED)
31 #    include <semaphore.h>
32      typedef sem_t *SEM_HANDLE;
33 #  endif
34 #  define HANDLE int
35 #  define SOCKET int
36 #  define BOOL int
37 #  define UINT32 uint32_t
38 #  define INT32 int32_t
39 #  define TRUE 1
40 #  define FALSE 0
41 #  define INVALID_HANDLE_VALUE (-1)
42 #endif
43 
44 /*
45  * Issue 3110 - Solaris does not define SEM_VALUE_MAX
46  */
47 #ifndef SEM_VALUE_MAX
48     #if defined(HAVE_SYSCONF) && defined(_SC_SEM_VALUE_MAX)
49         # define SEM_VALUE_MAX sysconf(_SC_SEM_VALUE_MAX)
50     #elif defined(_SEM_VALUE_MAX)
51         # define SEM_VALUE_MAX _SEM_VALUE_MAX
52     #elif defined(_POSIX_SEM_VALUE_MAX)
53         # define SEM_VALUE_MAX _POSIX_SEM_VALUE_MAX
54     #else
55         # define SEM_VALUE_MAX INT_MAX
56     #endif
57 #endif
58 
59 
60 /*
61  * Make sure Py_ssize_t available
62  */
63 
64 #if PY_VERSION_HEX < 0x02050000 && !defined(PY_SSIZE_T_MIN)
65    typedef int Py_ssize_t;
66 #  define PY_SSIZE_T_MAX INT_MAX
67 #  define PY_SSIZE_T_MIN INT_MIN
68 #  define F_PY_SSIZE_T "i"
69 #  define PyInt_FromSsize_t(n) PyInt_FromLong((long)n)
70 #else
71 #  define F_PY_SSIZE_T "n"
72 #endif
73 
74 /*
75  * Format codes
76  */
77 
78 #if SIZEOF_VOID_P == SIZEOF_LONG
79 #  define F_POINTER "k"
80 #  define T_POINTER T_ULONG
81 #elif defined(HAVE_LONG_LONG) && (SIZEOF_VOID_P == SIZEOF_LONG_LONG)
82 #  define F_POINTER "K"
83 #  define T_POINTER T_ULONGLONG
84 #else
85 #  error "can't find format code for unsigned integer of same size as void*"
86 #endif
87 
88 #ifdef MS_WINDOWS
89 #  define F_HANDLE F_POINTER
90 #  define T_HANDLE T_POINTER
91 #  define F_SEM_HANDLE F_HANDLE
92 #  define T_SEM_HANDLE T_HANDLE
93 #  define F_DWORD "k"
94 #  define T_DWORD T_ULONG
95 #else
96 #  define F_HANDLE "i"
97 #  define T_HANDLE T_INT
98 #  define F_SEM_HANDLE F_POINTER
99 #  define T_SEM_HANDLE T_POINTER
100 #endif
101 
102 #if PY_VERSION_HEX >= 0x03000000
103 #  define F_RBUFFER "y"
104 #else
105 #  define F_RBUFFER "s"
106 #endif
107 
108 /*
109  * Error codes which can be returned by functions called without GIL
110  */
111 
112 #define MP_SUCCESS (0)
113 #define MP_STANDARD_ERROR (-1)
114 #define MP_MEMORY_ERROR (-1001)
115 #define MP_END_OF_FILE (-1002)
116 #define MP_EARLY_END_OF_FILE (-1003)
117 #define MP_BAD_MESSAGE_LENGTH (-1004)
118 #define MP_SOCKET_ERROR (-1005)
119 #define MP_EXCEPTION_HAS_BEEN_SET (-1006)
120 
121 PyObject *mp_SetError(PyObject *Type, int num);
122 
123 /*
124  * Externs - not all will really exist on all platforms
125  */
126 
127 extern PyObject *pickle_dumps;
128 extern PyObject *pickle_loads;
129 extern PyObject *pickle_protocol;
130 extern PyObject *BufferTooShort;
131 extern PyTypeObject SemLockType;
132 extern PyTypeObject ConnectionType;
133 extern PyTypeObject PipeConnectionType;
134 extern HANDLE sigint_event;
135 
136 /*
137  * Py3k compatibility
138  */
139 
140 #if PY_VERSION_HEX >= 0x03000000
141 #  define PICKLE_MODULE "pickle"
142 #  define FROM_FORMAT PyUnicode_FromFormat
143 #  define PyInt_FromLong PyLong_FromLong
144 #  define PyInt_FromSsize_t PyLong_FromSsize_t
145 #else
146 #  define PICKLE_MODULE "cPickle"
147 #  define FROM_FORMAT PyString_FromFormat
148 #endif
149 
150 #ifndef PyVarObject_HEAD_INIT
151 #  define PyVarObject_HEAD_INIT(type, size) PyObject_HEAD_INIT(type) size,
152 #endif
153 
154 #ifndef Py_TPFLAGS_HAVE_WEAKREFS
155 #  define Py_TPFLAGS_HAVE_WEAKREFS 0
156 #endif
157 
158 /*
159  * Connection definition
160  */
161 
162 #define CONNECTION_BUFFER_SIZE 1024
163 
164 typedef struct {
165     PyObject_HEAD
166     HANDLE handle;
167     int flags;
168     PyObject *weakreflist;
169     char buffer[CONNECTION_BUFFER_SIZE];
170 } ConnectionObject;
171 
172 /*
173  * Miscellaneous
174  */
175 
176 #define MAX_MESSAGE_LENGTH 0x7fffffff
177 
178 #ifndef MIN
179 #  define MIN(x, y) ((x) < (y) ? x : y)
180 #  define MAX(x, y) ((x) > (y) ? x : y)
181 #endif
182 
183 #endif /* MULTIPROCESSING_H */
184