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