StRoot  1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
semLib.h
1 #ifndef _SEMLIB_H_
2 #define _SEMLIB_H_
3 
4 #include <semaphore.h>
5 #include <errno.h>
6 #include "objLib.h"
7 
8 typedef sem_t* SEM_ID;
9 
10 // deletes the ctl segment it it exists
11 void semClearCtl();
12 
13 // This returns a named process semaphore...
14 // if the semaphore already exists, its count will be reset to initialCount
15 SEM_ID semCreateNamed(int initialCount, char *name);
16 
17 // Attach to an existing semaphore by name...
18 // returns NULL if semaphore does not exist
19 SEM_ID semAttachNamed(char *name);
20 
21 // This returns a thread semaphore...or NULL if error
22 SEM_ID semCCreate(int options, int initialCount);
23 
24 // OK, or ERROR if semID invalid
25 inline STATUS semGive(SEM_ID semId)
26 {
27  return sem_post(semId);
28 }
29 
30 // NO_WAIT is properly supported.
31 // Timeouts are only supported in that if the timeout is WAIT_FOREVER
32 // the semephore is not interrupted by signals, but if it is any other
33 // value it is...
34 //
35 // OK, or ERROR
36 //
37 // errno set to
38 // EAGAIN - timeout set to NO_WAIT but would block
39 // EINVAL - bad semaphore id
40 // EINTR - interupted
41 // EDEADLK - would deadlock
42 inline STATUS semTake(SEM_ID semId, int timeout=WAIT_FOREVER)
43 {
44  if(timeout == NO_WAIT) return sem_trywait(semId);
45 
46  while(sem_wait(semId) == -1)
47  {
48  if(errno != EINTR) return -1;
49  if(timeout != WAIT_FOREVER) return -1;
50  }
51  return OK;
52 }
53 
54 // This works for both named and unnamed semaphores
55 STATUS semDelete(SEM_ID semId);
56 
57 #endif