- Remove Idle().
[wine/dcerpc.git] / include / async.h
bloba620f7271ea466eb4149facb581f2247127d0130
1 /*
2 * Structures and static functions for handling asynchronous I/O.
4 * Copyright (C) 2002 Mike McCormack, Martin Wilck
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.
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.
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
22 * This file declares static functions.
23 * It should only be included by those source files that implement async I/O requests.
26 #ifndef __WINE_ASYNC_H
27 #define __WINE_ASYNC_H
29 #include "wine/server.h"
30 #include "winternl.h"
32 struct async_private;
34 typedef void (*async_handler)(struct async_private *ovp);
35 typedef void (CALLBACK *async_call_completion_func)(ULONG_PTR data);
36 typedef DWORD (*async_get_count)(const struct async_private *ovp);
37 typedef void (*async_cleanup)(struct async_private *ovp);
39 typedef struct async_ops
41 async_get_count get_count;
42 async_call_completion_func call_completion;
43 async_cleanup cleanup;
44 } async_ops;
46 typedef struct async_private
48 struct async_ops* ops;
49 HANDLE handle;
50 HANDLE event;
51 int fd;
52 async_handler func;
53 int type;
54 IO_STATUS_BLOCK* iosb;
55 struct async_private* next;
56 struct async_private* prev;
57 } async_private;
59 /* All functions declared static for Dll separation purposes */
60 static void CALLBACK call_user_apc( ULONG_PTR arg1, ULONG_PTR arg2, ULONG_PTR arg3 )
62 PAPCFUNC func = (PAPCFUNC)arg1;
63 func( arg2 );
66 inline static void finish_async( async_private *ovp )
68 if (ovp->prev)
69 ovp->prev->next = ovp->next;
70 else
71 NtCurrentTeb()->pending_list = ovp->next;
73 if (ovp->next)
74 ovp->next->prev = ovp->prev;
76 ovp->next = ovp->prev = NULL;
78 close(ovp->fd);
79 if ( ovp->event != INVALID_HANDLE_VALUE )
80 NtSetEvent( ovp->event, NULL );
82 if ( ovp->ops->call_completion )
83 NtQueueApcThread( GetCurrentThread(), call_user_apc,
84 (ULONG_PTR)ovp->ops->call_completion, (ULONG_PTR)ovp, 0 );
85 else
86 ovp->ops->cleanup( ovp );
89 inline static NTSTATUS __register_async( async_private *ovp, const DWORD status )
91 NTSTATUS ret;
93 SERVER_START_REQ( register_async )
95 req->handle = ovp->handle;
96 req->overlapped = ovp;
97 req->type = ovp->type;
98 req->count = ovp->ops->get_count( ovp );
99 req->status = status;
100 ret = wine_server_call( req );
102 SERVER_END_REQ;
104 if (ret) ovp->iosb->u.Status = ret;
106 if ( ovp->iosb->u.Status != STATUS_PENDING )
107 finish_async(ovp);
109 return ret;
112 #define register_old_async(ovp) \
113 __register_async(ovp, ovp->iosb->u.Status);
115 inline static NTSTATUS register_new_async( async_private *ovp )
117 ovp->iosb->u.Status = STATUS_PENDING;
119 ovp->next = NtCurrentTeb()->pending_list;
120 ovp->prev = NULL;
121 if ( ovp->next ) ovp->next->prev = ovp;
122 NtCurrentTeb()->pending_list = ovp;
124 return __register_async( ovp, STATUS_PENDING );
127 inline static NTSTATUS cancel_async( async_private *ovp )
129 /* avoid multiple cancellations */
130 if ( ovp->iosb->u.Status != STATUS_PENDING )
131 return STATUS_SUCCESS;
132 ovp->iosb->u.Status = STATUS_CANCELLED;
133 return __register_async( ovp, STATUS_CANCELLED );
136 #endif /* __WINE_ASYNC_H */