Enhanced synchronization between playback thread and stop/close commands
[wine.git] / server / snapshot.c
blob5ac30ccd6a46b46f2cc9a9c13a0909f9c97b9752
1 /*
2 * Server-side snapshots
4 * Copyright (C) 1999 Alexandre Julliard
6 * FIXME: only process snapshots implemented for now
7 */
9 #include <assert.h>
10 #include <stdio.h>
11 #include <stdlib.h>
13 #include "winerror.h"
14 #include "winnt.h"
15 #include "tlhelp32.h"
17 #include "handle.h"
18 #include "process.h"
19 #include "thread.h"
20 #include "request.h"
23 struct snapshot
25 struct object obj; /* object header */
26 struct process_snapshot *process; /* processes snapshot */
27 int process_count; /* count of processes */
28 int process_pos; /* current position in proc snapshot */
31 static void snapshot_dump( struct object *obj, int verbose );
32 static void snapshot_destroy( struct object *obj );
34 static const struct object_ops snapshot_ops =
36 sizeof(struct snapshot),
37 snapshot_dump,
38 no_add_queue,
39 NULL, /* should never get called */
40 NULL, /* should never get called */
41 NULL, /* should never get called */
42 no_read_fd,
43 no_write_fd,
44 no_flush,
45 no_get_file_info,
46 snapshot_destroy
50 /* create a new snapshot */
51 static struct snapshot *create_snapshot( int flags )
53 struct snapshot *snapshot;
55 if ((snapshot = alloc_object( &snapshot_ops )))
57 if (flags & TH32CS_SNAPPROCESS)
58 snapshot->process = process_snap( &snapshot->process_count );
59 else
60 snapshot->process_count = 0;
61 snapshot->process_pos = 0;
63 return snapshot;
66 /* get the next process in the snapshot */
67 static int snapshot_next_process( struct snapshot *snapshot, struct next_process_request *req )
69 struct process_snapshot *ptr;
71 if (!snapshot->process_count)
73 set_error( ERROR_INVALID_PARAMETER ); /* FIXME */
74 return 0;
76 if (req->reset) snapshot->process_pos = 0;
77 else if (snapshot->process_pos >= snapshot->process_count)
79 set_error( ERROR_NO_MORE_FILES );
80 return 0;
82 ptr = &snapshot->process[snapshot->process_pos++];
83 req->pid = ptr->process;
84 req->threads = ptr->threads;
85 req->priority = ptr->priority;
86 return 1;
89 static void snapshot_dump( struct object *obj, int verbose )
91 struct snapshot *snapshot = (struct snapshot *)obj;
92 assert( obj->ops == &snapshot_ops );
93 fprintf( stderr, "Snapshot: %d processes\n",
94 snapshot->process_count );
97 static void snapshot_destroy( struct object *obj )
99 int i;
100 struct snapshot *snapshot = (struct snapshot *)obj;
101 assert( obj->ops == &snapshot_ops );
102 if (snapshot->process_count)
104 for (i = 0; i < snapshot->process_count; i++)
105 release_object( snapshot->process[i].process );
106 free( snapshot->process );
110 /* create a snapshot */
111 DECL_HANDLER(create_snapshot)
113 struct snapshot *snapshot;
115 req->handle = -1;
116 if ((snapshot = create_snapshot( req->flags )))
118 req->handle = alloc_handle( current->process, snapshot, 0, req->inherit );
119 release_object( snapshot );
123 /* get the next process from a snapshot */
124 DECL_HANDLER(next_process)
126 struct snapshot *snapshot;
128 if ((snapshot = (struct snapshot *)get_handle_obj( current->process, req->handle,
129 0, &snapshot_ops )))
131 snapshot_next_process( snapshot, req );
132 release_object( snapshot );