server: Make struct async into a real object, to keep it around while the APC is...
[wine] / server / async.c
1 /*
2  * Server-side async I/O support
3  *
4  * Copyright (C) 2007 Alexandre Julliard
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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19  */
20
21 #include <assert.h>
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <stdarg.h>
25
26 #include "ntstatus.h"
27 #define WIN32_NO_STATUS
28 #include "windef.h"
29 #include "winternl.h"
30
31 #include "object.h"
32 #include "file.h"
33 #include "request.h"
34
35 struct async
36 {
37     struct object        obj;             /* object header */
38     struct thread       *thread;          /* owning thread */
39     struct list          queue_entry;     /* entry in file descriptor queue */
40     struct timeout_user *timeout;
41     void                *apc;             /* apc function to call when alerted */
42     void                *user;            /* user-private argument for apc */
43     void                *sb;              /* pointer to I/O status block in client addr space */
44 };
45
46 static void async_dump( struct object *obj, int verbose );
47 static void async_destroy( struct object *obj );
48
49 static const struct object_ops async_ops =
50 {
51     sizeof(struct async),      /* size */
52     async_dump,                /* dump */
53     no_add_queue,              /* add_queue */
54     NULL,                      /* remove_queue */
55     NULL,                      /* signaled */
56     NULL,                      /* satisfied */
57     no_signal,                 /* signal */
58     no_get_fd,                 /* get_fd */
59     no_map_access,             /* map_access */
60     no_lookup_name,            /* lookup_name */
61     no_close_handle,           /* close_handle */
62     async_destroy              /* destroy */
63 };
64
65 static void async_dump( struct object *obj, int verbose )
66 {
67     struct async *async = (struct async *)obj;
68     assert( obj->ops == &async_ops );
69     fprintf( stderr, "Async thread=%p\n", async->thread );
70 }
71
72 static void async_destroy( struct object *obj )
73 {
74     struct async *async = (struct async *)obj;
75     assert( obj->ops == &async_ops );
76
77     if (async->timeout) remove_timeout_user( async->timeout );
78     release_object( async->thread );
79 }
80
81 /* notifies client thread of new status of its async request */
82 /* destroys the server side of it */
83 static void async_terminate( struct async *async, unsigned int status )
84 {
85     apc_call_t data;
86
87     memset( &data, 0, sizeof(data) );
88     data.type            = APC_ASYNC_IO;
89     data.async_io.func   = async->apc;
90     data.async_io.user   = async->user;
91     data.async_io.sb     = async->sb;
92     data.async_io.status = status;
93     thread_queue_apc( async->thread, &async->obj, &data );
94
95     if (async->timeout) remove_timeout_user( async->timeout );
96     async->timeout = NULL;
97     list_remove( &async->queue_entry );
98     release_object( async );
99 }
100
101 /* callback for timeout on an async request */
102 static void async_timeout( void *private )
103 {
104     struct async *async = private;
105
106     async->timeout = NULL;
107     async_terminate( async, STATUS_TIMEOUT );
108 }
109
110 /* create an async on a given queue of a fd */
111 struct async *create_async( struct thread *thread, const struct timeval *timeout,
112                             struct list *queue, void *io_apc, void *io_user, void *io_sb )
113 {
114     struct async *async = alloc_object( &async_ops );
115
116     if (!async) return NULL;
117
118     async->thread = (struct thread *)grab_object( thread );
119     async->apc = io_apc;
120     async->user = io_user;
121     async->sb = io_sb;
122
123     list_add_tail( queue, &async->queue_entry );
124
125     if (timeout) async->timeout = add_timeout_user( timeout, async_timeout, async );
126     else async->timeout = NULL;
127
128     return async;
129 }
130
131 /* terminate the async operation at the head of the queue */
132 void async_terminate_head( struct list *queue, unsigned int status )
133 {
134     struct list *ptr = list_head( queue );
135     if (ptr) async_terminate( LIST_ENTRY( ptr, struct async, queue_entry ), status );
136 }
137
138 /* terminate all async operations on the queue */
139 void async_terminate_queue( struct list *queue, unsigned int status )
140 {
141     struct list *ptr, *next;
142
143     LIST_FOR_EACH_SAFE( ptr, next, queue )
144     {
145         struct async *async = LIST_ENTRY( ptr, struct async, queue_entry );
146         async_terminate( async, status );
147     }
148 }