| 1 | /******************************************************************* |
|---|
| 2 | * DMW_Mutex.c |
|---|
| 3 | * |
|---|
| 4 | * General Purpose Mutex |
|---|
| 5 | * |
|---|
| 6 | * Copyright 2003 Digital STREAM Technology, Inc. |
|---|
| 7 | * All Rights Reserved |
|---|
| 8 | * |
|---|
| 9 | * $Id: DMW_Mutex.c,v 0.12 2004 cafrii Exp $ |
|---|
| 10 | * |
|---|
| 11 | ********************************************************************/ |
|---|
| 12 | |
|---|
| 13 | |
|---|
| 14 | |
|---|
| 15 | #include "DMW_Platform.h" |
|---|
| 16 | |
|---|
| 17 | #if 1 |
|---|
| 18 | //#include <string.h> |
|---|
| 19 | #endif |
|---|
| 20 | |
|---|
| 21 | #include "DMW_Mutex.h" |
|---|
| 22 | |
|---|
| 23 | |
|---|
| 24 | DHL_MODULE("$mtx", 0); |
|---|
| 25 | |
|---|
| 26 | |
|---|
| 27 | #define DMW_MUTEX_TEST 1 |
|---|
| 28 | |
|---|
| 29 | |
|---|
| 30 | |
|---|
| 31 | |
|---|
| 32 | #if COMMENT |
|---|
| 33 | |
|---|
| 34 | ____Example_Code____(){} |
|---|
| 35 | |
|---|
| 36 | /* Mutex = { SemID, Owner, lockCnt, FailCnt, UseOsMutex, Name, flag, traceLvl } |
|---|
| 37 | */ |
|---|
| 38 | DMW_MUTEX |
|---|
| 39 | mcmMutex = { 0, 0, 0, 0, FALSE, "McmMtx", OS_SEM_PRIO, 1, }; |
|---|
| 40 | |
|---|
| 41 | STATUS lock_mcm(int timeout) |
|---|
| 42 | { |
|---|
| 43 | return DMW_LockMutexWait(&mcmMutex, timeout); |
|---|
| 44 | } |
|---|
| 45 | |
|---|
| 46 | void unlock_mcm() |
|---|
| 47 | { |
|---|
| 48 | DMW_UnlockMutex(&mcmMutex); |
|---|
| 49 | } |
|---|
| 50 | |
|---|
| 51 | #endif /*.......................... */ |
|---|
| 52 | |
|---|
| 53 | |
|---|
| 54 | void DMW_PrintMutex(DMW_MUTEX *pMutex) |
|---|
| 55 | { |
|---|
| 56 | DHL_OS_TASK_INFO tInfo; |
|---|
| 57 | |
|---|
| 58 | DHL_OS_Printf("\t pMutex : 0x%x\n", pMutex); |
|---|
| 59 | |
|---|
| 60 | if (pMutex) { |
|---|
| 61 | DHL_OS_Printf("\t semId: 0x%x\n", pMutex->semId); |
|---|
| 62 | DHL_OS_Printf("\t lockCount: %d\n", pMutex->lockCount); |
|---|
| 63 | DHL_ASSERT(DHL_OS_GetTaskInfo(DHL_OS_GetTaskID(), &tInfo)==DHL_OK |
|---|
| 64 | , "!!DHL_OS_GetTaskInfo Error"); |
|---|
| 65 | DHL_OS_Printf("\t ownerTask: 0x%x, %s\n", pMutex->ownerTaskId, tInfo.name); |
|---|
| 66 | DHL_OS_Printf("\t FailCount: %d\n", pMutex->initFailCount); |
|---|
| 67 | DHL_OS_Printf("\t name: %s\n", pMutex->name); |
|---|
| 68 | DHL_OS_Printf("\t %sMutex, traceLvl %d, %s\n", pMutex->useOsMutex ? "Os" : "Mw", |
|---|
| 69 | pMutex->traceLevel, pMutex->flag & OS_SEM_PRIO ? "Priority" : "FIFO"); |
|---|
| 70 | } |
|---|
| 71 | DHL_OS_Printf("\n"); |
|---|
| 72 | } |
|---|
| 73 | |
|---|
| 74 | |
|---|
| 75 | /* debug util */ |
|---|
| 76 | void DMW_SetMutexTraceLevel(DMW_MUTEX *pMutex, int traceLevel) |
|---|
| 77 | { |
|---|
| 78 | if (pMutex && pMutex->semId) |
|---|
| 79 | pMutex->traceLevel = traceLevel; |
|---|
| 80 | |
|---|
| 81 | DMW_PrintMutex(pMutex); |
|---|
| 82 | } |
|---|
| 83 | |
|---|
| 84 | /* debug util */ |
|---|
| 85 | DHL_OS_TASK_ID DMW_GetOwnerTaskId(DMW_MUTEX *pMutex) |
|---|
| 86 | { |
|---|
| 87 | if (pMutex && pMutex->semId && pMutex->lockCount > 0) |
|---|
| 88 | return pMutex->ownerTaskId; |
|---|
| 89 | |
|---|
| 90 | return 0; |
|---|
| 91 | } |
|---|
| 92 | |
|---|
| 93 | STATUS DMW_InitMutex(DMW_MUTEX *pMutex, char *name) |
|---|
| 94 | { |
|---|
| 95 | DHL_OS_SEMA_ID mysema4; |
|---|
| 96 | |
|---|
| 97 | if (pMutex == NULL) { |
|---|
| 98 | dprint(0, "!! Null argument in DMW_InitMutex\n"); |
|---|
| 99 | return statusInvalidArgument; |
|---|
| 100 | } |
|---|
| 101 | |
|---|
| 102 | /* pSem¿¡ ÀÖ´Â sema4°¡ Á¦´ë·Î µÈ °ÍÀÎÁö È®ÀÎÇÑ´Ù. */ |
|---|
| 103 | |
|---|
| 104 | if (pMutex->semId) { |
|---|
| 105 | dprint(2, "Sema4 %x, %s Already inited.\n", pMutex->semId, pMutex->name); |
|---|
| 106 | return statusOK; /* already initialized. do nothing. */ |
|---|
| 107 | } |
|---|
| 108 | |
|---|
| 109 | if (pMutex->initFailCount < 5) |
|---|
| 110 | { |
|---|
| 111 | if (name == NULL || name[0] == 0) { |
|---|
| 112 | /* À̸§À» ÁöÁ¤ÇÏÁö ¾Ê¾ÒÀ» °æ¿ì ÀÌ ÇÔ¼ö¿¡¼ ÀÓÀÇ À̸§À» Áִ°͵µ ±¦Âú´Ù. */ |
|---|
| 113 | /* sprintf(pMutex->name, "%s%d", OS_TaskName(0), DHL_OS_GetMsCount()%10); */ |
|---|
| 114 | } |
|---|
| 115 | else { |
|---|
| 116 | strncpy(pMutex->name, name, MAX_MUTEX_NAME-1); |
|---|
| 117 | pMutex->name[MAX_MUTEX_NAME-1] = 0; |
|---|
| 118 | } |
|---|
| 119 | |
|---|
| 120 | if (pMutex->useOsMutex) { |
|---|
| 121 | mysema4 = DHL_OS_CreateMutexSemaphore(pMutex->name); |
|---|
| 122 | } |
|---|
| 123 | else { |
|---|
| 124 | mysema4 = DHL_OS_CreateBinarySemaphore(pMutex->name, OS_SEM_PRIO, TRUE); |
|---|
| 125 | /* OS_SEM_FIFO --> OS_SEM_PRIO */ |
|---|
| 126 | /* ¾Æ¹«³ª ¸ÕÀú ±ÞÇÑ task°¡ print¸¦ ÇÒ ¼ö ÀÖµµ·Ï. */ |
|---|
| 127 | } |
|---|
| 128 | |
|---|
| 129 | if (mysema4 == (DHL_OS_SEMA_ID)NULL) { |
|---|
| 130 | dprint(0, "!! Sema4 '%s' creation failed.\n", pMutex->name); |
|---|
| 131 | pMutex->initFailCount++; |
|---|
| 132 | /* semaphore°¡ ¾ø¾îµµ ÀÏ´Ü ÁøÇàÀº ½Ã۵µ·Ï ÇÑ´Ù. */ |
|---|
| 133 | |
|---|
| 134 | return statusOutOfResource; |
|---|
| 135 | } |
|---|
| 136 | |
|---|
| 137 | /* Semaphore »ý¼º ¼º°ø. ÀÌÁ¦ »ý¼ºµÈ sema4¸¦ pSem¿¡ ¾ÈÀüÇÏ°Ô assignÇÑ´Ù. |
|---|
| 138 | |
|---|
| 139 | // ThreadX¿¡¼´Â OS_LockTask ÀÚü°¡ Mutex¸¦ ÀÌ¿ëÇÏ´Â ¹æ½ÄÀ¸·Î ±¸ÇöµÇ¾ú±â ¶§¹®¿¡ |
|---|
| 140 | // InitMutex¿¡¼ ´Ù½Ã OS_LockTask¸¦ È£ÃâÇÒ ¼ö ¾ø´Ù.. |
|---|
| 141 | */ |
|---|
| 142 | |
|---|
| 143 | if (1) { |
|---|
| 144 | UINT32 mask; |
|---|
| 145 | mask = DHL_OS_DisableInterrupts(); |
|---|
| 146 | if (pMutex->semId == 0) { |
|---|
| 147 | /* OS_LockTask µÈ »óÅ¿¡¼ ÃʱâÈ ÇÑ´Ù. */ |
|---|
| 148 | pMutex->semId = mysema4; |
|---|
| 149 | mysema4 = (DHL_OS_SEMA_ID)NULL; |
|---|
| 150 | } |
|---|
| 151 | DHL_OS_RestoreInterrupts(mask); |
|---|
| 152 | } |
|---|
| 153 | |
|---|
| 154 | /* OS_LockTask/OS_UnlockTask »çÀÌ¿¡¼ swapÀ» Çߴµ¥µµ ¾ÆÁ÷ ±×´ë·ÎÀ̸é |
|---|
| 155 | // ÀÌ¹Ì pSemÀÌ ´Ù¸¥ ŽºÅ©¿¡ ÀÇÇØ »ý¼ºÀÌ µÈ °æ¿ìÀÌ´Ù. Áߺ¹ »ý¼ºµÇ¾úÀ¸¹Ç·Î Çϳª¸¦ »èÁ¦ÇØ¾ß ÇÑ´Ù. |
|---|
| 156 | */ |
|---|
| 157 | if (mysema4) { |
|---|
| 158 | dprint(0, "!! Simultaneous '%s' Sema4 Creation detected %x. Safe release ours %x\n", |
|---|
| 159 | pMutex->name, |
|---|
| 160 | pMutex->semId, mysema4); |
|---|
| 161 | DHL_OS_DeleteSemaphore(mysema4); |
|---|
| 162 | mysema4 = (DHL_OS_SEMA_ID)NULL; |
|---|
| 163 | } |
|---|
| 164 | else { |
|---|
| 165 | dprint(2, "Sema4 %x, '%s' created. DMW_Mutex 0x%x\n", |
|---|
| 166 | pMutex->semId, pMutex->name, (int)pMutex); |
|---|
| 167 | } |
|---|
| 168 | |
|---|
| 169 | return statusOK; |
|---|
| 170 | } |
|---|
| 171 | else { |
|---|
| 172 | |
|---|
| 173 | return statusOutOfResource; |
|---|
| 174 | } |
|---|
| 175 | } |
|---|
| 176 | |
|---|
| 177 | |
|---|
| 178 | #if 0 /* for reference */ |
|---|
| 179 | |
|---|
| 180 | Table 2-1: Task State Transitions |
|---|
| 181 | |
|---|
| 182 | -------------------------------------------------------------------------------- |
|---|
| 183 | State Symbol Description |
|---|
| 184 | -------------------------------------------------------------------------------- |
|---|
| 185 | |
|---|
| 186 | READY The state of a task that is not waiting for any resource other than the CPU. |
|---|
| 187 | PEND The state of a task that is blocked due to the unavailability of some resource. |
|---|
| 188 | DELAY The state of a task that is asleep for some duration. |
|---|
| 189 | SUSPEND The state of a task that is unavailable for execution. This state is used primarily for debugging. |
|---|
| 190 | Suspension does not inhibit state transition, only task execution. Thus pended-suspended tasks can still |
|---|
| 191 | unblock and delayed-suspended tasks can still awaken. |
|---|
| 192 | DELAY + S The state of a task that is both delayed and suspended. |
|---|
| 193 | PEND + S The state of a task that is both pended and suspended. |
|---|
| 194 | PEND + T The state of a task that is pended with a timeout value. |
|---|
| 195 | PEND + S + T The state of a task that is both pended with a timeout value and suspended. |
|---|
| 196 | state + I The state of task specified by state, plus an inherited priority. |
|---|
| 197 | |
|---|
| 198 | #endif |
|---|
| 199 | |
|---|
| 200 | #ifndef WIND_READY |
|---|
| 201 | #define WIND_READY 0x00 /* ready to run */ |
|---|
| 202 | #define WIND_SUSPEND 0x01 /* explicitly suspended */ |
|---|
| 203 | #define WIND_PEND 0x02 /* pending on semaphore */ |
|---|
| 204 | #define WIND_DELAY 0x04 /* task delay (or timeout) */ |
|---|
| 205 | #define WIND_DEAD 0x08 /* dead task */ |
|---|
| 206 | #endif |
|---|
| 207 | |
|---|
| 208 | |
|---|
| 209 | STATUS DMW_LockMutex(DMW_MUTEX *pMutex) |
|---|
| 210 | { |
|---|
| 211 | return DMW_LockMutexWait(pMutex, 0x7fffffff); /* wait forever */ |
|---|
| 212 | } |
|---|
| 213 | |
|---|
| 214 | STATUS DMW_LockMutexWait(DMW_MUTEX *pMutex, int timeout) |
|---|
| 215 | { |
|---|
| 216 | /* timeout 0À» ÁöÁ¤Çϸé WAIT_FOREVER°¡ ¾Æ´Ï¶ó NoWait°¡ µÇ¹ö¸²¿¡ À¯ÀÇ. |
|---|
| 217 | */ |
|---|
| 218 | int nWaitCount = 0; |
|---|
| 219 | int err; |
|---|
| 220 | DHL_OS_TASK_ID mytid; |
|---|
| 221 | // DHL_OS_TASK_INFO tInfo; |
|---|
| 222 | |
|---|
| 223 | int ticksToWait, ticksLeft, tickMonitorPeriod; |
|---|
| 224 | |
|---|
| 225 | mytid = DHL_OS_GetTaskID(); /* ÇöÀç task ID */ |
|---|
| 226 | |
|---|
| 227 | /* mutex¸¦ »ý¼ºÇÏ´Â °úÁ¤Àº ¾à°£Àº 'ºÎ´ã'µÇ´Â ÀÏÀÌ´Ù. (OS_LockTask/OS_UnlockTask, sema4 ¸®½ºÆ® °ü¸® µî..) |
|---|
| 228 | // ¸¸¾à ¸®¼Ò½º ºÎÁ· µîÀÇ ÀÌÀ¯·Î sema4»ý¼ºÀÌ ½ÇÆÐÇß¾ú´Ù¸é, ±× ÀÌÈÄ °è¼Ó »ý¼ºÀ» ½Ãµµ ÇÏ´Â °Í ¶ÇÇÑ |
|---|
| 229 | // ºñ½ÁÇÑ ÀÌÀ¯·Î ½ÇÆÐÇÒ °¡´É¼ºÀÌ Å©´Ù. |
|---|
| 230 | // ÀÏÁ¤ Ƚ¼ö ÀÌ»ó sema4 »ý¼º¿¡ ½ÇÆÐÇÑ °æ¿ì¶ó¸é ÀÌÈÄ¿¡´Â ´Ù½Ã »ý¼º ½Ãµµ¸¦ ÇÏÁö ¸»µµ·Ï ÇÑ´Ù. |
|---|
| 231 | */ |
|---|
| 232 | |
|---|
| 233 | if (pMutex == NULL) { |
|---|
| 234 | dprint(0, "!! NULL sema4 in DMW_LockMutex\n"); |
|---|
| 235 | return statusInvalidArgument; |
|---|
| 236 | } |
|---|
| 237 | |
|---|
| 238 | if (pMutex->semId == 0) { |
|---|
| 239 | STATUS r = DMW_InitMutex(pMutex, 0); |
|---|
| 240 | dprint(2, "Sema4 '%s' created, result %d\n", pMutex->name, r); |
|---|
| 241 | } |
|---|
| 242 | |
|---|
| 243 | /* Semaphore wait¿¡¼ OS_TakeSemaphore() ÇÔ¼ö¸¦ »ç¿ëÇÏÁö ¾Ê°í |
|---|
| 244 | // ÁÖ±âÀûÀ¸·Î wakeup ÇÏ¿© Semaphore Take ÀÛ¾÷ »óȲÀÌ ¾î¶²Áö º¸°íÇϵµ·Ï ÇÑ´Ù. |
|---|
| 245 | // µð¹ö±ë ¿ëµµÀ̹ǷΠcheck ÁÖ±â´Â ÃæºÐÈ÷ Ä¿µµ »ó°ü ¾ø´Ù. 1ÃÊ ¹Ì¸¸ÀÇ °ªÀº »ç¿ëÇÏÁö ¸»µµ·Ï ÇÏÀÚ. |
|---|
| 246 | */ |
|---|
| 247 | ticksLeft = timeout; |
|---|
| 248 | tickMonitorPeriod = 1000; |
|---|
| 249 | |
|---|
| 250 | while (1) |
|---|
| 251 | { |
|---|
| 252 | /* mutex semaphoreÀÇ multiple-take¸¦ Èä³»³»±â À§Çؼ wait Çϱâ Àü¿¡ ÀÌ¹Ì ÀÚ½ÅÀÇ task¿¡¼ |
|---|
| 253 | // semaphore°¡ takeµÇ¾ú´ÂÁö üũÇÑ´Ù. |
|---|
| 254 | */ |
|---|
| 255 | if (pMutex->useOsMutex == 0 && pMutex->ownerTaskId == mytid && pMutex->lockCount > 0) |
|---|
| 256 | { |
|---|
| 257 | /* ÀÌ¹Ì ÇöÀç task¿¡¼ ÀÌ Sema4¸¦ takeÇϰí ÀÖ´Ù°í ÆÇ´Ü.. */ |
|---|
| 258 | pMutex->lockCount++; |
|---|
| 259 | |
|---|
| 260 | if (pMutex->traceLevel) |
|---|
| 261 | dprint(2, "+ FakeLock '%s' B-sema4 in task %08x, lock count %d\n", pMutex->name, mytid, pMutex->lockCount); |
|---|
| 262 | |
|---|
| 263 | return statusOK; |
|---|
| 264 | } |
|---|
| 265 | |
|---|
| 266 | ticksToWait = ticksLeft > tickMonitorPeriod ? tickMonitorPeriod : ticksLeft; |
|---|
| 267 | |
|---|
| 268 | if (ticksToWait <= 0) |
|---|
| 269 | err = DHL_OS_TakeSemaphore(pMutex->semId, 0); |
|---|
| 270 | else |
|---|
| 271 | err = DHL_OS_TakeSemaphore(pMutex->semId, ticksToWait); |
|---|
| 272 | |
|---|
| 273 | if (err == 0) { |
|---|
| 274 | /* OK! semaphore is available and we take it! */ |
|---|
| 275 | pMutex->ownerTaskId = mytid; |
|---|
| 276 | pMutex->lockCount++; |
|---|
| 277 | if (pMutex->traceLevel) |
|---|
| 278 | dprint(2, "+ Lock '%s' %c-sema4 in task %08x, lock count %d\n", pMutex->name, |
|---|
| 279 | pMutex->useOsMutex ? 'M' : 'B', mytid, pMutex->lockCount); |
|---|
| 280 | |
|---|
| 281 | /* ¿øÇÏ´Â ¸ñÀûÀ» ´Þ¼ºÇßÀ¸¹Ç·Î ·çÇÁ Á¾·á. */ |
|---|
| 282 | return statusOK; |
|---|
| 283 | } |
|---|
| 284 | |
|---|
| 285 | ticksLeft -= ticksToWait; |
|---|
| 286 | |
|---|
| 287 | if (timeout > 0 && ticksLeft <= 0) { |
|---|
| 288 | /* timeout ¹ß»ý.. |
|---|
| 289 | */ |
|---|
| 290 | if (pMutex->traceLevel) |
|---|
| 291 | dprint(2, "'%s' %c-sema4 wait timeout!\n", pMutex->name, pMutex->useOsMutex ? 'M' : 'B'); |
|---|
| 292 | return statusTimeout; |
|---|
| 293 | } |
|---|
| 294 | |
|---|
| 295 | /* Semaphore ¸ð´ÏÅ͸µ ÄÚµå.. |
|---|
| 296 | // |
|---|
| 297 | // sema4¸¦ ±â´Ù¸®´Â µ¿¾È ¾î´À ŽºÅ©°¡ ÇöÀç sema4¸¦ Á¡À¯Çϰí ÀÖ´ÂÁö Ç¥½Ã. |
|---|
| 298 | // Ȥ½Ã sema4 °ü¸®°¡ ²¿¿©¼ hangµÉ °æ¿ì À¯¿ëÇÏ°Ô »ç¿ëµÉ ¼ö ÀÖ´Ù. |
|---|
| 299 | // ¸Þ¼¼Áö¸¦ Ç¥½ÃÇØÁÖ´Â ÁÖ±â´Â ´õ ±æ°Ô Àâ¾Æµµ »ó°ü¾øÀ½. |
|---|
| 300 | */ |
|---|
| 301 | if (pMutex->traceLevel) { |
|---|
| 302 | //DHL_ASSERT(DHL_OS_GetTaskInfo(pMutex->ownerTaskId, &tInfo)==DHL_OK |
|---|
| 303 | // , "!!DHL_OS_GetTaskInfo Error"); |
|---|
| 304 | // int i=3; |
|---|
| 305 | //dprint(2, "[%x] '%s' Sema4 owner: %s, %x, wait %d\n", |
|---|
| 306 | // mytid, pMutex->name, |
|---|
| 307 | // pMutex->ownerTaskId ? tInfo.name: "None", |
|---|
| 308 | // pMutex->ownerTaskId, |
|---|
| 309 | // nWaitCount); |
|---|
| 310 | } |
|---|
| 311 | ++nWaitCount; /* cafrii 060919 fix.. */ |
|---|
| 312 | /* |
|---|
| 313 | // Error Detection code |
|---|
| 314 | // |
|---|
| 315 | // »ó½ÄÀûÀÎ ½Ã°£ ÀÌ»óÀ¸·Î Semaphore°¡ °è¼Ó ´Ù¸¥ task¿¡ ÀÇÇØ Á¡À¯µÇ°í ÀÖ´Ù¸é À̰ÍÀº ¹®Á¦´Ù. |
|---|
| 316 | // Sema4 Á¡À¯Çϰí Àִ ŽºÅ© Á¤º¸¸¦ ÁÖ±âÀûÀ¸·Î Ãâ·ÂÇϵµ·Ï ÇÏ¿© µð¹ö±ë¿¡ µµ¿òÀ» ÁÙ¼ö ÀÖµµ·Ï ÇÑ´Ù. |
|---|
| 317 | */ |
|---|
| 318 | if (nWaitCount > 10 && pMutex->ownerTaskId) |
|---|
| 319 | { |
|---|
| 320 | // cafrii 090717 |
|---|
| 321 | // ±×³É ´Ü¼øÇÏ°Ô Æ¯Á¤ mutex lock ÀÌ ºñÁ¤»óÀûÀ¸·Î ±æ¾îÁö°í ÀÖ´Ù´Â »ç½Ç¸¸ ¾Ë·ÁÁÖ°í, |
|---|
| 322 | // °³¹ßÀÚ´Â shell ¿¡¼ º°µµÀÇ µð¹ö±ë ·çƾÀ» ÀÌ¿ëÇÏ¿© Á¤º¸¸¦ ȹµæÇϵµ·Ï ÇÏÀÚ. |
|---|
| 323 | |
|---|
| 324 | |
|---|
| 325 | /* ±× ¹Û¿¡ ´õ Ãß°¡ Á¤º¸¸¦ ¾òÀ» ¼ö ÀÖ´Ù¸é À̰÷¿¡ Ãß°¡. */ |
|---|
| 326 | |
|---|
| 327 | /* ¶Ç´Â ÀÚµ¿À¸·Î ¹®Á¦¸¦ RecoverÇÒ ¼ö ÀÖ´Â ¹æ¹ýÀÌ ÀÖ´Ù¸é À̰÷¿¡ Ãß°¡. */ |
|---|
| 328 | } |
|---|
| 329 | |
|---|
| 330 | |
|---|
| 331 | } /* end of Same4 Wait Loop */ |
|---|
| 332 | } |
|---|
| 333 | |
|---|
| 334 | |
|---|
| 335 | |
|---|
| 336 | void DMW_UnlockMutex(DMW_MUTEX *pMutex) |
|---|
| 337 | { |
|---|
| 338 | DHL_OS_TASK_ID mytid; |
|---|
| 339 | // DHL_OS_TASK_INFO tInfo; |
|---|
| 340 | |
|---|
| 341 | mytid = DHL_OS_GetTaskID(); |
|---|
| 342 | |
|---|
| 343 | if (pMutex == NULL) { |
|---|
| 344 | dprint(0, "!! NULL Sema4 in DMW_UnlockMutex\n"); |
|---|
| 345 | return; |
|---|
| 346 | } |
|---|
| 347 | |
|---|
| 348 | if (pMutex->semId == 0) { |
|---|
| 349 | dprint(0, "!! Uninitialized Sema4 in DMW_UnlockMutex\n"); |
|---|
| 350 | return; |
|---|
| 351 | } |
|---|
| 352 | |
|---|
| 353 | pMutex->lockCount--; |
|---|
| 354 | |
|---|
| 355 | if (pMutex->traceLevel) { |
|---|
| 356 | if (pMutex->useOsMutex == 0 && pMutex->lockCount > 0) |
|---|
| 357 | dprint(2, "- FakeUnlock '%s' B-sema4 in task %08x, lock count %d\n", |
|---|
| 358 | pMutex->name, mytid, pMutex->lockCount); |
|---|
| 359 | else |
|---|
| 360 | dprint(2, "- Unlock '%s' %c-sema4 in task %08x, lock count %d\n", |
|---|
| 361 | pMutex->name, pMutex->useOsMutex ? 'M' : 'B', mytid, pMutex->lockCount); |
|---|
| 362 | } |
|---|
| 363 | |
|---|
| 364 | if (pMutex->lockCount < 0) { |
|---|
| 365 | dprint(0, "!! Warning!! Negative lock count %d !, name=%s\n", pMutex->lockCount, pMutex->name); |
|---|
| 366 | } |
|---|
| 367 | |
|---|
| 368 | if (mytid != pMutex->ownerTaskId) { |
|---|
| 369 | //DHL_ASSERT(DHL_OS_GetTaskInfo(pMutex->ownerTaskId, &tInfo)==DHL_OK |
|---|
| 370 | // , "!!DHL_OS_GetTaskInfo Error"); |
|---|
| 371 | //dprint(0, "!! Warning!! try unlock from not owner! current owner %s, %08x\n", |
|---|
| 372 | // tInfo.name, pMutex->ownerTaskId); |
|---|
| 373 | } |
|---|
| 374 | |
|---|
| 375 | if (pMutex->lockCount <= 0) { |
|---|
| 376 | pMutex->ownerTaskId = 0; |
|---|
| 377 | } |
|---|
| 378 | |
|---|
| 379 | /* Mutex semaphoreÀÇ °æ¿ì¶ó¸é unlock ÇÒ¶§¸¶´Ù give¸¦ Çϰí, |
|---|
| 380 | // Binary sema4¶ó¸é ¸ðµç lock count°¡ Ç®¸± °æ¿ì¿¡¸¸ give¸¦ ÇÑ´Ù. |
|---|
| 381 | */ |
|---|
| 382 | if (pMutex->useOsMutex || pMutex->lockCount <= 0) |
|---|
| 383 | DHL_OS_GiveSemaphore(pMutex->semId); |
|---|
| 384 | } |
|---|
| 385 | |
|---|
| 386 | |
|---|
| 387 | void DMW_DeleteMutex(DMW_MUTEX *pMutex) |
|---|
| 388 | { |
|---|
| 389 | if (pMutex == NULL) { |
|---|
| 390 | dprint(0, "!! NULL Mutex in DMW_UnlockMutex\n"); |
|---|
| 391 | return; |
|---|
| 392 | } |
|---|
| 393 | |
|---|
| 394 | if (pMutex->semId == 0) { |
|---|
| 395 | dprint(0, "!! NULL semId in DMW_UnlockMutex\n"); |
|---|
| 396 | return; |
|---|
| 397 | } |
|---|
| 398 | |
|---|
| 399 | dprint(2, "Sema4 '%s' deleted..\n", pMutex->name); |
|---|
| 400 | |
|---|
| 401 | if (1) |
|---|
| 402 | { |
|---|
| 403 | // cafrii 091221 fix |
|---|
| 404 | // isr lock (disabled) »óÅ¿¡¼ os_deletesemaphore() µîÀ» Á÷Á¢ È£ÃâÇÏÁö ¾Ê´Â´Ù. |
|---|
| 405 | // linux usermode °°Àº °æ¿ì memfree °°Àº ÇÔ¼ö¸¦ isrlock »óÅ¿¡¼ È£ÃâÇÒ ¼ö ¾øÀ½. |
|---|
| 406 | #if 1 |
|---|
| 407 | DHL_OS_SEMA_ID sem; |
|---|
| 408 | UINT32 mask = DHL_OS_DisableInterrupts(); |
|---|
| 409 | sem = pMutex->semId; |
|---|
| 410 | pMutex->semId = 0; |
|---|
| 411 | DHL_OS_RestoreInterrupts(mask); |
|---|
| 412 | |
|---|
| 413 | if (sem) |
|---|
| 414 | DHL_OS_DeleteSemaphore(sem); |
|---|
| 415 | #else |
|---|
| 416 | UINT32 mask = DHL_OS_DisableInterrupts(); |
|---|
| 417 | DHL_OS_DeleteSemaphore(pMutex->semId); |
|---|
| 418 | pMutex->semId = 0; |
|---|
| 419 | DHL_OS_RestoreInterrupts(mask); |
|---|
| 420 | #endif |
|---|
| 421 | } |
|---|
| 422 | } |
|---|
| 423 | |
|---|
| 424 | |
|---|
| 425 | |
|---|
| 426 | |
|---|
| 427 | |
|---|
| 428 | #if 0 |
|---|
| 429 | ________() |
|---|
| 430 | #endif |
|---|
| 431 | |
|---|
| 432 | |
|---|
| 433 | |
|---|
| 434 | #if DMW_REGISTER_DEBUG_SYMBOL |
|---|
| 435 | |
|---|
| 436 | static DHL_SymbolTable MutexSymbols[] = |
|---|
| 437 | { |
|---|
| 438 | /*---- functions */ |
|---|
| 439 | /* DHL_FNC_SYM_ENTRY(Dmc_StopVideo), */ |
|---|
| 440 | |
|---|
| 441 | /*---- variables */ |
|---|
| 442 | //DHL_VAR_SYM_ENTRY(g_Trace_bMutexDebug), |
|---|
| 443 | 0 |
|---|
| 444 | }; |
|---|
| 445 | |
|---|
| 446 | #endif /* DMW_REGISTER_DEBUG_SYMBOL */ |
|---|
| 447 | |
|---|
| 448 | |
|---|
| 449 | void DMW_RegisterMutexSymbols() |
|---|
| 450 | { |
|---|
| 451 | #if DMW_REGISTER_DEBUG_SYMBOL |
|---|
| 452 | DHL_DBG_RegisterSymbols(MutexSymbols, DHL_NUMSYMBOLS(MutexSymbols)); |
|---|
| 453 | #endif |
|---|
| 454 | } |
|---|
| 455 | |
|---|
| 456 | |
|---|
| 457 | |
|---|
| 458 | |
|---|
| 459 | |
|---|
| 460 | |
|---|
| 461 | #if 0 |
|---|
| 462 | ________() |
|---|
| 463 | #endif |
|---|
| 464 | |
|---|
| 465 | |
|---|
| 466 | /*-------------------------------------------------------------------------------------------- |
|---|
| 467 | // |
|---|
| 468 | // OS Semaphore debugging.. |
|---|
| 469 | */ |
|---|
| 470 | |
|---|
| 471 | |
|---|
| 472 | /*================================================================= |
|---|
| 473 | // |
|---|
| 474 | // OS Semaphore Monitoring Task |
|---|
| 475 | // |
|---|
| 476 | // ÁÖ±âÀûÀ¸·Î Semaphore °¨½ÃÇÏ¿© ¿Àµ¿ÀÛ ÇÏ´Â Semaphore Á¤º¸ Ãâ·Â |
|---|
| 477 | */ |
|---|
| 478 | |
|---|
| 479 | |
|---|
| 480 | #if COMMENT |
|---|
| 481 | ____TEST____(){} |
|---|
| 482 | #endif |
|---|
| 483 | |
|---|
| 484 | |
|---|
| 485 | #if DMW_MUTEX_TEST |
|---|
| 486 | |
|---|
| 487 | /*=================================================================*/ |
|---|
| 488 | |
|---|
| 489 | //#include <string.h> /* rand(), srand() */ |
|---|
| 490 | |
|---|
| 491 | int gStopMutexTest = FALSE; |
|---|
| 492 | |
|---|
| 493 | |
|---|
| 494 | /* Mutex = { SemID, Owner, lockCnt, FailCnt, UseOsMutex, Name, flag, trace } */ |
|---|
| 495 | DMW_MUTEX g_TestMutex0 = { 0, 0, 0, 0, TRUE, "Sample", 0, 1 }; |
|---|
| 496 | DMW_MUTEX *g_TestMutex = &g_TestMutex0; |
|---|
| 497 | |
|---|
| 498 | /* |
|---|
| 499 | trace level 1À̹ǷÎ, ÀÌ ¸ðµâ level ¸¸ ÃæÁ·ÇÏ¸é µð¹ö±× ¸Þ½ÃÁö°¡ Ç¥½ÃµÊ. |
|---|
| 500 | Å×½ºÆ® ÀýÂ÷: |
|---|
| 501 | |
|---|
| 502 | Shell> MutexTestInit |
|---|
| 503 | Shell> sml "$MTX" 2 |
|---|
| 504 | Shell> TestMutex |
|---|
| 505 | |
|---|
| 506 | |
|---|
| 507 | Shell> set gStopMutexTest 1 // Å×½ºÆ® Á¾·á |
|---|
| 508 | */ |
|---|
| 509 | |
|---|
| 510 | void MutexTestInit(void) |
|---|
| 511 | { |
|---|
| 512 | DHL_DBG_RegisterVarSymbol(DHL_VAR_SYM_ARGS(gStopMutexTest)); |
|---|
| 513 | } |
|---|
| 514 | |
|---|
| 515 | void MutexTestTask1(UINT32 arg) |
|---|
| 516 | { |
|---|
| 517 | DHL_OS_Printf("**** Start Task1 **** \n"); |
|---|
| 518 | |
|---|
| 519 | DHL_OS_Delay(rand()%100); |
|---|
| 520 | |
|---|
| 521 | while (gStopMutexTest == FALSE) |
|---|
| 522 | { |
|---|
| 523 | DMW_LockMutex(g_TestMutex); |
|---|
| 524 | DHL_OS_Delay(rand()%200); |
|---|
| 525 | |
|---|
| 526 | DMW_UnlockMutex(g_TestMutex); |
|---|
| 527 | DHL_OS_Delay(rand()%100); |
|---|
| 528 | } |
|---|
| 529 | DHL_OS_Printf("**** Stop Task1 **** \n"); |
|---|
| 530 | DHL_OS_SelfDeleteTask(); |
|---|
| 531 | } |
|---|
| 532 | |
|---|
| 533 | |
|---|
| 534 | void MutexTestTask2(UINT32 arg) |
|---|
| 535 | { |
|---|
| 536 | DHL_OS_Printf("**** Start Task2 **** \n"); |
|---|
| 537 | |
|---|
| 538 | DHL_OS_Delay(rand()%100); |
|---|
| 539 | |
|---|
| 540 | while (gStopMutexTest == FALSE) |
|---|
| 541 | { |
|---|
| 542 | DMW_LockMutex(g_TestMutex); |
|---|
| 543 | DHL_OS_Delay(rand()%200); |
|---|
| 544 | |
|---|
| 545 | DMW_LockMutex(g_TestMutex); /* ÀÌÁß Lock Å×½ºÆ®.. */ |
|---|
| 546 | DHL_OS_Delay(rand()%200); |
|---|
| 547 | |
|---|
| 548 | DMW_UnlockMutex(g_TestMutex); |
|---|
| 549 | DHL_OS_Delay(rand()%100); |
|---|
| 550 | |
|---|
| 551 | DMW_UnlockMutex(g_TestMutex); |
|---|
| 552 | DHL_OS_Delay(rand()%100); |
|---|
| 553 | } |
|---|
| 554 | DHL_OS_Printf("**** Stop Task2 **** \n"); |
|---|
| 555 | DHL_OS_SelfDeleteTask(); |
|---|
| 556 | } |
|---|
| 557 | |
|---|
| 558 | |
|---|
| 559 | void MutexTestTask3(UINT32 arg) |
|---|
| 560 | { |
|---|
| 561 | DHL_OS_Printf("**** Start Task3 **** \n"); |
|---|
| 562 | |
|---|
| 563 | DHL_OS_Delay(rand()%100); |
|---|
| 564 | |
|---|
| 565 | while (gStopMutexTest == FALSE) |
|---|
| 566 | { |
|---|
| 567 | DMW_LockMutex(g_TestMutex); |
|---|
| 568 | DHL_OS_Delay(rand()%200); |
|---|
| 569 | |
|---|
| 570 | DMW_UnlockMutex(g_TestMutex); |
|---|
| 571 | DHL_OS_Delay(rand()%100); |
|---|
| 572 | } |
|---|
| 573 | DHL_OS_Printf("**** Stop Task3 **** \n"); |
|---|
| 574 | DHL_OS_SelfDeleteTask(); |
|---|
| 575 | } |
|---|
| 576 | |
|---|
| 577 | void TestMutex() |
|---|
| 578 | { |
|---|
| 579 | DHL_OS_Printf("g_TestMutex : 0x%x\n", g_TestMutex); |
|---|
| 580 | |
|---|
| 581 | srand(DHL_OS_GetMsCount()); |
|---|
| 582 | |
|---|
| 583 | DHL_OS_CreateTask(MutexTestTask1, "Mtx1", TASK_PRI_DHL_SHELL, 4096, 0); |
|---|
| 584 | |
|---|
| 585 | DHL_OS_CreateTask(MutexTestTask2, "Mtx2", TASK_PRI_DHL_SHELL+1, 4096, 0); |
|---|
| 586 | |
|---|
| 587 | DHL_OS_CreateTask(MutexTestTask3, "Mtx3", TASK_PRI_DHL_SHELL+2, 4096, 0); |
|---|
| 588 | } |
|---|
| 589 | |
|---|
| 590 | #endif /* DMW_MUTEX_TEST */ |
|---|
| 591 | |
|---|
| 592 | |
|---|
| 593 | |
|---|
| 594 | /************************************************************************* |
|---|
| 595 | Revision history: |
|---|
| 596 | |
|---|
| 597 | 0.12 2004/7/24 DeleteMutex added, bugfix |
|---|
| 598 | 0.11 2004/05/28 add selective debug print |
|---|
| 599 | 0.1 2004/01/01 initial coding |
|---|
| 600 | |
|---|
| 601 | **********************************************************************/ |
|---|
| 602 | |
|---|
| 603 | |
|---|