summaryrefslogtreecommitdiff
path: root/rts/win32/IOManager.h
blob: 145a1e549b42621a487b9b8575cd5542d98eb813 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
/* IOManager.h
 *
 * Non-blocking / asynchronous I/O for Win32.
 *
 * (c) sof, 2002-2003
 */

#ifndef WIN32_IOMANAGER_H
#define WIN32_IOMANAGER_H

#include <windows.h>

/*
 The IOManager subsystem provides a non-blocking view
 of I/O operations. It lets one (or more) OS thread(s)
 issue multiple I/O requests, which the IOManager then 
 handles independently of/concurrent to the thread(s)
 that issued the request. Upon completion, the issuing
 thread can inspect the result of the I/O operation &
 take appropriate action.

 The IOManager is intended used with the GHC RTS to
 implement non-blocking I/O in Concurrent Haskell.
 */

/*
 * Our WorkQueue holds WorkItems, encoding IO and
 * delay requests.
 *
 */
typedef void (*CompletionProc)(unsigned int requestID,
			       int   fd,
			       int   len,
			       void* buf,
			       int   errCode);

/* 
 * Asynchronous procedure calls executed by a worker thread
 * take a generic state argument pointer and return an int by 
 * default. 
 */
typedef int (*DoProcProc)(void *param);

typedef union workData {
    struct {
	int   fd;
	int   len;
	char *buf; 
    } ioData;
    struct { 
	int   msecs;
    } delayData;
    struct { 
	DoProcProc proc;
	void* param;
    } procData;
} WorkData;

typedef struct WorkItem {
  unsigned int     workKind;
  WorkData         workData;
  unsigned int     requestID;
  CompletionProc   onCompletion;
  unsigned int     abandonOp;
  struct WorkItem  *link;
} WorkItem;

extern CompletionProc onComplete;

/* the kind of operations supported; you could easily imagine
 * that instead of passing a tag describing the work to be performed,
 * a function pointer is passed instead. Maybe later.
 */
#define WORKER_READ        1
#define WORKER_WRITE       2
#define WORKER_DELAY       4
#define WORKER_FOR_SOCKET  8
#define WORKER_DO_PROC    16

/*
 * Starting up and shutting down. 
 */ 
extern BOOL StartIOManager     ( void );
extern void ShutdownIOManager  ( rtsBool wait_threads );

/*
 * Adding I/O and delay requests. With each request a
 * completion routine is supplied, which the worker thread
 * will invoke upon completion.
 */
extern int AddDelayRequest ( unsigned int   msecs,
			     CompletionProc onCompletion);

extern int AddIORequest ( int            fd,
			  BOOL           forWriting,
			  BOOL           isSocket,
			  int            len,
			  char*          buffer,
			  CompletionProc onCompletion);

extern int AddProcRequest ( void*          proc,
			    void*          data,
			    CompletionProc onCompletion);

extern void abandonWorkRequest ( int reqID );

#endif /* WIN32_IOMANAGER_H */