Quickier thread-specific data on OS/2
[p5sagit/p5-mst-13.2.git] / os2 / os2ish.h
1 #include <signal.h>
2
3 /* HAS_IOCTL:
4  *      This symbol, if defined, indicates that the ioctl() routine is
5  *      available to set I/O characteristics
6  */
7 #define HAS_IOCTL               /**/
8  
9 /* HAS_UTIME:
10  *      This symbol, if defined, indicates that the routine utime() is
11  *      available to update the access and modification times of files.
12  */
13 #define HAS_UTIME               /**/
14
15 #define HAS_KILL
16 #define HAS_WAIT
17 #define HAS_DLERROR
18 #define HAS_WAITPID_RUNTIME (_emx_env & 0x200)
19
20 /* USEMYBINMODE
21  *      This symbol, if defined, indicates that the program should
22  *      use the routine my_binmode(FILE *fp, char iotype) to insure
23  *      that a file is in "binary" mode -- that is, that no translation
24  *      of bytes occurs on read or write operations.
25  */
26 #undef USEMYBINMODE
27
28 /* Stat_t:
29  *      This symbol holds the type used to declare buffers for information
30  *      returned by stat().  It's usually just struct stat.  It may be necessary
31  *      to include <sys/stat.h> and <sys/types.h> to get any typedef'ed
32  *      information.
33  */
34 #define Stat_t struct stat
35
36 /* USE_STAT_RDEV:
37  *      This symbol is defined if this system has a stat structure declaring
38  *      st_rdev
39  */
40 #define USE_STAT_RDEV   /**/
41
42 /* ACME_MESS:
43  *      This symbol, if defined, indicates that error messages should be 
44  *      should be generated in a format that allows the use of the Acme
45  *      GUI/editor's autofind feature.
46  */
47 #undef ACME_MESS        /**/
48
49 /* ALTERNATE_SHEBANG:
50  *      This symbol, if defined, contains a "magic" string which may be used
51  *      as the first line of a Perl program designed to be executed directly
52  *      by name, instead of the standard Unix #!.  If ALTERNATE_SHEBANG
53  *      begins with a character other then #, then Perl will only treat
54  *      it as a command line if if finds the string "perl" in the first
55  *      word; otherwise it's treated as the first line of code in the script.
56  *      (IOW, Perl won't hand off to another interpreter via an alternate
57  *      shebang sequence that might be legal Perl code.)
58  */
59 #define ALTERNATE_SHEBANG "extproc "
60
61 #ifndef SIGABRT
62 #    define SIGABRT SIGILL
63 #endif
64 #ifndef SIGILL
65 #    define SIGILL 6         /* blech */
66 #endif
67 #define ABORT() kill(getpid(),SIGABRT);
68
69 #define BIT_BUCKET "/dev/nul"  /* Will this work? */
70
71 #if defined(I_SYS_UN) && !defined(TCPIPV4)
72 /* It is not working without TCPIPV4 defined. */
73 # undef I_SYS_UN
74 #endif 
75
76 #ifdef USE_THREADS
77
78 #define OS2_ERROR_ALREADY_POSTED 299    /* Avoid os2.h */
79
80 extern int rc;
81
82 #define MUTEX_INIT(m) \
83     STMT_START {                                                \
84         int rc;                                                 \
85         if ((rc = _rmutex_create(m,0)))                         \
86             croak("panic: MUTEX_INIT: rc=%i", rc);              \
87     } STMT_END
88 #define MUTEX_LOCK(m) \
89     STMT_START {                                                \
90         int rc;                                                 \
91         if ((rc = _rmutex_request(m,_FMR_IGNINT)))              \
92             croak("panic: MUTEX_LOCK: rc=%i", rc);              \
93     } STMT_END
94 #define MUTEX_UNLOCK(m) \
95     STMT_START {                                                \
96         int rc;                                                 \
97         if ((rc = _rmutex_release(m)))                          \
98             croak("panic: MUTEX_UNLOCK: rc=%i", rc);            \
99     } STMT_END
100 #define MUTEX_DESTROY(m) \
101     STMT_START {                                                \
102         int rc;                                                 \
103         if ((rc = _rmutex_close(m)))                            \
104             croak("panic: MUTEX_DESTROY: rc=%i", rc);           \
105     } STMT_END
106
107 #define COND_INIT(c) \
108     STMT_START {                                                \
109         int rc;                                                 \
110         if ((rc = DosCreateEventSem(NULL,c,0,0)))               \
111             croak("panic: COND_INIT: rc=%i", rc);               \
112     } STMT_END
113 #define COND_SIGNAL(c) \
114     STMT_START {                                                \
115         int rc;                                                 \
116         if ((rc = DosPostEventSem(*(c))) && rc != OS2_ERROR_ALREADY_POSTED)             \
117             croak("panic: COND_SIGNAL, rc=%ld", rc);            \
118     } STMT_END
119 #define COND_BROADCAST(c) \
120     STMT_START {                                                \
121         int rc;                                                 \
122         if ((rc = DosPostEventSem(*(c))) && rc != OS2_ERROR_ALREADY_POSTED)\
123             croak("panic: COND_BROADCAST, rc=%i", rc);          \
124     } STMT_END
125 /* #define COND_WAIT(c, m) \
126     STMT_START {                                                \
127         if (WaitForSingleObject(*(c),INFINITE) == WAIT_FAILED)  \
128             croak("panic: COND_WAIT");                          \
129     } STMT_END
130 */
131 #define COND_WAIT(c, m) os2_cond_wait(c,m)
132
133 #define COND_WAIT_win32(c, m) \
134     STMT_START {                                                \
135         int rc;                                                 \
136         if ((rc = SignalObjectAndWait(*(m),*(c),INFINITE,FALSE)))\
137             croak("panic: COND_WAIT");                          \
138         else                                                    \
139             MUTEX_LOCK(m);                                      \
140     } STMT_END
141 #define COND_DESTROY(c) \
142     STMT_START {                                                \
143         int rc;                                                 \
144         if ((rc = DosCloseEventSem(*(c))))                      \
145             croak("panic: COND_DESTROY, rc=%i", rc);            \
146     } STMT_END
147 /*#define THR ((struct thread *) TlsGetValue(PL_thr_key))
148 #define dTHR struct thread *thr = THR
149 */
150
151 #ifdef USE_SLOW_THREAD_SPECIFIC
152 #  define pthread_getspecific(k)        (*_threadstore())
153 #  define pthread_setspecific(k,v)      (*_threadstore()=v,0)
154 #  define pthread_key_create(keyp,flag) (*keyp=_gettid(),0)
155 #else
156 #  define pthread_getspecific(k)        (*(k))
157 #  define pthread_setspecific(k,v)      (*(k)=(v),0)
158 #  define pthread_key_create(keyp,flag) (DosAllocThreadLocalMemory(1,(U32*)keyp) ? croak("LocalMemory"),1 : 0)
159 #endif
160 #define pthread_key_delete(keyp)
161 #define pthread_self()                  _gettid()
162 #define YIELD                           DosSleep(0)
163
164 #ifdef PTHREADS_INCLUDED                /* For ./x2p stuff. */
165 int pthread_join(pthread_t tid, void **status);
166 int pthread_detach(pthread_t tid);
167 int pthread_create(pthread_t *tid, const pthread_attr_t *attr,
168                    void *(*start_routine)(void*), void *arg);
169 #endif 
170
171 #define THREADS_ELSEWHERE
172
173 #endif 
174  
175 void Perl_OS2_init(char **);
176
177 /* XXX This code hideously puts env inside: */
178
179 #ifdef __EMX__
180 #  define PERL_SYS_INIT(argcp, argvp) STMT_START {      \
181     _response(argcp, argvp);                    \
182     _wildcard(argcp, argvp);                    \
183     Perl_OS2_init(env); } STMT_END
184 #else  /* Compiling embedded Perl with non-EMX compiler */
185 #  define PERL_SYS_INIT(argcp, argvp) STMT_START {      \
186     Perl_OS2_init(env); } STMT_END
187 #  define PERL_CALLCONV _System
188 #endif
189 #define PERL_SYS_TERM()         MALLOC_TERM
190
191 /* #define PERL_SYS_TERM() STMT_START { \
192     if (Perl_HAB_set) WinTerminate(Perl_hab);   } STMT_END */
193
194 #define dXSUB_SYS OS2_XS_init()
195
196 #ifdef PERL_IS_AOUT
197 /* #  define HAS_FORK */
198 /* #  define HIDEMYMALLOC */
199 /* #  define PERL_SBRK_VIA_MALLOC */ /* gets off-page sbrk... */
200 #else /* !PERL_IS_AOUT */
201 #  ifndef PERL_FOR_X2P
202 #    ifdef EMX_BAD_SBRK
203 #      define USE_PERL_SBRK
204 #    endif 
205 #  else
206 #    define PerlIO FILE
207 #  endif 
208 #  define SYSTEM_ALLOC(a) sys_alloc(a)
209
210 void *sys_alloc(int size);
211
212 #endif /* !PERL_IS_AOUT */
213 #if !defined(PERL_CORE) && !defined(PerlIO) /* a2p */
214 #  define PerlIO FILE
215 #endif 
216
217 #define TMPPATH tmppath
218 #define TMPPATH1 "plXXXXXX"
219 extern char *tmppath;
220 PerlIO *my_syspopen(char *cmd, char *mode);
221 /* Cannot prototype with I32 at this point. */
222 int my_syspclose(PerlIO *f);
223 FILE *my_tmpfile (void);
224 char *my_tmpnam (char *);
225
226 #define tmpfile my_tmpfile
227 #define tmpnam  my_tmpnam
228 #define isatty  _isterm
229 #define rand    random
230 #define srand   srandom
231
232 /*
233  * fwrite1() should be a routine with the same calling sequence as fwrite(),
234  * but which outputs all of the bytes requested as a single stream (unlike
235  * fwrite() itself, which on some systems outputs several distinct records
236  * if the number_of_items parameter is >1).
237  */
238 #define fwrite1 fwrite
239
240 #define my_getenv(var) getenv(var)
241 #define flock   my_flock
242
243 void *emx_calloc (size_t, size_t);
244 void emx_free (void *);
245 void *emx_malloc (size_t);
246 void *emx_realloc (void *, size_t);
247
248 /*****************************************************************************/
249
250 #include <stdlib.h>     /* before the following definitions */
251 #include <unistd.h>     /* before the following definitions */
252
253 #define chdir   _chdir2
254 #define getcwd  _getcwd2
255
256 /* This guy is needed for quick stdstd  */
257
258 #if defined(USE_STDIO_PTR) && defined(STDIO_PTR_LVALUE) && defined(STDIO_CNT_LVALUE)
259         /* Perl uses ungetc only with successful return */
260 #  define ungetc(c,fp) \
261         (FILE_ptr(fp) > FILE_base(fp) && c == (int)*(FILE_ptr(fp) - 1) \
262          ? (--FILE_ptr(fp), ++FILE_cnt(fp), (int)c) : ungetc(c,fp))
263 #endif
264
265 #define OP_BINARY O_BINARY
266
267 #define OS2_STAT_HACK 1
268 #if OS2_STAT_HACK
269
270 #define Stat(fname,bufptr) os2_stat((fname),(bufptr))
271 #define Fstat(fd,bufptr)   fstat((fd),(bufptr))
272 #define Fflush(fp)         fflush(fp)
273 #define Mkdir(path,mode)   mkdir((path),(mode))
274
275 #undef S_IFBLK
276 #undef S_ISBLK
277 #define S_IFBLK         0120000
278 #define S_ISBLK(mode)   (((mode) & S_IFMT) == S_IFBLK)
279
280 #else
281
282 #define Stat(fname,bufptr) stat((fname),(bufptr))
283 #define Fstat(fd,bufptr)   fstat((fd),(bufptr))
284 #define Fflush(fp)         fflush(fp)
285 #define Mkdir(path,mode)   mkdir((path),(mode))
286
287 #endif
288
289 /* With SD386 it is impossible to debug register variables. */
290 #if !defined(PERL_IS_AOUT) && defined(DEBUGGING) && !defined(register)
291 #  define register
292 #endif
293
294 /* Our private OS/2 specific data. */
295
296 typedef struct OS2_Perl_data {
297   unsigned long flags;
298   unsigned long phab;
299   int (*xs_init)();
300   unsigned long rc;
301   unsigned long severity;
302   unsigned long phmq;                   /* Handle to message queue */
303   unsigned long phmq_refcnt;
304   unsigned long phmq_servers;
305   unsigned long initial_mode;           /* VIO etc. mode we were started in */
306 } OS2_Perl_data_t;
307
308 extern OS2_Perl_data_t OS2_Perl_data;
309
310 #define Perl_hab                ((HAB)OS2_Perl_data.phab)
311 #define Perl_rc                 (OS2_Perl_data.rc)
312 #define Perl_severity           (OS2_Perl_data.severity)
313 #define errno_isOS2             12345678
314 #define OS2_Perl_flags  (OS2_Perl_data.flags)
315 #define Perl_HAB_set_f  1
316 #define Perl_HAB_set    (OS2_Perl_flags & Perl_HAB_set_f)
317 #define set_Perl_HAB_f  (OS2_Perl_flags |= Perl_HAB_set_f)
318 #define set_Perl_HAB(h) (set_Perl_HAB_f, Perl_hab = h)
319 #define _obtain_Perl_HAB (init_PMWIN_entries(),                         \
320                           Perl_hab = (*PMWIN_entries.Initialize)(0),    \
321                           set_Perl_HAB_f, Perl_hab)
322 #define perl_hab_GET()  (Perl_HAB_set ? Perl_hab : _obtain_Perl_HAB)
323 #define Acquire_hab()   perl_hab_GET()
324 #define Perl_hmq        ((HMQ)OS2_Perl_data.phmq)
325 #define Perl_hmq_refcnt (OS2_Perl_data.phmq_refcnt)
326 #define Perl_hmq_servers        (OS2_Perl_data.phmq_servers)
327 #define Perl_os2_initial_mode   (OS2_Perl_data.initial_mode)
328
329 unsigned long Perl_hab_GET();
330 unsigned long Perl_Register_MQ(int serve);
331 void    Perl_Deregister_MQ(int serve);
332 int     Perl_Serve_Messages(int force);
333 /* Cannot prototype with I32 at this point. */
334 int     Perl_Process_Messages(int force, long *cntp);
335
336 struct _QMSG;
337 struct PMWIN_entries_t {
338     unsigned long (*Initialize)( unsigned long fsOptions );
339     unsigned long (*CreateMsgQueue)(unsigned long hab, long cmsg);
340     int (*DestroyMsgQueue)(unsigned long hmq);
341     int (*PeekMsg)(unsigned long hab, struct _QMSG *pqmsg,
342                    unsigned long hwndFilter, unsigned long msgFilterFirst,
343                    unsigned long msgFilterLast, unsigned long fl);
344     int (*GetMsg)(unsigned long hab, struct _QMSG *pqmsg,
345                   unsigned long hwndFilter, unsigned long msgFilterFirst,
346                   unsigned long msgFilterLast);
347     void * (*DispatchMsg)(unsigned long hab, struct _QMSG *pqmsg);
348 };
349 extern struct PMWIN_entries_t PMWIN_entries;
350 void init_PMWIN_entries(void);
351
352 #define perl_hmq_GET(serve)     Perl_Register_MQ(serve);
353 #define perl_hmq_UNSET(serve)   Perl_Deregister_MQ(serve);
354
355 #define OS2_XS_init() (*OS2_Perl_data.xs_init)()
356 /* The expressions below return true on error. */
357 /* INCL_DOSERRORS needed. rc should be declared outside. */
358 #define CheckOSError(expr) (!(rc = (expr)) ? 0 : (FillOSError(rc), 1))
359 /* INCL_WINERRORS needed. */
360 #define SaveWinError(expr) ((expr) ? : (FillWinError, 0))
361 #define CheckWinError(expr) ((expr) ? 0: (FillWinError, 1))
362 #define FillOSError(rc) (Perl_rc = rc,                                  \
363                         errno = errno_isOS2,                            \
364                         Perl_severity = SEVERITY_ERROR) 
365 #define FillWinError (Perl_rc = WinGetLastError(Perl_hab),              \
366                         errno = errno_isOS2,                            \
367                         Perl_severity = ERRORIDSEV(Perl_rc),            \
368                         Perl_rc = ERRORIDERROR(Perl_rc)) 
369
370 #define STATIC_FILE_LENGTH 127
371
372 #define PERLLIB_MANGLE(s, n) perllib_mangle((s), (n))
373 char *perllib_mangle(char *, unsigned int);
374
375 char *os2error(int rc);
376
377 /* ************************************************************ */
378 #define Dos32QuerySysState DosQuerySysState
379 #define QuerySysState(flags, pid, buf, bufsz) \
380         Dos32QuerySysState(flags, 0,  pid, 0, buf, bufsz)
381
382 #define QSS_PROCESS     1
383 #define QSS_MODULE      4
384 #define QSS_SEMAPHORES  2
385 #define QSS_FILE        8               /* Buggy until fixpack18 */
386 #define QSS_SHARED      16
387
388 #ifdef _OS2EMX_H
389
390 APIRET APIENTRY Dos32QuerySysState(ULONG func,ULONG arg1,ULONG pid,
391                         ULONG _res_,PVOID buf,ULONG bufsz);
392 typedef struct {
393         ULONG   threadcnt;
394         ULONG   proccnt;
395         ULONG   modulecnt;
396 } QGLOBAL, *PQGLOBAL;
397
398 typedef struct {
399         ULONG   rectype;
400         USHORT  threadid;
401         USHORT  slotid;
402         ULONG   sleepid;
403         ULONG   priority;
404         ULONG   systime;
405         ULONG   usertime;
406         UCHAR   state;
407         UCHAR   _reserved1_;    /* padding to ULONG */
408         USHORT  _reserved2_;    /* padding to ULONG */
409 } QTHREAD, *PQTHREAD;
410
411 typedef struct {
412         USHORT  sfn;
413         USHORT  refcnt;
414         USHORT  flags1;
415         USHORT  flags2;
416         USHORT  accmode1;
417         USHORT  accmode2;
418         ULONG   filesize;
419         USHORT  volhnd;
420         USHORT  attrib;
421         USHORT  _reserved_;
422 } QFDS, *PQFDS;
423
424 typedef struct qfile {
425         ULONG           rectype;
426         struct qfile    *next;
427         ULONG           opencnt;
428         PQFDS           filedata;
429         char            name[1];
430 } QFILE, *PQFILE;
431
432 typedef struct {
433         ULONG   rectype;
434         PQTHREAD threads;
435         USHORT  pid;
436         USHORT  ppid;
437         ULONG   type;
438         ULONG   state;
439         ULONG   sessid;
440         USHORT  hndmod;
441         USHORT  threadcnt;
442         ULONG   privsem32cnt;
443         ULONG   _reserved2_;
444         USHORT  sem16cnt;
445         USHORT  dllcnt;
446         USHORT  shrmemcnt;
447         USHORT  fdscnt;
448         PUSHORT sem16s;
449         PUSHORT dlls;
450         PUSHORT shrmems;
451         PUSHORT fds;
452 } QPROCESS, *PQPROCESS;
453
454 typedef struct sema {
455         struct sema *next;
456         USHORT  refcnt;
457         UCHAR   sysflags;
458         UCHAR   sysproccnt;
459         ULONG   _reserved1_;
460         USHORT  index;
461         CHAR    name[1];
462 } QSEMA, *PQSEMA;
463
464 typedef struct {
465         ULONG   rectype;
466         ULONG   _reserved1_;
467         USHORT  _reserved2_;
468         USHORT  syssemidx;
469         ULONG   index;
470         QSEMA   sema;
471 } QSEMSTRUC, *PQSEMSTRUC;
472
473 typedef struct {
474         USHORT  pid;
475         USHORT  opencnt;
476 } QSEMOWNER32, *PQSEMOWNER32;
477
478 typedef struct {
479         PQSEMOWNER32    own;
480         PCHAR           name;
481         PVOID           semrecs; /* array of associated sema's */
482         USHORT          flags;
483         USHORT          semreccnt;
484         USHORT          waitcnt;
485         USHORT          _reserved_;     /* padding to ULONG */
486 } QSEMSMUX32, *PQSEMSMUX32;
487
488 typedef struct {
489         PQSEMOWNER32    own;
490         PCHAR           name;
491         PQSEMSMUX32     mux;
492         USHORT          flags;
493         USHORT          postcnt;
494 } QSEMEV32, *PQSEMEV32;
495
496 typedef struct {
497         PQSEMOWNER32    own;
498         PCHAR           name;
499         PQSEMSMUX32     mux;
500         USHORT          flags;
501         USHORT          refcnt;
502         USHORT          thrdnum;
503         USHORT          _reserved_;     /* padding to ULONG */
504 } QSEMMUX32, *PQSEMMUX32;
505
506 typedef struct semstr32 {
507         struct semstr *next;
508         QSEMEV32 evsem;
509         QSEMMUX32  muxsem;
510         QSEMSMUX32 smuxsem;
511 } QSEMSTRUC32, *PQSEMSTRUC32;
512
513 typedef struct shrmem {
514         struct shrmem *next;
515         USHORT  hndshr;
516         USHORT  selshr;
517         USHORT  refcnt;
518         CHAR    name[1];
519 } QSHRMEM, *PQSHRMEM;
520
521 typedef struct module {
522         struct module *next;
523         USHORT  hndmod;
524         USHORT  type;
525         ULONG   refcnt;
526         ULONG   segcnt;
527         PVOID   _reserved_;
528         PCHAR   name;
529         USHORT  modref[1];
530 } QMODULE, *PQMODULE;
531
532 typedef struct {
533         PQGLOBAL        gbldata;
534         PQPROCESS       procdata;
535         PQSEMSTRUC      semadata;
536         PQSEMSTRUC32    sem32data;
537         PQSHRMEM        shrmemdata;
538         PQMODULE        moddata;
539         PVOID           _reserved2_;
540         PQFILE          filedata;
541 } QTOPLEVEL, *PQTOPLEVEL;
542 /* ************************************************************ */
543
544 PQTOPLEVEL get_sysinfo(ULONG pid, ULONG flags);
545
546 #endif /* _OS2EMX_H */
547