David Hendricks | 4b832f5 | 2011-01-20 14:28:34 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2003 Sun Microsystems, Inc. |
| 3 | * Copyright 2010 Google, Inc. |
| 4 | * |
| 5 | * Redistribution and use in source and binary forms, with or without |
| 6 | * modification, are permitted provided that the following conditions are |
| 7 | * met: |
| 8 | * |
| 9 | * * Redistributions of source code must retain the above copyright |
| 10 | * notice, this list of conditions and the following disclaimer. |
| 11 | * * Redistributions in binary form must reproduce the above |
| 12 | * copyright notice, this list of conditions and the following disclaimer |
| 13 | * in the documentation and/or other materials provided with the |
| 14 | * distribution. |
| 15 | * * Neither the name of Google Inc. nor the names of its |
| 16 | * contributors may be used to endorse or promote products derived from |
| 17 | * this software without specific prior written permission. |
| 18 | * |
| 19 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 20 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 21 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 22 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 23 | * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 24 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 25 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 26 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 27 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 28 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 29 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 30 | * |
| 31 | * Developer's note: This was open sourced by Sun Microsystems, which got it |
| 32 | * via Cobalt Networks. It has been fairly extensively modified since then. |
| 33 | */ |
| 34 | |
| 35 | #ifndef CSEM_H__ |
| 36 | #define CSEM_H__ |
| 37 | |
| 38 | #include <sys/ipc.h> |
| 39 | #include <time.h> |
| 40 | |
| 41 | /* create a private key */ |
| 42 | #define CSEM_PRIVATE IPC_PRIVATE |
| 43 | |
| 44 | /* |
| 45 | * Create a new semaphore with the specified key, initialized to the |
| 46 | * specified value. If the key is CSEM_PRIVATE, a new private semaphore |
| 47 | * is allocated. |
| 48 | * |
| 49 | * Returns the sempahore ID (>= 0) on success. |
| 50 | * Returns < 0 on error, or if the key already exists. |
| 51 | */ |
| 52 | extern int csem_create(key_t key, unsigned val); |
| 53 | |
| 54 | /* |
| 55 | * Fetch an existing semaphore with the specified key. |
| 56 | * |
| 57 | * Returns the sempahore ID (>= 0) on success. |
| 58 | * Returns < 0 on error, or if the key does not exist. |
| 59 | */ |
| 60 | extern int csem_get(key_t key); |
| 61 | |
| 62 | /* |
| 63 | * Fetch or create a semaphore with the specified key. If the semaphore |
| 64 | * did not exist, it will be created with the specified value. |
| 65 | * |
| 66 | * Returns the sempahore ID (>= 0) on success. |
| 67 | * Returns < 0 on error. |
| 68 | */ |
| 69 | extern int csem_get_or_create(key_t key, unsigned val); |
| 70 | |
| 71 | /* |
| 72 | * Destroy the semaphore. |
| 73 | * |
| 74 | * Returns 0 on success. |
| 75 | * Returns < 0 on error. |
| 76 | */ |
| 77 | extern int csem_destroy(int sem_id); |
| 78 | |
| 79 | /* |
| 80 | * Get the value of the semaphore. |
| 81 | * |
| 82 | * Returns the value (>= 0) on success. |
| 83 | * Returns < 0 on error. |
| 84 | */ |
| 85 | extern int csem_getval(int sem_id); |
| 86 | |
| 87 | /* |
| 88 | * Set the value of the semaphore. |
| 89 | * |
| 90 | * Returns 0 on success. |
| 91 | * Returns < 0 on error. |
| 92 | */ |
| 93 | extern int csem_setval(int sem_id, unsigned val); |
| 94 | |
| 95 | /* |
| 96 | * Increment the semaphore. |
| 97 | * |
| 98 | * Returns 0 on success. |
| 99 | * Returns < 0 on error. |
| 100 | */ |
| 101 | extern int csem_up(int sem_id); |
| 102 | |
| 103 | /* |
| 104 | * Increment the semaphore. This operation will be undone when the |
| 105 | * process terminates. |
| 106 | * |
| 107 | * Returns 0 on success. |
| 108 | * Returns < 0 on error. |
| 109 | */ |
| 110 | extern int csem_up_undo(int sem_id); |
| 111 | |
| 112 | /* |
| 113 | * Decrement the semaphore, or block if sem == 0. |
| 114 | * |
| 115 | * Returns 0 on success. |
| 116 | * Returns < 0 on error. |
| 117 | */ |
| 118 | extern int csem_down(int sem_id); |
| 119 | |
| 120 | /* |
| 121 | * Decrement the semaphore, or block if sem == 0. This operation will be |
| 122 | * undone when the process terminates. |
| 123 | * |
| 124 | * Returns 0 on success. |
| 125 | * Returns < 0 on error. |
| 126 | */ |
| 127 | extern int csem_down_undo(int sem_id); |
| 128 | |
| 129 | /* |
| 130 | * Decrement the semaphore, or block with a timeout if sem == 0. |
| 131 | * |
| 132 | * Returns 0 on success. |
| 133 | * Returns < 0 on error. |
| 134 | */ |
| 135 | extern int csem_down_timeout(int sem_id, struct timespec *timeout); |
| 136 | |
| 137 | /* |
| 138 | * Decrement the semaphore, or block with a timeout if sem == 0. This |
| 139 | * operation will be undone when the process terminates. |
| 140 | * |
| 141 | * Returns 0 on success. |
| 142 | * Returns < 0 on error. |
| 143 | */ |
| 144 | extern int csem_down_timeout_undo(int sem_id, struct timespec *timeout); |
| 145 | |
| 146 | /* |
| 147 | * Get the timestamp of the last csem_up()/csem_down() call. |
| 148 | * |
| 149 | * Returns sem_otime on success. |
| 150 | * Returns < 0 on error |
| 151 | */ |
| 152 | extern time_t csem_get_otime(int sem_id); |
| 153 | |
| 154 | #endif /* CSEM_H__ */ |