forked from wine-mirror/wine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy paththread.c
1826 lines (1609 loc) · 56.5 KB
/
thread.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Server-side thread management
*
* Copyright (C) 1998 Alexandre Julliard
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
*/
#include "config.h"
#include "wine/port.h"
#include <assert.h>
#include <errno.h>
#include <fcntl.h>
#include <signal.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <unistd.h>
#include <time.h>
#ifdef HAVE_POLL_H
#include <poll.h>
#endif
#ifdef HAVE_SCHED_H
#include <sched.h>
#endif
#include "ntstatus.h"
#define WIN32_NO_STATUS
#include "windef.h"
#include "winternl.h"
#include "file.h"
#include "handle.h"
#include "process.h"
#include "thread.h"
#include "request.h"
#include "user.h"
#include "security.h"
#ifdef __i386__
static const unsigned int supported_cpus = CPU_FLAG(CPU_x86);
#elif defined(__x86_64__)
static const unsigned int supported_cpus = CPU_FLAG(CPU_x86_64) | CPU_FLAG(CPU_x86);
#elif defined(__powerpc__)
static const unsigned int supported_cpus = CPU_FLAG(CPU_POWERPC);
#elif defined(__arm__)
static const unsigned int supported_cpus = CPU_FLAG(CPU_ARM);
#elif defined(__aarch64__)
static const unsigned int supported_cpus = CPU_FLAG(CPU_ARM64) | CPU_FLAG(CPU_ARM);
#else
#error Unsupported CPU
#endif
/* thread queues */
struct thread_wait
{
struct thread_wait *next; /* next wait structure for this thread */
struct thread *thread; /* owner thread */
int count; /* count of objects */
int flags;
int abandoned;
enum select_op select;
client_ptr_t key; /* wait key for keyed events */
client_ptr_t cookie; /* magic cookie to return to client */
timeout_t timeout;
struct timeout_user *user;
struct wait_queue_entry queues[1];
};
/* asynchronous procedure calls */
struct thread_apc
{
struct object obj; /* object header */
struct list entry; /* queue linked list */
struct thread *caller; /* thread that queued this apc */
struct object *owner; /* object that queued this apc */
int executed; /* has it been executed by the client? */
apc_call_t call; /* call arguments */
apc_result_t result; /* call results once executed */
};
static void dump_thread_apc( struct object *obj, int verbose );
static int thread_apc_signaled( struct object *obj, struct wait_queue_entry *entry );
static void thread_apc_destroy( struct object *obj );
static void clear_apc_queue( struct list *queue );
static const struct object_ops thread_apc_ops =
{
sizeof(struct thread_apc), /* size */
dump_thread_apc, /* dump */
no_get_type, /* get_type */
add_queue, /* add_queue */
remove_queue, /* remove_queue */
thread_apc_signaled, /* signaled */
no_satisfied, /* satisfied */
no_signal, /* signal */
no_get_fd, /* get_fd */
no_map_access, /* map_access */
default_get_sd, /* get_sd */
default_set_sd, /* set_sd */
no_lookup_name, /* lookup_name */
no_link_name, /* link_name */
NULL, /* unlink_name */
no_open_file, /* open_file */
no_close_handle, /* close_handle */
thread_apc_destroy /* destroy */
};
/* thread operations */
static void dump_thread( struct object *obj, int verbose );
static int thread_signaled( struct object *obj, struct wait_queue_entry *entry );
static unsigned int thread_map_access( struct object *obj, unsigned int access );
static void thread_poll_event( struct fd *fd, int event );
static void destroy_thread( struct object *obj );
static const struct object_ops thread_ops =
{
sizeof(struct thread), /* size */
dump_thread, /* dump */
no_get_type, /* get_type */
add_queue, /* add_queue */
remove_queue, /* remove_queue */
thread_signaled, /* signaled */
no_satisfied, /* satisfied */
no_signal, /* signal */
no_get_fd, /* get_fd */
thread_map_access, /* map_access */
default_get_sd, /* get_sd */
default_set_sd, /* set_sd */
no_lookup_name, /* lookup_name */
no_link_name, /* link_name */
NULL, /* unlink_name */
no_open_file, /* open_file */
no_close_handle, /* close_handle */
destroy_thread /* destroy */
};
static const struct fd_ops thread_fd_ops =
{
NULL, /* get_poll_events */
thread_poll_event, /* poll_event */
NULL, /* flush */
NULL, /* get_fd_type */
NULL, /* ioctl */
NULL, /* queue_async */
NULL /* reselect_async */
};
static struct list thread_list = LIST_INIT(thread_list);
/* initialize the structure for a newly allocated thread */
static inline void init_thread_structure( struct thread *thread )
{
int i;
thread->unix_pid = -1; /* not known yet */
thread->unix_tid = -1; /* not known yet */
thread->context = NULL;
thread->suspend_context = NULL;
thread->teb = 0;
thread->entry_point = 0;
thread->debug_ctx = NULL;
thread->debug_event = NULL;
thread->debug_break = 0;
thread->system_regs = 0;
thread->queue = NULL;
thread->wait = NULL;
thread->error = 0;
thread->req_data = NULL;
thread->req_toread = 0;
thread->reply_data = NULL;
thread->reply_towrite = 0;
thread->request_fd = NULL;
thread->reply_fd = NULL;
thread->wait_fd = NULL;
thread->state = RUNNING;
thread->exit_code = 0;
thread->priority = 0;
thread->suspend = 0;
thread->desktop_users = 0;
thread->token = NULL;
thread->creation_time = current_time;
thread->exit_time = 0;
list_init( &thread->mutex_list );
list_init( &thread->system_apc );
list_init( &thread->user_apc );
for (i = 0; i < MAX_INFLIGHT_FDS; i++)
thread->inflight[i].server = thread->inflight[i].client = -1;
}
/* check if address looks valid for a client-side data structure (TEB etc.) */
static inline int is_valid_address( client_ptr_t addr )
{
return addr && !(addr % sizeof(int));
}
/* create a new thread */
struct thread *create_thread( int fd, struct process *process, const struct security_descriptor *sd )
{
struct thread *thread;
int request_pipe[2];
if (fd == -1)
{
if (pipe( request_pipe ) == -1)
{
file_set_error();
return NULL;
}
if (send_client_fd( process, request_pipe[1], SERVER_PROTOCOL_VERSION ) == -1)
{
close( request_pipe[0] );
close( request_pipe[1] );
return NULL;
}
close( request_pipe[1] );
fd = request_pipe[0];
}
if (process->is_terminating)
{
close( fd );
set_error( STATUS_PROCESS_IS_TERMINATING );
return NULL;
}
if (!(thread = alloc_object( &thread_ops )))
{
close( fd );
return NULL;
}
init_thread_structure( thread );
thread->process = (struct process *)grab_object( process );
thread->desktop = process->desktop;
thread->affinity = process->affinity;
if (!current) current = thread;
list_add_head( &thread_list, &thread->entry );
if (sd && !set_sd_defaults_from_token( &thread->obj, sd,
OWNER_SECURITY_INFORMATION | GROUP_SECURITY_INFORMATION |
DACL_SECURITY_INFORMATION | SACL_SECURITY_INFORMATION,
process->token ))
{
close( fd );
release_object( thread );
return NULL;
}
if (!(thread->id = alloc_ptid( thread )))
{
close( fd );
release_object( thread );
return NULL;
}
if (!(thread->request_fd = create_anonymous_fd( &thread_fd_ops, fd, &thread->obj, 0 )))
{
release_object( thread );
return NULL;
}
set_fd_events( thread->request_fd, POLLIN ); /* start listening to events */
add_process_thread( thread->process, thread );
return thread;
}
/* handle a client event */
static void thread_poll_event( struct fd *fd, int event )
{
struct thread *thread = get_fd_user( fd );
assert( thread->obj.ops == &thread_ops );
grab_object( thread );
if (event & (POLLERR | POLLHUP)) kill_thread( thread, 0 );
else if (event & POLLIN) read_request( thread );
else if (event & POLLOUT) write_reply( thread );
release_object( thread );
}
/* cleanup everything that is no longer needed by a dead thread */
/* used by destroy_thread and kill_thread */
static void cleanup_thread( struct thread *thread )
{
int i;
clear_apc_queue( &thread->system_apc );
clear_apc_queue( &thread->user_apc );
free( thread->req_data );
free( thread->reply_data );
if (thread->request_fd) release_object( thread->request_fd );
if (thread->reply_fd) release_object( thread->reply_fd );
if (thread->wait_fd) release_object( thread->wait_fd );
free( thread->suspend_context );
cleanup_clipboard_thread(thread);
destroy_thread_windows( thread );
free_msg_queue( thread );
close_thread_desktop( thread );
for (i = 0; i < MAX_INFLIGHT_FDS; i++)
{
if (thread->inflight[i].client != -1)
{
close( thread->inflight[i].server );
thread->inflight[i].client = thread->inflight[i].server = -1;
}
}
thread->req_data = NULL;
thread->reply_data = NULL;
thread->request_fd = NULL;
thread->reply_fd = NULL;
thread->wait_fd = NULL;
thread->context = NULL;
thread->suspend_context = NULL;
thread->desktop = 0;
}
/* destroy a thread when its refcount is 0 */
static void destroy_thread( struct object *obj )
{
struct thread *thread = (struct thread *)obj;
assert( obj->ops == &thread_ops );
assert( !thread->debug_ctx ); /* cannot still be debugging something */
list_remove( &thread->entry );
cleanup_thread( thread );
release_object( thread->process );
if (thread->id) free_ptid( thread->id );
if (thread->token) release_object( thread->token );
}
/* dump a thread on stdout for debugging purposes */
static void dump_thread( struct object *obj, int verbose )
{
struct thread *thread = (struct thread *)obj;
assert( obj->ops == &thread_ops );
fprintf( stderr, "Thread id=%04x unix pid=%d unix tid=%d state=%d\n",
thread->id, thread->unix_pid, thread->unix_tid, thread->state );
}
static int thread_signaled( struct object *obj, struct wait_queue_entry *entry )
{
struct thread *mythread = (struct thread *)obj;
return (mythread->state == TERMINATED);
}
static unsigned int thread_map_access( struct object *obj, unsigned int access )
{
if (access & GENERIC_READ) access |= STANDARD_RIGHTS_READ | THREAD_QUERY_INFORMATION | THREAD_GET_CONTEXT;
if (access & GENERIC_WRITE) access |= STANDARD_RIGHTS_WRITE | THREAD_SET_INFORMATION | THREAD_SET_CONTEXT |
THREAD_TERMINATE | THREAD_SUSPEND_RESUME;
if (access & GENERIC_EXECUTE) access |= STANDARD_RIGHTS_EXECUTE | SYNCHRONIZE | THREAD_QUERY_LIMITED_INFORMATION;
if (access & GENERIC_ALL) access |= THREAD_ALL_ACCESS;
if (access & THREAD_QUERY_INFORMATION) access |= THREAD_QUERY_LIMITED_INFORMATION;
if (access & THREAD_SET_INFORMATION) access |= THREAD_SET_LIMITED_INFORMATION;
return access & ~(GENERIC_READ | GENERIC_WRITE | GENERIC_EXECUTE | GENERIC_ALL);
}
static void dump_thread_apc( struct object *obj, int verbose )
{
struct thread_apc *apc = (struct thread_apc *)obj;
assert( obj->ops == &thread_apc_ops );
fprintf( stderr, "APC owner=%p type=%u\n", apc->owner, apc->call.type );
}
static int thread_apc_signaled( struct object *obj, struct wait_queue_entry *entry )
{
struct thread_apc *apc = (struct thread_apc *)obj;
return apc->executed;
}
static void thread_apc_destroy( struct object *obj )
{
struct thread_apc *apc = (struct thread_apc *)obj;
if (apc->caller) release_object( apc->caller );
if (apc->owner) release_object( apc->owner );
}
/* queue an async procedure call */
static struct thread_apc *create_apc( struct object *owner, const apc_call_t *call_data )
{
struct thread_apc *apc;
if ((apc = alloc_object( &thread_apc_ops )))
{
apc->call = *call_data;
apc->caller = NULL;
apc->owner = owner;
apc->executed = 0;
apc->result.type = APC_NONE;
if (owner) grab_object( owner );
}
return apc;
}
/* get a thread pointer from a thread id (and increment the refcount) */
struct thread *get_thread_from_id( thread_id_t id )
{
struct object *obj = get_ptid_entry( id );
if (obj && obj->ops == &thread_ops) return (struct thread *)grab_object( obj );
set_error( STATUS_INVALID_CID );
return NULL;
}
/* get a thread from a handle (and increment the refcount) */
struct thread *get_thread_from_handle( obj_handle_t handle, unsigned int access )
{
return (struct thread *)get_handle_obj( current->process, handle,
access, &thread_ops );
}
/* find a thread from a Unix tid */
struct thread *get_thread_from_tid( int tid )
{
struct thread *thread;
LIST_FOR_EACH_ENTRY( thread, &thread_list, struct thread, entry )
{
if (thread->unix_tid == tid) return thread;
}
return NULL;
}
/* find a thread from a Unix pid */
struct thread *get_thread_from_pid( int pid )
{
struct thread *thread;
LIST_FOR_EACH_ENTRY( thread, &thread_list, struct thread, entry )
{
if (thread->unix_pid == pid) return thread;
}
return NULL;
}
int set_thread_affinity( struct thread *thread, affinity_t affinity )
{
int ret = 0;
#ifdef HAVE_SCHED_SETAFFINITY
if (thread->unix_tid != -1)
{
cpu_set_t set;
int i;
affinity_t mask;
CPU_ZERO( &set );
for (i = 0, mask = 1; mask; i++, mask <<= 1)
if (affinity & mask) CPU_SET( i, &set );
ret = sched_setaffinity( thread->unix_tid, sizeof(set), &set );
}
#endif
if (!ret) thread->affinity = affinity;
return ret;
}
affinity_t get_thread_affinity( struct thread *thread )
{
affinity_t mask = 0;
#ifdef HAVE_SCHED_SETAFFINITY
if (thread->unix_tid != -1)
{
cpu_set_t set;
unsigned int i;
if (!sched_getaffinity( thread->unix_tid, sizeof(set), &set ))
for (i = 0; i < 8 * sizeof(mask); i++)
if (CPU_ISSET( i, &set )) mask |= (affinity_t)1 << i;
}
#endif
if (!mask) mask = ~(affinity_t)0;
return mask;
}
#define THREAD_PRIORITY_REALTIME_HIGHEST 6
#define THREAD_PRIORITY_REALTIME_LOWEST -7
/* set all information about a thread */
static void set_thread_info( struct thread *thread,
const struct set_thread_info_request *req )
{
if (req->mask & SET_THREAD_INFO_PRIORITY)
{
int max = THREAD_PRIORITY_HIGHEST;
int min = THREAD_PRIORITY_LOWEST;
if (thread->process->priority == PROCESS_PRIOCLASS_REALTIME)
{
max = THREAD_PRIORITY_REALTIME_HIGHEST;
min = THREAD_PRIORITY_REALTIME_LOWEST;
}
if ((req->priority >= min && req->priority <= max) ||
req->priority == THREAD_PRIORITY_IDLE ||
req->priority == THREAD_PRIORITY_TIME_CRITICAL)
thread->priority = req->priority;
else
set_error( STATUS_INVALID_PARAMETER );
}
if (req->mask & SET_THREAD_INFO_AFFINITY)
{
if ((req->affinity & thread->process->affinity) != req->affinity)
set_error( STATUS_INVALID_PARAMETER );
else if (thread->state == TERMINATED)
set_error( STATUS_THREAD_IS_TERMINATING );
else if (set_thread_affinity( thread, req->affinity ))
file_set_error();
}
if (req->mask & SET_THREAD_INFO_TOKEN)
security_set_thread_token( thread, req->token );
if (req->mask & SET_THREAD_INFO_ENTRYPOINT)
thread->entry_point = req->entry_point;
}
/* stop a thread (at the Unix level) */
void stop_thread( struct thread *thread )
{
if (thread->context) return; /* already inside a debug event, no need for a signal */
/* can't stop a thread while initialisation is in progress */
if (is_process_init_done(thread->process)) send_thread_signal( thread, SIGUSR1 );
}
/* stop a thread if it's supposed to be suspended */
void stop_thread_if_suspended( struct thread *thread )
{
if (thread->suspend + thread->process->suspend > 0) stop_thread( thread );
}
/* suspend a thread */
static int suspend_thread( struct thread *thread )
{
int old_count = thread->suspend;
if (thread->suspend < MAXIMUM_SUSPEND_COUNT)
{
if (!(thread->process->suspend + thread->suspend++)) stop_thread( thread );
}
else set_error( STATUS_SUSPEND_COUNT_EXCEEDED );
return old_count;
}
/* resume a thread */
static int resume_thread( struct thread *thread )
{
int old_count = thread->suspend;
if (thread->suspend > 0)
{
if (!(--thread->suspend + thread->process->suspend)) wake_thread( thread );
}
return old_count;
}
/* add a thread to an object wait queue; return 1 if OK, 0 on error */
int add_queue( struct object *obj, struct wait_queue_entry *entry )
{
grab_object( obj );
entry->obj = obj;
list_add_tail( &obj->wait_queue, &entry->entry );
return 1;
}
/* remove a thread from an object wait queue */
void remove_queue( struct object *obj, struct wait_queue_entry *entry )
{
list_remove( &entry->entry );
release_object( obj );
}
struct thread *get_wait_queue_thread( struct wait_queue_entry *entry )
{
return entry->wait->thread;
}
enum select_op get_wait_queue_select_op( struct wait_queue_entry *entry )
{
return entry->wait->select;
}
client_ptr_t get_wait_queue_key( struct wait_queue_entry *entry )
{
return entry->wait->key;
}
void make_wait_abandoned( struct wait_queue_entry *entry )
{
entry->wait->abandoned = 1;
}
/* finish waiting */
static unsigned int end_wait( struct thread *thread, unsigned int status )
{
struct thread_wait *wait = thread->wait;
struct wait_queue_entry *entry;
int i;
assert( wait );
thread->wait = wait->next;
if (status < wait->count) /* wait satisfied, tell it to the objects */
{
if (wait->select == SELECT_WAIT_ALL)
{
for (i = 0, entry = wait->queues; i < wait->count; i++, entry++)
entry->obj->ops->satisfied( entry->obj, entry );
}
else
{
entry = wait->queues + status;
entry->obj->ops->satisfied( entry->obj, entry );
}
if (wait->abandoned) status += STATUS_ABANDONED_WAIT_0;
}
for (i = 0, entry = wait->queues; i < wait->count; i++, entry++)
entry->obj->ops->remove_queue( entry->obj, entry );
if (wait->user) remove_timeout_user( wait->user );
free( wait );
return status;
}
/* build the thread wait structure */
static int wait_on( const select_op_t *select_op, unsigned int count, struct object *objects[],
int flags, timeout_t timeout )
{
struct thread_wait *wait;
struct wait_queue_entry *entry;
unsigned int i;
if (!(wait = mem_alloc( FIELD_OFFSET(struct thread_wait, queues[count]) ))) return 0;
wait->next = current->wait;
wait->thread = current;
wait->count = count;
wait->flags = flags;
wait->select = select_op->op;
wait->cookie = 0;
wait->user = NULL;
wait->timeout = timeout;
wait->abandoned = 0;
current->wait = wait;
for (i = 0, entry = wait->queues; i < count; i++, entry++)
{
struct object *obj = objects[i];
entry->wait = wait;
if (!obj->ops->add_queue( obj, entry ))
{
wait->count = i;
end_wait( current, get_error() );
return 0;
}
}
return 1;
}
static int wait_on_handles( const select_op_t *select_op, unsigned int count, const obj_handle_t *handles,
int flags, timeout_t timeout )
{
struct object *objects[MAXIMUM_WAIT_OBJECTS];
unsigned int i;
int ret = 0;
assert( count <= MAXIMUM_WAIT_OBJECTS );
for (i = 0; i < count; i++)
if (!(objects[i] = get_handle_obj( current->process, handles[i], SYNCHRONIZE, NULL )))
break;
if (i == count) ret = wait_on( select_op, count, objects, flags, timeout );
while (i > 0) release_object( objects[--i] );
return ret;
}
/* check if the thread waiting condition is satisfied */
static int check_wait( struct thread *thread )
{
int i;
struct thread_wait *wait = thread->wait;
struct wait_queue_entry *entry;
assert( wait );
if ((wait->flags & SELECT_INTERRUPTIBLE) && !list_empty( &thread->system_apc ))
return STATUS_USER_APC;
/* Suspended threads may not acquire locks, but they can run system APCs */
if (thread->process->suspend + thread->suspend > 0) return -1;
if (wait->select == SELECT_WAIT_ALL)
{
int not_ok = 0;
/* Note: we must check them all anyway, as some objects may
* want to do something when signaled, even if others are not */
for (i = 0, entry = wait->queues; i < wait->count; i++, entry++)
not_ok |= !entry->obj->ops->signaled( entry->obj, entry );
if (!not_ok) return STATUS_WAIT_0;
}
else
{
for (i = 0, entry = wait->queues; i < wait->count; i++, entry++)
if (entry->obj->ops->signaled( entry->obj, entry )) return i;
}
if ((wait->flags & SELECT_ALERTABLE) && !list_empty(&thread->user_apc)) return STATUS_USER_APC;
if (wait->timeout <= current_time) return STATUS_TIMEOUT;
return -1;
}
/* send the wakeup signal to a thread */
static int send_thread_wakeup( struct thread *thread, client_ptr_t cookie, int signaled )
{
struct wake_up_reply reply;
int ret;
memset( &reply, 0, sizeof(reply) );
reply.cookie = cookie;
reply.signaled = signaled;
if ((ret = write( get_unix_fd( thread->wait_fd ), &reply, sizeof(reply) )) == sizeof(reply))
return 0;
if (ret >= 0)
fatal_protocol_error( thread, "partial wakeup write %d\n", ret );
else if (errno == EPIPE)
kill_thread( thread, 0 ); /* normal death */
else
fatal_protocol_error( thread, "write: %s\n", strerror( errno ));
return -1;
}
/* attempt to wake up a thread */
/* return >0 if OK, 0 if the wait condition is still not satisfied and -1 on error */
int wake_thread( struct thread *thread )
{
int signaled, count;
client_ptr_t cookie;
for (count = 0; thread->wait; count++)
{
if ((signaled = check_wait( thread )) == -1) break;
cookie = thread->wait->cookie;
signaled = end_wait( thread, signaled );
if (debug_level) fprintf( stderr, "%04x: *wakeup* signaled=%d\n", thread->id, signaled );
if (cookie && send_thread_wakeup( thread, cookie, signaled ) == -1) /* error */
{
if (!count) count = -1;
break;
}
}
return count;
}
/* attempt to wake up a thread from a wait queue entry, assuming that it is signaled */
int wake_thread_queue_entry( struct wait_queue_entry *entry )
{
struct thread_wait *wait = entry->wait;
struct thread *thread = wait->thread;
int signaled;
client_ptr_t cookie;
if (thread->wait != wait) return 0; /* not the current wait */
if (thread->process->suspend + thread->suspend > 0) return 0; /* cannot acquire locks */
assert( wait->select != SELECT_WAIT_ALL );
cookie = wait->cookie;
signaled = end_wait( thread, entry - wait->queues );
if (debug_level) fprintf( stderr, "%04x: *wakeup* signaled=%d\n", thread->id, signaled );
if (!cookie || send_thread_wakeup( thread, cookie, signaled ) != -1)
wake_thread( thread ); /* check other waits too */
return 1;
}
/* thread wait timeout */
static void thread_timeout( void *ptr )
{
struct thread_wait *wait = ptr;
struct thread *thread = wait->thread;
client_ptr_t cookie = wait->cookie;
wait->user = NULL;
if (thread->wait != wait) return; /* not the top-level wait, ignore it */
if (thread->suspend + thread->process->suspend > 0) return; /* suspended, ignore it */
if (debug_level) fprintf( stderr, "%04x: *wakeup* signaled=TIMEOUT\n", thread->id );
end_wait( thread, STATUS_TIMEOUT );
assert( cookie );
if (send_thread_wakeup( thread, cookie, STATUS_TIMEOUT ) == -1) return;
/* check if other objects have become signaled in the meantime */
wake_thread( thread );
}
/* try signaling an event flag, a semaphore or a mutex */
static int signal_object( obj_handle_t handle )
{
struct object *obj;
int ret = 0;
obj = get_handle_obj( current->process, handle, 0, NULL );
if (obj)
{
ret = obj->ops->signal( obj, get_handle_access( current->process, handle ));
release_object( obj );
}
return ret;
}
/* select on a list of handles */
static timeout_t select_on( const select_op_t *select_op, data_size_t op_size, client_ptr_t cookie,
int flags, timeout_t timeout )
{
int ret;
unsigned int count;
struct object *object;
if (timeout <= 0) timeout = current_time - timeout;
switch (select_op->op)
{
case SELECT_NONE:
if (!wait_on( select_op, 0, NULL, flags, timeout )) return timeout;
break;
case SELECT_WAIT:
case SELECT_WAIT_ALL:
count = (op_size - offsetof( select_op_t, wait.handles )) / sizeof(select_op->wait.handles[0]);
if (op_size < offsetof( select_op_t, wait.handles ) || count > MAXIMUM_WAIT_OBJECTS)
{
set_error( STATUS_INVALID_PARAMETER );
return 0;
}
if (!wait_on_handles( select_op, count, select_op->wait.handles, flags, timeout ))
return timeout;
break;
case SELECT_SIGNAL_AND_WAIT:
if (!wait_on_handles( select_op, 1, &select_op->signal_and_wait.wait, flags, timeout ))
return timeout;
if (select_op->signal_and_wait.signal)
{
if (!signal_object( select_op->signal_and_wait.signal ))
{
end_wait( current, get_error() );
return timeout;
}
/* check if we woke ourselves up */
if (!current->wait) return timeout;
}
break;
case SELECT_KEYED_EVENT_WAIT:
case SELECT_KEYED_EVENT_RELEASE:
object = (struct object *)get_keyed_event_obj( current->process, select_op->keyed_event.handle,
select_op->op == SELECT_KEYED_EVENT_WAIT ? KEYEDEVENT_WAIT : KEYEDEVENT_WAKE );
if (!object) return timeout;
ret = wait_on( select_op, 1, &object, flags, timeout );
release_object( object );
if (!ret) return timeout;
current->wait->key = select_op->keyed_event.key;
break;
default:
set_error( STATUS_INVALID_PARAMETER );
return 0;
}
if ((ret = check_wait( current )) != -1)
{
/* condition is already satisfied */
set_error( end_wait( current, ret ));
return timeout;
}
/* now we need to wait */
if (current->wait->timeout != TIMEOUT_INFINITE)
{
if (!(current->wait->user = add_timeout_user( current->wait->timeout,
thread_timeout, current->wait )))
{
end_wait( current, get_error() );
return timeout;
}
}
current->wait->cookie = cookie;
set_error( STATUS_PENDING );
return timeout;
}
/* attempt to wake threads sleeping on the object wait queue */
void wake_up( struct object *obj, int max )
{
struct list *ptr;
int ret;
LIST_FOR_EACH( ptr, &obj->wait_queue )
{
struct wait_queue_entry *entry = LIST_ENTRY( ptr, struct wait_queue_entry, entry );
if (!(ret = wake_thread( get_wait_queue_thread( entry )))) continue;
if (ret > 0 && max && !--max) break;
/* restart at the head of the list since a wake up can change the object wait queue */
ptr = &obj->wait_queue;
}
}
/* return the apc queue to use for a given apc type */
static inline struct list *get_apc_queue( struct thread *thread, enum apc_type type )
{
switch(type)
{
case APC_NONE:
case APC_USER:
case APC_TIMER:
return &thread->user_apc;
default:
return &thread->system_apc;
}
}
/* check if thread is currently waiting for a (system) apc */
static inline int is_in_apc_wait( struct thread *thread )
{
return (thread->process->suspend || thread->suspend ||
(thread->wait && (thread->wait->flags & SELECT_INTERRUPTIBLE)));
}
/* queue an existing APC to a given thread */
static int queue_apc( struct process *process, struct thread *thread, struct thread_apc *apc )
{
struct list *queue;
if (thread && thread->state == TERMINATED && process)
thread = NULL;
if (!thread) /* find a suitable thread inside the process */
{
struct thread *candidate;
/* first try to find a waiting thread */
LIST_FOR_EACH_ENTRY( candidate, &process->thread_list, struct thread, proc_entry )
{
if (candidate->state == TERMINATED) continue;
if (is_in_apc_wait( candidate ))
{
thread = candidate;
break;
}
}
if (!thread)
{
/* then use the first one that accepts a signal */
LIST_FOR_EACH_ENTRY( candidate, &process->thread_list, struct thread, proc_entry )
{
if (send_thread_signal( candidate, SIGUSR1 ))
{
thread = candidate;
break;
}
}
}
if (!thread) return 0; /* nothing found */
queue = get_apc_queue( thread, apc->call.type );
}
else
{
if (thread->state == TERMINATED) return 0;
queue = get_apc_queue( thread, apc->call.type );
/* send signal for system APCs if needed */
if (queue == &thread->system_apc && list_empty( queue ) && !is_in_apc_wait( thread ))
{
if (!send_thread_signal( thread, SIGUSR1 )) return 0;
}
/* cancel a possible previous APC with the same owner */
if (apc->owner) thread_cancel_apc( thread, apc->owner, apc->call.type );
}