blob: 3dd01994f4e089053391d9a47c192a7e37accfe2 [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
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() */
Jesse Noller4b413d32009-04-01 20:51:28 +000019# ifdef Py_DEBUG
20# include <crtdbg.h>
21# endif
Benjamin Petersonfa268032008-06-13 19:28:21 +000022# define SEM_HANDLE HANDLE
23# define SEM_VALUE_MAX LONG_MAX
24#else
25# include <fcntl.h> /* O_CREAT and O_EXCL */
Martin v. Löwisb37509b2008-11-04 20:45:29 +000026# include <netinet/in.h>
Benjamin Petersonfa268032008-06-13 19:28:21 +000027# include <sys/socket.h>
Martin v. Löwisb37509b2008-11-04 20:45:29 +000028# include <sys/uio.h>
Benjamin Petersonfa268032008-06-13 19:28:21 +000029# include <arpa/inet.h> /* htonl() and ntohl() */
Mark Dickinson875ada42009-11-28 12:52:39 +000030# if defined(HAVE_SEM_OPEN) && !defined(POSIX_SEMAPHORES_NOT_ENABLED)
Benjamin Petersonfa268032008-06-13 19:28:21 +000031# 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/*
Jesse Noller338f5782008-09-03 18:22:19 +000045 * Issue 3110 - Solaris does not define SEM_VALUE_MAX
46 */
47#ifndef SEM_VALUE_MAX
Benjamin Peterson965ce872009-04-05 21:24:58 +000048 #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
Jesus Cea7dfed632009-07-02 14:37:32 +000052 #elif defined(_POSIX_SEM_VALUE_MAX)
Benjamin Peterson965ce872009-04-05 21:24:58 +000053 # define SEM_VALUE_MAX _POSIX_SEM_VALUE_MAX
54 #else
55 # define SEM_VALUE_MAX INT_MAX
56 #endif
Jesse Noller338f5782008-09-03 18:22:19 +000057#endif
58
Benjamin Peterson965ce872009-04-05 21:24:58 +000059
Jesse Noller338f5782008-09-03 18:22:19 +000060/*
Benjamin Petersonfa268032008-06-13 19:28:21 +000061 * 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"
Benjamin Petersonfa268032008-06-13 19:28:21 +000069# 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
121PyObject *mp_SetError(PyObject *Type, int num);
122
123/*
124 * Externs - not all will really exist on all platforms
125 */
126
127extern PyObject *pickle_dumps;
128extern PyObject *pickle_loads;
129extern PyObject *pickle_protocol;
130extern PyObject *BufferTooShort;
131extern PyTypeObject SemLockType;
132extern PyTypeObject ConnectionType;
133extern PyTypeObject PipeConnectionType;
134extern 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
164typedef 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 */