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