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
=
42 struct object
*create_semaphore( const char *name
, unsigned int initial
, unsigned int max
)
44 struct semaphore
*sem
;
46 if (!max
|| (initial
> max
))
48 SET_ERROR( ERROR_INVALID_PARAMETER
);
51 if (!(sem
= (struct semaphore
*)create_named_object( name
, &semaphore_ops
, sizeof(*sem
) )))
53 if (GET_ERROR() != ERROR_ALREADY_EXISTS
)
55 /* initialize it if it didn't already exist */
62 int open_semaphore( unsigned int access
, int inherit
, const char *name
)
64 return open_object( name
, &semaphore_ops
, access
, inherit
);
67 int release_semaphore( int handle
, unsigned int count
, unsigned int *prev_count
)
69 struct semaphore
*sem
;
71 if (!(sem
= (struct semaphore
*)get_handle_obj( current
->process
, handle
,
72 SEMAPHORE_MODIFY_STATE
, &semaphore_ops
)))
75 *prev_count
= sem
->count
;
76 if (sem
->count
+ count
< sem
->count
|| sem
->count
+ count
> sem
->max
)
78 SET_ERROR( ERROR_TOO_MANY_POSTS
);
83 /* there cannot be any thread waiting if the count is != 0 */
84 assert( !sem
->obj
.head
);
90 wake_up( &sem
->obj
, count
);
92 release_object( sem
);
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 name='%s'\n",
101 sem
->count
, sem
->max
, get_object_name( &sem
->obj
) );
104 static int semaphore_signaled( struct object
*obj
, struct thread
*thread
)
106 struct semaphore
*sem
= (struct semaphore
*)obj
;
107 assert( obj
->ops
== &semaphore_ops
);
108 return (sem
->count
> 0);
111 static int semaphore_satisfied( struct object
*obj
, struct thread
*thread
)
113 struct semaphore
*sem
= (struct semaphore
*)obj
;
114 assert( obj
->ops
== &semaphore_ops
);
115 assert( sem
->count
);
117 return 0; /* not abandoned */
120 static void semaphore_destroy( struct object
*obj
)
122 struct semaphore
*sem
= (struct semaphore
*)obj
;
123 assert( obj
->ops
== &semaphore_ops
);