thread_semaphore.h
上传用户:shtangtang
上传日期:2007-01-04
资源大小:167k
文件大小:2k
源码类别:
Linux/Unix编程
开发平台:
Unix_Linux
- #ifndef __THREADS_SEMAPHORE_H
- #define __THREADS_SEMAPHORE_H
- #include <thread_attributes.h>
- #include <thread_spinlock.h>
- class wait_queue;
- /**
- * A semaphore, it functions in much a similar way as a mutex
- * does, but with some exceptions.
- *
- * @author Orn E. Hansen <oe.hansen@gamma.telenordia.se>
- * @short Implements a semaphore, for thread synchronisation.
- */
- class semaphore {
- protected:
- static char *s_project;
- int s_id;
- wait_queue *s_waiting;
- attributes::scope s_scope;
- struct storage {
- int s_magic;
- spinlock s_sync;
- int s_count;
- } *_s;
- public:
- semaphore(attributes::scope);
- semaphore(attributes::scope, int);
- semaphore();
- semaphore(int);
- ~semaphore();
- /**
- * Increment the semaphore count, the top most blocking thread on
- * the waiting list is restarted.
- *
- * @return The updated value, of the semaphore count.
- */
- int post();
- /**
- * Block the calling process, until the semaphore count becomes
- * greater than 1, then atomically decrement it.
- *
- * @return The current count of the semaphore.
- */
- int wait();
- /**
- * Decrement the count, if it is greater than zero. This is a
- * non-blocking call.
- *
- * @return The count of the semaphore after decrement, or -1 not.
- */
- int trywait();
- /**
- * The shared memory scheme, wants a single name to identify the
- * overall program. It is possible, and perhaps desired that
- * part cond/mutex/semaphore have their own name identification
- * tree. This is possible, by stating that it should be branch
- * of the main file name, with a give extension.
- *
- * <pre>
- * main()
- * {
- * semaphore *sv;
- *
- * pthread::set_project( "my_project" );
- * semaphore::project_par( "sem" );
- * sv = new semaphore(attributes::process_shared);
- * ...
- * }
- * </pre>
- *
- * This will set the project to my_project, and all semaphores
- * will be derived from my_project_sem.
- *
- * @param part A C string containing the semaphore part name.
- */
- static void project_part(const char *);
- };
- #endif /* __THREADS_SEMAPHORE_H */