- move async activation into the server
[wine] / server / async.c
1 /*
2  * Server-side support for async i/o operations
3  *
4  * Copyright (C) 1998 Alexandre Julliard
5  * Copyright (C) 2000 Mike McCormack
6  *
7  */
8
9 #include "config.h"
10
11 #include <assert.h>
12 #include <string.h>
13 #include <stdlib.h>
14 #include <unistd.h>
15 #include <stdio.h>
16
17 #include "handle.h"
18 #include "thread.h"
19 #include "request.h"
20
21 #include "async.h"
22
23 void destroy_async( struct async *async )
24 {
25     struct async_queue *aq = async->q;
26
27     /*fprintf(stderr,"destroyed async %p\n",async->overlapped); */
28
29     if(async->timeout)
30         remove_timeout_user(async->timeout);
31     async->timeout = NULL;
32
33     if(async->prev)
34         async->prev->next = async->next;
35     else
36         aq->head = async->next;
37
38     if(async->next)
39         async->next->prev = async->prev;
40     else
41         aq->tail = async->prev;
42
43     async->q = NULL;
44     async->next = NULL;
45     async->prev = NULL;
46
47     free(async);
48 }
49
50 void async_notify(struct async *async, int status)
51 {
52     /* fprintf(stderr,"notifying %p!\n",async->overlapped); */
53     async->status = status;
54     thread_queue_apc(async->thread, NULL, async->func, APC_ASYNC, 1, 2, async->overlapped, status);
55 }
56
57 void destroy_async_queue( struct async_queue *q )
58 {
59     while(q->head)
60     {
61         async_notify(q->head, STATUS_HANDLES_CLOSED);
62         destroy_async(q->head);
63     }
64 }
65
66 struct async *find_async(struct async_queue *q, struct thread *thread, void *overlapped)
67 {
68     struct async *async;
69
70     /* fprintf(stderr,"find_async: %p\n",overlapped); */
71
72     if(!q)
73         return NULL;
74
75     for(async = q->head; async; async = async->next)
76         if((async->overlapped==overlapped) && (async->thread == thread))
77              return async;
78
79     return NULL;
80 }
81
82 void async_insert(struct async_queue *q, struct async *async)
83 {
84     async->q = q;
85     async->prev = q->tail;
86     async->next = NULL;
87
88     if(q->tail)
89         q->tail->next = async;
90     else
91         q->head = async;
92
93     q->tail = async;
94 }
95
96 static void async_callback(void *private)
97 {
98     struct async *async = (struct async *)private;
99
100     /* fprintf(stderr,"%p timeout out\n",async->overlapped); */
101     async->timeout = NULL;
102     async_notify(async, STATUS_TIMEOUT);
103     destroy_async(async);
104 }
105
106 struct async *create_async(struct object *obj, struct thread *thread, void *func, 
107                            void *overlapped)
108 {
109     struct async *async = (struct async *) malloc(sizeof(struct async));
110     if(!async)
111     {
112         set_error(STATUS_NO_MEMORY);
113         return NULL;
114     }
115
116     async->obj = obj;
117     async->thread = thread;
118     async->func = func;
119     async->overlapped = overlapped;
120     async->next = NULL;
121     async->prev = NULL;
122     async->q = NULL;
123     async->status = STATUS_PENDING;
124     async->timeout = NULL;
125
126     return async;
127 }
128
129 void async_add_timeout(struct async *async, int timeout)
130 {
131     if(timeout)
132     {
133         gettimeofday( &async->when, 0 );
134         add_timeout( &async->when, timeout );
135         async->timeout = add_timeout_user( &async->when, async_callback, async );
136     }
137 }
138
139 DECL_HANDLER(register_async)
140 {
141     struct object *obj;
142
143     if (!(obj = get_handle_obj( current->process, req->handle, 0, NULL)) )
144         return;
145
146     if(obj->ops->queue_async)
147     {
148         struct async_queue *q = obj->ops->queue_async(obj, NULL, req->type, 0);
149         struct async *async;
150
151         async = find_async(q, current, req->overlapped);
152         if(req->status==STATUS_PENDING)
153         {
154             if(!async)
155                 async = create_async(obj, current, req->func, req->overlapped);
156
157             if(async)
158             {
159                 async->status = req->status;
160                 if(!obj->ops->queue_async(obj, async, req->type, req->count))
161                     destroy_async(async);
162             }
163         }
164         else
165         {
166             if(async)
167                 destroy_async(async);
168             else
169                 set_error(STATUS_INVALID_PARAMETER);
170         }
171
172         set_select_events(obj,obj->ops->get_poll_events(obj));
173     }
174     else
175         set_error(STATUS_INVALID_HANDLE);
176
177     release_object(obj);
178 }
179