freemyipod r691 - Code Review

Jump to: navigation, search
Repository:freemyipod
Revision:r690‎ | r691 | r692 >
Date:00:54, 6 April 2011
Author:theseven
Status:new
Tags:
Comment:
emCORE: Fix mutexes not being released properly on thread termination. Bump API version, because this is an incompatible change.
Modified paths:
  • /apps/fastboot/version.h (modified) (history)
  • /emcore/trunk/export/syscallapi.h (modified) (history)
  • /emcore/trunk/thread.c (modified) (history)
  • /emcore/trunk/thread.h (modified) (history)
  • /emcore/trunk/version.h (modified) (history)

Diff [purge]

Index: apps/fastboot/version.h
@@ -25,10 +25,10 @@
2626 #define __VERSION_H__
2727
2828
29 -#define VERSION "0.1.0"
 29+#define VERSION "0.1.1"
3030 #define VERSION_MAJOR 0
3131 #define VERSION_MINOR 1
32 -#define VERSION_PATCH 0
 32+#define VERSION_PATCH 1
3333 #define VERSION_SVN "$REVISION$"
3434 #define VERSION_SVN_INT $REVISIONINT$
3535
Index: emcore/trunk/version.h
@@ -25,10 +25,10 @@
2626 #define __VERSION_H__
2727
2828
29 -#define VERSION "0.2.0"
 29+#define VERSION "0.2.1"
3030 #define VERSION_MAJOR 0
3131 #define VERSION_MINOR 2
32 -#define VERSION_PATCH 0
 32+#define VERSION_PATCH 1
3333 #define VERSION_SVN "$REVISION$"
3434 #define VERSION_SVN_INT $REVISIONINT$
3535
Index: emcore/trunk/export/syscallapi.h
@@ -71,12 +71,12 @@
7272 #endif
7373
7474 /* increase this every time the api struct changes */
75 -#define EMCORE_API_VERSION 2
 75+#define EMCORE_API_VERSION 3
7676
7777 /* update this to latest version if a change to the api struct breaks
7878 backwards compatibility (and please take the opportunity to sort in any
7979 new function which are "waiting" at the end of the function table) */
80 -#define EMCORE_MIN_API_VERSION 2
 80+#define EMCORE_MIN_API_VERSION 3
8181
8282 /* NOTE: To support backwards compatibility, only add new functions at
8383 the end of the structure. Every time you add a new function,
Index: emcore/trunk/thread.c
@@ -93,6 +93,8 @@
9494 {
9595 obj->count = 1;
9696 obj->owner = current_thread;
 97+ obj->owned_next = current_thread->owned_mutexes;
 98+ current_thread->owned_mutexes = obj;
9799 }
98100 else if (obj->owner == current_thread) obj->count++;
99101 else
@@ -117,6 +119,35 @@
118120 return ret;
119121 }
120122
 123+void mutex_unlock_internal(struct mutex* obj)
 124+{
 125+ struct mutex* o;
 126+ if (!obj->owner->owned_mutexes) return;
 127+ if (obj->owner->owned_mutexes == obj) obj->owner->owned_mutexes = obj->owned_next;
 128+ else
 129+ {
 130+ o = obj->owner->owned_mutexes;
 131+ while (o->owned_next)
 132+ {
 133+ if (o->owned_next == obj) o->owned_next = obj->owned_next;
 134+ o = o->owned_next;
 135+ }
 136+ }
 137+ if (obj->waiters)
 138+ {
 139+ obj->count = 1;
 140+ obj->owner = obj->waiters;
 141+ obj->waiters->state = THREAD_READY;
 142+ obj->waiters->block_type = THREAD_NOT_BLOCKED;
 143+ obj->waiters->blocked_by = NULL;
 144+ obj->waiters->timeout = 0;
 145+ obj->waiters = obj->waiters->queue_next;
 146+ obj->owned_next = obj->owner->owned_mutexes;
 147+ obj->owner->owned_mutexes = obj;
 148+ }
 149+ else obj->count = 0;
 150+}
 151+
121152 int mutex_unlock(struct mutex* obj)
122153 {
123154 int ret = THREAD_OK;
@@ -133,18 +164,8 @@
134165 leave_critical_section(mode);
135166 panicf(PANIC_KILLTHREAD, "Trying to unlock mutex owned by different thread! (%08X)", obj);
136167 }
137 -
138168 if (--(obj->count)) ret = obj->count;
139 - else if (obj->waiters)
140 - {
141 - obj->count = 1;
142 - obj->owner = obj->waiters;
143 - obj->waiters->state = THREAD_READY;
144 - obj->waiters->block_type = THREAD_NOT_BLOCKED;
145 - obj->waiters->blocked_by = NULL;
146 - obj->waiters->timeout = 0;
147 - obj->waiters = obj->waiters->queue_next;
148 - }
 169+ else mutex_unlock_internal(obj);
149170
150171 leave_critical_section(mode);
151172 return ret;
@@ -485,6 +506,10 @@
486507 thread->state = THREAD_SUSPENDED;
487508 }
488509
 510+ struct mutex* m;
 511+ for (m = thread->owned_mutexes; m; m = m->owned_next)
 512+ mutex_unlock_internal(m);
 513+
489514 leave_critical_section(mode);
490515
491516 library_release_all_of_thread(thread);
Index: emcore/trunk/thread.h
@@ -68,8 +68,10 @@
6969 };
7070
7171
72 -#define SCHEDULER_THREAD_INFO_VERSION 2
 72+#define SCHEDULER_THREAD_INFO_VERSION 3
7373
 74+struct mutex;
 75+
7476 struct scheduler_thread
7577 {
7678 uint32_t regs[16];
@@ -81,6 +83,7 @@
8284 uint32_t startusec;
8385 struct scheduler_thread* thread_next;
8486 struct scheduler_thread* queue_next;
 87+ struct mutex* owned_mutexes;
8588 uint32_t timeout;
8689 uint32_t blocked_since;
8790 void* blocked_by;
@@ -96,6 +99,7 @@
97100 {
98101 struct scheduler_thread* owner;
99102 struct scheduler_thread* waiters;
 103+ struct mutex* owned_next;
100104 int count;
101105 };
102106