|
[ 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 ordering); 058 059 059 060 060 /* 061 /* 061 * De-initialize a kernel semaphore 062 * De-initialize a kernel semaphore 062 * 063 * 063 * @return -SOS_EBUSY when semaphore could not 064 * @return -SOS_EBUSY when semaphore could not be de-initialized 064 * because at least a thread is in the waitq. 065 * because at least a thread is in the waitq. 065 */ 066 */ 066 sos_ret_t sos_ksema_dispose(struct sos_ksema * 067 sos_ret_t sos_ksema_dispose(struct sos_ksema *sema); 067 068 068 069 069 /* 070 /* 070 * Enters the semaphore 071 * Enters the semaphore 071 * 072 * 072 * @param timeout Maximum time to wait for the 073 * @param timeout Maximum time to wait for the semaphore. Or NULL for 073 * "no limit". Updated on return to reflect th 074 * "no limit". Updated on return to reflect the time remaining (0 when 074 * timeout has been triggered) 075 * timeout has been triggered) 075 * 076 * 076 * @return -SOS_EINTR when timeout was trigger 077 * @return -SOS_EINTR when timeout was triggered or when another waitq 077 * woke us up. 078 * woke us up. 078 * 079 * 079 * @note This is a BLOCKING FUNCTION 080 * @note This is a BLOCKING FUNCTION 080 */ 081 */ 081 sos_ret_t sos_ksema_down(struct sos_ksema *sem 082 sos_ret_t sos_ksema_down(struct sos_ksema *sema, 082 struct sos_time *time 083 struct sos_time *timeout); 083 084 084 085 085 /* 086 /* 086 * Try to enter the semaphore without blocking 087 * Try to enter the semaphore without blocking. 087 * 088 * 088 * @return -SOS_EBUSY when locking the semapho 089 * @return -SOS_EBUSY when locking the semaphore would block 089 */ 090 */ 090 sos_ret_t sos_ksema_trydown(struct sos_ksema * 091 sos_ret_t sos_ksema_trydown(struct sos_ksema *sema); 091 092 092 093 093 /** 094 /** 094 * Increments the semaphore's value, eventuall 095 * Increments the semaphore's value, eventually waking up a thread 095 */ 096 */ 096 sos_ret_t sos_ksema_up(struct sos_ksema *sema) 097 sos_ret_t sos_ksema_up(struct sos_ksema *sema); 097 098 098 099 099 100 100 /* =========================================== 101 /* ==================================================================== 101 * Kernel mutex (ie binary semaphore with stro 102 * Kernel mutex (ie binary semaphore with strong ownership), 102 * NON-recursive ! 103 * NON-recursive ! 103 */ 104 */ 104 105 105 106 106 /** 107 /** 107 * The structure of a (NON-RECURSIVE) kernel M 108 * The structure of a (NON-RECURSIVE) kernel Mutex 108 */ 109 */ 109 struct sos_kmutex 110 struct sos_kmutex 110 { 111 { 111 struct sos_thread *owner; 112 struct sos_thread *owner; 112 struct sos_kwaitq kwaitq; 113 struct sos_kwaitq kwaitq; 113 }; 114 }; 114 115 115 116 116 /* 117 /* 117 * Initialize a kernel mutex structure with th 118 * Initialize a kernel mutex structure with the given name 118 * 119 * 119 * @param name Name of the mutex (for debuggin 120 * @param name Name of the mutex (for debugging purpose only; safe 120 * [deep copied]) 121 * [deep copied]) 121 * 122 * 122 * @param initial_value The value of the mutex 123 * @param initial_value The value of the mutex before any up/down 123 */ 124 */ 124 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_t ordering); 125 127 126 128 127 /* 129 /* 128 * De-initialize a kernel mutex 130 * De-initialize a kernel mutex 129 * 131 * 130 * @return -SOS_EBUSY when mutex could not be 132 * @return -SOS_EBUSY when mutex could not be de-initialized 131 * because at least a thread is in the waitq. 133 * because at least a thread is in the waitq. 132 */ 134 */ 133 sos_ret_t sos_kmutex_dispose(struct sos_kmutex 135 sos_ret_t sos_kmutex_dispose(struct sos_kmutex *mutex); 134 136 135 137 136 /* 138 /* 137 * Lock the mutex. If the same thread multiply 139 * Lock the mutex. If the same thread multiply locks the same mutex, 138 * it won't hurt (no deadlock, return value = 140 * it won't hurt (no deadlock, return value = -SOS_EBUSY). 139 * 141 * 140 * @param timeout Maximum time to wait for the 142 * @param timeout Maximum time to wait for the mutex. Or NULL for "no 141 * limit". Updated on return to reflect the ti 143 * limit". Updated on return to reflect the time remaining (0 when 142 * timeout has been triggered) 144 * timeout has been triggered) 143 * 145 * 144 * @return -SOS_EINTR when timeout was trigger 146 * @return -SOS_EINTR when timeout was triggered or when another waitq 145 * woke us up, -SOS_EBUSY when the thread alre 147 * woke us up, -SOS_EBUSY when the thread already owns the mutex. 146 * 148 * 147 * @note This is a BLOCKING FUNCTION 149 * @note This is a BLOCKING FUNCTION 148 */ 150 */ 149 sos_ret_t sos_kmutex_lock(struct sos_kmutex *m 151 sos_ret_t sos_kmutex_lock(struct sos_kmutex *mutex, 150 struct sos_time *tim 152 struct sos_time *timeout); 151 153 152 154 153 /* 155 /* 154 * Try to lock the mutex without blocking. 156 * Try to lock the mutex without blocking. 155 * 157 * 156 * @return -SOS_EBUSY when locking the mutex w 158 * @return -SOS_EBUSY when locking the mutex would block 157 */ 159 */ 158 sos_ret_t sos_kmutex_trylock(struct sos_kmutex 160 sos_ret_t sos_kmutex_trylock(struct sos_kmutex *mutex); >> 161 >> 162 >> 163 /** >> 164 * Predicate returns TRUE when the current thread owns the mutex >> 165 */ >> 166 sos_bool_t sos_kmutex_owned_by_me(struct sos_kmutex const* mutex); 159 167 160 168 161 /** 169 /** 162 * Unlock the mutex, eventually waking up a th 170 * Unlock the mutex, eventually waking up a thread 163 * 171 * 164 * @return -SOS_EPERM when the calling thread 172 * @return -SOS_EPERM when the calling thread is NOT the owner of the 165 * mutex 173 * mutex 166 */ 174 */ 167 sos_ret_t sos_kmutex_unlock(struct sos_kmutex 175 sos_ret_t sos_kmutex_unlock(struct sos_kmutex *mutex); 168 176 169 177 170 #endif /* _SOS_KSYNCH_H_ */ 178 #endif /* _SOS_KSYNCH_H_ */
[ source navigation ] | [ diff markup ] | [ identifier search ] | [ general search ] |