blob: 4f4f9d735e71ac9fff79a26d5d7e285a8e6dc70c [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() */
30# if HAVE_SEM_OPEN
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/*
Jesse Noller338f5782008-09-03 18:22:19 +000045 * Issue 3110 - Solaris does not define SEM_VALUE_MAX
46 */
47#ifndef SEM_VALUE_MAX
48# ifdef _SEM_VALUE_MAX
49# define SEM_VALUE_MAX _SEM_VALUE_MAX
50# else
51# define SEM_VALUE_MAX INT_MAX
52# endif
53#endif
54
55/*
Benjamin Petersonfa268032008-06-13 19:28:21 +000056 * Make sure Py_ssize_t available
57 */
58
59#if PY_VERSION_HEX < 0x02050000 && !defined(PY_SSIZE_T_MIN)
60 typedef int Py_ssize_t;
61# define PY_SSIZE_T_MAX INT_MAX
62# define PY_SSIZE_T_MIN INT_MIN
63# define F_PY_SSIZE_T "i"
Benjamin Petersonfa268032008-06-13 19:28:21 +000064# define PyInt_FromSsize_t(n) PyInt_FromLong((long)n)
65#else
66# define F_PY_SSIZE_T "n"
67#endif
68
69/*
70 * Format codes
71 */
72
73#if SIZEOF_VOID_P == SIZEOF_LONG
74# define F_POINTER "k"
75# define T_POINTER T_ULONG
76#elif defined(HAVE_LONG_LONG) && (SIZEOF_VOID_P == SIZEOF_LONG_LONG)
77# define F_POINTER "K"
78# define T_POINTER T_ULONGLONG
79#else
80# error "can't find format code for unsigned integer of same size as void*"
81#endif
82
83#ifdef MS_WINDOWS
84# define F_HANDLE F_POINTER
85# define T_HANDLE T_POINTER
86# define F_SEM_HANDLE F_HANDLE
87# define T_SEM_HANDLE T_HANDLE
88# define F_DWORD "k"
89# define T_DWORD T_ULONG
90#else
91# define F_HANDLE "i"
92# define T_HANDLE T_INT
93# define F_SEM_HANDLE F_POINTER
94# define T_SEM_HANDLE T_POINTER
95#endif
96
97#if PY_VERSION_HEX >= 0x03000000
98# define F_RBUFFER "y"
99#else
100# define F_RBUFFER "s"
101#endif
102
103/*
104 * Error codes which can be returned by functions called without GIL
105 */
106
107#define MP_SUCCESS (0)
108#define MP_STANDARD_ERROR (-1)
109#define MP_MEMORY_ERROR (-1001)
110#define MP_END_OF_FILE (-1002)
111#define MP_EARLY_END_OF_FILE (-1003)
112#define MP_BAD_MESSAGE_LENGTH (-1004)
113#define MP_SOCKET_ERROR (-1005)
114#define MP_EXCEPTION_HAS_BEEN_SET (-1006)
115
116PyObject *mp_SetError(PyObject *Type, int num);
117
118/*
119 * Externs - not all will really exist on all platforms
120 */
121
122extern PyObject *pickle_dumps;
123extern PyObject *pickle_loads;
124extern PyObject *pickle_protocol;
125extern PyObject *BufferTooShort;
126extern PyTypeObject SemLockType;
127extern PyTypeObject ConnectionType;
128extern PyTypeObject PipeConnectionType;
129extern HANDLE sigint_event;
130
131/*
132 * Py3k compatibility
133 */
134
135#if PY_VERSION_HEX >= 0x03000000
136# define PICKLE_MODULE "pickle"
137# define FROM_FORMAT PyUnicode_FromFormat
138# define PyInt_FromLong PyLong_FromLong
139# define PyInt_FromSsize_t PyLong_FromSsize_t
140#else
141# define PICKLE_MODULE "cPickle"
142# define FROM_FORMAT PyString_FromFormat
143#endif
144
145#ifndef PyVarObject_HEAD_INIT
146# define PyVarObject_HEAD_INIT(type, size) PyObject_HEAD_INIT(type) size,
147#endif
148
149#ifndef Py_TPFLAGS_HAVE_WEAKREFS
150# define Py_TPFLAGS_HAVE_WEAKREFS 0
151#endif
152
153/*
154 * Connection definition
155 */
156
157#define CONNECTION_BUFFER_SIZE 1024
158
159typedef struct {
160 PyObject_HEAD
161 HANDLE handle;
162 int flags;
163 PyObject *weakreflist;
164 char buffer[CONNECTION_BUFFER_SIZE];
165} ConnectionObject;
166
167/*
168 * Miscellaneous
169 */
170
171#define MAX_MESSAGE_LENGTH 0x7fffffff
172
173#ifndef MIN
174# define MIN(x, y) ((x) < (y) ? x : y)
175# define MAX(x, y) ((x) > (y) ? x : y)
176#endif
177
178#endif /* MULTIPROCESSING_H */