blob: 14425de82186b09e92cd8abe8a7eed28212e8506 [file] [log] [blame]
Benjamin Petersonfa268032008-06-13 19:28:21 +00001#ifndef MULTIPROCESSING_H
2#define MULTIPROCESSING_H
3
4#define PY_SSIZE_T_CLEAN
5
Martin v. Löwisec78d0a2010-06-04 17:20:56 +00006#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
Benjamin Petersonfa268032008-06-13 19:28:21 +000012#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>
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000024# include <process.h> /* getpid() */
Jesse Noller4b413d32009-04-01 20:51:28 +000025# ifdef Py_DEBUG
26# include <crtdbg.h>
27# endif
Benjamin Petersonfa268032008-06-13 19:28:21 +000028# define SEM_HANDLE HANDLE
29# define SEM_VALUE_MAX LONG_MAX
30#else
31# include <fcntl.h> /* O_CREAT and O_EXCL */
Martin v. Löwisb37509b2008-11-04 20:45:29 +000032# include <netinet/in.h>
Benjamin Petersonfa268032008-06-13 19:28:21 +000033# include <sys/socket.h>
Martin v. Löwisb37509b2008-11-04 20:45:29 +000034# include <sys/uio.h>
Benjamin Petersonfa268032008-06-13 19:28:21 +000035# include <arpa/inet.h> /* htonl() and ntohl() */
Mark Dickinsona614f042009-11-28 12:48:43 +000036# if defined(HAVE_SEM_OPEN) && !defined(POSIX_SEMAPHORES_NOT_ENABLED)
Benjamin Petersonfa268032008-06-13 19:28:21 +000037# 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/*
Jesse Noller338f5782008-09-03 18:22:19 +000051 * Issue 3110 - Solaris does not define SEM_VALUE_MAX
52 */
53#ifndef SEM_VALUE_MAX
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000054 #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
Jesse Noller338f5782008-09-03 18:22:19 +000063#endif
64
Benjamin Peterson965ce872009-04-05 21:24:58 +000065
Jesse Noller338f5782008-09-03 18:22:19 +000066/*
Benjamin Petersonfa268032008-06-13 19:28:21 +000067 * 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"
Benjamin Petersonfa268032008-06-13 19:28:21 +000075# 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
127PyObject *mp_SetError(PyObject *Type, int num);
128
129/*
130 * Externs - not all will really exist on all platforms
131 */
132
133extern PyObject *pickle_dumps;
134extern PyObject *pickle_loads;
135extern PyObject *pickle_protocol;
136extern PyObject *BufferTooShort;
137extern PyTypeObject SemLockType;
138extern PyTypeObject ConnectionType;
139extern PyTypeObject PipeConnectionType;
140extern 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
170typedef struct {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000171 PyObject_HEAD
172 HANDLE handle;
173 int flags;
174 PyObject *weakreflist;
175 char buffer[CONNECTION_BUFFER_SIZE];
Benjamin Petersonfa268032008-06-13 19:28:21 +0000176} 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 */