Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit e893412

Browse files
Added error checking.
Improved coexistance with dl module.
1 parent dd10440 commit e893412

1 file changed

Lines changed: 82 additions & 21 deletions

File tree

Python/thread.c

Lines changed: 82 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ static int thread_debug = 0;
1414
#include <sys/types.h>
1515
#include <sys/prctl.h>
1616
#include <ulocks.h>
17+
#include <errno.h>
1718

1819
#define MAXPROC 100 /* max # of threads that can be started */
1920

@@ -101,6 +102,9 @@ void init_thread _P0()
101102
{
102103
#ifdef __sgi
103104
struct sigaction s;
105+
#ifdef USE_DL
106+
long addr, size;
107+
#endif
104108
#endif
105109

106110
#ifdef DEBUG
@@ -112,6 +116,17 @@ void init_thread _P0()
112116
dprintf(("init_thread called\n"));
113117

114118
#ifdef __sgi
119+
#ifdef USE_DL
120+
if ((size = usconfig(CONF_INITSIZE, 64*1024)) < 0)
121+
perror("usconfig - CONF_INITSIZE (check)");
122+
if (usconfig(CONF_INITSIZE, size) < 0)
123+
perror("usconfig - CONF_INITSIZE (reset)");
124+
addr = (long) dl_getrange(size + sizeof(ushdr_t));
125+
dprintf(("trying to use addr %lx-%lx for shared arena\n", addr, addr+size));
126+
errno = 0;
127+
if ((addr = usconfig(CONF_ATTACHADDR, addr)) < 0 && errno != 0)
128+
perror("usconfig - CONF_ATTACHADDR (set)");
129+
#endif
115130
if (usconfig(CONF_INITUSERS, 16) < 0)
116131
perror("usconfig - CONF_INITUSERS");
117132
my_pid = getpid(); /* so that we know which is the main thread */
@@ -128,13 +143,16 @@ void init_thread _P0()
128143
/*usconfig(CONF_LOCKTYPE, US_DEBUGPLUS);*/
129144
if ((shared_arena = usinit(tmpnam(0))) == 0)
130145
perror("usinit");
131-
count_lock = usnewlock(shared_arena);
146+
#ifdef USE_DL
147+
if (usconfig(CONF_ATTACHADDR, addr) < 0) /* reset address */
148+
perror("usconfig - CONF_ATTACHADDR (reset)");
149+
#endif
150+
if ((count_lock = usnewlock(shared_arena)) == NULL)
151+
perror("usnewlock (count_lock)");
132152
(void) usinitlock(count_lock);
133-
wait_lock = usnewlock(shared_arena);
153+
if ((wait_lock = usnewlock(shared_arena)) == NULL)
154+
perror("usnewlock (wait_lock)");
134155
dprintf(("arena start: %lx, arena size: %ld\n", (long) shared_arena, (long) usconfig(CONF_GETSIZE, shared_arena)));
135-
#ifdef USE_DL /* for python */
136-
dl_setrange((long) shared_arena, (long) shared_arena + 64 * 1024);
137-
#endif
138156
#endif
139157
#ifdef sun
140158
lwp_setstkcache(STACKSIZE, NSTACKS);
@@ -151,6 +169,10 @@ int start_new_thread _P2(func, void (*func) _P((void *)), arg, void *arg)
151169
{
152170
#ifdef sun
153171
thread_t tid;
172+
#endif
173+
#if defined(__sgi) && defined(USE_DL)
174+
long addr, size;
175+
static int local_initialized = 0;
154176
#endif
155177
int success = 0; /* init not needed when SOLARIS and */
156178
/* C_THREADS implemented properly */
@@ -159,18 +181,42 @@ int start_new_thread _P2(func, void (*func) _P((void *)), arg, void *arg)
159181
if (!initialized)
160182
init_thread();
161183
#ifdef __sgi
162-
if (ussetlock(count_lock) == 0)
163-
return 0;
184+
switch (ussetlock(count_lock)) {
185+
case 0: return 0;
186+
case -1: perror("ussetlock (count_lock)");
187+
}
164188
if (maxpidindex >= MAXPROC)
165189
success = -1;
166190
else {
167-
success = sproc(func, PR_SALL, arg);
191+
#ifdef USE_DL
192+
if (!local_initialized) {
193+
if ((size = usconfig(CONF_INITSIZE, 64*1024)) < 0)
194+
perror("usconfig - CONF_INITSIZE (check)");
195+
if (usconfig(CONF_INITSIZE, size) < 0)
196+
perror("usconfig - CONF_INITSIZE (reset)");
197+
addr = (long) dl_getrange(size + sizeof(ushdr_t));
198+
dprintf(("trying to use addr %lx-%lx for sproc\n", addr, addr+size));
199+
errno = 0;
200+
if ((addr = usconfig(CONF_ATTACHADDR, addr)) < 0 && errno != 0)
201+
perror("usconfig - CONF_ATTACHADDR (set)");
202+
}
203+
#endif
204+
if ((success = sproc(func, PR_SALL, arg)) < 0)
205+
perror("sproc");
206+
#ifdef USE_DL
207+
if (!local_initialized) {
208+
if (usconfig(CONF_ATTACHADDR, addr) < 0) /* reset address */
209+
perror("usconfig - CONF_ATTACHADDR (reset)");
210+
local_initialized = 1;
211+
}
212+
#endif
168213
if (success >= 0) {
169214
nthreads++;
170215
pidlist[maxpidindex++] = success;
171216
}
172217
}
173-
(void) usunsetlock(count_lock);
218+
if (usunsetlock(count_lock) < 0)
219+
perror("usunsetlock (count_lock)");
174220
#endif
175221
#ifdef SOLARIS
176222
(void) thread_create(0, 0, func, arg, THREAD_NEW_LWP);
@@ -193,7 +239,8 @@ static void do_exit_thread _P1(no_cleanup, int no_cleanup)
193239
else
194240
exit(0);
195241
#ifdef __sgi
196-
(void) ussetlock(count_lock);
242+
if (ussetlock(count_lock) < 0)
243+
perror("ussetlock (count_lock)");
197244
nthreads--;
198245
if (getpid() == my_pid) {
199246
/* main thread; wait for other threads to exit */
@@ -210,7 +257,8 @@ static void do_exit_thread _P1(no_cleanup, int no_cleanup)
210257
}
211258
}
212259
waiting_for_threads = 1;
213-
ussetlock(wait_lock);
260+
if (ussetlock(wait_lock) < 0)
261+
perror("ussetlock (wait_lock)");
214262
for (;;) {
215263
if (nthreads < 0) {
216264
dprintf(("really exit (%d)\n", exit_status));
@@ -219,19 +267,24 @@ static void do_exit_thread _P1(no_cleanup, int no_cleanup)
219267
else
220268
exit(exit_status);
221269
}
222-
usunsetlock(count_lock);
270+
if (usunsetlock(count_lock) < 0)
271+
perror("usunsetlock (count_lock)");
223272
dprintf(("waiting for other threads (%d)\n", nthreads));
224-
ussetlock(wait_lock);
225-
ussetlock(count_lock);
273+
if (ussetlock(wait_lock) < 0)
274+
perror("ussetlock (wait_lock)");
275+
if (ussetlock(count_lock) < 0)
276+
perror("ussetlock (count_lock)");
226277
}
227278
}
228279
/* not the main thread */
229280
if (waiting_for_threads) {
230281
dprintf(("main thread is waiting\n"));
231-
usunsetlock(wait_lock);
282+
if (usunsetlock(wait_lock) < 0)
283+
perror("usunsetlock (wait_lock)");
232284
} else if (do_exit)
233285
(void) kill(my_pid, SIGUSR1);
234-
(void) usunsetlock(count_lock);
286+
if (usunsetlock(count_lock) < 0)
287+
perror("usunsetlock (count_lock)");
235288
_exit(0);
236289
#endif
237290
#ifdef SOLARIS
@@ -301,7 +354,8 @@ type_lock allocate_lock _P0()
301354
init_thread();
302355

