Store all object names as Unicode in the server.
[wine/multimedia.git] / server / semaphore.c
blob36656f4c6e88e0fa692576007bffd93b9b9ad215
1 /*
2 * Server-side semaphore 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 semaphore
20 struct object obj; /* object header */
21 unsigned int count; /* current count */
22 unsigned int max; /* maximum possible count */
25 static void semaphore_dump( struct object *obj, int verbose );
26 static int semaphore_signaled( struct object *obj, struct thread *thread );
27 static int semaphore_satisfied( struct object *obj, struct thread *thread );
29 static const struct object_ops semaphore_ops =
31 sizeof(struct semaphore),
32 semaphore_dump,
33 add_queue,
34 remove_queue,
35 semaphore_signaled,
36 semaphore_satisfied,
37 no_read_fd,
38 no_write_fd,
39 no_flush,
40 no_get_file_info,
41 no_destroy
45 static struct semaphore *create_semaphore( const WCHAR *name, size_t len,
46 unsigned int initial, unsigned int max )
48 struct semaphore *sem;
50 if (!max || (initial > max))
52 set_error( ERROR_INVALID_PARAMETER );
53 return NULL;
55 if ((sem = create_named_object( &semaphore_ops, name, len )))
57 if (get_error() != ERROR_ALREADY_EXISTS)
59 /* initialize it if it didn't already exist */
60 sem->count = initial;
61 sem->max = max;
64 return sem;
67 static unsigned int release_semaphore( int handle, unsigned int count )
69 struct semaphore *sem;
70 unsigned int prev = 0;
72 if ((sem = (struct semaphore *)get_handle_obj( current->process, handle,
73 SEMAPHORE_MODIFY_STATE, &semaphore_ops )))
75 prev = sem->count;
76 if (sem->count + count < sem->count || sem->count + count > sem->max)
78 set_error( ERROR_TOO_MANY_POSTS );
80 else if (sem->count)
82 /* there cannot be any thread waiting if the count is != 0 */
83 assert( !sem->obj.head );
84 sem->count += count;
86 else
88 sem->count = count;
89 wake_up( &sem->obj, count );
91 release_object( sem );
93 return prev;
96 static void semaphore_dump( struct object *obj, int verbose )
98 struct semaphore *sem = (struct semaphore *)obj;
99 assert( obj->ops == &semaphore_ops );
100 fprintf( stderr, "Semaphore count=%d max=%d ", sem->count, sem->max );
101 dump_object_name( &sem->obj );
102 fputc( '\n', stderr );
105 static int semaphore_signaled( struct object *obj, struct thread *thread )
107 struct semaphore *sem = (struct semaphore *)obj;
108 assert( obj->ops == &semaphore_ops );
109 return (sem->count > 0);
112 static int semaphore_satisfied( struct object *obj, struct thread *thread )
114 struct semaphore *sem = (struct semaphore *)obj;
115 assert( obj->ops == &semaphore_ops );
116 assert( sem->count );
117 sem->count--;
118 return 0; /* not abandoned */
121 /* create a semaphore */
122 DECL_HANDLER(create_semaphore)
124 size_t len = get_req_strlenW( req->name );
125 struct semaphore *sem;
127 req->handle = -1;
128 if ((sem = create_semaphore( req->name, len, req->initial, req->max )))
130 req->handle = alloc_handle( current->process, sem, SEMAPHORE_ALL_ACCESS, req->inherit );
131 release_object( sem );
135 /* open a handle to a semaphore */
136 DECL_HANDLER(open_semaphore)
138 size_t len = get_req_strlenW( req->name );
139 req->handle = open_object( req->name, len, &semaphore_ops, req->access, req->inherit );
142 /* release a semaphore */
143 DECL_HANDLER(release_semaphore)
145 req->prev_count = release_semaphore( req->handle, req->count );