home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / nspr30-e.zip / nspr30-e / include / md / _win95.h < prev    next >
C/C++ Source or Header  |  1998-11-02  |  17KB  |  438 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_win95_defs_h___
  20. #define nspr_win95_defs_h___
  21.  
  22. #include "prio.h"
  23.  
  24. #include <windows.h>
  25. #include <winsock.h>
  26. #include <errno.h>
  27.  
  28. /*
  29.  * Internal configuration macros
  30.  */
  31.  
  32. #define PR_LINKER_ARCH      "win32"
  33. #define _PR_SI_SYSNAME        "WIN95"
  34. #define _PR_SI_ARCHITECTURE   "x86"    /* XXXMB hardcode for now */
  35.  
  36. #define HAVE_DLL
  37. #undef  HAVE_THREAD_AFFINITY
  38. #define HAVE_SOCKET_REUSEADDR
  39. #define HAVE_SOCKET_KEEPALIVE
  40. #define _PR_HAVE_ATOMIC_OPS
  41.  
  42. /* --- Common User-Thread/Native-Thread Definitions --------------------- */
  43.  
  44. /* --- Globals --- */
  45. extern struct PRLock                      *_pr_schedLock;
  46.  
  47. /* --- Typedefs --- */
  48. typedef void (*FiberFunc)(void *);
  49.  
  50. #define PR_NUM_GCREGS           8
  51. typedef PRInt32                    PR_CONTEXT_TYPE[PR_NUM_GCREGS];
  52. #define GC_VMBASE               0x40000000
  53. #define GC_VMLIMIT              0x00FFFFFF
  54.  
  55. #define _MD_MAGIC_THREAD    0x22222222
  56. #define _MD_MAGIC_THREADSTACK    0x33333333
  57. #define _MD_MAGIC_SEGMENT    0x44444444
  58. #define _MD_MAGIC_DIR        0x55555555
  59. #define _MD_MAGIC_CV        0x66666666
  60.  
  61. struct _MDCPU {
  62.     int              unused;
  63. };
  64.  
  65. struct _MDThread {
  66.     HANDLE           blocked_sema;      /* Threads block on this when waiting
  67.                                          * for IO or CondVar.
  68.                                          */
  69.     PRBool           inCVWaitQueue;     /* PR_TRUE if the thread is in the
  70.                                          * wait queue of some cond var.
  71.                                          * PR_FALSE otherwise.  */
  72.     HANDLE           handle;            /* Win32 thread handle */
  73.     PRUint32         id;
  74.     void            *sp;                /* only valid when suspended */
  75.     PRUint32         magic;             /* for debugging */
  76.     PR_CONTEXT_TYPE  gcContext;         /* Thread context for GC */
  77.     struct PRThread *prev, *next;       /* used by the cvar wait queue to
  78.                                          * chain the PRThread structures
  79.                                          * together */
  80. };
  81.  
  82. struct _MDThreadStack {
  83.     PRUint32           magic;          /* for debugging */
  84. };
  85.  
  86. struct _MDSegment {
  87.     PRUint32           magic;          /* for debugging */
  88. };
  89.  
  90. #undef PROFILE_LOCKS
  91.  
  92. struct _MDDir {
  93.     HANDLE           d_hdl;
  94.     WIN32_FIND_DATA  d_entry;
  95.     PRBool           firstEntry;     /* Is this the entry returned
  96.                                       * by FindFirstFile()? */
  97.     PRUint32         magic;          /* for debugging */
  98. };
  99.  
  100. struct _MDCVar {
  101.     PRUint32 magic;
  102.     struct PRThread *waitHead, *waitTail;  /* the wait queue: a doubly-
  103.                                             * linked list of threads
  104.                                             * waiting on this condition
  105.                                             * variable */
  106.     PRIntn nwait;                          /* number of threads in the
  107.                                             * wait queue */
  108. };
  109.  
  110. #define _MD_CV_NOTIFIED_LENGTH 6
  111. typedef struct _MDNotified _MDNotified;
  112. struct _MDNotified {
  113.     PRIntn length;                     /* # of used entries in this
  114.                                         * structure */
  115.     struct {
  116.         struct _MDCVar *cv;            /* the condition variable notified */
  117.         PRIntn times;                  /* and the number of times notified */
  118.         struct PRThread *notifyHead;   /* list of threads to wake up */
  119.     } cv[_MD_CV_NOTIFIED_LENGTH];
  120.     _MDNotified *link;                 /* link to another of these, or NULL */
  121. };
  122.  
  123. struct _MDLock {
  124.     CRITICAL_SECTION mutex;          /* this is recursive on NT */
  125.  
  126.     /*
  127.      * When notifying cvars, there is no point in actually
  128.      * waking up the threads waiting on the cvars until we've
  129.      * released the lock.  So, we temporarily record the cvars.
  130.      * When doing an unlock, we'll then wake up the waiting threads.
  131.      */
  132.     struct _MDNotified notified;     /* array of conditions notified */
  133. #ifdef PROFILE_LOCKS
  134.     PRInt32 hitcount;
  135.     PRInt32 misscount;
  136. #endif
  137. };
  138.  
  139. struct _MDSemaphore {
  140.     HANDLE           sem;
  141. };
  142.  
  143. struct _MDFileDesc {
  144.     PRInt32 osfd;    /* The osfd can come from one of three spaces:
  145.                       * - For stdin, stdout, and stderr, we are using
  146.                       *   the libc file handle (0, 1, 2), which is an int.
  147.                       * - For files and pipes, we are using Win32 HANDLE,
  148.                       *   which is a void*.
  149.                       * - For sockets, we are using Winsock SOCKET, which
  150.                       *   is a u_int.
  151.                       */
  152. };
  153.  
  154. struct _MDProcess {
  155.     HANDLE handle;
  156.     DWORD id;
  157. };
  158.  
  159. /* --- Misc stuff --- */
  160. #define _MD_GET_SP(thread)            (thread)->md.gcContext[6]
  161.  
  162. /* --- IO stuff --- */
  163.  
  164. #define _MD_OPEN                      _PR_MD_OPEN
  165. #define _MD_READ                      _PR_MD_READ
  166. #define _MD_WRITE                     _PR_MD_WRITE
  167. #define _MD_WRITEV                    _PR_MD_WRITEV
  168. #define _MD_LSEEK                     _PR_MD_LSEEK
  169. #define _MD_LSEEK64                   _PR_MD_LSEEK64
  170. extern PRInt32 _MD_CloseFile(PRInt32 osfd);
  171. #define _MD_CLOSE_FILE                _MD_CloseFile
  172. #define _MD_GETFILEINFO               _PR_MD_GETFILEINFO
  173. #define _MD_GETFILEINFO64             _PR_MD_GETFILEINFO64
  174. #define _MD_GETOPENFILEINFO           _PR_MD_GETOPENFILEINFO
  175. #define _MD_GETOPENFILEINFO64         _PR_MD_GETOPENFILEINFO64
  176. #define _MD_STAT                      _PR_MD_STAT
  177. #define _MD_RENAME                    _PR_MD_RENAME     
  178. #define _MD_ACCESS                    _PR_MD_ACCESS     
  179. #define _MD_DELETE                    _PR_MD_DELETE     
  180. #define _MD_MKDIR                     _PR_MD_MKDIR      
  181. #define _MD_RMDIR                     _PR_MD_RMDIR      
  182. #define _MD_LOCKFILE                  _PR_MD_LOCKFILE
  183. #define _MD_TLOCKFILE                 _PR_MD_TLOCKFILE
  184. #define _MD_UNLOCKFILE                _PR_MD_UNLOCKFILE
  185.  
  186. /* --- Socket IO stuff --- */
  187. #define _MD_EACCES                WSAEACCES
  188. #define _MD_EADDRINUSE            WSAEADDRINUSE
  189. #define _MD_EADDRNOTAVAIL         WSAEADDRNOTAVAIL
  190. #define _MD_EAFNOSUPPORT          WSAEAFNOSUPPORT
  191. #define _MD_EAGAIN                WSAEWOULDBLOCK
  192. #define _MD_EALREADY              WSAEALREADY
  193. #define _MD_EBADF                 WSAEBADF
  194. #define _MD_ECONNREFUSED          WSAECONNREFUSED
  195. #define _MD_ECONNRESET            WSAECONNRESET
  196. #define _MD_EFAULT                WSAEFAULT
  197. #define _MD_EINPROGRESS           WSAEINPROGRESS
  198. #define _MD_EINTR                 WSAEINTR
  199. #define _MD_EINVAL                EINVAL
  200. #define _MD_EISCONN               WSAEISCONN
  201. #define _MD_ENETUNREACH           WSAENETUNREACH
  202. #define _MD_ENOENT                ENOENT
  203. #define _MD_ENOTCONN              WSAENOTCONN
  204. #define _MD_ENOTSOCK              WSAENOTSOCK
  205. #define _MD_EOPNOTSUPP            WSAEOPNOTSUPP
  206. #define _MD_EWOULDBLOCK           WSAEWOULDBLOCK
  207. #define _MD_GET_SOCKET_ERROR()    WSAGetLastError()
  208. #define _MD_SET_SOCKET_ERROR(_err) WSASetLastError(_err)
  209.  
  210. extern void _MD_MakeNonblock(PRFileDesc *f);
  211. #define _MD_MAKE_NONBLOCK             _MD_MakeNonblock
  212. #define _MD_SHUTDOWN                  _PR_MD_SHUTDOWN
  213. #define _MD_LISTEN(s, backlog)        listen(s->secret->md.osfd,backlog)
  214. extern PRInt32 _MD_CloseSocket(PRInt32 osfd);
  215. #define _MD_CLOSE_SOCKET              _MD_CloseSocket
  216. #define _MD_SENDTO                    _PR_MD_SENDTO
  217. #define _MD_RECVFROM                  _PR_MD_RECVFROM
  218. #define _MD_SOCKETPAIR(s, type, proto, sv) -1
  219. #define _MD_GETSOCKNAME               _PR_MD_GETSOCKNAME
  220. #define _MD_GETPEERNAME               _PR_MD_GETPEERNAME
  221. #define _MD_GETSOCKOPT                _PR_MD_GETSOCKOPT
  222. #define _MD_SETSOCKOPT                _PR_MD_SETSOCKOPT
  223. #define _MD_SELECT                    select
  224. #define _MD_FSYNC                     _PR_MD_FSYNC
  225. #define READ_FD                       1
  226. #define WRITE_FD                      2
  227.  
  228. #define _MD_INIT_ATOMIC()
  229. #if defined(_M_IX86) || defined(_X86_)
  230. #define _MD_ATOMIC_INCREMENT          _PR_MD_ATOMIC_INCREMENT
  231. #define _MD_ATOMIC_ADD                _PR_MD_ATOMIC_ADD
  232. #define _MD_ATOMIC_DECREMENT          _PR_MD_ATOMIC_DECREMENT
  233. #else /* non-x86 processors */
  234. #define _MD_ATOMIC_INCREMENT(x)       InterlockedIncrement((PLONG)x)
  235. #define _MD_ATOMIC_ADD(ptr,val)    (InterlockedExchangeAdd((PLONG)ptr, (LONG)val) + val)
  236. #define _MD_ATOMIC_DECREMENT(x)       InterlockedDecrement((PLONG)x)
  237. #endif /* x86 */
  238. #define _MD_ATOMIC_SET(x,y)           InterlockedExchange((PLONG)x, (LONG)y)
  239.  
  240. #define _MD_INIT_IO                   _PR_MD_INIT_IO
  241. #define _MD_TRANSMITFILE              _PR_MD_TRANSMITFILE
  242.  
  243.  
  244. /* win95 doesn't have async IO */
  245. #define _MD_SOCKET                    _PR_MD_SOCKET
  246. extern PRInt32 _MD_SocketAvailable(PRFileDesc *fd);
  247. #define _MD_SOCKETAVAILABLE           _MD_SocketAvailable
  248. #define _MD_CONNECT                   _PR_MD_CONNECT
  249. extern PRInt32 _MD_Accept(PRFileDesc *fd, PRNetAddr *raddr, PRUint32 *rlen,
  250.         PRIntervalTime timeout);
  251. #define _MD_ACCEPT                    _MD_Accept
  252. #define _MD_BIND                      _PR_MD_BIND
  253. #define _MD_RECV                      _PR_MD_RECV
  254. #define _MD_SEND                      _PR_MD_SEND
  255. #define _MD_PR_POLL                   _PR_MD_PR_POLL
  256.  
  257. /* --- Scheduler stuff --- */
  258. // #define _MD_PAUSE_CPU                 _PR_MD_PAUSE_CPU
  259. #define _MD_PAUSE_CPU
  260.  
  261. /* --- DIR stuff --- */
  262. #define PR_DIRECTORY_SEPARATOR        '\\'
  263. #define PR_DIRECTORY_SEPARATOR_STR    "\\"
  264. #define PR_PATH_SEPARATOR        ';'
  265. #define PR_PATH_SEPARATOR_STR        ";"
  266. #define _MD_ERRNO()                   GetLastError()
  267. #define _MD_OPEN_DIR                  _PR_MD_OPEN_DIR
  268. #define _MD_CLOSE_DIR                 _PR_MD_CLOSE_DIR
  269. #define _MD_READ_DIR                  _PR_MD_READ_DIR
  270.  
  271. /* --- Segment stuff --- */
  272. #define _MD_INIT_SEGS()
  273. #define _MD_ALLOC_SEGMENT(seg, size, vaddr)   0
  274. #define _MD_FREE_SEGMENT(seg)
  275.  
  276. /* --- Environment Stuff --- */
  277. #define _MD_GET_ENV                 _PR_MD_GET_ENV
  278. #define _MD_PUT_ENV                 _PR_MD_PUT_ENV
  279.  
  280. /* --- Threading Stuff --- */
  281. #define _MD_DEFAULT_STACK_SIZE            0
  282. #define _MD_INIT_THREAD             _PR_MD_INIT_THREAD
  283. #define _MD_INIT_ATTACHED_THREAD    _PR_MD_INIT_THREAD
  284. #define _MD_INIT_PRIMORDIAL_THREAD  _PR_MD_INIT_PRIMORDIAL_THREAD
  285. #define _MD_CREATE_THREAD           _PR_MD_CREATE_THREAD
  286. #define _MD_YIELD                   _PR_MD_YIELD
  287. #define _MD_SET_PRIORITY            _PR_MD_SET_PRIORITY
  288. #define _MD_CLEAN_THREAD            _PR_MD_CLEAN_THREAD
  289. #define _MD_SETTHREADAFFINITYMASK   _PR_MD_SETTHREADAFFINITYMASK
  290. #define _MD_GETTHREADAFFINITYMASK   _PR_MD_GETTHREADAFFINITYMASK
  291. #define _MD_EXIT_THREAD             _PR_MD_EXIT_THREAD
  292. #define _MD_SUSPEND_THREAD          _PR_MD_SUSPEND_THREAD
  293. #define _MD_RESUME_THREAD           _PR_MD_RESUME_THREAD
  294. #define _MD_SUSPEND_CPU             _PR_MD_SUSPEND_CPU
  295. #define _MD_RESUME_CPU              _PR_MD_RESUME_CPU
  296. #define _MD_BEGIN_SUSPEND_ALL()
  297. #define _MD_BEGIN_RESUME_ALL()
  298. #define _MD_END_SUSPEND_ALL()
  299. #define _MD_END_RESUME_ALL()
  300.  
  301. /* --- Lock stuff --- */
  302. #define _PR_LOCK                      _MD_LOCK
  303. #define _PR_UNLOCK                      _MD_UNLOCK
  304.  
  305. #define _MD_NEW_LOCK(lock)            (InitializeCriticalSection(&((lock)->mutex)),(lock)->notified.length=0,(lock)->notified.link=NULL,PR_SUCCESS)
  306. #define _MD_FREE_LOCK(lock)           DeleteCriticalSection(&((lock)->mutex))
  307. #define _MD_LOCK(lock)                EnterCriticalSection(&((lock)->mutex))
  308. #define _MD_TEST_AND_LOCK(lock)       (EnterCriticalSection(&((lock)->mutex)),PR_SUCCESS)
  309. #define _MD_UNLOCK                    _PR_MD_UNLOCK
  310.  
  311. /* --- lock and cv waiting --- */
  312. #define _MD_WAIT                      _PR_MD_WAIT
  313. #define _MD_WAKEUP_WAITER             _PR_MD_WAKEUP_WAITER
  314.  
  315. /* --- CVar ------------------- */
  316. #define _MD_WAIT_CV                      _PR_MD_WAIT_CV
  317. #define _MD_NEW_CV                      _PR_MD_NEW_CV
  318. #define _MD_FREE_CV                      _PR_MD_FREE_CV
  319. #define _MD_NOTIFY_CV                  _PR_MD_NOTIFY_CV    
  320. #define _MD_NOTIFYALL_CV              _PR_MD_NOTIFYALL_CV
  321.  
  322.    /* XXXMB- the IOQ stuff is certainly not working correctly yet. */
  323. // extern  struct _MDLock              _pr_ioq_lock;
  324. #define _MD_IOQ_LOCK()                
  325. #define _MD_IOQ_UNLOCK()              
  326.  
  327.  
  328. /* --- Initialization stuff --- */
  329. #define _MD_START_INTERRUPTS()
  330. #define _MD_STOP_INTERRUPTS()
  331. #define _MD_DISABLE_CLOCK_INTERRUPTS()
  332. #define _MD_ENABLE_CLOCK_INTERRUPTS()
  333. #define _MD_BLOCK_CLOCK_INTERRUPTS()
  334. #define _MD_UNBLOCK_CLOCK_INTERRUPTS()
  335. #define _MD_EARLY_INIT                _PR_MD_EARLY_INIT
  336. #define _MD_FINAL_INIT()
  337. #define _MD_INIT_CPUS()
  338. #define _MD_INIT_RUNNING_CPU(cpu)
  339.  
  340. struct PRProcess;
  341. struct PRProcessAttr;
  342.  
  343. #define _MD_CREATE_PROCESS _PR_CreateWindowsProcess
  344. extern struct PRProcess * _PR_CreateWindowsProcess(
  345.     const char *path,
  346.     char *const *argv,
  347.     char *const *envp,
  348.     const struct PRProcessAttr *attr
  349. );
  350.  
  351. #define _MD_DETACH_PROCESS _PR_DetachWindowsProcess
  352. extern PRStatus _PR_DetachWindowsProcess(struct PRProcess *process);
  353.  
  354. /* --- Wait for a child process to terminate --- */
  355. #define _MD_WAIT_PROCESS _PR_WaitWindowsProcess
  356. extern PRStatus _PR_WaitWindowsProcess(struct PRProcess *process, 
  357.     PRInt32 *exitCode);
  358.  
  359. #define _MD_KILL_PROCESS _PR_KillWindowsProcess
  360. extern PRStatus _PR_KillWindowsProcess(struct PRProcess *process);
  361.  
  362. #define _MD_CLEANUP_BEFORE_EXIT           _PR_MD_CLEANUP_BEFORE_EXIT
  363. #define _MD_INIT_CONTEXT
  364. #define _MD_SWITCH_CONTEXT
  365. #define _MD_RESTORE_CONTEXT
  366.  
  367. /* --- Intervals --- */
  368. #define _MD_INTERVAL_INIT                 _PR_MD_INTERVAL_INIT
  369. #define _MD_GET_INTERVAL                  _PR_MD_GET_INTERVAL
  370. #define _MD_INTERVAL_PER_SEC              _PR_MD_INTERVAL_PER_SEC
  371. #define _MD_INTERVAL_PER_MILLISEC()       (_PR_MD_INTERVAL_PER_SEC() / 1000)
  372. #define _MD_INTERVAL_PER_MICROSEC()       (_PR_MD_INTERVAL_PER_SEC() / 1000000)
  373.  
  374. /* --- Native-Thread Specific Definitions ------------------------------- */
  375.  
  376. extern struct PRThread * _MD_CURRENT_THREAD(void);
  377.  
  378. #ifdef _PR_USE_STATIC_TLS
  379. extern __declspec(thread) struct PRThread *_pr_currentThread;
  380. #define _MD_GET_ATTACHED_THREAD() _pr_currentThread
  381. #define _MD_SET_CURRENT_THREAD(_thread) (_pr_currentThread = (_thread))
  382.  
  383. extern __declspec(thread) struct PRThread *_pr_thread_last_run;
  384. #define _MD_LAST_THREAD() _pr_thread_last_run
  385. #define _MD_SET_LAST_THREAD(_thread) (_pr_thread_last_run = 0)
  386.  
  387. extern __declspec(thread) struct _PRCPU *_pr_currentCPU;
  388. #define _MD_CURRENT_CPU() _pr_currentCPU
  389. #define _MD_SET_CURRENT_CPU(_cpu) (_pr_currentCPU = 0)
  390. #else /* _PR_USE_STATIC_TLS */
  391. extern DWORD _pr_currentThreadIndex;
  392. #define _MD_GET_ATTACHED_THREAD() ((PRThread *) TlsGetValue(_pr_currentThreadIndex))
  393. #define _MD_SET_CURRENT_THREAD(_thread) TlsSetValue(_pr_currentThreadIndex, (_thread))
  394.  
  395. extern DWORD _pr_lastThreadIndex;
  396. #define _MD_LAST_THREAD() ((PRThread *) TlsGetValue(_pr_lastThreadIndex))
  397. #define _MD_SET_LAST_THREAD(_thread) TlsSetValue(_pr_lastThreadIndex, 0)
  398.  
  399. extern DWORD _pr_currentCPUIndex;
  400. #define _MD_CURRENT_CPU() ((struct _PRCPU *) TlsGetValue(_pr_currentCPUIndex))
  401. #define _MD_SET_CURRENT_CPU(_cpu) TlsSetValue(_pr_currentCPUIndex, 0)
  402. #endif /* _PR_USE_STATIC_TLS */
  403.  
  404. /* --- Scheduler stuff --- */
  405. #define LOCK_SCHEDULER()                 0
  406. #define UNLOCK_SCHEDULER()               0
  407. #define _PR_LockSched()                     0
  408. #define _PR_UnlockSched()                0
  409.  
  410. /* --- Initialization stuff --- */
  411. #define _MD_INIT_LOCKS()
  412.  
  413. /* --- Stack stuff --- */
  414. #define _MD_INIT_STACK(stack, redzone)
  415. #define _MD_CLEAR_STACK(stack)
  416.  
  417. /* --- Memory-mapped files stuff --- */
  418.  
  419. struct _MDFileMap {
  420.     HANDLE hFileMap;
  421.     DWORD dwAccess;
  422. };
  423.  
  424. extern PRStatus _MD_CreateFileMap(struct PRFileMap *fmap, PRInt64 size);
  425. #define _MD_CREATE_FILE_MAP _MD_CreateFileMap
  426.  
  427. extern void * _MD_MemMap(struct PRFileMap *fmap, PRInt64 offset,
  428.         PRUint32 len);
  429. #define _MD_MEM_MAP _MD_MemMap
  430.  
  431. extern PRStatus _MD_MemUnmap(void *addr, PRUint32 size);
  432. #define _MD_MEM_UNMAP _MD_MemUnmap
  433.  
  434. extern PRStatus _MD_CloseFileMap(struct PRFileMap *fmap);
  435. #define _MD_CLOSE_FILE_MAP _MD_CloseFileMap
  436.  
  437. #endif /* nspr_win32_defs_h___ */
  438.