Changeset 15d9fe6 in mainline
- Timestamp:
- 2017-10-28T11:26:43Z (7 years ago)
- Branches:
- lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
- Children:
- c1f68b0
- Parents:
- 7cf8522
- Location:
- kernel/generic
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
kernel/generic/include/synch/mutex.h
r7cf8522 r15d9fe6 43 43 typedef enum { 44 44 MUTEX_PASSIVE, 45 MUTEX_RECURSIVE, 45 46 MUTEX_ACTIVE 46 47 } mutex_type_t; 48 49 struct thread; 47 50 48 51 typedef struct { 49 52 mutex_type_t type; 50 53 semaphore_t sem; 54 struct thread *owner; 55 unsigned nesting; 51 56 } mutex_t; 52 57 -
kernel/generic/src/synch/mutex.c
r7cf8522 r15d9fe6 46 46 /** Initialize mutex. 47 47 * 48 * @param mtx Mutex.49 * @param type Type of the mutex.48 * @param mtx Mutex. 49 * @param type Type of the mutex. 50 50 */ 51 51 void mutex_initialize(mutex_t *mtx, mutex_type_t type) 52 52 { 53 53 mtx->type = type; 54 mtx->owner = NULL; 55 mtx->nesting = 0; 54 56 semaphore_initialize(&mtx->sem, 1); 55 57 } … … 57 59 /** Find out whether the mutex is currently locked. 58 60 * 59 * @param mtx Mutex. 60 * @return True if the mutex is locked, false otherwise. 61 * @param mtx Mutex. 62 * 63 * @return True if the mutex is locked, false otherwise. 61 64 */ 62 65 bool mutex_locked(mutex_t *mtx) … … 71 74 * Timeout mode and non-blocking mode can be requested. 72 75 * 73 * @param mtx Mutex.74 * @param usec Timeout in microseconds.75 * @param flags Specify mode of operation.76 * @param mtx Mutex. 77 * @param usec Timeout in microseconds. 78 * @param flags Specify mode of operation. 76 79 * 77 * For exact description of possible combinations of 78 * usec and flags, seecomment for waitq_sleep_timeout().80 * For exact description of possible combinations of usec and flags, see 81 * comment for waitq_sleep_timeout(). 79 82 * 80 83 * @return See comment for waitq_sleep_timeout(). … … 85 88 int rc; 86 89 87 if ( (mtx->type == MUTEX_PASSIVE) && (THREAD)) {90 if (mtx->type == MUTEX_PASSIVE && THREAD) { 88 91 rc = _semaphore_down_timeout(&mtx->sem, usec, flags); 92 } else if (mtx->type == MUTEX_RECURSIVE) { 93 assert(THREAD); 94 95 if (mtx->owner == THREAD) { 96 mtx->nesting++; 97 return ESYNCH_OK_ATOMIC; 98 } else { 99 rc = _semaphore_down_timeout(&mtx->sem, usec, flags); 100 if (SYNCH_OK(rc)) { 101 mtx->owner = THREAD; 102 mtx->nesting = 1; 103 } 104 } 89 105 } else { 90 assert((mtx->type == MUTEX_ACTIVE) || (!THREAD));106 assert((mtx->type == MUTEX_ACTIVE) || !THREAD); 91 107 assert(usec == SYNCH_NO_TIMEOUT); 92 108 assert(!(flags & SYNCH_FLAGS_INTERRUPTIBLE)); … … 114 130 /** Release mutex. 115 131 * 116 * @param mtx Mutex.132 * @param mtx Mutex. 117 133 */ 118 134 void mutex_unlock(mutex_t *mtx) 119 135 { 136 if (mtx->type == MUTEX_RECURSIVE) { 137 assert(mtx->owner == THREAD); 138 if (--mtx->nesting > 0) 139 return; 140 mtx->owner = NULL; 141 } 120 142 semaphore_up(&mtx->sem); 121 143 }
Note:
See TracChangeset
for help on using the changeset viewer.