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