server: Notify the async object when the APC call completed.
[wine/dibdrv.git] / server / async.c
blob2d8018a0f72952d62938aa91403e8d9a355d6586
1 /*
2 * Server-side async I/O support
4 * Copyright (C) 2007 Alexandre Julliard
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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21 #include <assert.h>
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <stdarg.h>
26 #include "ntstatus.h"
27 #define WIN32_NO_STATUS
28 #include "windef.h"
29 #include "winternl.h"
31 #include "object.h"
32 #include "file.h"
33 #include "request.h"
35 struct async
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 struct event *event;
42 async_data_t data; /* data for async I/O call */
45 static void async_dump( struct object *obj, int verbose );
46 static void async_destroy( struct object *obj );
48 static const struct object_ops async_ops =
50 sizeof(struct async), /* size */
51 async_dump, /* dump */
52 no_add_queue, /* add_queue */
53 NULL, /* remove_queue */
54 NULL, /* signaled */
55 NULL, /* satisfied */
56 no_signal, /* signal */
57 no_get_fd, /* get_fd */
58 no_map_access, /* map_access */
59 no_lookup_name, /* lookup_name */
60 no_close_handle, /* close_handle */
61 async_destroy /* destroy */
64 static void async_dump( struct object *obj, int verbose )
66 struct async *async = (struct async *)obj;
67 assert( obj->ops == &async_ops );
68 fprintf( stderr, "Async thread=%p\n", async->thread );
71 static void async_destroy( struct object *obj )
73 struct async *async = (struct async *)obj;
74 assert( obj->ops == &async_ops );
76 if (async->timeout) remove_timeout_user( async->timeout );
77 if (async->event) release_object( async->event );
78 release_object( async->thread );
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 )
85 apc_call_t data;
87 memset( &data, 0, sizeof(data) );
88 data.type = APC_ASYNC_IO;
89 data.async_io.func = async->data.callback;
90 data.async_io.user = async->data.arg;
91 data.async_io.sb = async->data.iosb;
92 data.async_io.status = status;
93 thread_queue_apc( async->thread, &async->obj, &data );
95 if (async->timeout) remove_timeout_user( async->timeout );
96 async->timeout = NULL;
97 list_remove( &async->queue_entry );
98 release_object( async );
101 /* callback for timeout on an async request */
102 static void async_timeout( void *private )
104 struct async *async = private;
106 async->timeout = NULL;
107 async_terminate( async, STATUS_TIMEOUT );
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, const async_data_t *data )
114 struct event *event = NULL;
115 struct async *async;
117 if (data->event && !(event = get_event_obj( thread->process, data->event, EVENT_MODIFY_STATE )))
118 return NULL;
120 if (!(async = alloc_object( &async_ops )))
122 if (event) release_object( event );
123 return NULL;
126 async->thread = (struct thread *)grab_object( thread );
127 async->event = event;
128 async->data = *data;
130 list_add_tail( queue, &async->queue_entry );
132 if (timeout) async->timeout = add_timeout_user( timeout, async_timeout, async );
133 else async->timeout = NULL;
135 if (event) reset_event( event );
136 return async;
139 /* store the result of the client-side async callback */
140 void async_set_result( struct object *obj, unsigned int status )
142 struct async *async = (struct async *)obj;
144 if (obj->ops != &async_ops) return; /* in case the client messed up the APC results */
146 if (status == STATUS_PENDING)
148 /* FIXME: restart the async operation */
150 else
152 if (async->event) set_event( async->event );
156 /* terminate the async operation at the head of the queue */
157 void async_terminate_head( struct list *queue, unsigned int status )
159 struct list *ptr = list_head( queue );
160 if (ptr) async_terminate( LIST_ENTRY( ptr, struct async, queue_entry ), status );
163 /* terminate all async operations on the queue */
164 void async_terminate_queue( struct list *queue, unsigned int status )
166 struct list *ptr, *next;
168 LIST_FOR_EACH_SAFE( ptr, next, queue )
170 struct async *async = LIST_ENTRY( ptr, struct async, queue_entry );
171 async_terminate( async, status );