2 * Server-side semaphore management
4 * Copyright (C) 1998 Alexandre Julliard
13 #include "server/thread.h"
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
=
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
);
45 if (!(sem
= (struct semaphore
*)create_named_object( name
, &semaphore_ops
, sizeof(*sem
) )))
47 if (GET_ERROR() != ERROR_ALREADY_EXISTS
)
49 /* initialize it if it didn't already exist */
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
)))
69 *prev_count
= sem
->count
;
70 if (sem
->count
+ count
< sem
->count
|| sem
->count
+ count
> sem
->max
)
72 SET_ERROR( ERROR_TOO_MANY_POSTS
);
77 /* there cannot be any thread waiting if the count is != 0 */
78 assert( !sem
->obj
.head
);
84 wake_up( &sem
->obj
, count
);
86 release_object( sem
);
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
);
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
);