Fix some treeview notification codes between ANSI/Unicode mode.
[wine/multimedia.git] / server / semaphore.c
blob4e7e6b6aa0d3a8ac623489f4e9f45311eaaa2c99
1 /*
2 * Server-side semaphore management
4 * Copyright (C) 1998 Alexandre Julliard
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 #include "config.h"
22 #include "wine/port.h"
24 #include <assert.h>
25 #include <stdio.h>
26 #include <stdlib.h>
28 #include "winnt.h"
30 #include "handle.h"
31 #include "thread.h"
32 #include "request.h"
34 struct semaphore
36 struct object obj; /* object header */
37 unsigned int count; /* current count */
38 unsigned int max; /* maximum possible count */
41 static void semaphore_dump( struct object *obj, int verbose );
42 static int semaphore_signaled( struct object *obj, struct thread *thread );
43 static int semaphore_satisfied( struct object *obj, struct thread *thread );
45 static const struct object_ops semaphore_ops =
47 sizeof(struct semaphore), /* size */
48 semaphore_dump, /* dump */
49 add_queue, /* add_queue */
50 remove_queue, /* remove_queue */
51 semaphore_signaled, /* signaled */
52 semaphore_satisfied, /* satisfied */
53 NULL, /* get_poll_events */
54 NULL, /* poll_event */
55 no_get_fd, /* get_fd */
56 no_flush, /* flush */
57 no_get_file_info, /* get_file_info */
58 NULL, /* queue_async */
59 no_destroy /* destroy */
63 static struct semaphore *create_semaphore( const WCHAR *name, size_t len,
64 unsigned int initial, unsigned int max )
66 struct semaphore *sem;
68 if (!max || (initial > max))
70 set_error( STATUS_INVALID_PARAMETER );
71 return NULL;
73 if ((sem = create_named_object( sync_namespace, &semaphore_ops, name, len )))
75 if (get_error() != STATUS_OBJECT_NAME_COLLISION)
77 /* initialize it if it didn't already exist */
78 sem->count = initial;
79 sem->max = max;
82 return sem;
85 static unsigned int release_semaphore( obj_handle_t handle, unsigned int count )
87 struct semaphore *sem;
88 unsigned int prev = 0;
90 if ((sem = (struct semaphore *)get_handle_obj( current->process, handle,
91 SEMAPHORE_MODIFY_STATE, &semaphore_ops )))
93 prev = sem->count;
94 if (sem->count + count < sem->count || sem->count + count > sem->max)
96 set_error( STATUS_SEMAPHORE_LIMIT_EXCEEDED );
98 else if (sem->count)
100 /* there cannot be any thread waiting if the count is != 0 */
101 assert( !sem->obj.head );
102 sem->count += count;
104 else
106 sem->count = count;
107 wake_up( &sem->obj, count );
109 release_object( sem );
111 return prev;
114 static void semaphore_dump( struct object *obj, int verbose )
116 struct semaphore *sem = (struct semaphore *)obj;
117 assert( obj->ops == &semaphore_ops );
118 fprintf( stderr, "Semaphore count=%d max=%d ", sem->count, sem->max );
119 dump_object_name( &sem->obj );
120 fputc( '\n', stderr );
123 static int semaphore_signaled( struct object *obj, struct thread *thread )
125 struct semaphore *sem = (struct semaphore *)obj;
126 assert( obj->ops == &semaphore_ops );
127 return (sem->count > 0);
130 static int semaphore_satisfied( struct object *obj, struct thread *thread )
132 struct semaphore *sem = (struct semaphore *)obj;
133 assert( obj->ops == &semaphore_ops );
134 assert( sem->count );
135 sem->count--;
136 return 0; /* not abandoned */
139 /* create a semaphore */
140 DECL_HANDLER(create_semaphore)
142 struct semaphore *sem;
144 reply->handle = 0;
145 if ((sem = create_semaphore( get_req_data(), get_req_data_size(),
146 req->initial, req->max )))
148 reply->handle = alloc_handle( current->process, sem, SEMAPHORE_ALL_ACCESS, req->inherit );
149 release_object( sem );
153 /* open a handle to a semaphore */
154 DECL_HANDLER(open_semaphore)
156 reply->handle = open_object( sync_namespace, get_req_data(), get_req_data_size(),
157 &semaphore_ops, req->access, req->inherit );
160 /* release a semaphore */
161 DECL_HANDLER(release_semaphore)
163 reply->prev_count = release_semaphore( req->handle, req->count );