New file. HKEY_CLASSES_ROOT handling.
[wine/multimedia.git] / server / semaphore.c
blob9cdedb9bf3e427c7b30b732094d397a3d41a0cc1
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"
13 #include "server/thread.h"
15 struct semaphore
17 struct object obj; /* object header */
18 unsigned int count; /* current count */
19 unsigned int max; /* maximum possible count */
22 static void dump_semaphore( struct object *obj, int verbose );
23 static int semaphore_signaled( struct object *obj, struct thread *thread );
24 static int semaphore_satisfied( struct object *obj, struct thread *thread );
25 static void destroy_semaphore( struct object *obj );
27 static const struct object_ops semaphore_ops =
29 dump_semaphore,
30 semaphore_signaled,
31 semaphore_satisfied,
32 destroy_semaphore
36 struct object *create_semaphore( const char *name, unsigned int initial, unsigned int max )
38 struct semaphore *sem;
40 if (!max || (initial > max))
42 SET_ERROR( ERROR_INVALID_PARAMETER );
43 return NULL;
45 if (!(sem = (struct semaphore *)create_named_object( name, &semaphore_ops, sizeof(*sem) )))
46 return NULL;
47 if (GET_ERROR() != ERROR_ALREADY_EXISTS)
49 /* initialize it if it didn't already exist */
50 sem->count = initial;
51 sem->max = max;
53 return &sem->obj;
56 int open_semaphore( unsigned int access, int inherit, const char *name )
58 return open_object( name, &semaphore_ops, access, inherit );
61 int release_semaphore( int handle, unsigned int count, unsigned int *prev_count )
63 struct semaphore *sem;
65 if (!(sem = (struct semaphore *)get_handle_obj( current->process, handle,
66 SEMAPHORE_MODIFY_STATE, &semaphore_ops )))
67 return 0;
69 *prev_count = sem->count;
70 if (sem->count + count < sem->count || sem->count + count > sem->max)
72 SET_ERROR( ERROR_TOO_MANY_POSTS );
73 return 0;
75 if (sem->count)
77 /* there cannot be any thread waiting if the count is != 0 */
78 assert( !sem->obj.head );
79 sem->count += count;
81 else
83 sem->count = count;
84 wake_up( &sem->obj, count );
86 release_object( sem );
87 return 1;
90 static void dump_semaphore( struct object *obj, int verbose )
92 struct semaphore *sem = (struct semaphore *)obj;
93 assert( obj->ops == &semaphore_ops );
94 printf( "Semaphore count=%d max=%d\n", sem->count, sem->max );
97 static int semaphore_signaled( struct object *obj, struct thread *thread )
99 struct semaphore *sem = (struct semaphore *)obj;
100 assert( obj->ops == &semaphore_ops );
101 return (sem->count > 0);
104 static int semaphore_satisfied( struct object *obj, struct thread *thread )
106 struct semaphore *sem = (struct semaphore *)obj;
107 assert( obj->ops == &semaphore_ops );
108 assert( sem->count );
109 sem->count--;
110 return 0; /* not abandoned */
113 static void destroy_semaphore( struct object *obj )
115 struct semaphore *sem = (struct semaphore *)obj;
116 assert( obj->ops == &semaphore_ops );
117 free( sem );