home *** CD-ROM | disk | FTP | other *** search
/ Tools / WinSN5.0Ver.iso / NETSCAP.50 / WIN1998.ZIP / ns / nsprpub / pr / include / md / _winnt.h < prev    next >
Encoding:
C/C++ Source or Header  |  1998-04-08  |  15.8 KB  |  415 lines

  1. /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
  2. /*
  3.  * The contents of this file are subject to the Netscape Public License
  4.  * Version 1.0 (the "NPL"); you may not use this file except in
  5.  * compliance with the NPL.  You may obtain a copy of the NPL at
  6.  * http://www.mozilla.org/NPL/
  7.  * 
  8.  * Software distributed under the NPL is distributed on an "AS IS" basis,
  9.  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the NPL
  10.  * for the specific language governing rights and limitations under the
  11.  * NPL.
  12.  * 
  13.  * The Initial Developer of this code under the NPL is Netscape
  14.  * Communications Corporation.  Portions created by Netscape are
  15.  * Copyright (C) 1998 Netscape Communications Corporation.  All Rights
  16.  * Reserved.
  17.  */
  18.  
  19. #ifndef nspr_win32_defs_h___
  20. #define nspr_win32_defs_h___
  21.  
  22. /* Need to force service-pack 3 extensions to be defined by
  23. ** setting _WIN32_WINNT to NT 4.0 for winsock.h, winbase.h, winnt.h.
  24. */ 
  25. #ifndef  _WIN32_WINNT
  26.     #define _WIN32_WINNT 0x0400
  27. #elif   (_WIN32_WINNT < 0x0400)
  28.     #undef  _WIN32_WINNT
  29.     #define _WIN32_WINNT 0x0400
  30. #endif /* _WIN32_WINNT */
  31.  
  32. #include <windows.h>
  33. #include <winsock.h>
  34. #include <errno.h>
  35.  
  36. #include "prio.h"
  37.  
  38. /*
  39.  * Internal configuration macros
  40.  */
  41.  
  42. #define PR_LINKER_ARCH      "win32"
  43. #define _PR_SI_SYSNAME        "WINNT"
  44. #define _PR_SI_ARCHITECTURE   "x86"    /* XXXMB hardcode for now */
  45.  
  46. #define HAVE_DLL
  47. #define HAVE_CUSTOM_USER_THREADS
  48. #define HAVE_THREAD_AFFINITY
  49. #define HAVE_SOCKET_REUSEADDR
  50. #define HAVE_SOCKET_KEEPALIVE
  51. #define _PR_HAVE_ATOMIC_OPS
  52.  
  53. typedef char * caddr_t;
  54. typedef int    ptrdiff_t;   /* used in prnetdb.h */
  55.  
  56.  
  57. /* --- Common User-Thread/Native-Thread Definitions --------------------- */
  58.  
  59. /* --- Globals --- */
  60. extern struct PRLock                      *_pr_schedLock;
  61.  
  62. /* --- Typedefs --- */
  63. typedef void (*FiberFunc)(void *);
  64.  
  65. #define PR_NUM_GCREGS           8
  66. typedef PRInt32                    PR_CONTEXT_TYPE[PR_NUM_GCREGS];
  67. #define GC_VMBASE               0x40000000
  68. #define GC_VMLIMIT              0x00FFFFFF
  69.  
  70. #define _MD_MAGIC_THREAD    0x22222222
  71. #define _MD_MAGIC_THREADSTACK    0x33333333
  72. #define _MD_MAGIC_SEGMENT    0x44444444
  73. #define _MD_MAGIC_DIR        0x55555555
  74.  
  75. struct _MDCPU {
  76.     int              unused;
  77. };
  78.  
  79. struct _MDThread {
  80.         /* The overlapped structure must be first! */
  81.     OVERLAPPED       overlapped;        /* Used for async IO for this thread */
  82.     void            *acceptex_buf;      /* Used for AcceptEx() */
  83.     TRANSMIT_FILE_BUFFERS *xmit_bufs;   /* Used for TransmitFile() */
  84.     HANDLE           blocked_sema;      /* Threads block on this when waiting
  85.                                          * for IO or CondVar.
  86.                                          */
  87.     PRInt32          blocked_io_status; /* Status of the completed IO */
  88.     PRInt32          blocked_io_bytes;  /* Bytes transferred for completed IO */
  89.     PRInt32          blocked_io_error;  /* Save error if status is FALSE */
  90.     HANDLE           handle;
  91.     PRUint32         id;
  92.     void            *sp;                /* only valid when suspended */
  93.     PRUint32         magic;             /* for debugging */
  94.     PR_CONTEXT_TYPE  gcContext;         /* Thread context for GC */
  95.  
  96.     /* The following are used only if this is a fiber */
  97.     void            *fiber_id;          /* flag whether or not this is a fiber*/
  98.     FiberFunc        fiber_fn;          /* main fiber routine */
  99.     void            *fiber_arg;         /* arg to main fiber routine */
  100.     PRUint32         fiber_stacksize;   /* stacksize for fiber */
  101.     PRInt32          fiber_last_error;  /* last error for the fiber */
  102. };
  103.  
  104. struct _MDThreadStack {
  105.     PRUint32           magic;          /* for debugging */
  106. };
  107.  
  108. struct _MDSegment {
  109.     PRUint32           magic;          /* for debugging */
  110. };
  111.  
  112. #undef PROFILE_LOCKS
  113.  
  114. struct _MDLock {
  115.     CRITICAL_SECTION mutex;          /* this is recursive on NT */
  116. #ifdef PROFILE_LOCKS
  117.     PRInt32 hitcount;
  118.     PRInt32 misscount;
  119. #endif
  120. };
  121.  
  122. struct _MDDir {
  123.     HANDLE           d_hdl;
  124.     WIN32_FIND_DATA  d_entry;
  125.     PRBool           firstEntry;     /* Is this the entry returned
  126.                                       * by FindFirstFile()? */
  127.     PRUint32         magic;          /* for debugging */
  128. };
  129.  
  130. struct _MDCVar {
  131.     PRUint32         unused;
  132. };
  133.  
  134. struct _MDSemaphore {
  135.     HANDLE           sem;
  136. };
  137.  
  138. struct _MDFileDesc {
  139.     PRInt32 osfd;    /* The osfd can come from one of three spaces:
  140.                       * - For stdin, stdout, and stderr, we are using
  141.                       *   the libc file handle (0, 1, 2), which is an int.
  142.                       * - For files and pipes, we are using Win32 HANDLE,
  143.                       *   which is a void*.
  144.                       * - For sockets, we are using Winsock SOCKET, which
  145.                       *   is a u_int.
  146.                       */
  147.     PRBool io_model_committed;  /* The io model (blocking or nonblocking)
  148.                                  * for this osfd has been committed and
  149.                                  * cannot be changed.  The osfd has been
  150.                                  * either associated with the io
  151.                                  * completion port or made nonblocking.
  152.                                  * This is only used for sockets, not
  153.                                  * for files. */
  154.     PRBool nonoverlapped;
  155.     PRBool accepted_socket;     /* Is this an accepted socket (on the
  156.                                  * server side)? */
  157.     PRNetAddr peer_addr;        /* If this is an accepted socket, cache
  158.                                  * the peer's address returned by
  159.                                  * AcceptEx().  This is to work around
  160.                                  * the bug that getpeername() on an
  161.                                  * socket accepted by AcceptEx() returns
  162.                                  * an all-zero net address. */
  163. };
  164.  
  165. struct _MDProcess {
  166.     HANDLE handle;
  167.     DWORD id;
  168. };
  169.  
  170. /* --- Misc stuff --- */
  171. #define _MD_GET_SP(thread)            (thread)->md.gcContext[6]
  172.  
  173. /* --- IO stuff --- */
  174.  
  175. extern PRInt32 _PR_MD_CLOSE(PRInt32 osfd, PRBool socket);
  176.  
  177. #define _MD_OPEN                      _PR_MD_OPEN
  178. #define _MD_READ                      _PR_MD_READ
  179. #define _MD_WRITE                     _PR_MD_WRITE
  180. #define _MD_WRITEV                    _PR_MD_WRITEV
  181. #define _MD_LSEEK                     _PR_MD_LSEEK
  182. #define _MD_CLOSE_FILE(f)             _PR_MD_CLOSE(f, PR_FALSE)
  183. #define _MD_GETFILEINFO               _PR_MD_GETFILEINFO
  184. #define _MD_GETOPENFILEINFO           _PR_MD_GETOPENFILEINFO
  185. #define _MD_STAT                      _PR_MD_STAT
  186. #define _MD_RENAME                    _PR_MD_RENAME     
  187. #define _MD_ACCESS                    _PR_MD_ACCESS     
  188. #define _MD_DELETE                    _PR_MD_DELETE     
  189. #define _MD_MKDIR                     _PR_MD_MKDIR      
  190. #define _MD_RMDIR                     _PR_MD_RMDIR      
  191. #define _MD_LOCKFILE                  _PR_MD_LOCKFILE
  192. #define _MD_TLOCKFILE                 _PR_MD_TLOCKFILE
  193. #define _MD_UNLOCKFILE                _PR_MD_UNLOCKFILE
  194.  
  195. /* --- Socket IO stuff --- */
  196. #define _MD_GET_SOCKET_ERROR()    WSAGetLastError()
  197. #define _MD_SET_SOCKET_ERROR(_err) WSASetLastError(_err)
  198.  
  199. #define _MD_MAKE_NONBLOCK             _PR_MD_MAKE_NONBLOCK
  200. #define _MD_SHUTDOWN                  _PR_MD_SHUTDOWN
  201. #define _MD_LISTEN                    _PR_MD_LISTEN
  202. #define _MD_CLOSE_SOCKET(s)           _PR_MD_CLOSE(s, PR_TRUE)
  203. #define _MD_SENDTO                    _PR_MD_SENDTO
  204. #define _MD_RECVFROM                  _PR_MD_RECVFROM
  205. #define _MD_SOCKETPAIR(s, type, proto, sv) -1
  206. #define _MD_GETSOCKNAME               _PR_MD_GETSOCKNAME
  207. #define _MD_GETPEERNAME               _PR_MD_GETPEERNAME
  208. #define _MD_GETSOCKOPT                _PR_MD_GETSOCKOPT
  209. #define _MD_SETSOCKOPT                _PR_MD_SETSOCKOPT
  210. #define _MD_SELECT                    select
  211. #define _MD_FSYNC                     _PR_MD_FSYNC
  212. #define _MD_SOCKETAVAILABLE           _PR_MD_SOCKETAVAILABLE
  213.  
  214. #define _MD_INIT_ATOMIC()
  215. #define _MD_ATOMIC_INCREMENT(x)       InterlockedIncrement((PLONG)x)
  216. #define _MD_ATOMIC_DECREMENT(x)       InterlockedDecrement((PLONG)x)
  217. #define _MD_ATOMIC_SET(x,y)           InterlockedExchange((PLONG)x, (LONG)y)
  218.  
  219. #define _MD_INIT_IO                   _PR_MD_INIT_IO
  220. #define _MD_SOCKET                    _PR_MD_SOCKET
  221. #define _MD_CONNECT                   _PR_MD_CONNECT
  222.  
  223. #define _MD_ACCEPT(s, a, l, to)       \
  224.         _MD_FAST_ACCEPT(s, a, l, to, PR_FALSE, NULL, NULL)
  225. #define _MD_FAST_ACCEPT(s, a, l, to, fast, cb, cba) \
  226.         _PR_MD_FAST_ACCEPT(s, a, l, to, fast, cb, cba)
  227. #define _MD_ACCEPT_READ(s, ns, ra, buf, l, t) \
  228.         _MD_FAST_ACCEPT_READ(s, ns, ra, buf, l, t, PR_FALSE, NULL, NULL)
  229. #define _MD_FAST_ACCEPT_READ(s, ns, ra, buf, l, t, fast, cb, cba) \
  230.         _PR_MD_FAST_ACCEPT_READ(s, ns, ra, buf, l, t, fast, cb, cba)
  231. #define _MD_UPDATE_ACCEPT_CONTEXT     _PR_MD_UPDATE_ACCEPT_CONTEXT
  232.  
  233. #define _MD_BIND                      _PR_MD_BIND
  234. #define _MD_RECV                      _PR_MD_RECV
  235. #define _MD_SEND                      _PR_MD_SEND
  236. #define _MD_TRANSMITFILE              _PR_MD_TRANSMITFILE
  237. #define _MD_PR_POLL                   _PR_MD_PR_POLL
  238.  
  239. /* --- Scheduler stuff --- */
  240. #define _MD_PAUSE_CPU                   _PR_MD_PAUSE_CPU
  241.  
  242. /* --- DIR stuff --- */
  243. #define PR_DIRECTORY_SEPARATOR        '\\'
  244. #define PR_DIRECTORY_SEPARATOR_STR    "\\"
  245. #define PR_PATH_SEPARATOR        ';'
  246. #define PR_PATH_SEPARATOR_STR        ";"
  247. #define _MD_ERRNO()                   GetLastError()
  248. #define _MD_OPEN_DIR                  _PR_MD_OPEN_DIR
  249. #define _MD_CLOSE_DIR                 _PR_MD_CLOSE_DIR
  250. #define _MD_READ_DIR                  _PR_MD_READ_DIR
  251.  
  252. /* --- Segment stuff --- */
  253. #define _MD_INIT_SEGS()
  254. #define _MD_ALLOC_SEGMENT(seg, size, vaddr)   0
  255. #define _MD_FREE_SEGMENT(seg)
  256.  
  257. /* --- Environment Stuff --- */
  258. #define _MD_GET_ENV                 _PR_MD_GET_ENV
  259. #define _MD_PUT_ENV                 _PR_MD_PUT_ENV
  260.  
  261. /* --- Threading Stuff --- */
  262. #define _MD_DEFAULT_STACK_SIZE            0
  263. #define _MD_INIT_THREAD             _PR_MD_INIT_THREAD
  264. #define _MD_INIT_ATTACHED_THREAD    _PR_MD_INIT_THREAD
  265. #define _MD_INIT_PRIMORDIAL_THREAD  _PR_MD_INIT_PRIMORDIAL_THREAD
  266. #define _MD_CREATE_THREAD           _PR_MD_CREATE_THREAD
  267. #define _MD_YIELD                   _PR_MD_YIELD
  268. #define _MD_SET_PRIORITY            _PR_MD_SET_PRIORITY
  269. #define _MD_CLEAN_THREAD            _PR_MD_CLEAN_THREAD
  270. #define _MD_SETTHREADAFFINITYMASK   _PR_MD_SETTHREADAFFINITYMASK
  271. #define _MD_GETTHREADAFFINITYMASK   _PR_MD_GETTHREADAFFINITYMASK
  272. #define _MD_EXIT_THREAD             _PR_MD_EXIT_THREAD
  273. #define _MD_SUSPEND_THREAD          _PR_MD_SUSPEND_THREAD
  274. #define _MD_RESUME_THREAD           _PR_MD_RESUME_THREAD
  275. #define _MD_SUSPEND_CPU             _PR_MD_SUSPEND_CPU
  276. #define _MD_RESUME_CPU              _PR_MD_RESUME_CPU
  277. #define _MD_BEGIN_SUSPEND_ALL()
  278. #define _MD_BEGIN_RESUME_ALL()
  279. #define _MD_END_SUSPEND_ALL()
  280. #define _MD_END_RESUME_ALL()
  281.  
  282. /* --- Lock stuff --- */
  283. #define _MD_NEW_LOCK(lock)            (InitializeCriticalSection(&((lock)->mutex)),PR_SUCCESS)
  284. #define _MD_FREE_LOCK(lock)           DeleteCriticalSection(&((lock)->mutex))
  285. #ifndef PROFILE_LOCKS
  286. #define _MD_LOCK(lock)                EnterCriticalSection(&((lock)->mutex))
  287. #define _MD_TEST_AND_LOCK(lock)       0  /* XXXMB */
  288. #define _MD_UNLOCK(lock)              LeaveCriticalSection(&((lock)->mutex))
  289. #else
  290. #define _MD_LOCK(lock)                 \
  291.     PR_BEGIN_MACRO \
  292.     BOOL rv = TryEnterCriticalSection(&((lock)->mutex)); \
  293.     if (rv == TRUE) { \
  294.         InterlockedIncrement(&((lock)->hitcount)); \
  295.     } else { \
  296.         InterlockedIncrement(&((lock)->misscount)); \
  297.         EnterCriticalSection(&((lock)->mutex)); \
  298.     } \
  299.     PR_END_MACRO
  300. #define _MD_TEST_AND_LOCK(lock)       0  /* XXXMB */
  301. #define _MD_UNLOCK(lock)              LeaveCriticalSection(&((lock)->mutex))
  302. #endif
  303. #define _PR_LOCK                      _MD_LOCK
  304. #define _PR_UNLOCK                      _MD_UNLOCK
  305.  
  306. /* --- lock and cv waiting --- */
  307. #define _MD_WAIT                      _PR_MD_WAIT
  308. #define _MD_WAKEUP_WAITER             _PR_MD_WAKEUP_WAITER
  309.  
  310.    /* XXXMB- the IOQ stuff is certainly not working correctly yet. */
  311. extern  struct _MDLock              _pr_ioq_lock;
  312. #define _MD_IOQ_LOCK()                _MD_LOCK(&_pr_ioq_lock)
  313. #define _MD_IOQ_UNLOCK()              _MD_UNLOCK(&_pr_ioq_lock)
  314.  
  315.  
  316. /* --- Initialization stuff --- */
  317. #define _MD_START_INTERRUPTS()
  318. #define _MD_STOP_INTERRUPTS()
  319. #define _MD_DISABLE_CLOCK_INTERRUPTS()
  320. #define _MD_BLOCK_CLOCK_INTERRUPTS()
  321. #define _MD_UNBLOCK_CLOCK_INTERRUPTS()
  322. #define _MD_EARLY_INIT                _PR_MD_EARLY_INIT
  323. #define _MD_FINAL_INIT()
  324. #define _MD_INIT_CPUS()
  325. #define _MD_INIT_RUNNING_CPU(cpu)
  326.  
  327. struct PRProcess;
  328. struct PRProcessAttr;
  329.  
  330. /* --- Create a new process --- */
  331. #define _MD_CREATE_PROCESS _PR_CreateWindowsProcess
  332. extern struct PRProcess * _PR_CreateWindowsProcess(
  333.     const char *path,
  334.     char *const *argv,
  335.     char *const *envp,
  336.     const struct PRProcessAttr *attr
  337. );
  338.  
  339. #define _MD_DETACH_PROCESS _PR_DetachWindowsProcess
  340. extern PRStatus _PR_DetachWindowsProcess(struct PRProcess *process);
  341.  
  342. /* --- Wait for a child process to terminate --- */
  343. #define _MD_WAIT_PROCESS _PR_WaitWindowsProcess
  344. extern PRStatus _PR_WaitWindowsProcess(struct PRProcess *process, 
  345.     PRInt32 *exitCode);
  346.  
  347. #define _MD_KILL_PROCESS _PR_KillWindowsProcess
  348. extern PRStatus _PR_KillWindowsProcess(struct PRProcess *process);
  349.  
  350. /* --- User Threading stuff --- */
  351. #define HAVE_FIBERS
  352. #define _MD_CREATE_USER_THREAD            _PR_MD_CREATE_USER_THREAD
  353. #define _MD_CREATE_PRIMORDIAL_USER_THREAD _PR_MD_CREATE_PRIMORDIAL_USER_THREAD
  354. #define _MD_CLEANUP_BEFORE_EXIT           _PR_MD_CLEANUP_BEFORE_EXIT
  355. #define _MD_EXIT                          _PR_MD_EXIT
  356. #define _MD_INIT_CONTEXT                  _PR_MD_INIT_CONTEXT
  357. #define _MD_SWITCH_CONTEXT                _PR_MD_SWITCH_CONTEXT
  358. #define _MD_RESTORE_CONTEXT               _PR_MD_RESTORE_CONTEXT
  359.  
  360. /* --- Intervals --- */
  361. #define _MD_INTERVAL_INIT                 _PR_MD_INTERVAL_INIT
  362. #define _MD_GET_INTERVAL                  _PR_MD_GET_INTERVAL
  363. #define _MD_INTERVAL_PER_SEC              _PR_MD_INTERVAL_PER_SEC
  364. #define _MD_INTERVAL_PER_MILLISEC()       (_PR_MD_INTERVAL_PER_SEC() / 1000)
  365. #define _MD_INTERVAL_PER_MICROSEC()       (_PR_MD_INTERVAL_PER_SEC() / 1000000)
  366.  
  367. /* --- Native-Thread Specific Definitions ------------------------------- */
  368.  
  369. extern __declspec(thread) struct PRThread *_pr_current_fiber;
  370. #define _MD_CURRENT_THREAD() _pr_current_fiber
  371. #define _MD_SET_CURRENT_THREAD(_thread) (_pr_current_fiber = (_thread))
  372.  
  373. extern __declspec(thread) struct PRThread *_pr_fiber_last_run;
  374. #define _MD_LAST_THREAD() _pr_fiber_last_run
  375. #define _MD_SET_LAST_THREAD(_thread) (_pr_fiber_last_run = (_thread))
  376.  
  377. extern __declspec(thread) struct _PRCPU *_pr_current_cpu;
  378. #define _MD_CURRENT_CPU() _pr_current_cpu
  379. #define _MD_SET_CURRENT_CPU(_cpu) (_pr_current_cpu = (_cpu))
  380.  
  381. extern __declspec(thread) PRUintn _pr_ints_off;
  382. #define _MD_SET_INTSOFF(_val) (_pr_ints_off = (_val))
  383. #define _MD_GET_INTSOFF() _pr_ints_off
  384. #define _MD_INCREMENT_INTSOFF() (_pr_ints_off++)
  385. #define _MD_DECREMENT_INTSOFF() (_pr_ints_off--)
  386.  
  387. /* --- Initialization stuff --- */
  388. #define _MD_INIT_LOCKS()
  389.  
  390. /* --- Stack stuff --- */
  391. #define _MD_INIT_STACK(stack, redzone)
  392. #define _MD_CLEAR_STACK(stack)
  393.  
  394. /* --- Memory-mapped files stuff --- */
  395.  
  396. struct _MDFileMap {
  397.     HANDLE hFileMap;
  398.     DWORD dwAccess;
  399. };
  400.  
  401. extern PRStatus _MD_CreateFileMap(struct PRFileMap *fmap, PRInt64 size);
  402. #define _MD_CREATE_FILE_MAP _MD_CreateFileMap
  403.  
  404. extern void * _MD_MemMap(struct PRFileMap *fmap, PRInt64 offset,
  405.         PRUint32 len);
  406. #define _MD_MEM_MAP _MD_MemMap
  407.  
  408. extern PRStatus _MD_MemUnmap(void *addr, PRUint32 size);
  409. #define _MD_MEM_UNMAP _MD_MemUnmap
  410.  
  411. extern PRStatus _MD_CloseFileMap(struct PRFileMap *fmap);
  412. #define _MD_CLOSE_FILE_MAP _MD_CloseFileMap
  413.  
  414. #endif /* nspr_win32_defs_h___ */
  415.