Back to index

glibc  2.9
semaphore.h
Go to the documentation of this file.
00001 /* Linuxthreads - a simple clone()-based implementation of Posix        */
00002 /* threads for Linux.                                                   */
00003 /* Copyright (C) 1996 Xavier Leroy (Xavier.Leroy@inria.fr)              */
00004 /*                                                                      */
00005 /* This program is free software; you can redistribute it and/or        */
00006 /* modify it under the terms of the GNU Library General Public License  */
00007 /* as published by the Free Software Foundation; either version 2       */
00008 /* of the License, or (at your option) any later version.               */
00009 /*                                                                      */
00010 /* This program is distributed in the hope that it will be useful,      */
00011 /* but WITHOUT ANY WARRANTY; without even the implied warranty of       */
00012 /* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the        */
00013 /* GNU Library General Public License for more details.                 */
00014 
00015 #ifndef _SEMAPHORE_H
00016 #define _SEMAPHORE_H    1
00017 
00018 #include <features.h>
00019 #include <sys/types.h>
00020 #ifdef __USE_XOPEN2K
00021 # define __need_timespec
00022 # include <time.h>
00023 #endif
00024 
00025 #ifndef _PTHREAD_DESCR_DEFINED
00026 /* Thread descriptors.  Needed for `sem_t' definition.  */
00027 typedef struct _pthread_descr_struct *_pthread_descr;
00028 # define _PTHREAD_DESCR_DEFINED
00029 #endif
00030 
00031 /* System specific semaphore definition.  */
00032 typedef struct
00033 {
00034   struct _pthread_fastlock __sem_lock;
00035   int __sem_value;
00036   _pthread_descr __sem_waiting;
00037 } sem_t;
00038 
00039 
00040 
00041 /* Value returned if `sem_open' failed.  */
00042 #define SEM_FAILED   ((sem_t *) 0)
00043 
00044 /* Maximum value the semaphore can have.  */
00045 #define SEM_VALUE_MAX       (2147483647)
00046 
00047 
00048 __BEGIN_DECLS
00049 
00050 /* Initialize semaphore object SEM to VALUE.  If PSHARED then share it
00051    with other processes.  */
00052 extern int sem_init (sem_t *__sem, int __pshared, unsigned int __value) __THROW;
00053 
00054 /* Free resources associated with semaphore object SEM.  */
00055 extern int sem_destroy (sem_t *__sem) __THROW;
00056 
00057 /* Open a named semaphore NAME with open flaot OFLAG.  */
00058 extern sem_t *sem_open (__const char *__name, int __oflag, ...) __THROW;
00059 
00060 /* Close descriptor for named semaphore SEM.  */
00061 extern int sem_close (sem_t *__sem) __THROW;
00062 
00063 /* Remove named semaphore NAME.  */
00064 extern int sem_unlink (__const char *__name) __THROW;
00065 
00066 /* Wait for SEM being posted.  */
00067 extern int sem_wait (sem_t *__sem);
00068 
00069 #ifdef __USE_XOPEN2K
00070 /* Similar to `sem_wait' but wait only until ABSTIME.  */
00071 extern int sem_timedwait (sem_t *__restrict __sem,
00072                        __const struct timespec *__restrict __abstime);
00073 #endif
00074 
00075 /* Test whether SEM is posted.  */
00076 extern int sem_trywait (sem_t *__sem) __THROW;
00077 
00078 /* Post SEM.  */
00079 extern int sem_post (sem_t *__sem) __THROW;
00080 
00081 /* Get current value of SEM and store it in *SVAL.  */
00082 extern int sem_getvalue (sem_t *__restrict __sem, int *__restrict __sval)
00083      __THROW;
00084 
00085 __END_DECLS
00086 
00087 #endif  /* semaphore.h */