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