|
[ source navigation ] [ diff markup ] [ identifier search ] [ general search ] |
|||
|
001 /* Copyright (C) 2004 David Decotigny 002 003 This program is free software; you can redistribute it and/or 004 modify it under the terms of the GNU General Public License 005 as published by the Free Software Foundation; either version 2 006 of the License, or (at your option) any later version. 007 008 This program is distributed in the hope that it will be useful, 009 but WITHOUT ANY WARRANTY; without even the implied warranty of 010 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 011 GNU General Public License for more details. 012 013 You should have received a copy of the GNU General Public License 014 along with this program; if not, write to the Free Software 015 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, 016 USA. 017 */ 018 #ifndef _SOS_KSYNCH_H_ 019 #define _SOS_KSYNCH_H_ 020 021 022 /** 023 * @file synch.h 024 * 025 * Common kernel synchronisation primitives. 026 */ 027 028 029 #include <sos/errno.h> 030 #include <sos/kwaitq.h> 031 032 033 /* ==================================================================== 034 * Kernel semaphores, NON-recursive 035 */ 036 037 038 /** 039 * The structure of a (NON-RECURSIVE) kernel Semaphore 040 */ 041 struct sos_ksema 042 { 043 int value; 044 struct sos_kwaitq kwaitq; 045 }; 046 047 048 /* 049 * Initialize a kernel semaphore structure with the given name 050 * 051 * @param name Name of the semaphore (for debugging purpose only; safe 052 * [deep copied]) 053 * 054 * @param initial_value The value of the semaphore before any up/down 055 */ 056 sos_ret_t sos_ksema_init(struct sos_ksema *sema, const char *name, 057 int initial_value); 058 059 060 /* 061 * De-initialize a kernel semaphore 062 * 063 * @return -SOS_EBUSY when semaphore could not be de-initialized 064 * because at least a thread is in the waitq. 065 */ 066 sos_ret_t sos_ksema_dispose(struct sos_ksema *sema); 067 068 069 /* 070 * Enters the semaphore 071 * 072 * @param timeout Maximum time to wait for the semaphore. Or NULL for 073 * "no limit". Updated on return to reflect the time remaining (0 when 074 * timeout has been triggered) 075 * 076 * @return -SOS_EINTR when timeout was triggered or when another waitq 077 * woke us up. 078 * 079 * @note This is a BLOCKING FUNCTION 080 */ 081 sos_ret_t sos_ksema_down(struct sos_ksema *sema, 082 struct sos_time *timeout); 083 084 085 /* 086 * Try to enter the semaphore without blocking. 087 * 088 * @return -SOS_EBUSY when locking the semaphore would block 089 */ 090 sos_ret_t sos_ksema_trydown(struct sos_ksema *sema); 091 092 093 /** 094 * Increments the semaphore's value, eventually waking up a thread 095 */ 096 sos_ret_t sos_ksema_up(struct sos_ksema *sema); 097 098 099 100 /* ==================================================================== 101 * Kernel mutex (ie binary semaphore with strong ownership), 102 * NON-recursive ! 103 */ 104 105 106 /** 107 * The structure of a (NON-RECURSIVE) kernel Mutex 108 */ 109 struct sos_kmutex 110 { 111 struct sos_thread *owner; 112 struct sos_kwaitq kwaitq; 113 }; 114 115 116 /* 117 * Initialize a kernel mutex structure with the given name 118 * 119 * @param name Name of the mutex (for debugging purpose only; safe 120 * [deep copied]) 121 * 122 * @param initial_value The value of the mutex before any up/down 123 */ 124 sos_ret_t sos_kmutex_init(struct sos_kmutex *mutex, const char *name); 125 126 127 /* 128 * De-initialize a kernel mutex 129 * 130 * @return -SOS_EBUSY when mutex could not be de-initialized 131 * because at least a thread is in the waitq. 132 */ 133 sos_ret_t sos_kmutex_dispose(struct sos_kmutex *mutex); 134 135 136 /* 137 * Lock the mutex. If the same thread multiply locks the same mutex, 138 * it won't hurt (no deadlock, return value = -SOS_EBUSY). 139 * 140 * @param timeout Maximum time to wait for the mutex. Or NULL for "no 141 * limit". Updated on return to reflect the time remaining (0 when 142 * timeout has been triggered) 143 * 144 * @return -SOS_EINTR when timeout was triggered or when another waitq 145 * woke us up, -SOS_EBUSY when the thread already owns the mutex. 146 * 147 * @note This is a BLOCKING FUNCTION 148 */ 149 sos_ret_t sos_kmutex_lock(struct sos_kmutex *mutex, 150 struct sos_time *timeout); 151 152 153 /* 154 * Try to lock the mutex without blocking. 155 * 156 * @return -SOS_EBUSY when locking the mutex would block 157 */ 158 sos_ret_t sos_kmutex_trylock(struct sos_kmutex *mutex); 159 160 161 /** 162 * Unlock the mutex, eventually waking up a thread 163 * 164 * @return -SOS_EPERM when the calling thread is NOT the owner of the 165 * mutex 166 */ 167 sos_ret_t sos_kmutex_unlock(struct sos_kmutex *mutex); 168 169 170 #endif /* _SOS_KSYNCH_H_ */
[ source navigation ] | [ diff markup ] | [ identifier search ] | [ general search ] |