xref: /netbsd-src/external/gpl3/binutils/dist/gprofng/src/ipcio.h (revision cb63e24e8d6aae7ddac1859a9015f48b1d8bd90e)
1 /* Copyright (C) 2021-2024 Free Software Foundation, Inc.
2    Contributed by Oracle.
3 
4    This file is part of GNU Binutils.
5 
6    This program is free software; you can redistribute it and/or modify
7    it under the terms of the GNU General Public License as published by
8    the Free Software Foundation; either version 3, or (at your option)
9    any later version.
10 
11    This program is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14    GNU General Public License for more details.
15 
16    You should have received a copy of the GNU General Public License
17    along with this program; if not, write to the Free Software
18    Foundation, 51 Franklin Street - Fifth Floor, Boston,
19    MA 02110-1301, USA.  */
20 
21 /* Defines the external interface between er_ipc and the routines */
22 
23 #ifndef _IPCIO_H
24 #define _IPCIO_H
25 #include <pthread.h>
26 #include "gp-defs.h"
27 #include "StringBuilder.h"
28 
29 class DbeThreadPool;
30 typedef long long DbeObj;
31 typedef void *Object;
32 typedef char *String;
33 
34 #define BUFFER_SIZE_SMALL           512
35 #define BUFFER_SIZE_MEDIUM          512
36 #define BUFFER_SIZE_LARGE           1024*1024
37 
38 #define REQUEST_HAS_NO_BODY         0xFFFFFFFF
39 #define RESPONSE_STATUS_DEFAULT     0
40 #define RESPONSE_STATUS_SUCCESS     1
41 #define RESPONSE_STATUS_FAILURE     2
42 #define RESPONSE_STATUS_CANCELLED   3
43 
44 #define RESPONSE_TYPE_ACK           0
45 #define RESPONSE_TYPE_PROGRESS      1
46 #define RESPONSE_TYPE_COMPLETE      2
47 #define RESPONSE_TYPE_HANDSHAKE     3
48 #define HEADER_MARKER               0xff
49 
50 #define REQUEST_TYPE_DEFAULT        0
51 #define REQUEST_TYPE_CANCEL         1
52 #define REQUEST_TYPE_HANDSHAKE      2
53 
54 #define IPC_PROTOCOL_STR            "IPC_PROTOCOL_38"
55 #define IPC_VERSION_NUMBER          38
56 
57 enum IPCrequestStatus
58 {
59   INITIALIZED = 0,
60   IN_PROGRESS,
61   COMPLETED,
62   CANCELLED_DEFAULT,
63   CANCELLED_IMMEDIATE
64 };
65 
66 enum IPCTraceLevel
67 {
68   TRACE_LVL_0 = 0,
69   TRACE_LVL_1,
70   TRACE_LVL_2,
71   TRACE_LVL_3,
72   TRACE_LVL_4
73 };
74 
75 class IPCrequest
76 {
77   char *buf;
78   int size;
79   int idx;
80   int requestID;
81   int channelID;
82   IPCrequestStatus status;
83   bool cancelImmediate;
84 public:
85   IPCrequest (int, int, int);
86   ~IPCrequest ();
87   IPCrequestStatus getStatus ();
88   void setStatus (IPCrequestStatus);
89   void read ();
90 
getRequestID()91   int getRequestID ()               { return requestID; }
getChannelID()92   int getChannelID ()               { return channelID; }
isCancelImmediate()93   bool isCancelImmediate ()         { return cancelImmediate; }
setCancelImmediate()94   void setCancelImmediate ()        { cancelImmediate = true; }
rgetc()95   char rgetc ()                     { return buf[idx++]; }
96 };
97 
98 class IPCresponse
99 {
100 public:
101   IPCresponse (int sz);
102   ~IPCresponse ();
103 
getRequestID()104   int getRequestID ()               { return requestID; }
getChannelID()105   int getChannelID ()               { return channelID; }
setRequestID(int r)106   void setRequestID (int r)         { requestID = r; }
setChannelID(int c)107   void setChannelID (int c)         { channelID = c; }
setResponseType(int r)108   void setResponseType (int r)      { responseType = r; }
setResponseStatus(int s)109   void setResponseStatus (int s)    { responseStatus = s; }
getCurBufSize()110   int getCurBufSize ()              { return sb->capacity (); }
111   void sendByte (int);
112   void sendIVal (int);
113   void sendLVal (long long);
114   void sendDVal (double);
115   void sendSVal (const char *);
116   void sendBVal (bool);
117   void sendCVal (char);
118   void sendAVal (void*);
119   void print (void);
120   void reset ();
121   IPCresponse *next;
122 
123 private:
124   int requestID;
125   int channelID;
126   int responseType;
127   int responseStatus;
128   StringBuilder *sb;
129 };
130 
131 class BufferPool
132 {
133 public:
134   BufferPool ();
135   ~BufferPool ();
136   IPCresponse* getNewResponse (int);
137   void recycle (IPCresponse *);
138 private:
139   pthread_mutex_t p_mutex;
140   IPCresponse *smallBuf;
141   IPCresponse *largeBuf;
142 };
143 
144 // Read from the wire
145 int readInt (IPCrequest*);
146 bool readBoolean (IPCrequest*);
147 long long readLong (IPCrequest*);
148 DbeObj readObject (IPCrequest*);
149 Object readArray (IPCrequest*);
150 String readString (IPCrequest*);
151 void readRequestHeader ();
152 
153 // write to the wire
154 void writeString (const char *, IPCrequest*);
155 void writeBoolean (bool, IPCrequest*);
156 void writeInt (int, IPCrequest*);
157 void writeChar (char, IPCrequest*);
158 void writeLong (long long, IPCrequest*);
159 void writeDouble (double, IPCrequest*);
160 void writeArray (void *, IPCrequest*);
161 void writeObject (DbeObj, IPCrequest*);
162 void writeResponseGeneric (int, int, int);
163 int setProgress (int, const char *);    // Update the progress bar
164 int ipc_doWork (void *);                // The argument is an IPCrequest
165 
166 extern int ipc_flags;
167 extern int ipc_single_threaded_mode;
168 extern DbeThreadPool *responseThreadPool;
169 extern DbeThreadPool *ipcThreadPool;
170 extern int cancelRequestedChannelID;
171 extern int cancellableChannelID;
172 extern int error_flag;
173 
174 void ipc_default_log (const char *fmt, ...) __attribute__ ((format (printf, 1, 2)));
175 void ipc_response_log (IPCTraceLevel, const char *fmt, ...) __attribute__ ((format (printf, 2, 3)));
176 void ipc_request_log (IPCTraceLevel, const char *fmt, ...) __attribute__ ((format (printf, 2, 3)));
177 
178 #endif
179