Use angle brackets (<>) rather than quotes ("") for the include
[wine] / include / async.h
1 /*
2  * Structures and static functions for handling asynchronous I/O.
3  *
4  * Copyright (C) 2002 Mike McCormack,  Martin Wilck
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library 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 GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  */
20
21 /*
22  * This file declares static functions.
23  * It should only be included by those source files that implement async I/O requests.
24  */
25
26 #ifndef __WINE_ASYNC_H
27 #define __WINE_ASYNC_H
28
29 #include <thread.h>
30 #include <wine/server.h>
31 #include <winternl.h>
32
33 struct async_private;
34
35 typedef void (*async_handler)(struct async_private *ovp);
36 typedef void (CALLBACK *async_call_completion_func)(ULONG_PTR data);
37 typedef DWORD (*async_get_count)(const struct async_private *ovp);
38 typedef void (*async_cleanup)(struct async_private *ovp);
39
40 typedef struct async_ops
41 {
42     async_get_count             get_count;
43     async_call_completion_func  call_completion;
44     async_cleanup               cleanup;
45 } async_ops;
46
47 typedef struct async_private
48 {
49     struct async_ops*   ops;
50     HANDLE              handle;
51     HANDLE              event;
52     int                 fd;
53     async_handler       func;
54     int                 type;
55     IO_STATUS_BLOCK*    iosb;
56     struct async_private* next;
57     struct async_private* prev;
58 } async_private;
59
60 /* All functions declared static for Dll separation purposes */
61 static void CALLBACK call_user_apc( ULONG_PTR arg1, ULONG_PTR arg2, ULONG_PTR arg3 )
62 {
63     PAPCFUNC func = (PAPCFUNC)arg1;
64     func( arg2 );
65 }
66
67 inline static void finish_async( async_private *ovp )
68 {
69     if (ovp->prev)
70         ovp->prev->next = ovp->next;
71     else
72         NtCurrentTeb()->pending_list = ovp->next;
73
74     if (ovp->next)
75         ovp->next->prev = ovp->prev;
76
77     ovp->next = ovp->prev = NULL;
78
79     close(ovp->fd);
80     if ( ovp->event != INVALID_HANDLE_VALUE )
81         NtSetEvent( ovp->event, NULL );
82
83     if ( ovp->ops->call_completion )
84         NtQueueApcThread( GetCurrentThread(), call_user_apc, 
85                           (ULONG_PTR)ovp->ops->call_completion, (ULONG_PTR)ovp, 0 );
86     else
87         ovp->ops->cleanup( ovp );
88 }
89
90 inline static NTSTATUS __register_async( async_private *ovp, const DWORD status )
91 {
92     NTSTATUS    ret;
93
94     SERVER_START_REQ( register_async )
95     {
96         req->handle = ovp->handle;
97         req->overlapped = ovp;
98         req->type = ovp->type;
99         req->count = ovp->ops->get_count( ovp );
100         req->status = status;
101         ret = wine_server_call( req );
102     }
103     SERVER_END_REQ;
104
105     if (ret) ovp->iosb->u.Status = ret;
106
107     if ( ovp->iosb->u.Status != STATUS_PENDING )
108         finish_async(ovp);
109
110     return ret;
111 }
112
113 #define register_old_async(ovp) \
114     __register_async(ovp, ovp->iosb->u.Status);
115
116 inline static NTSTATUS register_new_async( async_private *ovp )
117 {
118     ovp->iosb->u.Status = STATUS_PENDING;
119
120     ovp->next = NtCurrentTeb()->pending_list;
121     ovp->prev = NULL;
122     if ( ovp->next ) ovp->next->prev = ovp;
123     NtCurrentTeb()->pending_list = ovp;
124
125     return __register_async( ovp, STATUS_PENDING );
126 }
127
128 inline static NTSTATUS cancel_async( async_private *ovp )
129 {
130      /* avoid multiple cancellations */
131      if ( ovp->iosb->u.Status != STATUS_PENDING )
132           return STATUS_SUCCESS;
133      ovp->iosb->u.Status = STATUS_CANCELLED;
134      return __register_async( ovp, STATUS_CANCELLED );
135 }
136
137 #endif /* __WINE_ASYNC_H */