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