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