Implemented chains of surfaces. This allows an unlimited number
[wine/wine64.git] / server / mutex.c
blob32b74075be5ee8e2b288518f87ef97896f39f2c6
1 /*
2 * Server-side mutex management
4 * Copyright (C) 1998 Alexandre Julliard
5 */
7 #include <assert.h>
8 #include <stdio.h>
9 #include <stdlib.h>
11 #include "winerror.h"
12 #include "winnt.h"
14 #include "handle.h"
15 #include "thread.h"
16 #include "request.h"
18 struct mutex
20 struct object obj; /* object header */
21 struct thread *owner; /* mutex owner */
22 unsigned int count; /* recursion count */
23 int abandoned; /* has it been abandoned? */
24 struct mutex *next;
25 struct mutex *prev;
28 static void mutex_dump( struct object *obj, int verbose );
29 static int mutex_signaled( struct object *obj, struct thread *thread );
30 static int mutex_satisfied( struct object *obj, struct thread *thread );
32 static const struct object_ops mutex_ops =
34 sizeof(struct mutex),
35 mutex_dump,
36 add_queue,
37 remove_queue,
38 mutex_signaled,
39 mutex_satisfied,
40 no_read_fd,
41 no_write_fd,
42 no_flush,
43 no_get_file_info,
44 no_destroy
48 static struct mutex *create_mutex( const char *name, size_t len, int owned )
50 struct mutex *mutex;
52 if ((mutex = create_named_object( &mutex_ops, name, len )))
54 if (get_error() != ERROR_ALREADY_EXISTS)
56 /* initialize it if it didn't already exist */
57 mutex->count = 0;
58 mutex->owner = NULL;
59 mutex->abandoned = 0;
60 mutex->next = mutex->prev = NULL;
61 if (owned) mutex_satisfied( &mutex->obj, current );
64 return mutex;
67 /* release a mutex once the recursion count is 0 */
68 static void do_release( struct mutex *mutex, struct thread *thread )
70 assert( !mutex->count );
71 /* remove the mutex from the thread list of owned mutexes */
72 if (mutex->next) mutex->next->prev = mutex->prev;
73 if (mutex->prev) mutex->prev->next = mutex->next;
74 else thread->mutex = mutex->next;
75 mutex->owner = NULL;
76 mutex->next = mutex->prev = NULL;
77 wake_up( &mutex->obj, 0 );
80 void abandon_mutexes( struct thread *thread )
82 while (thread->mutex)
84 struct mutex *mutex = thread->mutex;
85 assert( mutex->owner == thread );
86 mutex->count = 0;
87 mutex->abandoned = 1;
88 do_release( mutex, thread );
92 static void mutex_dump( struct object *obj, int verbose )
94 struct mutex *mutex = (struct mutex *)obj;
95 assert( obj->ops == &mutex_ops );
96 printf( "Mutex count=%u owner=%p name='%s'\n",
97 mutex->count, mutex->owner, get_object_name( &mutex->obj) );
100 static int mutex_signaled( struct object *obj, struct thread *thread )
102 struct mutex *mutex = (struct mutex *)obj;
103 assert( obj->ops == &mutex_ops );
104 return (!mutex->count || (mutex->owner == thread));
107 static int mutex_satisfied( struct object *obj, struct thread *thread )
109 struct mutex *mutex = (struct mutex *)obj;
110 assert( obj->ops == &mutex_ops );
111 assert( !mutex->count || (mutex->owner == thread) );
113 if (!mutex->count++) /* FIXME: avoid wrap-around */
115 assert( !mutex->owner );
116 mutex->owner = thread;
117 mutex->prev = NULL;
118 if ((mutex->next = thread->mutex)) mutex->next->prev = mutex;
119 thread->mutex = mutex;
121 if (!mutex->abandoned) return 0;
122 mutex->abandoned = 0;
123 return 1;
126 /* create a mutex */
127 DECL_HANDLER(create_mutex)
129 size_t len = get_req_strlen( req->name );
130 struct mutex *mutex;
132 req->handle = -1;
133 if ((mutex = create_mutex( req->name, len, req->owned )))
135 req->handle = alloc_handle( current->process, mutex, MUTEX_ALL_ACCESS, req->inherit );
136 release_object( mutex );
140 /* open a handle to a mutex */
141 DECL_HANDLER(open_mutex)
143 size_t len = get_req_strlen( req->name );
144 req->handle = open_object( req->name, len, &mutex_ops, req->access, req->inherit );
147 /* release a mutex */
148 DECL_HANDLER(release_mutex)
150 struct mutex *mutex;
152 if ((mutex = (struct mutex *)get_handle_obj( current->process, req->handle,
153 MUTEX_MODIFY_STATE, &mutex_ops )))
155 if (!mutex->count || (mutex->owner != current)) set_error( ERROR_NOT_OWNER );
156 else if (!--mutex->count) do_release( mutex, current );
157 release_object( mutex );