Make the PowerPC interlocked assembler code volatile.
[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 "wine/server.h"
30 #include "winternl.h"
31
32 struct async_private;
33
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);
38
39 typedef struct async_ops
40 {
41     async_get_count             get_count;
42     async_call_completion_func  call_completion;
43     async_cleanup               cleanup;
44 } async_ops;
45
46 typedef struct async_private
47 {
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;
58
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 )
61 {
62     PAPCFUNC func = (PAPCFUNC)arg1;
63     func( arg2 );
64 }
65
66 inline static void finish_async( async_private *ovp )
67 {
68     if (ovp->prev)
69         ovp->prev->next = ovp->next;
70     else
71         NtCurrentTeb()->pending_list = ovp->next;
72
73     if (ovp->next)
74         ovp->next->prev = ovp->prev;
75
76     ovp->next = ovp->prev = NULL;
77
78     close(ovp->fd);
79     if ( ovp->event != INVALID_HANDLE_VALUE )
80         NtSetEvent( ovp->event, NULL );
81
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 );
87 }
88
89 inline static NTSTATUS __register_async( async_private *ovp, const DWORD status )
90 {
91     NTSTATUS    ret;
92
93     SERVER_START_REQ( register_async )
94     {
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 );
101     }
102     SERVER_END_REQ;
103
104     if (ret) ovp->iosb->u.Status = ret;
105
106     if ( ovp->iosb->u.Status != STATUS_PENDING )
107         finish_async(ovp);
108
109     return ret;
110 }
111
112 #define register_old_async(ovp) \
113     __register_async(ovp, ovp->iosb->u.Status);
114
115 inline static NTSTATUS register_new_async( async_private *ovp )
116 {
117     ovp->iosb->u.Status = STATUS_PENDING;
118
119     ovp->next = NtCurrentTeb()->pending_list;
120     ovp->prev = NULL;
121     if ( ovp->next ) ovp->next->prev = ovp;
122     NtCurrentTeb()->pending_list = ovp;
123
124     return __register_async( ovp, STATUS_PENDING );
125 }
126
127 inline static NTSTATUS cancel_async( async_private *ovp )
128 {
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 );
134 }
135
136 #endif /* __WINE_ASYNC_H */