1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226
|
<html>
<head>
<title>~/src/firstworks/rudiments-0.31/include/rudiments/semaphoreset.h.html</title>
<meta name="Generator" content="Vim/7.0">
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
</head>
<body bgcolor="#ffffff" text="#000000">
<pre>
<font color="#0000ff">// Copyright (c) 1999-2002 David Muse</font>
<font color="#0000ff">// See the COPYING file for more information.</font>
<font color="#a020f0">#ifndef RUDIMENTS_SEMAPHORESET_H</font>
<font color="#a020f0">#define RUDIMENTS_SEMAPHORESET_H</font>
<font color="#a020f0">#include </font><font color="#ff00ff"><rudiments/private/semaphoresetincludes.h></font>
<font color="#0000ff">// Semaphores allow processes to synchronize their activities.</font>
<font color="#0000ff">//</font>
<font color="#0000ff">// A semaphore is just a number with two primary operations that can be </font>
<font color="#0000ff">// performed on it: signal() and wait()</font>
<font color="#0000ff">// </font>
<font color="#0000ff">// The operations are analagous to:</font>
<font color="#0000ff">//</font>
<font color="#0000ff">// int semaphore;</font>
<font color="#0000ff">//</font>
<font color="#0000ff">// void signal() {</font>
<font color="#0000ff">// semaphore++; // increment the semaphore</font>
<font color="#0000ff">// }</font>
<font color="#0000ff">//</font>
<font color="#0000ff">// void wait() {</font>
<font color="#0000ff">// while (!(semaphore>0)); // wait until the semaphore>0</font>
<font color="#0000ff">// semaphore--; // decrement the semaphore</font>
<font color="#0000ff">// }</font>
<font color="#0000ff">//</font>
<font color="#0000ff">// The actual signal() and wait() operations are atomic. There is no chance </font>
<font color="#0000ff">// of another process getting context-switched in and changing the semaphore </font>
<font color="#0000ff">// value between the two lines of code in the wait() process.</font>
<font color="#0000ff">//</font>
<font color="#0000ff">// Semaphores can be initialized to any number.</font>
<font color="#0000ff">// </font>
<font color="#0000ff">// The initial value of the semaphore corresponds to the number of processes</font>
<font color="#0000ff">// that will pass directly through their wait() calls without being blocked.</font>
<font color="#0000ff">//</font>
<font color="#0000ff">// Processes that get blocked calling wait() are placed in a queue. When </font>
<font color="#0000ff">// another process calls signal(), the process at the head of the queue is </font>
<font color="#0000ff">// unblocked.</font>
<font color="#0000ff">//</font>
<font color="#0000ff">// A semaphoreset is just a collection of related semaphores.</font>
<font color="#0000ff">//</font>
<font color="#0000ff">// A semaphoreset is owned by a user and group and has access permissions</font>
<font color="#0000ff">// just like a file.</font>
<font color="#a020f0">#ifdef RUDIMENTS_NAMESPACE</font>
<font color="#2e8b57"><b>namespace</b></font> rudiments {
<font color="#a020f0">#endif</font>
<font color="#2e8b57"><b>class</b></font> semaphoresetprivate;
<font color="#2e8b57"><b>class</b></font> semaphoreset {
<font color="#a52a2a"><b>public</b></font>:
semaphoreset();
<font color="#0000ff">// Creates a semaphore set.</font>
~semaphoreset();
<font color="#0000ff">// Cleans up and removes the semaphore set</font>
<font color="#0000ff">// if it was created by create() or </font>
<font color="#0000ff">// createOrAttach() below. If the semaphore</font>
<font color="#0000ff">// was just attached to, it is not removed.</font>
<font color="#2e8b57"><b>bool</b></font> supportsTimedSemaphoreOperations();
<font color="#0000ff">// Returns true if the system supports timed</font>
<font color="#0000ff">// semaphore operations or false otherwise.</font>
<font color="#2e8b57"><b>bool</b></font> create(key_t key, mode_t permissions,
<font color="#2e8b57"><b>int</b></font> semcount, <font color="#2e8b57"><b>const</b></font> <font color="#2e8b57"><b>int</b></font> *values);
<font color="#0000ff">// Creates a semaphore set identified by "key"</font>
<font color="#0000ff">// containing "semcount" semaphores.</font>
<font color="#0000ff">// "key" should be generated using the ftok </font>
<font color="#0000ff">// function. </font>
<font color="#0000ff">// "permissions" sets the access permissions</font>
<font color="#0000ff">// for the set.</font>
<font color="#0000ff">// "values" should be an array of starting </font>
<font color="#0000ff">// values for each of the semaphores </font>
<font color="#0000ff">// in the set.</font>
<font color="#2e8b57"><b>bool</b></font> attach(key_t key, <font color="#2e8b57"><b>int</b></font> semcount);
<font color="#0000ff">// Attaches to an already existing semaphore set</font>
<font color="#0000ff">// identified by "key", containing "semcount" </font>
<font color="#0000ff">// semaphores.</font>
<font color="#2e8b57"><b>bool</b></font> createOrAttach(key_t key, mode_t permissions,
<font color="#2e8b57"><b>int</b></font> semcount, <font color="#2e8b57"><b>const</b></font> <font color="#2e8b57"><b>int</b></font> *values);
<font color="#0000ff">// Attempts to create the semaphore set </font>
<font color="#0000ff">// identified by "key". If this fails, it </font>
<font color="#0000ff">// attempts to attach to a semaphore set</font>
<font color="#0000ff">// identified by "key".</font>
<font color="#2e8b57"><b>void</b></font> dontRemove();
<font color="#0000ff">// Instructs the destructor not to remove the semaphore</font>
<font color="#0000ff">// set if it was created during a call to create() or </font>
<font color="#0000ff">// createOrAttach() above. This is useful if an </font>
<font color="#0000ff">// application creates a semaphore set then forks and </font>
<font color="#0000ff">// wants to delete the semaphore set in the forked </font>
<font color="#0000ff">// process but does not want the semaphore removed from</font>
<font color="#0000ff">// the system.</font>
<font color="#2e8b57"><b>bool</b></font> forceRemove();
<font color="#0000ff">// Removes the semaphore set, whether it</font>
<font color="#0000ff">// was created or attached to.</font>
<font color="#2e8b57"><b>int</b></font> getId() <font color="#2e8b57"><b>const</b></font>;
<font color="#0000ff">// Returns the internal id for the semaphore set.</font>
<font color="#2e8b57"><b>bool</b></font> wait(<font color="#2e8b57"><b>int</b></font> index);
<font color="#0000ff">// wait on the "index"'th semaphore in the set</font>
<font color="#2e8b57"><b>bool</b></font> wait(<font color="#2e8b57"><b>int</b></font> index, <font color="#2e8b57"><b>long</b></font> seconds, <font color="#2e8b57"><b>long</b></font> nanoseconds);
<font color="#0000ff">// Wait on the "index"'th semaphore in the set until</font>
<font color="#0000ff">// "seconds" and "nanoseconds" elapse. Returns false</font>
<font color="#0000ff">// and sets errno to EAGAIN if a timeout occurs.</font>
<font color="#0000ff">// Returns false if the system doesn't support timed</font>
<font color="#0000ff">// semaphore operations.</font>
<font color="#2e8b57"><b>bool</b></font> signal(<font color="#2e8b57"><b>int</b></font> index);
<font color="#0000ff">// signal on the "index"'th semaphore in the set</font>
<font color="#2e8b57"><b>bool</b></font> waitWithUndo(<font color="#2e8b57"><b>int</b></font> index);
<font color="#0000ff">// wait on the "index"'th semaphore in the set and</font>
<font color="#0000ff">// undo the wait when the program exits</font>
<font color="#2e8b57"><b>bool</b></font> waitWithUndo(<font color="#2e8b57"><b>int</b></font> index, <font color="#2e8b57"><b>long</b></font> seconds, <font color="#2e8b57"><b>long</b></font> nanoseconds);
<font color="#0000ff">// Wait on the "index"'th semaphore in the set until</font>
<font color="#0000ff">// "seconds" and "nanoseconds" elapse. Undo the wait</font>
<font color="#0000ff">// when the program exits. Returns false and sets</font>
<font color="#0000ff">// errno to EAGAIN if a timeout occurs.</font>
<font color="#0000ff">// Returns false if the system doesn't support timed</font>
<font color="#0000ff">// semaphore operations.</font>
<font color="#2e8b57"><b>bool</b></font> signalWithUndo(<font color="#2e8b57"><b>int</b></font> index);
<font color="#0000ff">// signal on the "index"'th semaphore in the set and</font>
<font color="#0000ff">// undo the signal when the program exits</font>
<font color="#2e8b57"><b>bool</b></font> setValue(<font color="#2e8b57"><b>int</b></font> index, <font color="#2e8b57"><b>int</b></font> value);
<font color="#0000ff">// set the "index"'th semaphore in the set to "value"</font>
<font color="#2e8b57"><b>int</b></font> getValue(<font color="#2e8b57"><b>int</b></font> index);
<font color="#0000ff">// return the value of the "index"'th </font>
<font color="#0000ff">// semaphore in the set</font>
<font color="#2e8b57"><b>bool</b></font> setUserName(<font color="#2e8b57"><b>const</b></font> <font color="#2e8b57"><b>char</b></font> *username);
<font color="#0000ff">// Makes this semaphore set owned by </font>
<font color="#0000ff">// the user "username".</font>
<font color="#0000ff">// </font>
<font color="#0000ff">// Note that setUserName() uses the passwdentry class.</font>
<font color="#0000ff">// If you are using this method in a multithreaded</font>
<font color="#0000ff">// application, you may need to supply the passwdentry</font>
<font color="#0000ff">// class a mutex. See passwdentry.h for more detail.</font>
<font color="#2e8b57"><b>bool</b></font> setGroupName(<font color="#2e8b57"><b>const</b></font> <font color="#2e8b57"><b>char</b></font> *groupname);
<font color="#0000ff">// Makes this semaphore set owned by </font>
<font color="#0000ff">// the group "groupname".</font>
<font color="#0000ff">// </font>
<font color="#0000ff">// Note that setGroupName() uses the groupentry class.</font>
<font color="#0000ff">// If you are using this method in a multithreaded</font>
<font color="#0000ff">// application, you may need to supply the groupentry</font>
<font color="#0000ff">// class a mutex. See groupentry.h for more detail.</font>
<font color="#2e8b57"><b>bool</b></font> setUserId(uid_t uid);
<font color="#0000ff">// makes this semaphore set owned by </font>
<font color="#0000ff">// the user identified by "uid"</font>
<font color="#2e8b57"><b>bool</b></font> setGroupId(gid_t gid);
<font color="#0000ff">// makes this semaphore set owned by </font>
<font color="#0000ff">// the group identified by "gid"</font>
<font color="#2e8b57"><b>bool</b></font> setPermissions(mode_t permissions);
<font color="#0000ff">// sets the access permissions for this </font>
<font color="#0000ff">// semaphore set to "permissions"</font>
<font color="#2e8b57"><b>const</b></font> <font color="#2e8b57"><b>char</b></font> *getUserName();
<font color="#0000ff">// returns the name of the user that owns this</font>
<font color="#0000ff">// semaphore set</font>
<font color="#0000ff">//</font>
<font color="#0000ff">// Note that this method allocates a buffer</font>
<font color="#0000ff">// internally and returns it. The calling program must</font>
<font color="#0000ff">// deallocate this buffer.</font>
<font color="#0000ff">// </font>
<font color="#0000ff">// Note that getUserName() uses the passwdentry class.</font>
<font color="#0000ff">// If you are using this method in a multithreaded</font>
<font color="#0000ff">// application, you may need to supply the passwdentry</font>
<font color="#0000ff">// class a mutex. See passwdentry.h for more detail.</font>
<font color="#2e8b57"><b>const</b></font> <font color="#2e8b57"><b>char</b></font> *getGroupName();
<font color="#0000ff">// returns the name of the group that owns this</font>
<font color="#0000ff">// semaphore set</font>
<font color="#0000ff">//</font>
<font color="#0000ff">// Note that this method allocates a buffer</font>
<font color="#0000ff">// internally and returns it. The calling program must</font>
<font color="#0000ff">// deallocate this buffer.</font>
<font color="#0000ff">// </font>
<font color="#0000ff">// Note that getGroupName() uses the groupentry class.</font>
<font color="#0000ff">// If you are using this method in a multithreaded</font>
<font color="#0000ff">// application, you may need to supply the groupentry</font>
<font color="#0000ff">// class a mutex. See groupentry.h for more detail.</font>
uid_t getUserId();
<font color="#0000ff">// returns the user id of the user that owns this</font>
<font color="#0000ff">// semaphore set</font>
gid_t getGroupId();
<font color="#0000ff">// returns the group id of the group that owns this</font>
<font color="#0000ff">// semaphore set</font>
mode_t getPermissions();
<font color="#0000ff">// returns the access permissions for this</font>
<font color="#0000ff">// semaphore set</font>
<font color="#2e8b57"><b>int</b></font> getWaitingForZero(<font color="#2e8b57"><b>int</b></font> index);
<font color="#0000ff">// returns the number of processes that</font>
<font color="#0000ff">// are waiting for the semaphore to become 0</font>
<font color="#2e8b57"><b>int</b></font> getWaitingForIncrement(<font color="#2e8b57"><b>int</b></font> index);
<font color="#0000ff">// returns the number of processes that</font>
<font color="#0000ff">// are waiting for the semaphore to increment</font>
<font color="#a020f0"> #include </font><font color="#ff00ff"><rudiments/private/semaphoreset.h></font>
};
<font color="#a020f0">#ifdef RUDIMENTS_NAMESPACE</font>
}
<font color="#a020f0">#endif</font>
<font color="#a020f0">#endif</font>
</pre>
</body>
</html>
|