303356
#ifdef __sgi
304-
lock = usnewlock(shared_arena);
357+
if ((lock = usnewlock(shared_arena)) == NULL)
358+
perror("usnewlock");
305359
(void) usinitlock(lock);
306360
#endif
307361
#ifdef sun
@@ -332,10 +386,13 @@ int acquire_lock _P2(lock, type_lock lock, waitflag, int waitflag)
332386

333387
dprintf(("acquire_lock(%lx, %d) called\n", (long)lock, waitflag));
334388
#ifdef __sgi
389+
errno = 0; /* clear it just in case */
335390
if (waitflag)
336391
success = ussetlock((ulock_t) lock);
337392
else
338393
success = uscsetlock((ulock_t) lock, 1); /* Try it once */
394+
if (success < 0)
395+
perror(waitflag ? "ussetlock" : "uscsetlock");
339396
#endif
340397
#ifdef sun
341398
success = 0;
@@ -359,7 +416,8 @@ void release_lock _P1(lock, type_lock lock)
359416
{
360417
dprintf(("release_lock(%lx) called\n", (long)lock));
361418
#ifdef __sgi
362-
(void) usunsetlock((ulock_t) lock);
419+
if (usunsetlock((ulock_t) lock) < 0)
420+
perror("usunsetlock");
363421
#endif
364422
#ifdef sun
365423
(void) mon_enter(((struct lock *) lock)->lock_monitor);
@@ -383,7 +441,8 @@ type_sema allocate_sema _P1(value, int value)
383441
init_thread();
384442

385443
#ifdef __sgi
386-
sema = usnewsema(shared_arena, value);
444+
if ((sema = usnewsema(shared_arena, value)) == NULL)
445+
perror("usnewsema");
387446
dprintf(("allocate_sema() -> %lx\n", (long) sema));
388447
return (type_sema) sema;
389448
#endif
@@ -401,7 +460,8 @@ void down_sema _P1(sema, type_sema sema)
401460
{
402461
dprintf(("down_sema(%lx) called\n", (long) sema));
403462
#ifdef __sgi
404-
(void) uspsema((usema_t *) sema);
463+
if (uspsema((usema_t *) sema) < 0)
464+
perror("uspsema");
405465
#endif
406466
dprintf(("down_sema(%lx) return\n", (long) sema));
407467
}
@@ -410,6 +470,7 @@ void up_sema _P1(sema, type_sema sema)
410470
{
411471
dprintf(("up_sema(%lx)\n", (long) sema));
412472
#ifdef __sgi
413-
(void) usvsema((usema_t *) sema);
473+
if (usvsema((usema_t *) sema) < 0)
474+
perror("usvsema");
414475
#endif
415476
}

0 commit comments

Comments
 (0)