1 diff -urN db-4.7.25.NC/lock/lock_deadlock.c db-4.7.25.NC.new/lock/lock_deadlock.c
2 --- db-4.7.25.NC/lock/lock_deadlock.c 2008-03-10 14:31:33.000000000 +0100
3 +++ db-4.7.25.NC.new/lock/lock_deadlock.c 2009-11-08 12:53:25.000000000 +0100
8 - u_int32_t *bitmap, *copymap, **deadp, **free_me, *tmpmap;
9 + u_int32_t *bitmap, *copymap, **deadp, **deadlist, *tmpmap;
10 u_int32_t i, cid, keeper, killid, limit, nalloc, nlockers;
11 u_int32_t lock_max, txn_max;
14 if (IS_REP_CLIENT(env))
15 atype = DB_LOCK_MINWRITE;
18 + copymap = tmpmap = NULL;
24 memcpy(copymap, bitmap, nlockers * sizeof(u_int32_t) * nalloc);
26 if ((ret = __os_calloc(env, sizeof(u_int32_t), nalloc, &tmpmap)) != 0)
30 /* Find a deadlock. */
32 - __dd_find(env, bitmap, idmap, nlockers, nalloc, &deadp)) != 0)
33 + __dd_find(env, bitmap, idmap, nlockers, nalloc, &deadlist)) != 0)
38 txn_max = TXN_MAXIMUM;
42 - for (; *deadp != NULL; deadp++) {
43 + for (deadp = deadlist; *deadp != NULL; deadp++) {
46 killid = (u_int32_t)(*deadp - bitmap) / nalloc;
49 "Aborting locker %lx", (u_long)idmap[killid].id);
51 - __os_free(env, tmpmap);
52 -err1: __os_free(env, copymap);
54 -err: if (free_me != NULL)
55 - __os_free(env, free_me);
56 +err: if(copymap != NULL)
57 + __os_free(env, copymap);
58 + if (deadlist != NULL)
59 + __os_free(env, deadlist);
61 + __os_free(env, tmpmap);
62 __os_free(env, bitmap);
63 __os_free(env, idmap);
67 #define DD_INVALID_ID ((u_int32_t) -1)
71 + * Build the lock dependency bit maps.
72 + * Notes on syncronization:
73 + * LOCK_SYSTEM_LOCK is used to hold objects locked when we have
74 + * a single partition.
75 + * LOCK_LOCKERS is held while we are walking the lockers list and
76 + * to single thread the use of lockerp->dd_id.
77 + * LOCK_DD protects the DD list of objects.
81 __dd_build(env, atype, bmp, nlockers, allocp, idmap, rejectp)
84 * In particular we do not build the conflict array and our caller
85 * needs to expect this.
87 + LOCK_SYSTEM_LOCK(lt, region);
88 if (atype == DB_LOCK_EXPIRE) {
89 skip: LOCK_DD(env, region);
90 op = SH_TAILQ_FIRST(®ion->dd_objs, __db_lockobj);
92 OBJECT_UNLOCK(lt, region, indx);
94 UNLOCK_DD(env, region);
95 + LOCK_SYSTEM_UNLOCK(lt, region);
100 - * We'll check how many lockers there are, add a few more in for
101 - * good measure and then allocate all the structures. Then we'll
102 - * verify that we have enough room when we go back in and get the
103 - * mutex the second time.
104 + * Allocate after locking the region
105 + * to make sure the structures are large enough.
107 -retry: count = region->stat.st_nlockers;
108 + LOCK_LOCKERS(env, region);
109 + count = region->stat.st_nlockers;
111 + UNLOCK_LOCKERS(env, region);
115 @@ -448,50 +462,37 @@
116 if (FLD_ISSET(env->dbenv->verbose, DB_VERB_DEADLOCK))
117 __db_msg(env, "%lu lockers", (u_long)count);
120 nentries = (u_int32_t)DB_ALIGN(count, 32) / 32;
123 - * Allocate enough space for a count by count bitmap matrix.
126 - * We can probably save the malloc's between iterations just
127 - * reallocing if necessary because count grew by too much.
129 + /* Allocate enough space for a count by count bitmap matrix. */
130 if ((ret = __os_calloc(env, (size_t)count,
131 - sizeof(u_int32_t) * nentries, &bitmap)) != 0)
132 + sizeof(u_int32_t) * nentries, &bitmap)) != 0) {
133 + UNLOCK_LOCKERS(env, region);
137 if ((ret = __os_calloc(env,
138 sizeof(u_int32_t), nentries, &tmpmap)) != 0) {
139 + UNLOCK_LOCKERS(env, region);
140 __os_free(env, bitmap);
144 if ((ret = __os_calloc(env,
145 (size_t)count, sizeof(locker_info), &id_array)) != 0) {
146 + UNLOCK_LOCKERS(env, region);
147 __os_free(env, bitmap);
148 __os_free(env, tmpmap);
153 - * Now go back in and actually fill in the matrix.
155 - if (region->stat.st_nlockers > count) {
156 - __os_free(env, bitmap);
157 - __os_free(env, tmpmap);
158 - __os_free(env, id_array);
163 * First we go through and assign each locker a deadlock detector id.
166 - LOCK_LOCKERS(env, region);
167 SH_TAILQ_FOREACH(lip, ®ion->lockers, ulinks, __db_locker) {
168 if (lip->master_locker == INVALID_ROFF) {
169 + DB_ASSERT(env, id < count);
171 id_array[lip->dd_id].id = lip->id;
174 lip->dd_id = DD_INVALID_ID;
177 - UNLOCK_LOCKERS(env, region);
180 * We only need consider objects that have waiters, so we use
182 * status after building the bit maps so that we will not detect
183 * a blocked transaction without noting that it is already aborting.
185 - LOCK_LOCKERS(env, region);
186 for (id = 0; id < count; id++) {
187 if (!id_array[id].valid)
190 id_array[id].in_abort = 1;
192 UNLOCK_LOCKERS(env, region);
193 + LOCK_SYSTEM_UNLOCK(lt, region);
196 * Now we can release everything except the bitmap matrix that we
200 /* We must lock so this locker cannot go away while we abort it. */
201 + LOCK_SYSTEM_LOCK(lt, region);
202 LOCK_LOCKERS(env, region);
206 done: OBJECT_UNLOCK(lt, region, info->last_ndx);
208 out: UNLOCK_LOCKERS(env, region);
209 + LOCK_SYSTEM_UNLOCK(lt, region);