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 semaphore_dump( 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 semaphore_destroy( struct object
*obj
);
27 static const struct object_ops semaphore_ops
=
38 struct object
*create_semaphore( const char *name
, unsigned int initial
, unsigned int max
)
40 struct semaphore
*sem
;
42 if (!max
|| (initial
> max
))
44 SET_ERROR( ERROR_INVALID_PARAMETER
);
47 if (!(sem
= (struct semaphore
*)create_named_object( name
, &semaphore_ops
, sizeof(*sem
) )))
49 if (GET_ERROR() != ERROR_ALREADY_EXISTS
)
51 /* initialize it if it didn't already exist */
58 int open_semaphore( unsigned int access
, int inherit
, const char *name
)
60 return open_object( name
, &semaphore_ops
, access
, inherit
);
63 int release_semaphore( int handle
, unsigned int count
, unsigned int *prev_count
)
65 struct semaphore
*sem
;
67 if (!(sem
= (struct semaphore
*)get_handle_obj( current
->process
, handle
,
68 SEMAPHORE_MODIFY_STATE
, &semaphore_ops
)))
71 *prev_count
= sem
->count
;
72 if (sem
->count
+ count
< sem
->count
|| sem
->count
+ count
> sem
->max
)
74 SET_ERROR( ERROR_TOO_MANY_POSTS
);
79 /* there cannot be any thread waiting if the count is != 0 */
80 assert( !sem
->obj
.head
);
86 wake_up( &sem
->obj
, count
);
88 release_object( sem
);
92 static void semaphore_dump( struct object
*obj
, int verbose
)
94 struct semaphore
*sem
= (struct semaphore
*)obj
;
95 assert( obj
->ops
== &semaphore_ops
);
96 printf( "Semaphore count=%d max=%d\n", sem
->count
, sem
->max
);
99 static int semaphore_signaled( struct object
*obj
, struct thread
*thread
)
101 struct semaphore
*sem
= (struct semaphore
*)obj
;
102 assert( obj
->ops
== &semaphore_ops
);
103 return (sem
->count
> 0);
106 static int semaphore_satisfied( struct object
*obj
, struct thread
*thread
)
108 struct semaphore
*sem
= (struct semaphore
*)obj
;
109 assert( obj
->ops
== &semaphore_ops
);
110 assert( sem
->count
);
112 return 0; /* not abandoned */
115 static void semaphore_destroy( struct object
*obj
)
117 struct semaphore
*sem
= (struct semaphore
*)obj
;
118 assert( obj
->ops
== &semaphore_ops
);