David Hendricks | 4b832f5 | 2011-01-20 14:28:34 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2010 Google Inc. |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or |
| 5 | * modify it under the terms of the GNU General Public License |
| 6 | * as published by the Free Software Foundation; either version 2 |
| 7 | * of the License, or (at your option) any later version. |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU General Public License |
| 15 | * along with this program; if not, write to the Free Software |
| 16 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA |
| 17 | * |
| 18 | */ |
| 19 | |
| 20 | #ifndef IPC_LOCK_H__ |
| 21 | #define IPC_LOCK_H__ |
| 22 | |
| 23 | #include <sys/ipc.h> |
| 24 | |
| 25 | struct ipc_lock { |
| 26 | key_t key; /* provided by the developer */ |
| 27 | int sem; /* internal */ |
| 28 | int is_held; /* internal */ |
| 29 | }; |
| 30 | |
| 31 | /* don't use C99 initializers here, so this can be used in C++ code */ |
| 32 | #define IPC_LOCK_INIT(key) \ |
| 33 | { \ |
| 34 | key, /* name */ \ |
| 35 | -1, /* sem */ \ |
| 36 | 0, /* is_held */ \ |
| 37 | } |
| 38 | |
| 39 | /* |
| 40 | * acquire_lock: acquire a lock |
| 41 | * |
| 42 | * timeout <0 = no timeout (try forever) |
| 43 | * timeout 0 = do not wait (return immediately) |
| 44 | * timeout >0 = wait up to $timeout milliseconds (subject to kernel scheduling) |
| 45 | * |
| 46 | * return 0 = lock acquired |
| 47 | * return >0 = lock was already held |
| 48 | * return <0 = failed to acquire lock |
| 49 | */ |
| 50 | extern int acquire_lock(struct ipc_lock *lock, int timeout_msecs); |
| 51 | |
| 52 | /* |
| 53 | * release_lock: release a lock |
| 54 | * |
| 55 | * returns 0 if lock was released successfully |
| 56 | * returns -1 if lock had not been held before the call |
| 57 | */ |
| 58 | extern int release_lock(struct ipc_lock *lock); |
| 59 | |
| 60 | #endif /* IPC_LOCK_H__ */ |